Lab1 C++
Lab1 C++
Lab 1
Instuction: Write pseudocode for problems below. Upload to google classroom if finish.
Problem 1:
To determine either a number is odd or even
Analyse
Pseudocode
BEGIN
DECLARE int number
INPUT number
if (number % 2 == 0)
OUTPUT number is even
else
OUTPUT number is odd
END
C++ Program
#include <iostream>
using namespace std;
int main(){
int number;
cout<< "Enter a number: ",
cin>> number;
if (number % 2 == 0 )
cout << number << "is even";
else
cout<<number<< "is odd";
return 0;
}
1
Screen Output
Problem 2:
To determine the higher between two numbers
Analyse
Pseudocode
BEGIN
DECLARE int number1, number2
INPUT number1, number2
if (number1 > number2)
OUTPUT number1 is higher
else
OUTPUT number2 is higher
END
C++ Program
#include <iostream>
using namespace std;
int main(){
int number1,number2;
cout<<" Enter a number:";
cin>>number1;
cout<<" Enter a number:";
cin>>number2;
2
if (number1 > number2)
cout << number1 << "is higher";
else
cout<<number2<< "is higher";
return 0;
}
Screen Output
Problem 3:
Enter height in meter and display height in feet.
Analyse
Output: message
Input:height number
Process:CALCULATE feet = meter * 3.28
Pseudocode
BEGIN
DECLARE double meter, feet
INPUT meter
CALCULATE feet = meter * 3.28
DISPLAY feet
END
C++ Program
#include <iostream>
3
using namespace std;
int main(){
float meter, feet;
cout << "Display length in feet: " << feet << endl;
return 0;
}
Screen Output
Problem 4:
To calculate perimeter and area of rectangle
Analyse
Perimeter = 2*(a + b)
4
Pseudocode
BEGIN
DECLARE int
length,heigth,perimeter,area INPUT
height,length
PROCESS
Area = a * b;
Perimeter = 2*(a + b)
DISPLAY
END
C++ Program
#include<iostream>
int main()
{
int a = 5;
int b = 10;
cout << "Area = " << areaRectangle(a, b) << endl;
cout << "Perimeter = " << perimeterRectangle(a, b);
return 0;
}
Screen Output
5
Problem 5:
Ask user for a weight, you need to convert the weight to pound and display it. Hint 1 kg = 2.2
pounds.
Analyse
Output: pound
Input:weight
Process:
pound = kg* 2.20,
Pseudocode
BEGIN
DECLARE float
weight,pound INPUT
weight
PROCESS
pound= weight*2.20
DISPLAY
END
C++ Program
#include <iostream>
6
int main(){
float kg, pound;
cout << "Display weight in pound: " << pound << endl;
return 0;
}
Screen Output
Problem 6:
Convert miles into kilometres and metres. User need to input distance in miles and find the distance
in kilometres and metres.
Hint:
1 miles = 1.609 kilometers.
1 miles = 1609 metres
Analyse
7
km = miles* 1.609;
m = miles* 1609;
Pseudocode
BEGIN
DECLARE
float miles,km, m;
INPUT miles
PROCESS
kilometre=miles*1.609,
metre=miles*1609
C++ Program
#include <iostream>
int main(){
float miles,km, m;
km = miles* 1.609;
m = miles* 1609;
cout << miles << " miles = " << km << " km" << endl;
cout << miles << " miles = " << m << " m" << endl;
return 0;
}
Screen Output
8
Problem 7:
Write a C++ program that asks the user to enter a number of 10 cents and a number of 50 cents
syilling. The program will calculate and output the total of cents entered.
Sample Output
Analyse
Pseudocode
BEGIN
DECLARE
float number10,number50,total_cents,total;
INPUT
number10,number50
PROCESS
total_cents= (number10*10)+ (number50*50),
total= total_cents/100,
END
9
C++ Program
#include <iostream>
using namespace std;
int main() {
float number10,number50,total_cents,total;
cout<<"enter a number of 10 cents:";
cin>>number10;
cout<<" Enter a number of 50 cents:";
cin>>number50;
total_cents= (number10*10)+ (number50*50),
total= total_cents/100,
cout<<"total of cents:"<<total_cents<<endl;
cout<<"total RM:"<<total<<"0"<<endl;
return 0;
}
Screen Output
Problem 8:
Four workers were responsible to pluck oranges. The owner of the orange farm will be given 40%
of the oranges. The workers share equally the balance of the oranges. The balance of oranges after
divided will be used to make juice. Write a complete program that accepts input for the amount of
oranges that were plucked. You are also required to calculate and print amount of oranges that
were received by the owner, each of the workers and balance of oranges to make juice.
10
Sample Output
Analyse
Output: total oranges for owner and worker and last balance
Input:oranges that plucked
Process:
Total oranges for the owner is " << (numOranges * 4) / 10 << endl;
Total oranges for each worker is " << (numOranges - (numOranges * 4) / 10) / 4 << endl;
Pseudocode
BEGIN
DECLARE
Int numOranges = 0;
INPUT
numOranges;
PROCESS
Total oranges for the owner is " << (numOranges * 4) / 10 << endl;
Total oranges for each worker is " << (numOranges - (numOranges * 4) / 10) / 4 << endl;
DISPLAY
total oranges for owner and worker and last balance
END
C++ Program
#include <iostream>
int main()
{
int numOranges = 0;
cout << "Enter number of oranges plucked: ";
cin >> numOranges;
cout << "Total oranges for the owner is " << (numOranges * 4) / 10 << endl;
cout << "Total oranges for each worker is " << (numOranges - (numOranges * 4) / 10) / 4
<< endl;
cout << "Balance of oranges to make juice is " << numOranges - (numOranges * 4) / 10 -
((numOranges - (numOranges * 4) / 10) / 4) * 4 << endl;
return 0;
11
}
Screen Output
Problem 9:
Write a program that calculates and displays the cost to cut the grass in the yard of the following
house. The rate charged is RM2.50 per square meter. The input values are length and width of the
house and the yard.
Analyse
Pseudocode
BEGIN
DECLARE
12
INPUT
length,width,x,y
PROCESS
area_yard=(length*width)-(x*y),
cost=(area_yard*2.5),
DISPLAY
END
C++ Program
#include <iostream>
using namespace std;
area_yard=(length*width)-(x*y),
cost=(area_yard*2.5),
Screen Output
13
14