Print Number Entered by User
#include <iostream.h>
int main()
// variable declaration
int number;
cout << "Enter an integer: ";
cin >> number;
cout << "You entered " << number;
return 0;
Program to Add Two Integers
#include <iostream.h>
#include<conio.h>
int main()
int firstNumber, secondNumber, sumOfTwoNumbers;
clrscr();
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;
getch();
return 0;
Compute quotient and remainder
#include <iostream.h>
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;
}
Swap Numbers (Using Temporary Variable)
#include <iostream.h>
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;
Before swapping.
a = 5, b = 10
After swapping.
a = 10, b = 5
Data Types
Data types define the type of data a variable can hold, for example an integer
variable can hold integer data, a character type variable can hold character data
etc. Data types in C++ are categorised in three groups: Built-in, user-defined
and Derived.
Basic Data Types
The data type specifies the size and type of information the variable will store:
Data Type Size Description
int 4 bytes Stores whole numbers, without decimals
float 4 bytes Stores fractional numbers, containing one
or more decimals. Sufficient for storing 7 decimal digits
double 8 bytes Stores fractional numbers, containing one
or more decimals. Sufficient for storing 15 decimal digits
boolean 1 byte Stores true or false values
char 1 byte Stores a single character/letter/number, or
ASCII values
C++ Operators
Operators are used to perform operations on variables and values.
In the example below, we use the + operator to add together two values:
Example
int x = 100 + 50;
C++ divides the operators into the following groups:
1. Arithmetic operators
2. Assignment operators
3. Comparison operators
4. Logical operators
5. Bitwise operators
Arithmetic Operators
Arithmetic operators are used to perform common mathematical operations.
Operator Name Description
+ Addition Adds together two values x+y
- Subtraction Subtracts one value from another x-y
* Multiplication Multiplies two values x * y
/ Division Divides one value by another x / y
% Modulus Returns the division remainder x % y
++ Increment Increases the value of a variable by 1 ++x
-- Decrement Decreases the value of a variable by 1 --x
Operator Example Same as
= x=5 x=5
+= x += 3 x=x+3
-= x -= 3 x=x-3
*= x *= 3 x=x*3
/= x /= 3 x=x/3
%= x %= 3 x=x%3
&= x &= 3 x=x&3
|= x |= 3 x=x|3
^= x ^= 3 x=x^3
>>= x >>= 3 x = x >> 3
<<= x <<= 3 x = x << 3
Comparison Operators
Comparison operators are used to compare two values.
Note: The return value of a comparison is either true (1) or false (0).
In the following example, we use the greater than operator (>) to find out if 5 is
greater than 3:
Example
int x = 5; char x = ‘A’;
int y = 3; char y = ‘a’; cout<<(x==y); //returns false because c++ is case
sensitive
cout << (x > y); // returns 1 (true) because 5 is greater than 3
A list of all comparison operators:
Operator Name Example
== Equal to x == y
!= Not equal x != y
> Greater than x>y
< Less than x<y
>= Greater than or equal to x >= y
<= Less than or equal to x <= y
Logical Operators
Logical operators are used to determine the logic between variables or values:
Operator Name Description Example
If x = 2
&& Logical and Returns true if both statements are true (x < 5 && x <
10)
|| Logical or Returns true if one of the statements is true
x < 5 || x < 4
! Logical not Reverse the result, returns false if
the result is true !(x < 5 && x < 10)
/* program for pre-increment and pre- decrement operator */
#include<iostream.h>
void main()
int x = 0;
// x+1 = x; 0 +1 = 1 = x first it adds the value and store it in the variable
// x-1 = x; 01 = -1 =x first subtraction takes place then values is stored
cout<<” pre-increment value is”<<++x;
cout<<” pre-decrement value is”<<--x;
/* program for post-increment and post- decrement operator */
#include<iostream.h>
void main()
int x = 0;
// x = x +1; // after the value of x is assigned then addition takes place
// x = x-1; //after value of x is assigned then subtraction takes place
cout<<” post-increment value is”<<x++;
cout<<” post-decrement value is”<<x--;