0% found this document useful (0 votes)
10 views9 pages

SIG - Day2

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)
10 views9 pages

SIG - Day2

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/ 9

Data Types and sizeof( ) operator

#include <iostream>
using namespace std;

int main(){

int a;
float b;
double c;
char d;
bool e;
string f;

cout << "Size of int: " << sizeof(a) << endl;


cout << "Size of float: " << sizeof(b) << endl;
cout << "Size of double: " << sizeof(c) << endl;
cout << "Size of char : " << sizeof(d) << endl;
cout << "Size of bool : " << sizeof(e) << endl;
cout << "Size of string : " << sizeof(f) << endl;

return 0;
}

Local and Global Variable


#include <iostream>
using namespace std;

int x = 10;

int main(){
int x=40;
cout<<x<<endl; //local
cout<<::x<<endl; //global
return 0;
}

Arithmetic Operators

#include <iostream>
using namespace std;

int main()
{
int a = 4, b = 5;
cout << "The value of a + b is " << a + b << endl;
cout << "The value of a - b is " << a - b << endl;
cout << "The value of a * b is " << a * b << endl;
cout << "The value of a / b is " << a / b << endl;
cout << "The value of a % b is " << a % b << endl;
cout << "The value of a++ is " << a++ << endl;
cout << "The value of a-- is " << a-- << endl;
cout << "The value of ++a is " << ++a << endl;
cout << "The value of --a is " << --a << endl;

return 0;
}

Assignment Operators

#include <iostream>
using namespace std;

//Assignment Operators

int main(){
int a = 15, b = 4;
cout << " a = b : " << (a = b) << endl;
cout << " a += b : " << (a += b) << endl;
cout << " a -= b : " << (a -= b) << endl;
cout << " a *= b : " << (a *= b) << endl;
cout << " a /= b : " << (a /= b) << endl;
cout << " a %= b : " << (a %= b) << endl;
cout << " a &= b : " << (a &= b) << endl;
cout << " a ^= b : " << (a ^= b) << endl;
cout << " a |= b : " << (a |= b) << endl;

return 0;
}

Relational Operators

#include <iostream>
using namespace std;

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

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


cout << "a != b : " << (a != b) << endl;
cout << "a > b : " << (a > b) << endl;
cout << "a < b : " << (a < b) << endl;
cout << "a >= b : " << (a >= b) << endl;
cout << "a <= b : " << (a <= b) << endl;

return 0;
}

Logical Operators

#include <iostream>
using namespace std;

int main()
{
bool a=true, b=false;

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


cout << "a || b : " << (a || b) << endl;
cout << "!a : " << !a << endl;

return 0;
}

Bitwise Operators

#include <iostream>
using namespace std;
int main(){
int a = 4, b = 6;
cout << (a & b) << endl;
cout << (a | b) << endl;
cout << (a ^ b) << endl;
cout << (~a) << endl;
cout << (a << 2) << endl;
cout << (a >> 2) << endl;
return 0;
Other Operators

#include <iostream>
using namespace std;

int main(){

//Sizeof Operator
int a = 10;
cout<<sizeof(a)<<endl;

//Ternary Operator
int b = 10;
int c = 20;

b>c? cout<<"b is greater":cout<<"c is greater";

return 0;
}

Conditional Statements - if , else if , else

#include <iostream>
using namespace std;

int main(){
//If statement
int n=4;
//if condition is true
if(n<5)
{
cout<<"Less than 5"<<endl;
}
cout<<"Exit"<<endl;

int m=6;
//if condition is false
if(m<5)
{
cout<<"Less than 5"<<endl;
}
cout<<"Exit"<<endl;
//If else

int x=4;

//if condition is true


if(x<5)
{
cout<<"Less than 5"<<endl;
}
//if condition is false
else
{
cout<<"Greater than 5"<<endl;
}

//if else if else

int y=6;

//if condition is true


if(y<5)
{
cout<<"Less than 5"<<endl;
}
//if 2nd condition is true
else if(y>5)
{
cout<<"Greater than 5"<<endl;
}
//if all of above conditions are false
else
{
cout<<"Equal to 5"<<endl;
}

return 0;
}
Switch Statement

#include <iostream>
using namespace std;

int main(){

int day;
cin >> day;
switch(day){
case 1:
cout << "Monday";
break;
case 2:
cout << "Tuesday";
break;
case 3:
cout << "Wednesday";
break;
case 4:
cout << "Thursday";
break;
case 5:
cout << "Friday";
break;
case 6:
cout << "Saturday";
break;
case 7:
cout << "Sunday";
break;
default:
cout << "Invalid day";
}
return 0;
}
Loops

#include <iostream>
using namespace std;

int main(){

//For Loop
for(int i = 0; i < 10; i++){
cout << i << endl;
}

//While Loop
int i = 0;
while(i < 10){
cout << i << endl;
i++;
}

//Do While Loop


int i = 0;
do{
cout << i << endl;
i++;
}while(i < 10);

return 0;
}

Break and Continue Statement

#include <iostream>
using namespace std;

int main(){

//Break Statement
for(int i = 0; i < 10; i++){
if(i == 5){
break;
}
cout << i << endl;
}
//Continue Statement
for(int i = 0; i < 10; i++){
if(i == 5){
continue;
}
cout << i << endl;
}

return 0;
}

Patterns

#include <iostream>
using namespace std;

int main(){

//Square Pattern
int n=4;
// cin>>n;
for(int i=1;i<=n;i++){
for(int j=1;j<=n;j++){
cout<<'*'<<" ";
}
cout<<endl;
}

//Triangle Pattern
for(int i=1;i<=n;i++){
for(int j=1;j<=i;j++){
cout<<'*'<<" ";
}
cout<<endl;
}

//Right Triangle Pattern


for(int i=1;i<=n;i++){
for(int j=1;j<=i;j++){
cout<<j<<" ";
}
cout<<endl;
}

return 0;
}
Homework Question

1. Write a C++ program to take two numbers as input and display their
sum, difference, product, and quotient using arithmetic operators.
2. Write a C++ program to check whether a number is positive,
negative, or zero using if-else statements.
3. Write a C++ program to check whether a given number is even or
odd.
4. Write a C++ program to calculate the sum of the first 5 natural
numbers (1, 2, 3, 4, 5) using a for loop.
5. Write a C++ program to print numbers from 1 to 5, but skip printing 3
using a continue statement.
6. Write a C++ program to print the numbers from 1 to 10 using a
do-while loop. (try this using both for loop and while loop as well)
7. Write a C++ program that takes numbers as input from the user. If the
user enters a negative number, the program should stop taking inputs
(use break statement).
8. Write a C++ program that prints only even numbers from 1 to 10
using a for loop and the continue statement.
9. Write a C++ program using a switch-case statement to print
"Hello" if the user inputs 1, "Goodbye" if they input 2, and "Invalid" for
any other input.
10. Write a C++ program that implements a calculator using switch
statement, allowing the user to choose addition, subtraction,
multiplication, division, or exit, and handle division by zero.

You might also like