PPS EXP 2B.docx
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:
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]) | |
Write a program to read your details like age, name, gender, city, height etc and display it. | |
Write a program to exchange values of two variables without using 3rd variable | |
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. | |
Write a program to find area & perimeter of a circle | |
Write a program to calculate simple interest. | |
Write a program to convert temperature in Celsius to Fahrenheit. | |
A four-digit number is inputted through the keyboard. Write a program to calculate sum of digits of a number. | |
A four-digit number is inputted through the keyboard. Write a program to reverse the number. | |
Write a program to find largest of two numbers using ternary operator. | |
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. | |
Write a program to calculate compound interest. |
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: - int main() { int age=17; char name [20]="Anushka Shetiye"; char gender='F'; char city [20]="Mumbai"; float height=5.3; cout<<"The name is "<<name<<endl; cout<<"The age is "<<age<<endl; cout<<"The gender is "<<gender<<endl; cout<<"The height is "<<height<<endl; cout<<"The city is "<<city<<endl; return 0; } Input Output: - | |
2 | Write a program to read your details like age, name, gender, city, height etc and display it. |
Executed Code: - int main() { int age; char name [20]; char gender; float height; cout<<"Enter age: "; cin>>age; cout<<"Enter name: "; cin>>name; cout<<"Enter gender (F/M): "; cin>>gender; cout<<"Enter height: "; cin>>height; cout<<"Age - "<<age<<"\n"; cout<<"Name - "<<name<<"\n"; cout<<"Gender - "<<gender<<"\n"; cout<<"Height - "<<height<<"\n"; return 0; } Input Output: - | |
3 | Write a program to exchange values of two variables without using 3rd variable |
Executed Code: - int main() { int x,y; cout<<"Enter value for x: "; cin>>x; cout<<"Enter value for y: "; cin>>y; { x=x+y; y=x-y; x=x-y; } cout<<"Exchanged value of x: "<<x<<"\n"; cout<<"Exchanged value of y: "<<y; return 0; } Input Output: - | |
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: - int main() { int x; cout<<"Enter value for x: "; cin>>x; int y; cout<<"Enter value for y: "; cin>>y; int z; cout<<"Enter value for z: "; cin>>z; { int a=x; x=y; y=z; z=a; } cout<<"New values of x, y, z are: "<<x<<", "<<y<<", "<<z<<" "; return 0; }
Input Output: - | |
5 | Write a program to find area & perimeter of a circle |
Executed Code: - int main() { int radius; cout<<"Enter radius: "; cin>>radius; float peri=2*3.14*radius; float area=3.14*radius*radius; cout<<"The perimeter of the circle is: "<<peri<<"\n"; cout<<"The area of the circle is: "<<area; return 0; } Input Output: - | |
6 | Write a program to calculate simple interest. |
Executed Code: - int main() { int P,R,T; cout<<"Enter Principal Amount: "; cin>>P; cout<<"Enter Rate: "; cin>>R; cout<<"Enter Time (in years): "; cin>>T; float SI=(P*R*T)/100; cout<<"\nSimple Interest is: "<<SI; return 0; } int main() { int P,R,T; cout<<"Enter Principal Amount: "; cin>>P; cout<<"Enter Rate: "; cin>>R; cout<<"Enter Time (in years): "; cin>>T; float SI=(P*R*T)/100; cout<<"\nSimple Interest is: "<<SI; return 0; } Input Output: - | |
7 | Write a program to convert temperature in Celsius to Fahrenheit. |
Executed Code: - int main() { float temp; cout<<"Enter temperature in Celcius: "; cin>>temp; float fah; fah=temp+32; cout<<"The temperature in Fahrenheit is: "<<fah; return 0; } Input Output: - | |
8 | A four-digit number is inputted through the keyboard. Write a program to calculate sum of digits of a number. |
Executed Code: - int main() { int number, sum = 0; cout<<"Enter a four-digit number: "; cin>>number;
for(int i=0; i<4; i++) { sum += number % 10; number /= 10; } cout << "The sum of the digits is: "<<sum; return 0; }
Input Output: - | |
9 | A four-digit number is inputted through the keyboard. Write a program to reverse the number. |
Executed Code: - int main() { int num1, num2, biggest; cout<<"Enter two numbers: "; cin>>num1 >> num2; biggest=(num1 > num2) ? num1 : num2; cout<<"The biggest number is: "<<biggest; return 0; } Input Output: - | |
10 | Write a program to find largest of two numbers using ternary operator. |
Executed Code: - int main() { int n1 = 5, n2 = 10, max;
max = (n1 > n2) ? n1 : n2;
cout << "Largest number between " << n1 << " and " << n2 << " is " << max << "."; return 0 }
Input Output: - | |
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: - int main() { int a; cout<<"enter an integer :\n"; cin>>a; int b; cout<<"enter another integer :\n"; cin>>b; int c; cout<<"enter another integer :\n"; cin>>c;
if(a + b > c || a + c >b || b + c > a) { cout<<"valid";
} else { cout<<"invalid"; } } Input Output: - | |
12 | Write a program to calculate compound interest. |
Executed Code: - // Paste the executed code here Input Output: - // Paste the input/output of executed code | |
Observation and Learning: -
- Write your observation and learning
Question of Curiosity
[To be answered by student based on the practical performed and learning/observations]
- Convert Following Mathematical Equations to programming equivalent statement
- C= a2+b2
- a2+b2=C
- r= ab+c
- r= ab+ac
- A=12bh
- r = 2
- A=πr2
- S=4πr2
- r= anbn
- A = P(1 + rn)nt
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 | |
x=-bb2-4ac2a