0% found this document useful (0 votes)
17 views3 pages

Coding Example

The document contains 3 coding questions and their solutions. Question 1 prompts the user to input the number of coins they have in denominations of 5, 10, 20 and 50 cents and calculates the total value in Malaysian Ringgit. Question 2 prompts the user for speed and time values and calculates the distance traveled. Question 3 prompts the user for their age in years and converts it to months, days and hours.

Uploaded by

hazim
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views3 pages

Coding Example

The document contains 3 coding questions and their solutions. Question 1 prompts the user to input the number of coins they have in denominations of 5, 10, 20 and 50 cents and calculates the total value in Malaysian Ringgit. Question 2 prompts the user for speed and time values and calculates the distance traveled. Question 3 prompts the user for their age in years and converts it to months, days and hours.

Uploaded by

hazim
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

Question 1

Write a program that prompts user to input the number of 5 cents, 10 cents, 20
cents and 50 cents that the user has. The program then outputs the total value
of the coins in Ringgit Malaysia(RM).

Code:
#include<iostream>
using namespace std;
int main()
{
float a,b,c,d,e;
cout <<"Please input
cin >>a;
cout <<"Please input
cin>>b;
cout <<"Please input
cin>>c;
cout <<"Please input
cin>>d;

the number of 5 cents"<<endl;


the number of 10 cents"<<endl;
the number of 20 cents"<<endl;
the number of 50 cents"<<endl;

e=a*0.05+b*0.1+c*0.2+d*0.5;
cout <<"The total value of the coins in Ringgit Malaysia(RM) is "<<e<<endl;
return 0;

Result:
Please input the number of 5 cents
2
Please input the number of 10 cents
2
Please input the number of 20 cents
5
Please input the number of 50 cents
1
The total value of the coins in Ringgit Malaysia(RM) is 1.8

Question 2
Write a program that simulates an odometer. The user inputs the speed (In kmph) and
the time (in second), and the program calculates the distance travelled. Display the
speed, time and travelled distance in an organized order.
Code:
#include<iostream>
using namespace std;
int main()
{
float x,y,z;
cout <<"Please input the speed (in kmph)"<<endl;
cin >>x;
cout <<"Please input the time (in seconds)"<<endl;
cin >>y;
z=x*y/3600;
cout <<"The speed is (in kmph) "<< x <<",the time (in seconds) is"<< y
<<" and the the total traveled distance(in km) is "<< z <<endl;
return 0;

}
Result:
Please input the speed (in kmph)
120
Please input the time (in seconds)
60
The speed is (in kmph) 120,the time (in seconds) is60 and the the total traveled
distance(in km) is 2

Question 3
Write a program that asks user their age in years and converts the age
a) To months
b) To days
c) To hours
And displays the result
Code:
#include <iostream>
using namespace std;
int main()
{
int x,months,days,hours;
cout<<"Please input your age in years"<<endl;
cin>>x;
months=x*12;
days=x*365;
hours=days*24;
cout<<"Your age in months is "<<months<<endl;
cout<<"Your age in days is "<<days<<endl;
cout<<"Your age in hours is "<<hours<<endl;
return 0;
}

Result:
Please input your age in years
10
Your age in months is 120
Your age in days is 3650
Your age in hours is 87600

You might also like