Pps Lab Expt2 E028
Pps Lab Expt2 E028
Pps Lab Expt2 E028
Experiment: 2
PART B
Students must execute all the programs, write executed code in the workbook, and submit
part B of experiment 2 on the student portal. The filename should be
PPS_batch_rollno_experimentno. Example: PPS_A1_A001_P1
Tasks:
1. Write a program to initialize your details like age, name, gender, city, height etc
and display it. (for name & city use character array ex. char name [20])
2. Write a program to read your details like age, name, gender, city, height etc and
display it.
3. Write a program to exchange values of two variables without using 3rd variable
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.
5. Write a program to find area & perimeter of a circle
6. Write a program to calculate simple interest.
7. Write a program to convert temperature in Celsius to Fahrenheit.
8. A four-digit number is inputted through the keyboard. Write a program to calculate
sum of digits of a number.
9. A four-digit number is inputted through the keyboard. Write a program to reverse
the number.
10. Write a program to find largest of two numbers using ternary operator.
11. If the length of three sides of a triangle is 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
SVKM’s NMIMS
Mukesh Patel School of Technology Management & Engineering /
School of Technology Management & Engineering
1. Write a program to initialize your details like age, name, gender, city, height etc
and display it. (for name & city use character array ex. char name [20])
Executed Code: -
// Paste the executed code here
#include <iostream>
int main()
double height=153.0;
cout<<"name: "<<name<<endl;
cout<<"age: "<<age<<endl;
cout<<"gender: "<<gender<<endl;
cout<<"city: "<<city<<endl;
cout<<"height: "<<height<<"cm"<<endl;
return 0;
Input Output: -
// Paste the input/output of executed code
name: Poorva
SVKM’s NMIMS
Mukesh Patel School of Technology Management & Engineering /
School of Technology Management & Engineering
age: 18
gender: Female
city: Mumbai
height: 153cm
2. Write a program to read your details like age, name, gender, city, height etc and
display it.
Executed Code: -
// Paste the executed code here
#include <iostream>
int main()
double height;
cin>>name;
cin>>gender;
cin>>city;
cin>>age;
SVKM’s NMIMS
Mukesh Patel School of Technology Management & Engineering /
School of Technology Management & Engineering
cin>>height;
cout<<"name: "<<name<<endl;
cout<<"gender: "<<gender<<endl;
cout<<"city: "<<city<<endl;
cout<<"age: "<<age<<endl;
cout<<"height: "<<height<<endl;
return 0;
Input Output: -
// Paste the input/output of executed code
3. Write a program to exchange values of two variables without using 3rd variable
SVKM’s NMIMS
Mukesh Patel School of Technology Management & Engineering /
School of Technology Management & Engineering
Executed Code: -
// Paste the executed code here
#include <iostream>
int main()
int a,b;
cin>>a;
cin>>b;
a=a+b;
b=a-b;
a=a-b;
return 0;
Input Output: -
// Paste the input/output of executed code
enter the first number
47
enter the second number
154
after swapping, the first number is 154
SVKM’s NMIMS
Mukesh Patel School of Technology Management & Engineering /
School of Technology Management & Engineering
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.
Executed Code: -
// Paste the executed code here
#include <iostream>
int main()
cin>>x;
cin>>y;
cin>>z;
temp=x;
x=y;
y=z;
z=temp;
return 0;
Input Output: -
// Paste the input/output of executed code
enter the first number:
12
enter the second number:
14
enter the third number:
17
numbers before rotating are: 12 14 17
numbers after rotating are: 14 17 12
#include <iostream>
int main()
cin>>radius;
circumference=2*3.14*radius;
SVKM’s NMIMS
Mukesh Patel School of Technology Management & Engineering /
School of Technology Management & Engineering
area=3.14*radius*radius;
return 0;
Input Output: -
// Paste the input/output of executed code
enter the radius of the circle:
6
circumference of the circle is 37.68
area of the circle is 113.04
int main()
cin>>p;
cin>>r;
SVKM’s NMIMS
Mukesh Patel School of Technology Management & Engineering /
School of Technology Management & Engineering
cin>>t;
SI=(p*r*t)/100;
return 0;
Input Output: -
// Paste the input/output of executed code
enter the principal amount
10000
enter the rate
4
enter the time
5
Simple interest = 2000
int main()
double c,f;
SVKM’s NMIMS
Mukesh Patel School of Technology Management & Engineering /
School of Technology Management & Engineering
cin>>c;
f=(c*(9.0/5.0))+32.0;
return 0;
Input Output: -
// Paste the input/output of executed code
enter the temperature in celsius
15
converted temperature in fahrenheit is 59
int main()
int num,sum=0,cnt=0;
cin>>num;
SVKM’s NMIMS
Mukesh Patel School of Technology Management & Engineering /
School of Technology Management & Engineering
int temp=num;
while(temp>0)
temp/=10;
cnt++;
if(cnt==4)
while(num>0)
sum=sum+(num%10);
num/=10;
else
return 0;
Input Output: -
// Paste the input/output of executed code
SVKM’s NMIMS
Mukesh Patel School of Technology Management & Engineering /
School of Technology Management & Engineering
#include <iostream>
int main()
cin>>num;
temp=num;
while(temp>0)
temp/=10;
SVKM’s NMIMS
Mukesh Patel School of Technology Management & Engineering /
School of Technology Management & Engineering
cnt++;
if(cnt==4)
while(num>0)
rem=num%10;
rev=rev*10+rem;
num/=10;
else
return 0;
Input Output: -
// Paste the input/output of executed code
enter a number 3245
reversed four digit number is: 5423
10. Write a program to find largest of two numbers using ternary operator.
Executed Code: -
// Paste the executed code here
#include <iostream>
int main()
int a, b,largest;
cin>>a;
cin>>b;
largest= (a>b)?a:b;
return 0;
Input Output: -
// Paste the input/output of executed code
11. If the length of three sides of a triangle is 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.
Executed Code: -
// Paste the executed code here
#include <iostream>
#include <cmath>
int main()
double a,b,c,area,s;
cin>>a;
cin>>b;
cin>>c;
cout<<endl;
s=(a+b+c)/2;
SVKM’s NMIMS
Mukesh Patel School of Technology Management & Engineering /
School of Technology Management & Engineering
area=sqrt(s*(s-a)*(s-b)*(s-c));
else
return 0;
Input Output: -
// Paste the input/output of executed code
triangle is valid
area of the triangle is: 16.2481
#include <cmath>
int main()
double A,P,r,t,CI;
int n;
cin>>P;
cin>>r;
cin>>t;
cin>>n;
A=P*pow((1+(r/(n*100))),(n*t));
CI=A-P;
return 0;
Input Output: -
SVKM’s NMIMS
Mukesh Patel School of Technology Management & Engineering /
School of Technology Management & Engineering
Question of Curiosity
[To be answered by student based on the practical performed and learning/observations]
#include <cmath>
SVKM’s NMIMS
Mukesh Patel School of Technology Management & Engineering /
School of Technology Management & Engineering
int main()
int C,a,b;
cin>>a;
cin>>b;
C=(pow(a,2)+pow(b,2));
cout<<"C=(a^2+b^2)= "<<C<<endl;
return 0;
b. a 2+ b2=C
#include <iostream>
#include <cmath>
int main()
int C,a,b;
cin>>a;
cin>>b;
C=(pow(a,2)+pow(b,2));
cout<<"a^2+b^2= "<<C<<endl;
return 0;
c. r =a ( b+ c )
#include <iostream>
#include <cmath>
int main()
int r,a,b,c;
cin>>a;
cin>>b;
cin>>c;
r=a*(b+c);
cout<<"r= "<<r<<endl;
return 0;
d. r =ab+ ac
#include <iostream>
SVKM’s NMIMS
Mukesh Patel School of Technology Management & Engineering /
School of Technology Management & Engineering
#include <cmath>
int main()
int r,a,b,c;
cin>>a;
cin>>b;
cin>>c;
r=((a*b)+(a*c));
cout<<"r= "<<r<<endl;
return 0;
1
e. A= bh
2
#include <iostream>
#include <cmath>
int main()
double A,h,b;
cin>>b;
cin>>h;
A=(0.5)*b*h;
cout<<"area is "<<A<<endl;
return 0;
π
f. r =
2
#include<iostream>
#include<cmath>
int main()
double r;
r=3.14/2;
cout<<"r= "<<r;
return 0;
g. A=π r 2
#include <iostream>
#include <cmath>
int main()
SVKM’s NMIMS
Mukesh Patel School of Technology Management & Engineering /
School of Technology Management & Engineering
double A,r;
cin>>r;
A=((3.14)*pow(r,2));
return 0;
h. S=4 π r 2
#include <iostream>
#include <cmath>
int main()
double S,r;
cin>>r;
S=((4*3.14)*pow(r,2));
return 0;
n
a
i. r = n
b
SVKM’s NMIMS
Mukesh Patel School of Technology Management & Engineering /
School of Technology Management & Engineering
#include <iostream>
#include <cmath>
int main()
double a,b,n,r;
cin>>a;
cin>>b;
cin>>n;
r=(pow(a,n)/pow(b,n));
cout<<"r= "<<r;
return 0;
( )
nt
r
j. A=P 1+
n
#include<iostream>
#include<cmath>
using namespace std;
int main()
{ double A, P, r,n,t;
SVKM’s NMIMS
Mukesh Patel School of Technology Management & Engineering /
School of Technology Management & Engineering
cout<<"Enter P: ";
cin>>P;
cout<<"Enter r: ";
cin>>r;
cout<<"Enter n: ";
cin>>n;
cout<<"Enter t: ";
cin>>t;
A = P*pow(1+r/n,n*t);
cout<<"A = "<<A<<endl;
return 0;
}
−b ± √ b2−4 ac
k. x=
2a
#include <iostream>
#include <cmath>
using namespace std;
int main()
{
double a, b, c;
double root1, root2, discriminant, realPart, imaginaryPart;
discriminant = b * b - 4 * a * c;
if (discriminant > 0)
{
root1 = (-b + sqrt(discriminant)) / (2 * a);
root2 = (-b - sqrt(discriminant)) / (2 * a);
cout << "Roots are real and different." << endl;
cout << "Root 1 = " << root1 << endl;
cout << "Root 2 = " << root2 << endl;
}
else if (discriminant == 0)
{
root1 = -b / (2 * a);
cout << "Roots are real and the same." << endl;
cout << "Root 1 = Root 2 = " << root1 << endl;
SVKM’s NMIMS
Mukesh Patel School of Technology Management & Engineering /
School of Technology Management & Engineering
}
else
{
realPart = -b / (2 * a);
imaginaryPart = sqrt(-discriminant) / (2 * a);
cout << "Roots are complex and different." << endl;
cout << "Root 1 = " << realPart << " + " << imaginaryPart << "i" << endl;
cout << "Root 2 = " << realPart << " - " << imaginaryPart << "i" << endl;
}
return 0;
}