C++ Session
C++ Session
https://www.programiz.com/cpp-programming
Variables
#include <iostream>
using namespace std;
// Main Function
int main()
{
// Declaration of Variable
int num1 = 24;
int num2 = 34;
// Output
cout << result << endl;
// Return Statement
return 0;
}
// Declaring float variable
float simpleInterest;
int main()
{
// this is declaration of variable a
int a;
// this is initialisation of a
a = 10;
return 0;
}
Operators
Addition
Subtraction
Division
Multiplication
Modulo
// Addition operator
cout << "a + b = " << (a + b) << endl;
// Subtraction operator
cout << "a - b = " << (a - b) << endl;
// Multiplication operator
cout << "a * b = " << (a * b) << endl;
// Division operator
cout << "a / b = " << (a / b) << endl;
// Modulo operator
cout << "a % b = " << (a % b) << endl;
return 0;
}
int main()
{
int a = 6, b = 4;
// Equal to operator
cout << "a == b is " << (a == b) << endl;
// true
cout << "a != b is " << (a != b) << endl;
return 0;
}
int main()
{
int a = 6, b = 4;
// Logical OR operator
cout << "a ! b is " << (a > b) << endl;
return 0;
}
int main()
{
int a = 6, b = 4;
// Binary OR operator
cout << "a | b is " << (a | b) << endl;
return 0;
}
Unary operators
int main()
{
int a = 5;
int b = 5;
printf("Pre-Incrementing a = %d\n", ++a);
printf("Post-Incrementing b = %d", b++);
return 0;
}
TASK: Input two integers. Add them and store it in a variable. Subtract them and
store it in a variable. Then print the result.
Assignment operators
// C program to demonstrate
// working of Assignment operators
#include <stdio.h>
int main()
{
// Assigning value 10 to a
// using "=" operator
int a = 10;
printf("Value of a is %d\n", a);
return 0;
}
Input
#include <iostream>
using namespace std;
int main()
{
return 0;
}
TASK: Take two integers input from the use and print the
addition and subtraction of those two numbers
Conditional statements
#include <stdio.h>
int main()
{
int i = 10;
if (i < 18) {
Cout << “you are not an adult”;
}
}
#include <stdio.h>
int main()
{
int i = 10;
if (i > 15) {
printf("10 is greater than 15 \n");
}
#include <stdio.h>
int main()
{
int i = 25;
if (i > 15)
Cout << "i is greater than 15";
else
Cout << "i is smaller than 15";
return 0;
}
int main()
{
cout << "Hello World\n";
cout << "Hello World\n";
cout << "Hello World\n";
cout << "Hello World\n";
cout << "Hello World\n";
return 0;
}