0% found this document useful (0 votes)
84 views

Programming Exercises

The document contains 5 programming exercises that involve taking user input, performing calculations, and outputting results. The exercises include: 1) calculating the sum and average of two numbers, 2) converting height from inches to feet and inches, 3) converting between Celsius and Fahrenheit temperatures, 4) breaking down a amount of rupees into denominations, and 5) reversing a two-digit integer. For each exercise, sample input/output is provided, along with the code to solve the problem.

Uploaded by

Maryam Sommro
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
84 views

Programming Exercises

The document contains 5 programming exercises that involve taking user input, performing calculations, and outputting results. The exercises include: 1) calculating the sum and average of two numbers, 2) converting height from inches to feet and inches, 3) converting between Celsius and Fahrenheit temperatures, 4) breaking down a amount of rupees into denominations, and 5) reversing a two-digit integer. For each exercise, sample input/output is provided, along with the code to solve the problem.

Uploaded by

Maryam Sommro
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 10

Programming Exercise:

Use setw(width) manipulator for formatting output where required.

Q #1: Write a program that read two integers from the keyboard and print their sum and

average.

Sample run:

Enter first number: 25

Enter second number: 25

Sum is 50

Average is 25

CODING

#include<iostream>

#include<conio.h>

using namespace std;

int main()

int num1,num2,sum,average;

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

cin>>num1;

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

cin>>num2;

sum=num1+num2;
average=(num1+num2)/2;

cout<<"sum is "<<sum<<endl;

cout<<"average is "<<average<<endl;

return 0;

OUTPUT:

Q #1: Write a program that prompts for a person’s height in inches. Convert this height

measurement into feet by using the conversion factor of foot2Inch= 12 inch.

Now, the value obtained can easily, be translated into feet and inches which are

then output by the program.

Sample run:

Enter the height in inches: 20”


This is equivalent to 1’ 8”

CODING:

#include<iostream>

#include<conio.h>

using namespace std;

int main()

int inch1,inch2,feet;

cout<<"Enter the height in inches :" ;

cin>>inch1;

feet=inch1/12;

inch2=inch1%12;

cout<<"This is equivalent to :"<<feet<<"'"<<inch2<<"''"<<endl;

return 0;

SCREENSHOT:
Q #4: Write a program that calculates the temperature in Fahrenheit. For that it prompts

for temperature in Celsius degrees. Formula to calculate Fahrenheit temperature

is Fahrenheit=Celsius (9/5+32). Once if the task done do the vice versa i.e.

Celsius=5/9(Fahrenheit -32)

#include<iostream>

#include<conio.h>

using namespace std;

int main()

int farenhite,celcius,Farenhite,Celcius;

cout<<"Enter temperature in Celcius degree : " ;

cin>>celcius;

farenhite=celcius* (9/5+32);
cout<<"Farenhite temperature is :"<<farenhite<<endl;

cout<<"Enter temperature in Farenhite : " ;

cin>>Farenhite;

Celcius=(5/184) *Farenhite;

cout<<"Celcius temperature is :"<<Celcius<<endl;

return 0;

CODING:

OUTPUT:

Q #2: Write a program that prompts for time in seconds and output that time in hours,

minutes, and seconds. Here student will learn the usage of divide and modulus

arithmetic operators in integers.

Sample run:

Enter the time in seconds:3713


Hours in time is: 1

Minutes in time: 1

Seconds in time is: 53

CODING:

#include<iostream>

#include<conio.h>

using namespace std;

int main()

int seconds,hour,minutes,n,n1;

cout<<"Enter the time in seconds : " ;

cin>>n;

n1= n% (24*3600);

hour=n1/3600;

n%=3600;

minutes=n /60;

n%=60;

seconds=n;

cout<<"Hours in time is : "<<hour<<endl;

cout<<"Minutes in time is : "<<minutes<<endl;

cout<<"Seconds in time is : "<<seconds<<endl;

return 0;

}
OUTPUT:

Q #3: Write a program that prompts for amount in rupees and show how many 1000’s,

500’s, 100’s, 50’s, 10’s, 5’s, 2’s and 1’s in it.

Sample run:

Enter amount in rupees: 5788

1000’s in the given amount is: 5

500’s in the given amount is: 1

100’s in the given amount is: 2

50’s in the given amount is: 1

10’s in the given amount is: 3

5’s in the given amount is: 1

2’s in the given amount is: 2

1’s in the given amount is: 1

CODING:

#include<iostream>
#include<conio.h>

using namespace std;

int main()

int amt,R1000,R500,R100,R50,R10,R5,R2,R1,R20;

cout<<"Enter amount in rupees :";

cin>>amt;

R1000=amt/1000;

R500=amt/500;

amt=amt%1000;

R100=amt/100;

amt=amt%1000;

R50=amt/50;

amt=amt%50;

R20=amt/20;

amt=amt%20;

R10=amt/10;

amt=amt%10;

R5=amt/5;

amt=amt%5;

R1=amt;

cout<<"1000’s in the given amount is: "<<R1000<<endl;

cout<<"500’s in the given amount is: "<<R500<<endl;

cout<<"100’s in the given amount is: "<<R100<<endl;


cout<<"50’s in the given amount is: "<<R50<<endl;

cout<<"10’s in the given amount is: "<<R10<<endl;

cout<<"5’s in the given amount is: "<<R5<<endl;

cout<<"2’s in the given amount is:"<< R2<<endl;

cout<<"1’s in the given amount is: "<<R1<<endl;

return 0;

OUTPUT:

Q #5: Write a program that inputs a two digit integer value, and output its reverse order.

Sample run:

Enter a 2 digit integer value: 45

Reverse of this value is: 54

CODING:

#include <iostream>
using namespace std;

int main() {

int num , rev = 0;

cout<<"Enter number : ";

cin>>num;

while(num > 0) {

rev = rev*10 + num%10;

num = num/10;

cout<<"Reverse of number is "<<rev;

return 0;

OUTPUT:

You might also like