OOPS with C++ Assignment
Name : Sohom Ghorai Regd No : 190301120001
1 . Program to display the details of user (like name, age, sex and salary…).
Code :
//Program to display user entered details .
#include<iostream>
#include<string>
using namespace std ;
int main()
{
string n , se;
int a , sa ;
cout << "What's your name? : ";
getline (cin, n);
cout << "What is your sex ? : ";
getline (cin, se);
cout << "What is your age ? : ";
cin >> a;
cout << "What is your salary ? : ";
cin >> sa;
cout << "Hello " << n ;
cout << "\n Your age is : " << a ;
cout << "\n Your sex is : " << se ;
cout << "\n Your salary is : " << sa ;
return 0 ;
}
Output :
What's your name? : Sohom
What is your sex ? : Male
What is your age ? : 19
What is your salary ? : 12000
Hello Sohom
Your age is : 19
Your sex is : Male
Your salary is : 12000
2 . Write a program to read 2 integers and find the addition, subtraction,
multiplication and division .
Code :
// Calculator program
#include<iostream>
using namespace std ;
int main()
{
int a , b , sum , sub , mul , divs ;
cout << "Enter First Number : " ;
cin >> a;
cout << "Enter Second Number : " ;
cin >> b ;
sum = a+b ;
sub = a-b ;
mul = a*b ;
divs = a/b ;
cout << "Sum of two numbers is : " << sum ;
cout << "\nSubtraction of two numbers is : " << sub ;
cout << "\nMultiplication of two numbers is : " << mul ;
cout << "\nDivision of two numbers is : " << divs ;
return 0 ;
Output :
Enter First Number : 12
Enter Second Number : 4
Sum of two numbers is : 16
Subtraction of two numbers is : 8
Multiplication of two numbers is : 48
Division of two numbers is : 3
3 . Program to find area and circumference of circle.
Code :
//Program to find area and circumference of circle.
#include<iostream>
using namespace std ;
int main()
{
float r , a , c ;
cout << "Enter the radius of the circle " ;
cin >> r ;
a = 3.14*r*r ;
c = 2*3.14*r ;
cout << "\n Area of the circle is : " << a ;
cout << "\n Circumference of the circle is : " << c ;
return 0 ;
}
Output :
Enter the radius of the circle 4
Area of the circle is : 50.24
Circumference of the circle is : 25.12
4 . Program to find the simple interest.
Code :
#include<iostream>
using namespace std ;
int main()
{
float a, r , t, si , total ;
cout<<"Enter Principal Amount: ";
cin>>a;
cout<<"Enter Rate of Interest: ";
cin>>r;
cout<<"Enter Period of Time: ";
cin>>t;
si = (a*r*t) / 100;
total = a+si ;
cout<<"Simple Interest: "<<si;
cout<<"\nTotal amount after adding interest : "<< total ;
return 0 ;
}
Output :
Enter Principal Amount: 1200
Enter Rate of Interest: 5
Enter Period of Time: 3
Simple Interest: 180
Total amount after adding interest : 1380
5 . Write a C++ program to find the area of trapezium.
Code :
#include<iostream>
using namespace std ;
int main()
{
float b1 , b2 , h , a ;
cout << " Enter the base 1 of the trapezium : " ;
cin>> b1 ;
cout << " Enter the base 2 of the trapezium : " ;
cin>> b2 ;
cout << " Enter the height of the trapezium : " ;
cin>> h ;
a = 0.5*(b1+b2)*h ;
cout<< " Area of Trapezium is : " << a ;
return 0 ;
}
Output :
Enter the base 1 of the trapezium : 12
Enter the base 2 of the trapezium : 13.5
Enter the height of the trapezium : 6.8
Area of Trapezium is : 86.7
6. Write a C++ program to find the area of rhombus.
Code :
#include<iostream>
using namespace std ;
int main()
{
float d1 , d2 , a ;
cout << " Enter the first diagonal length of the rhombus : " ;
cin >> d1 ;
cout << " Enter the second diagonal length of the rhombus : " ;
cin >> d2 ;
a = 0.5*d1*d2 ;
cout << " Area of the rhombus is : " << a ;
return 0 ;
}
Output :
Enter the first diagonal length of the rhombus : 12
Enter the second diagonal length of the rhombus : 13
Area of the rhombus is : 78
7 . Write C++ program to find the area of rectangle.
Code
#include<iostream>
using namespace std ;
int main()
{
float l , w , a ;
cout << " Enter the length of the rectangle : " ;
cin >> l ;
cout << " Enter the width of the rectangle : " ;
cin >> w ;
a = l*w ;
cout << " Area of the rectangle is : " << a ;
return 0 ;
}
Output :
Enter the length of the rectangle : 6
Enter the width of the rectangle : 5
Area of the rectangle is : 30
8 . Write a C++ program to find the volume and surface area of cube.
Code :
#include<iostream>
using namespace std ;
int main ()
{
float l , SA , V ;
cout << " Enter the length of the cube : " ;
cin >> l ;
SA = 6*(l*l) ;
V = l*l*l ;
cout << " Surface area of the cube is : " << SA ;
cout << "\nVolume of the cube is : " << V ;
return 0 ;
}
Output :
Enter the length of the cube : 5
Surface area of the cube is : 150
Volume of the cube is : 125
9. Write a C++ program to find the volume and surface area of cylinder.
Code :
#include <iostream>
using namespace std;
int main()
{
float r ,h , sa , vol ;
cout<<" Input the radius of the cylinder : ";
cin>> r;
cout<<" Input the height of the cylinder : ";
cin>>h;
sa = (2*3.14*r*h)+(2*3.14*r*r*r);
vol=(3.14*r*r*h);
cout << " The surface area of the cylinder is : " << sa ;
cout<<"\n The volume of a cylinder is : "<< vol ;
return 0;
}
Output :
Input the radius of the cylinder : 5
Input the height of the cylinder : 6
The surface area of the cylinder is : 973.4
The volume of a cylinder is : 471
10 . Write a C++ program to find the volume and surface area of sphere.
Code :
#include<iostream>
using namespace std ;
const float pi=3.14;
int main()
{
float r , a , v ;
cout << " Enter the radis of the sphere : " ;
cin >> r ;
a = pi*r*r ;
v = 4/3*pi*r*r*r;
cout << " Area of the sphere : " << a;
cout << "\n Volume of the sphere : "<< v ;
return 0 ;
Output :
Enter the radis of the sphere : 5
Area of the sphere : 78.5
Volume of the sphere : 392.5
11. Program to convert temperature from degree centigrade to Fahrenheit.
Code :
#include<iostream>
using namespace std ;
int main()
{
float cel , far ;
cout<<" Convert Temparature from degree celcius to fahrenheit \n" ;
cout<<" Enter the Temparature in Degree Celcius : " ;
cin>> cel ;
far = (cel*9.0)/5.0+32 ;
cout<<"\nTemparature after conversion : " << far ;
return 0;
}
Output :
Convert Temparature from degree celcius to fahrenheit
Enter the Temparature in Degree Celcius : 40
Temparature after conversion : 104
12 . Program to calculate sum of 5 subjects & find percentage.
Code :
//Program to calculate sum of 5 subjects & find percentage.
#include<iostream>
using namespace std ;
int main()
{
float a , b , c, d , e , tmp , sum , per ;
cout << " Enter the mark of first subject : " ;
cin>> a ;
cout << "\nEnter the mark of second subject : " ;
cin>> b ;
cout << "\nEnter the mark of third subject : " ;
cin>> c ;
cout << "\nEnter the mark of fouth subject : " ;
cin>> d ;
cout << "\nEnter the mark of fifth subject : " ;
cin>> e ;
tmp = 5*100 ;
sum = a+b+c+d+e ;
per = (sum*100)/tmp ;
cout << "\nThe percentage is : "<< per ;
return 0 ;
}
Output :
Enter the mark of first subject : 78
Enter the mark of second subject : 84
Enter the mark of third subject : 87
Enter the mark of fouth subject : 92
Enter the mark of fifth subject : 81
The percentage is : 84.4
13 . Program to find gross salary. ( Using salary + HRA +DA) .
Code :
#include<iostream>
using namespace std;
int main()
{
float GrossPayment,basic,da,hra,da1,hra1;
cout<<" Enter basic salary : ";
cin>>basic;
cout<<" Enter DA : ";
cin>>da1;
cout<<" Enter HRA : ";
cin>>hra1;
da = (da1 * basic) / 100;
hra = (hra1 * basic) / 100;
GrossPayment = basic + da + hra;
cout<<"\n Gross Salary :"<<GrossPayment ;
return 0 ;
}
Output :
Enter basic salary : 45000
Enter DA : 6
Enter HRA : 10
Gross Salary :52200
14 . Program to show swap of two no’s without using third variable.
Code :
#include<iostream>
using namespace std;
int main ()
{
float a , b ;
cout << "Swap two numbers without using third variable " ;
cout << "\nEnter first number : " ;
cin >> a ;
cout << "\nEnter second number : " ;
cin >> b ;
cout << "\nBefore swapping first number is : " << a << "\t & second number is : " << b ;
a = a+b ;
b = a-b ;
a = a-b ;
cout << "\nAftre swapping first number is : " << a << "\t & second number is : " << b ;
return 0 ;
}
Output :
Swap two numbers without using third variable
Enter first number : 5
Enter second number : 6
Before swapping first number is : 5 & second number is : 6
Aftre swapping first number is : 6 & second number is : 5
15 . Program to read a number and display its square and cube of that
number.
Code :
//Program to read a number and display its square and cube of that number.
#include<iostream>
using namespace std ;
int main()
{
float a , sq , cub ;
cout << "Enter the your desired number : " ;
cin >> a ;
sq = a*a ;
cub = a*a*a ;
cout << "Square of the number is : " << sq ;
cout << "\nCube of the number is : " << cub ;
return 0 ;
}
Output :
Enter the your desired number : 5
Square of the number is : 25
Cube of the number is : 125
16 . Program to find the biggest number between 2 integers.
Code :
#include <iostream>
using namespace std;
int main()
{
int num1, num2;
cout<<"Enter first number:";
cin>>num1;
cout<<"Enter second number:";
cin>>num2;
if(num1>num2)
{
cout<<"First number "<<num1<<" is the largest";
}
else
{
cout<<"Second number "<<num2<<" is the largest";
}
return 0;
}
Output :
Enter first number:45
Enter second number:35
First number 45 is the largest
17 . Program to find that whether the given number is even or odd.
Code :
// Program to find that whether the given number is even or odd.
#include<iostream>
using namespace std ;
int main()
{
int num ;
cout << "Check whether a number is even or odd " ;
cout << "\nEnter a number to check : " ;
cin >> num ;
if (num % 2 == 0)
{
cout << "\nThe number "<< num << " is even. " ;
}
else
{
cout << "\nThe number "<< num << " is odd . " ;
}
return 0 ;
}
Output :
Check whether a number is even or odd
Enter a number to check : 55
The number 55 is odd .
18 . Program to find that whether the given number is positive or negative
Code :
//Program to find that whether the given number is positive or negative or zero
#include<iostream>
using namespace std ;
int main()
{
int num ;
cout << "Program to find that whether the given number is positive or negative or zero";
cout << "\nEnter a number to check : " ;
cin >> num ;
if (num > 0 )
{
cout << "\nThe number " << num << " is postive number ." ;
}
else if (num == 0 )
{
cout << "\nThe number " << num << " is zero ." ;
}
else
{
cout << "\nThe number " << num << " is negative number . " ;
}
return 0 ;
}
Output :
Program to find that whether the given number is positive or negative or zero
Enter a number to check : 85
The number 85 is postive number .
19 . Program to find that eligibility for voting.
Code :
//Elidgibilty of Voting
#include<iostream>
using namespace std ;
int main()
{
int age , w ;
string name ;
cout << " Enter your name : " ;
getline (cin ,name);
cout << "Enter your age : " ;
cin >> age ;
w = 18 - age ;
if (age >= 18)
{
cout << "Congratulations.... " << name << " you are elidgible for voting . " ;
}
else
{
cout << "\nSorry " << name << " you are not yet elidgible , you have to wait " << w << "
more years ." ;
}
return 0 ;
}
Output :
Enter your name : Sohom
Enter your age : 19
Congratulations.... Sohom you are elidgible for voting .
Enter your name : Rik
Enter your age : 13
Sorry Rik you are not yet elidgible , you have to wait 5 more years .
20 . Program to find that whether the triangle is valid or invalid.
Code :
//Program to find that whether the triangle is valid or invalid.
#include<iostream>
using namespace std ;
int main ()
{
int a , b , c ;
cout << "Enter the value of angle 1 of the triangle : " ;
cin >> a ;
cout << "\nEnter the value of angle 2 of the triangle : " ;
cin >> b ;
cout << "\nEnter the value of angle 3 of the triangle : " ;
cin >> c ;
if (a+b+c == 180 )
{
cout << " The triangle is valid " ;
}
else
{
cout << "The triangle is invalid " ;
}
return 0 ;
}
Output :
Enter the value of angle 1 of the triangle : 90
Enter the value of angle 2 of the triangle : 45
Enter the value of angle 3 of the triangle : 45
The triangle is valid
21 . Program to read cp, sp and find that profit or loss.
Code :
//Program to read cp, sp and find that profit or loss.
#include<iostream>
using namespace std ;
int main ()
{
int cp , sp , x , y ;
cout << "Enter the cost price : " ;
cin >> cp ;
cout << "Enter the selling price : " ;
cin >> sp;
if (cp<sp)
{
x = sp-cp ;
cout << "\n Profit = " << x ;
}
else if (cp>sp)
{
y = cp-sp ;
cout << "\n Loss = " << y ;
}
else
{
cout << "\nNo Profit No Loss" ;
}
return 0 ;
}
Output :
Enter the cost price : 1200
Enter the selling price : 1300
Profit = 100
22 . Program to read a character and check whether it is vowel or not.
Code :
#include<iostream>
#include<ctype.h>
using namespace std;
int main ()
{
char ch;
cout << "Enter a character : ";
cin >> ch;
if (isdigit(ch))
cout << "\nThe entered character is a digit.";
else if (isalpha(ch))
{
if ((ch == 'A') || (ch == 'E')|| (ch == 'I')|| (ch == 'O')|| (ch == 'U')|| (ch == 'a')|| (ch == 'e')||
(ch == 'i')|| (ch == 'o')|| (ch == 'u'))
cout << "\nThe entered character is a vowel.";
else
cout << "\nThe entered character is a consonant.";
}
else
cout << "\nThe entered character is a special character.";
return 0;
}
Output :
Enter a character : U
The entered character is a vowel.
23 . Program to find the biggest number among 4 integers .
Code :
#include<iostream>
using namespace std ;
int main ()
{
int a , b , c , d ;
cout << " Enter any four integers : \n" ;
cin >> a >> b >> c >> d ;
if (a > b && a > c && a > d)
cout << "\n The biggest integer is : " << a ;
if (b > a && b > c && b > d)
cout << "\n The biggest integer is : " << b ;
if (c > b && c > a && c > d)
cout << "\n The biggest integer is : " << c ;
if (d > b && d > c && d > a)
cout << "\n The biggest integer is : " << d ;
return 0 ;
}
Output :
Enter any four integers :
56
45
74
82
The biggest integer is : 82
24 . Program to check whether the year is leap year or not.
Code :
#include<iostream>
using namespace std ;
int main ()
{
int year ;
cout << "Enter Year to check whether it is leap year or not : " ;
cin >> year ;
if (year % 4 == 0)
{
if (year % 100 == 0)
{
if (year % 400 == 0)
cout << year << " is a leap year";
else
cout << year << " is not a leap year";
} else
cout << year << " is a leap year";
} else
cout << year << " is not a leap year";
}
Output :
Enter Year to check whether it is leap year or not : 2020
2020 is a leap year
Name : Sohom Ghorai
B.Tech CSE , 3rd Sem
CUTM , BBSR