Example 1: C++ "Hello World!" Program
In this example, we will learn to create a simple program named "Hello World" in C++ programming.
C++ "Hello World!" Program
// Your First C++ Program
#include <iostream>
int main() {
std::cout << "Hello World!";
return 0;
}
Output
Hello World!
Working of C++ "Hello World!" Program
1. // Your First C++ Program
In C++, any line starting with // is a comment. Comments are intended for the person reading the code to better understand the functionality of the program. It is completely ignored by the C++ compiler.
2. #include <iostream>
The #include is a preprocessor directive used to include files in our program. The above code is including the contents of the iostream file.
This allows us to use cout in our program to print output on the screen.
For now, just remember that we need to use #include <iostream> to use cout that allows us to print output on the screen.
3. int main() {...}
A valid C++ program must have the main() function. The curly braces indicate the start and the end of the function.
The execution of code beings from this function.
4. std::cout << "Hello World!";
std::cout prints the content inside the quotation marks. It must be followed by << followed by the format string. In our example, "Hello World!" is the format string.
Note: All C++ statements must always end with a ;.
5. return 0;
The return 0; statement is the "Exit status" of the program. In simple terms, the program ends with this statement.
int main() {
// Write your code here
}
Example 2: C++ Program to Print Number Entered by User
In this example, you'll learn to print the number entered by a user using C++ cout statement.
Program: Print Number Entered by User
#include <iostream>
using namespace std;
int main()
{
int number;
cout << "Enter an integer: ";
cin >> number;
cout << "You entered " << number;
return 0;
}
Output
Enter an integer: 23You entered 23
This program asks user to enter a number.
When user enters an integer, it is stored in variable number using cin.
Then it is displayed in the screen using cout.
Example 3: C++ Program to Add Two Numbers
In this program, user is asked to enter two integers. Then, the sum of those two integers is stored in a variable and displayed on the screen. Primary tabs
Program: Add Two Integers
#include <iostream>
using namespace std;
int main()
{
int firstNumber, secondNumber, sumOfTwoNumbers;
cout << "Enter two integers: ";
cin >> firstNumber >> secondNumber;
// sum of two numbers in stored in variable sumOfTwoNumbers
sumOfTwoNumbers = firstNumber + secondNumber;
// Prints sum
cout << firstNumber << " + " << secondNumber << " = " << sumOfTwoNumbers;
return 0;
}Output
Enter two integers: 4
5
4 + 5 = 9
In this program, user is asked to enter two integers. These two integers are stored in variables firstNumber and secondNumber respectively.
Then, the variables firstNumber and secondNumber are added using + operator and stored in sumOfTwoNumbers variable.
Finally, sumOfTwoNumbers is displayed on the screen.
Example 4: C++ Program to Find Quotient and Remainder
In this example, you will learn to find the quotient and remainder of a given dividend and divisor.
In this program, user is asked to enter two integers (divisor and dividend) and computes the quotient and remainder.
To compute quotient and remainder, both divisor and dividend should be integers.
Program: Compute quotient and remainder
#include <iostream>
using namespace std;
int main()
{
int divisor, dividend, quotient, remainder;
cout << "Enter dividend: ";
cin >> dividend;
cout << "Enter divisor: ";
cin >> divisor;
quotient = dividend / divisor;
remainder = dividend % divisor;
cout << "Quotient = " << quotient << endl;
cout << "Remainder = " << remainder;
return 0;
}Output
Enter dividend: 13
Enter divisor: 4
Quotient = 3
Remainder = 1
The division operator / is computes the quotient (either between float or integer variables).
The modulus operator % computes the remainder when one integer is divided by another (modulus operator cannot be used for floating-type variables).
Example 5: C++ Program to Find Size of int, float, double and char in Your System
This program declares 4 variables of type int, float, double and char. Then, the size of each variable is evaluated using sizeof operator.
To find the size of variable, sizeof operator is used.
sizeof(dataType);
Program: Find Size of a Variable
#include <iostream>
using namespace std;
int main()
{
cout << "Size of char: " << sizeof(char) << " byte" << endl;
cout << "Size of int: " << sizeof(int) << " bytes" << endl;
cout << "Size of float: " << sizeof(float) << " bytes" << endl;
cout << "Size of double: " << sizeof(double) << " bytes" << endl;
return 0;
}Output
Size of char: 1 byte
Size of int: 4 bytes
Size of float: 4 bytes
Size of double: 8 bytes
Example 6: C++ Program to Swap Two Numbers
This example contains two different techniques to swap numbers in C programming. The first program uses temporary variable to swap numbers, whereas the second program doesn't use temporary variables.
Program 1: Swap Numbers (Using Temporary Variable)
#include <iostream>
using namespace std;
int main()
{
int a = 5, b = 10, temp;
cout << "Before swapping." << endl;
cout << "a = " << a << ", b = " << b << endl;
temp = a;
a = b;
b = temp;
cout << "\nAfter swapping." << endl;
cout << "a = " << a << ", b = " << b << endl;
return 0;
}
Output
Before swapping.
a = 5, b = 10After swapping.
a = 10, b = 5
To perform swapping in above example, three variables are used.
The contents of the first variable is copied into the temp variable. Then, the contents of second variable is copied to the first variable.
Finally, the contents of the temp variable is copied back to the second variable which completes the swapping process.
You can also perform swapping using only two variables as below.
Program 2: Swap Numbers Without Using Temporary Variables
#include <iostream>
using namespace std;
int main()
{
int a = 5, b = 10;
cout << "Before swapping." << endl;
cout << "a = " << a << ", b = " << b << endl;
a = a + b;
b = a - b;
a = a - b;
cout << "\nAfter swapping." << endl;
cout << "a = " << a << ", b = " << b << endl;
return 0;
}
The output of this program is same as first program above.
Example 7: C++ Program to Find ASCII Value of a Character
In this example, you will learn to find ASCII value of a character in C++.
A character variable holds ASCII value (an integer number between 0 and 127) rather than that character itself in C programming. That value is known as ASCII value.
For example, ASCII value of 'A' is 65.
What this means is that, if you assign 'A' to a character variable, 65 is stored in that variable rather than 'A' itself.
Program: Print ASCII Value in C++
#include <iostream>
using namespace std;
int main()
{
char c;
cout << "Enter a character: ";
cin >> c;
cout << "ASCII Value of " << c << " is " << int(c);
return 0;
}Output
Enter a character: p
ASCII Value of p is 112
When we explicitly print the integer value of a char type, it's corresponding ASCII value is printed.
Example 8: C++ Program to Multiply two Numbers
In this program, user is asked to enter two numbers (floating point numbers). Then, the product of those two numbers is stored in a variable and displayed on the screen.
Program: Multiply Two Numbers
#include <iostream>
using namespace std;
int main()
{
double firstNumber, secondNumber, productOfTwoNumbers;
cout << "Enter two numbers: ";
// Stores two floating point numbers in variable firstNumber and secondNumber
respectively
cin >> firstNumber >> secondNumber;
// Performs multiplication and stores the result in variable productOfTwoNumbers
productOfTwoNumbers = firstNumber * secondNumber;
cout << "Product = " << productOfTwoNumbers;
return 0;
}Output
Enter two numbers: 3.4
5.5
Product = 18.7
In this program, user is asked to enter two numbers. These two numbers entered by the user is stored in variable firstNumber and secondNumber respectively.
Then, the product of firstNumber and secondNumber is evaluated and the result is stored in variable productOfTwoNumbers.
Finally, the productOfTwoNumbers is displayed on the screen.
73 videos|7 docs|23 tests
|
1. What is C programming? |
2. What are the basic examples of C programming? |
3. How is C programming used in backend development? |
4. What are the advantages of using C programming in backend development? |
5. What are some popular backend frameworks and libraries in C programming? |
73 videos|7 docs|23 tests
|
|
Explore Courses for Back-End Programming exam
|