Pps Lab Expt2 E028

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 27

SVKM’s NMIMS

Mukesh Patel School of Technology Management & Engineering /


School of Technology Management & Engineering

B. Tech/MBA Tech Lab and Workbook Academic Year- 2024-25

Year:-First Subject:- Programming for Problem Solving Semester:- First

Experiment: 2
PART B

(PART A: TO BE COMPLETED AND SUBMITTED BY STUDENTS)

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

Aim: Implementing various programs using operators, expressions and input/output


operations

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

B. Tech/MBA Tech Lab and Workbook Academic Year- 2024-25

Year:-First Subject:- Programming for Problem Solving Semester:- First

greater than the third side.

12. Write a program to calculate compound interest.


SVKM’s NMIMS
Mukesh Patel School of Technology Management & Engineering /
School of Technology Management & Engineering

B. Tech/MBA Tech Lab and Workbook Academic Year- 2024-25

Year:-First Subject:- Programming for Problem Solving Semester:- First

Executed Code, Input and Output

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>

using namespace std;

int main()

char name[]="Poorva ";

unsigned int age=18;

char gender[]="Female ";

char city[]="Mumbai ";

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

B. Tech/MBA Tech Lab and Workbook Academic Year- 2024-25

Year:-First Subject:- Programming for Problem Solving Semester:- First

age: 18
gender: Female
city: Mumbai
height: 153cm

Process returned 0 (0x0) execution time : 0.235 s


Press any key to continue.

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>

using namespace std;

int main()

char name[50], gender[50], city[50];

unsigned int age;

double height;

cout<<"enter your name "<<endl;

cin>>name;

cout<<"enter your gender "<<endl;

cin>>gender;

cout<<"enter your city "<<endl;

cin>>city;

cout<<"enter your age "<<endl;

cin>>age;
SVKM’s NMIMS
Mukesh Patel School of Technology Management & Engineering /
School of Technology Management & Engineering

B. Tech/MBA Tech Lab and Workbook Academic Year- 2024-25

Year:-First Subject:- Programming for Problem Solving Semester:- First

cout<<"enter your height in cm "<<endl;

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

enter your name


poorva
enter your gender
female
enter your city
delhi
enter your age
18
enter your height in cm
157
name: poorva
gender: female
city: delhi
age: 18
height: 157

Process returned 0 (0x0) execution time : 19.411 s


Press any key to continue

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

B. Tech/MBA Tech Lab and Workbook Academic Year- 2024-25

Year:-First Subject:- Programming for Problem Solving Semester:- First

Executed Code: -
// Paste the executed code here
#include <iostream>

using namespace std;

int main()

int a,b;

cout<<"enter the first number "<<endl;

cin>>a;

cout<<"enter the second number "<<endl;

cin>>b;

a=a+b;

b=a-b;

a=a-b;

cout<<"after swapping, the first number is "<<a<<endl;

cout<<"after swapping, the second number is "<<b<<endl;

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

B. Tech/MBA Tech Lab and Workbook Academic Year- 2024-25

Year:-First Subject:- Programming for Problem Solving Semester:- First

after swapping, the second number is 47

Process returned 0 (0x0) execution time : 5.328 s


Press any key to continue.

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>

using namespace std;

int main()

int x,y,z, temp;

cout<<"enter the first number: "<<endl;

cin>>x;

cout<<"enter the second number: "<<endl;

cin>>y;

cout<<"enter the third number: "<<endl;

cin>>z;

cout<<"numbers before rotating are: "<<x<<" "<<y<<" "<<z<<endl;

temp=x;

x=y;

y=z;

z=temp;

cout<<"numbers after rotating are: "<<x<<" "<<y<<" "<<z<<endl;


SVKM’s NMIMS
Mukesh Patel School of Technology Management & Engineering /
School of Technology Management & Engineering

B. Tech/MBA Tech Lab and Workbook Academic Year- 2024-25

Year:-First Subject:- Programming for Problem Solving Semester:- First

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

Process returned 0 (0x0) execution time : 9.102 s


Press any key to continue.

5. Write a program to find area & perimeter of a circle


Executed Code: -
// Paste the executed code here

#include <iostream>

using namespace std;

int main()

double radius, circumference, area;

cout<<"enter the radius of the circle: "<<endl;

cin>>radius;

circumference=2*3.14*radius;
SVKM’s NMIMS
Mukesh Patel School of Technology Management & Engineering /
School of Technology Management & Engineering

B. Tech/MBA Tech Lab and Workbook Academic Year- 2024-25

Year:-First Subject:- Programming for Problem Solving Semester:- First

area=3.14*radius*radius;

cout<< "circumference of the circle is "<<circumference<<endl;

cout<< "area of the circle is "<<area<<endl;

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

Process returned 0 (0x0) execution time : 3.318 s


Press any key to continue.

6. Write a program to calculate simple interest.


Executed Code: -
// Paste the executed code here
#include <iostream>

using namespace std;

int main()

double p,r,t, SI;

cout<<"enter the principal amount "<<endl;

cin>>p;

cout<<"enter the rate "<<endl;

cin>>r;
SVKM’s NMIMS
Mukesh Patel School of Technology Management & Engineering /
School of Technology Management & Engineering

B. Tech/MBA Tech Lab and Workbook Academic Year- 2024-25

Year:-First Subject:- Programming for Problem Solving Semester:- First

cout<< "enter the time "<<endl;

cin>>t;

SI=(p*r*t)/100;

cout<< "Simple interest = "<<SI<<endl;

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

Process returned 0 (0x0) execution time : 6.418 s


Press any key to continue.

7. Write a program to convert temperature in Celsius to Fahrenheit.


Executed Code: -
// Paste the executed code here
#include <iostream>

using namespace std;

int main()

double c,f;
SVKM’s NMIMS
Mukesh Patel School of Technology Management & Engineering /
School of Technology Management & Engineering

B. Tech/MBA Tech Lab and Workbook Academic Year- 2024-25

Year:-First Subject:- Programming for Problem Solving Semester:- First

cout<< "enter the temperature in celsius "<<endl;

cin>>c;

f=(c*(9.0/5.0))+32.0;

cout<<"converted temperature in fahrenheit is "<<f<<endl;

return 0;

Input Output: -
// Paste the input/output of executed code
enter the temperature in celsius
15
converted temperature in fahrenheit is 59

Process returned 0 (0x0) execution time : 2.514 s


Press any key to continue.

8. A four-digit number is inputted through the keyboard. Write a program to


calculate sum of digits of a number.
Executed Code: -
// Paste the executed code here
#include <iostream>

using namespace std;

int main()

int num,sum=0,cnt=0;

cout<<"enter a four digit number: "<<endl;

cin>>num;
SVKM’s NMIMS
Mukesh Patel School of Technology Management & Engineering /
School of Technology Management & Engineering

B. Tech/MBA Tech Lab and Workbook Academic Year- 2024-25

Year:-First Subject:- Programming for Problem Solving Semester:- First

int temp=num;

while(temp>0)

temp/=10;

cnt++;

if(cnt==4)

while(num>0)

sum=sum+(num%10);

num/=10;

cout<<"sum of the four digit number is "<<sum;

else

cout<<"it is not a four digit number ";

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

B. Tech/MBA Tech Lab and Workbook Academic Year- 2024-25

Year:-First Subject:- Programming for Problem Solving Semester:- First

enter a four digit number:


1245
sum of the four digit number is 12
Process returned 0 (0x0) execution time : 3.056 s
Press any key to continue.

9. A four-digit number is inputted through the keyboard. Write a program to


reverse the number.
Executed Code: -
// Paste the executed code here

#include <iostream>

using namespace std;

int main()

int num,temp,rev=0, rem, cnt=0;

cout<<"enter a number ";

cin>>num;

temp=num;

while(temp>0)

temp/=10;
SVKM’s NMIMS
Mukesh Patel School of Technology Management & Engineering /
School of Technology Management & Engineering

B. Tech/MBA Tech Lab and Workbook Academic Year- 2024-25

Year:-First Subject:- Programming for Problem Solving Semester:- First

cnt++;

if(cnt==4)

while(num>0)

rem=num%10;

rev=rev*10+rem;

num/=10;

cout<<"reversed four digit number is: "<<rev<<endl;

else

cout<<"it is not a four digit number ";

return 0;

Input Output: -
// Paste the input/output of executed code
enter a number 3245
reversed four digit number is: 5423

Process returned 0 (0x0) execution time : 4.181 s


SVKM’s NMIMS
Mukesh Patel School of Technology Management & Engineering /
School of Technology Management & Engineering

B. Tech/MBA Tech Lab and Workbook Academic Year- 2024-25

Year:-First Subject:- Programming for Problem Solving Semester:- First

Press any key to continue.

10. Write a program to find largest of two numbers using ternary operator.
Executed Code: -
// Paste the executed code here
#include <iostream>

using namespace std;

int main()

int a, b,largest;

cout<<"enter the first number ";

cin>>a;

cout<<"enter the second number ";

cin>>b;

largest= (a>b)?a:b;

cout<<"largest number: "<<largest;

return 0;

Input Output: -
// Paste the input/output of executed code

enter the first number 15


enter the second number 47
largest number: 47
Process returned 0 (0x0) execution time : 6.111 s
Press any key to continue.
SVKM’s NMIMS
Mukesh Patel School of Technology Management & Engineering /
School of Technology Management & Engineering

B. Tech/MBA Tech Lab and Workbook Academic Year- 2024-25

Year:-First Subject:- Programming for Problem Solving Semester:- First

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>

using namespace std;

int main()

double a,b,c,area,s;

cout<<"\n enter the first side of the triangle: ";

cin>>a;

cout<<"\n enter the second side of the triangle: ";

cin>>b;

cout<<"\n enter the third side of the triangle: ";

cin>>c;

cout<<endl;

if((a+b)>c || (b+c)>a || (a+c)>b)

cout<<"triangle is valid "<<endl;

s=(a+b+c)/2;
SVKM’s NMIMS
Mukesh Patel School of Technology Management & Engineering /
School of Technology Management & Engineering

B. Tech/MBA Tech Lab and Workbook Academic Year- 2024-25

Year:-First Subject:- Programming for Problem Solving Semester:- First

area=sqrt(s*(s-a)*(s-b)*(s-c));

cout<<"area of the triangle is: "<<area<<endl;

else

cout<<"it is an invalid triangle ";

return 0;

Input Output: -
// Paste the input/output of executed code

enter the first side of the triangle: 7

enter the second side of the triangle: 10

enter the third side of the triangle: 5

triangle is valid
area of the triangle is: 16.2481

Process returned 0 (0x0) execution time : 13.106 s


Press any key to continue.

12. Write a program to calculate compound interest.


Executed Code: -
SVKM’s NMIMS
Mukesh Patel School of Technology Management & Engineering /
School of Technology Management & Engineering

B. Tech/MBA Tech Lab and Workbook Academic Year- 2024-25

Year:-First Subject:- Programming for Problem Solving Semester:- First

// Paste the executed code here


#include <iostream>

#include <cmath>

using namespace std;

int main()

double A,P,r,t,CI;

int n;

cout<<"enter principal amount: "<<endl;

cin>>P;

cout<<"enter rate of interest: "<<endl;

cin>>r;

cout<<"enter time: "<<endl;

cin>>t;

cout<<"enter period: "<<endl;

cin>>n;

A=P*pow((1+(r/(n*100))),(n*t));

CI=A-P;

cout<<"compound interest is: "<<CI;

return 0;

Input Output: -
SVKM’s NMIMS
Mukesh Patel School of Technology Management & Engineering /
School of Technology Management & Engineering

B. Tech/MBA Tech Lab and Workbook Academic Year- 2024-25

Year:-First Subject:- Programming for Problem Solving Semester:- First

// Paste the input/output of executed code


enter principal amount:
10000
enter rate of interest:
5
enter time:
4
enter period:
4
compound interest is: 2198.9
Process returned 0 (0x0) execution time : 7.381 s
Press any key to continue.

Observation and Learning: -


- Write your observation and learning

Question of Curiosity
[To be answered by student based on the practical performed and learning/observations]

1. Convert Following Mathematical Equations to programming equivalent statement


a. C=a 2+ b2
#include <iostream>

#include <cmath>
SVKM’s NMIMS
Mukesh Patel School of Technology Management & Engineering /
School of Technology Management & Engineering

B. Tech/MBA Tech Lab and Workbook Academic Year- 2024-25

Year:-First Subject:- Programming for Problem Solving Semester:- First

using namespace std;

int main()

int C,a,b;

cout<<"enter the first number "<<endl;

cin>>a;

cout<<"enter the second number "<<endl;

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>

using namespace std;

int main()

int C,a,b;

cout<<"enter the first number "<<endl;

cin>>a;

cout<<"enter the second number "<<endl;


SVKM’s NMIMS
Mukesh Patel School of Technology Management & Engineering /
School of Technology Management & Engineering

B. Tech/MBA Tech Lab and Workbook Academic Year- 2024-25

Year:-First Subject:- Programming for Problem Solving Semester:- First

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>

using namespace std;

int main()

int r,a,b,c;

cout<<"enter the first number "<<endl;

cin>>a;

cout<<"enter the second number "<<endl;

cin>>b;

cout<<"enter the third number "<<endl;

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

B. Tech/MBA Tech Lab and Workbook Academic Year- 2024-25

Year:-First Subject:- Programming for Problem Solving Semester:- First

#include <cmath>

using namespace std;

int main()

int r,a,b,c;

cout<<"enter the first number "<<endl;

cin>>a;

cout<<"enter the second number "<<endl;

cin>>b;

cout<<"enter the third number "<<endl;

cin>>c;

r=((a*b)+(a*c));

cout<<"r= "<<r<<endl;

return 0;

1
e. A= bh
2
#include <iostream>

#include <cmath>

using namespace std;

int main()

double A,h,b;

cout<<"enter the base of the triangle "<<endl;


SVKM’s NMIMS
Mukesh Patel School of Technology Management & Engineering /
School of Technology Management & Engineering

B. Tech/MBA Tech Lab and Workbook Academic Year- 2024-25

Year:-First Subject:- Programming for Problem Solving Semester:- First

cin>>b;

cout<<"enter the height of the triangle "<<endl;

cin>>h;

A=(0.5)*b*h;

cout<<"area is "<<A<<endl;

return 0;

π
f. r =
2
#include<iostream>

#include<cmath>

using namespace std;

int main()

double r;

r=3.14/2;

cout<<"r= "<<r;

return 0;

g. A=π r 2
#include <iostream>

#include <cmath>

using namespace std;

int main()
SVKM’s NMIMS
Mukesh Patel School of Technology Management & Engineering /
School of Technology Management & Engineering

B. Tech/MBA Tech Lab and Workbook Academic Year- 2024-25

Year:-First Subject:- Programming for Problem Solving Semester:- First

double A,r;

cout<<"enter the radius of the circle "<<endl;

cin>>r;

A=((3.14)*pow(r,2));

cout<<"area of the circle is "<<A;

return 0;

h. S=4 π r 2
#include <iostream>

#include <cmath>

using namespace std;

int main()

double S,r;

cout<<"enter the radius of the sphere ";

cin>>r;

S=((4*3.14)*pow(r,2));

cout<<"the surface area of the sphere "<<S;

return 0;

n
a
i. r = n
b
SVKM’s NMIMS
Mukesh Patel School of Technology Management & Engineering /
School of Technology Management & Engineering

B. Tech/MBA Tech Lab and Workbook Academic Year- 2024-25

Year:-First Subject:- Programming for Problem Solving Semester:- First

#include <iostream>

#include <cmath>

using namespace std;

int main()

double a,b,n,r;

cout<<"enter the value of the numerator ";

cin>>a;

cout<<"enter the value of the denominator ";

cin>>b;

cout<<"enter the value of the power ";

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

B. Tech/MBA Tech Lab and Workbook Academic Year- 2024-25

Year:-First Subject:- Programming for Problem Solving Semester:- First

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;

cout << "Enter coefficients a, b, and c: ";


cin >> a >> b >> c;

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

B. Tech/MBA Tech Lab and Workbook Academic Year- 2024-25

Year:-First Subject:- Programming for Problem Solving Semester:- First

}
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;
}

You might also like