SIG - Day2
SIG - Day2
#include <iostream>
using namespace std;
int main(){
int a;
float b;
double c;
char d;
bool e;
string f;
return 0;
}
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;
return 0;
}
Logical Operators
#include <iostream>
using namespace std;
int main()
{
bool a=true, b=false;
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;
return 0;
}
#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;
int y=6;
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++;
}
return 0;
}
#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;
}
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.