Archive for March 2019
Write a program in C++ to find area of Triangle, Rectangle or Circle using IF ELSE (must be menu driven)
#include <iostream.h>
#include <conio.h>
int main() {
int shape, a, b;
char ans;
Repeat:
system("CLS");
cout << "Select Shape:" << endl;
cout << "1.Triangle" << endl;
cout << "2.Rectangle" << endl;
cout << "3.Circle" << endl;
cin >> shape;
if(shape==1) {
cout << "Base: ";
cin >> a;
cout << "Altitude: ";
cin >> b;
cout << endl << "Area of Triangle: " << 0.5*a*b;
} else if(shape==2) {
cout << "Length: ";
cin >> a;
cout << "Breadth: ";
cin >> b;
cout << endl << "Area of Rectangle: " << a*b;
} else if(shape==3) {
cout << "Radius: ";
cin >> a;
cout << endl << "Area of Circle: " << 3.14*a*a;
} else {
cout << "Invalid Shape";
}
cout << endl << "Do you want more calculation? (y/n)";
cin >> ans;
if (ans=='y') {
goto Repeat;
}
getch();
return 0;
}
Output:
Write a program in C++ to find whether the entered year is LEAP YEAR or not
#include <iostream.h>
#include <conio.h>
int main() {
int year;
cout << "Enter Year: ";
cin >> year;
(year%4)==0 ? cout<<"Leap Year" : cout<<"NOT Leap Year";
getch();
return 0;
}
Output:
Write a program in C++ to find SIMPLE INTEREST
#include <iostream.h>
#include <conio.h>
int main() {
//p=principle, r=rate, t=time in years
float p, r, t;
cout << "Principle: ";
cin >> p;
cout << "Rate: ";
cin >> r;
cout << "Time: ";
cin >> t;
cout << endl << "Simple Interest:" << p*r*t/100;
getch();
return 0;
}
Output:
Write a program in C++ to find highest of 3 numbers using conditional operator
#include <iostream.h>
#include <conio.h>
int main() {
int a, b, c, big;
cout << "First No: ";
cin >> a;
cout << "Second No: ";
cin >> b;
cout << "Third No: ";
cin >> c;
big = a > b ? ( a > c ? a : c) : (b > c ? b : c) ;
cout << "Highest Number: " << big;
getch();
return 0;
}
