0% found this document useful (0 votes)
19 views

C++ Session

Uploaded by

112301046
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views

C++ Session

Uploaded by

112301046
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

C++ Bootcamp

https://www.programiz.com/cpp-programming

Hello world program


#include <iostream>
using namespace std;

// Main() function: where the execution of


// program begins
int main()
{
// Prints hello world
cout << "Hello World";
return 0;
}

Variables
#include <iostream>
using namespace std;

// Main Function
int main()
{

// Body of the Function

// Declaration of Variable
int num1 = 24;
int num2 = 34;

int result = num1 + num2;

// Output
cout << result << endl;

// Return Statement
return 0;
}
// Declaring float variable
float simpleInterest;

// Declaring integer variable


int time, speed;

// Declaring character variable


char var;

Valid variable names:


int x; //can be letters
int _yz; //can be underscores
int z40;//can be letters

Invalid variable names:


int 89; Should not be a number
int a b; //Should not contain any whitespace
int double;// C++ keyword CAN NOT BE USED

// C++ program to show difference between


// definition and declaration of a
// variable
#include <iostream>
using namespace std;

int main()
{
// this is declaration of variable a
int a;
// this is initialisation of a
a = 10;

// this is definition = declaration + initialisation


int b = 20;

// declaration and definition


// of variable 'a123'
char a123 = 'a';

// This is also both declaration and definition


// as 'c' is allocated memory and
// assigned some garbage value.
float c;

// multiple declarations and definitions


int _c, _d45, e;

// Let us print a variable


cout << a123 << endl;

return 0;
}

Global and local variables

Operators
Addition
Subtraction
Division
Multiplication
Modulo

// CPP Program to demonstrate the Binary Operators


#include <iostream>
using namespace std;
int main()
{
int a = 8, b = 3;

// 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;
}

// CPP Program to demonstrate the Relational Operators


#include <iostream>
using namespace std;

int main()
{
int a = 6, b = 4;
// Equal to operator
cout << "a == b is " << (a == b) << endl;

// Greater than operator


cout << "a > b is " << (a > b) << endl;

// Greater than or Equal to operator


cout << "a >= b is " << (a >= b) << endl;

// Lesser than operator


cout << "a < b is " << (a < b) << endl;

// Lesser than or Equal to operator


cout << "a <= b is " << (a <= b) << endl;

// true
cout << "a != b is " << (a != b) << endl;

return 0;
}

// CPP Program to demonstrate the Logical Operators


#include <iostream>
using namespace std;

int main()
{
int a = 6, b = 4;

// Logical AND operator


cout << "a && b is " << (a && b) << endl;

// Logical OR operator
cout << "a ! b is " << (a > b) << endl;

// Logical NOT operator


cout << "!b is " << (!b) << endl;

return 0;
}

// CPP Program to demonstrate the Bitwise Operators


#include <iostream>
using namespace std;

int main()
{
int a = 6, b = 4;

// Binary AND operator


cout << "a & b is " << (a & b) << endl;

// Binary OR operator
cout << "a | b is " << (a | b) << endl;

// Binary XOR operator


cout << "a ^ b is " << (a ^ b) << endl;

// Left Shift operator


cout << "a>>1 is " << (a >> 1) << endl;
// Right Shift operator
cout << "a<<1 is " << (a << 1) << endl;

// One’s Complement operator


cout << "~(a) is " << ~(a) << endl;

return 0;
}

Unary operators

// C program to illustrate increment


#include <stdio.h>

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.

// C program to illustrate decrement


#include <stdio.h>
int main()
{
int a = 5;
int b = 5;
printf("Pre-Decrementing a = %d\n", --a);
printf("Post-Decrementing b = %d", b--);
return 0;
}

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);

// Assigning value by adding 10 to a


// using "+=" operator
a += 10;
printf("Value of a is %d\n", a);
// Assigning value by subtracting 10 from a
// using "-=" operator
a -= 10;
printf("Value of a is %d\n", a);

// Assigning value by multiplying 10 to a


// using "*=" operator
a *= 10;
printf("Value of a is %d\n", a);

// Assigning value by dividing 10 from a


// using "/=" operator
a /= 10;
printf("Value of a is %d\n", a);

return 0;
}

Input

#include <iostream>
using namespace std;

int main()
{

cout << "Enter your age:";


cin >> age;
cout << "Your age is: " << age;

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”;
}
}

// C program to illustrate If statement

#include <stdio.h>

int main()
{
int i = 10;

if (i > 15) {
printf("10 is greater than 15 \n");
}

printf("I am Not in if");


}

// C program to illustrate If statement

#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;
}

TASK: Take two integers from user. Then take the


operation(+,-,*/) and print the result.

// C++ program to Demonstrate the need of loops


#include <iostream>
using namespace std;

int main()
{
cout << "Hello World\n";
cout << "Hello World\n";
cout << "Hello World\n";
cout << "Hello World\n";
cout << "Hello World\n";
return 0;
}

You might also like