Coding Example

4
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 the number of 5 cents"<<endl; cin >>a; cout <<"Please input the number of 10 cents"<<endl; cin>>b; cout <<"Please input the number of 20 cents"<<endl; cin>>c; cout <<"Please input the number of 50 cents"<<endl; cin>>d; 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

description

Simple Coding example

Transcript of Coding Example

Page 1: Coding Example

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 the number of 5 cents"<<endl;cin >>a;cout <<"Please input the number of 10 cents"<<endl;cin>>b;cout <<"Please input the number of 20 cents"<<endl;cin>>c;cout <<"Please input the number of 50 cents"<<endl;cin>>d;

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 cents2Please input the number of 10 cents2Please input the number of 20 cents5Please input the number of 50 cents1The total value of the coins in Ringgit Malaysia(RM) is 1.8

Page 2: Coding Example

Question 2Write 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)120Please input the time (in seconds)60The speed is (in kmph) 120,the time (in seconds) is60 and the the total traveled distance(in km) is 2

Page 3: Coding Example

Question 3Write a program that asks user their age in years and converts the age

a) To monthsb) To daysc) To hours

And displays the resultCode:#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 years10Your age in months is 120Your age in days is 3650Your age in hours is 87600