Experiment 02
AIM: Implementing various programs using operators, expressions and input/output operations
Task 1: Write a program to initialize your details like age, name, gender, city, height etc and display it. (for name & city use character array)
#include <iostream>
using namespace std;
int main()
{
int age = 17;
char name[10] = "Kundan";
string gender = "Male";
string city = "Shahada";
int height = 182; // In cm
cout << "Name : " << name << endl
<< "Age : " << age << endl
<< "Gender: " << gender << endl
<< "City : " << city << endl
<< "Height: " << height << "cm";
}
Task 2: Write a program to read your details like age, name, gender, city, height etc and display it.
#include <iostream>
using namespace std;
int main()
{
int age;
string name;
string gender;
string city;
int height; // In cm
cout << "Enter the Name: ";
cin >> name;
cout << "Enter the Age: ";
cin >> age;
cout << "Enter the City: ";
cin >> city;
cout << "Enter the Gender: ";
cin >> gender;
cout << "Enter the Height (in cm): ";
cin >> height;
cout << "Name : " << name << endl;
cout << "Age : " << age << endl;
cout << "Gender: " << gender << endl;
cout << "City : " << city << endl;
cout << "Height: " << height << "cm";
return 0;
}
Task 3: Write a program to exchange values of two variables without using 3rd variable
#include <iostream>
using namespace std;
int main()
{
int no1;
int no2;
cout << "Enter the First Number : ";
cin >> no1;
cout << "Enter the Second Number: ";
cin >> no2;
no1 = no1 + no2;
no2 = no1 - no2;
no1 = no1 - no2;
cout << "No1: " << no1 << endl;
cout << "No2: " << no2;
return 0;
}
Task 4: Given the value of x, y, and z. Write a program to rotate their values such that x has value of y, y has value of z and z has value of x.
#include <iostream>
using namespace std;
int main()
{
int x, y, z, a;
cout << "Enter the value of X : ";
cin >> x;
cout << "Enter the value of Y : ";
cin >> y;
cout << "Enter the value of Z : ";
cin >> z;
a = x;
x = y;
y = z;
z = a;
cout << "X: " << x << endl;
cout << "Y: " << y << endl;
cout << "Z: " << z;
return 0;
}
Task 5: Write a program to find area & perimeter of a circle
#include <iostream>
using namespace std;
int main()
{
int r, a, c;
cout << "Enter the Radius of circle : ";
cin >> r;
a = 22/7 * r * r;
c = 22/7 * 2 * r;
cout << endl << "Area of the circle is: " << a << " cm square" << endl;
cout << "Circumference of the Circle is: " << c << " cm";
return 0;
}
Task 6: Write a program to calculate simple interest.
#include <iostream>
using namespace std;
int main()
{
int p, r, t, si;
cout << "Enter the Principal amount : ";
cin >> p;
cout << "Enter the Rate of Interest : ";
cin >> r;
cout << "Enter the Number of Years : ";
cin >> t;
si = p * r * t / 100;
cout << endl << "The Simple Interest is: " << si;
return 0;
}
Task 7: Write a program to convert temperature in Fahrenheit to Celsius.
#include <iostream>
using namespace std;
int main()
{
float tf, tc;
cout << "Enter the Temperature in Fahrenheit : ";
cin >> tf;
tc = (tf - 32) * 5/9;
cout << endl << "The Temperature in Celsius is: " << tc;
return 0;
}
Task 8: A four-digit number is inputted through the keyboard. Write a program to calculate sum of digits of a number.
#include <iostream>
using namespace std;
int main()
{
int no1, no2, sum;
cout << "Enter the Four-Digit Number : ";
cin >> no1;
sum = 0, no2 = no1;
sum = sum + (no1 % 10);
no1 = no1 / 10;
sum = sum + (no1 % 10);
no1 = no1 / 10;
sum = sum + (no1 % 10);
no1 = no1 / 10;
sum = sum + (no1 % 10);
cout << endl << "The Sum of the digits of " << no2 << " is: " << sum;
return 0;
}
Task 9: A four-digit number is inputted through the keyboard. Write a program to reverse the number.
#include <iostream>
using namespace std;
int main()
{
int no1, no2, revno;
cout << "Enter the Four-Digit Number : ";
cin >> no1;
revno = 0, no2 = no1;
revno = revno + (no1 % 10);
no1 = no1 / 10;
revno = revno * 10 + (no1 % 10);
no1 = no1 / 10;
revno = revno * 10 + (no1 % 10);
no1 = no1 / 10;
revno = revno * 10 + (no1 % 10);
cout << endl << "The Reverse of the digits of " << no2 << " is: " << revno;
return 0;
}
Task 10: Write a program to find largest of two numbers using ternary operator.
#include <iostream>
using namespace std;
int main()
{
int no1, no2;
cout << "Enter No1 : ";
cin >> no1;
cout << "Enter No2 : ";
cin >> no2;
(no1 > no2) ? cout << no1 << " is Greater" : cout << no2 << " is Greater";
return 0;
}
Task 11: If the length of three sides of a triangle are input through the keyboard, write a program to find the area of triangle and check whether the triangle is valid or not using conditional operator. Hint:- A triangle is valid if the sum of its two sides is greater than the third side.
#include <iostream>
using namespace std;
#include <math.h>
int main()
{
float a, b, c, s, ar;
cout << "Enter A : ";
cin >> a;
cout << "Enter B : ";
cin >> b;
cout << "Enter C : ";
cin >> c;
s = (a + b + c) / 2;
ar = sqrt(s * (s - a) * (s - b) * (s - c));
(a + b > c && b + c > a && a + c > b) ? cout << "Triangle is Valid " << endl << "Area of the triangle is " << ar : cout << "Triangle is Invalid";
return 0;
}
Task 12: Write a program to calculate compound interest.
#include <iostream>
using namespace std;
#include <math.h>
int main()
{
int p, r, n, t, ci;
cout << "Enter the Principal amount : ";
cin >> p;
cout << "Enter the Rate of Interest : ";
cin >> r;
cout << "Enter the number of Compounding per year : ";
cin >> n;
cout << "Enter the Number of Years : ";
cin >> t;
ci = p * pow(1 + (r/n), n*t);
cout << endl << "The Compound Interest is: " << ci;
return 0;
}