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

Lab1 C++

Uploaded by

Aidil Aiman
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

Lab1 C++

Uploaded by

Aidil Aiman
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 14

CSC128

Lab 1

Name: Muhammad Aidil Aiman bin Shahriman


Matric No: 2022697148
Group: CEEC110E
Date: 4/3/2023

Instuction: Write pseudocode for problems below. Upload to google classroom if finish.

Problem 1:
To determine either a number is odd or even

Analyse

Output: Number is even or odd


Input: Number
Process: if (number % 2 == 0)

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

Output: Number1 ,Number2 is higher


Input: Number1,Number2
Process: if (number1 > number2)

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 << "Enter the height in meters: ";


cin >> meter;

feet = meter* 3.28;

cout << "Display length in feet: " << feet << endl;

return 0;
}

Screen Output

Problem 4:
To calculate perimeter and area of rectangle

Analyse

Output: massage perimeter/area


Input:number of height,lenght
Process:
Area = a * b;

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

Area ,perimeter of rectangle

END

C++ Program
#include<iostream>

using namespace std;

int areaRectangle(int a, int b)


{
int area = a * b;
return area;
}

int perimeterRectangle(int a, int b)


{
int perimeter = 2*(a + b);
return perimeter;
}

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

Display weight in pound: " << pound << endl;

END

C++ Program

#include <iostream>

using namespace std;

6
int main(){
float kg, pound;

cout << "Enter the weight in kilograms: ";


cin >> kg;

pound = kg* 2.20;

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

Output: distance in kilometres and metres


Input: distance in mile
Process:

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

DISPLAY distance in kilometre and metre

C++ Program
#include <iostream>

using namespace std;

int main(){
float miles,km, m;

cout << "Enter the miles value: " << endl;


cin >> miles;

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

Enter a number of 10 cents: 10


Enter a number of 50 cents: 3

Total cents: 250


Total: RM2.50

Analyse

Output: total cent


Input: number of cent
Process:

total_cents= (number10*10)+ (number50*50),


total= total_cents/100,

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

Enter number of oranges plucked: 125


Total oranges for the owner is 50
Total oranges for each worker is 18
Balances of oranges to make juice is 3

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>

using namespace std;

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

Output: cost to cut the grass in theyard


Input: values length and width
Process: total cost to cut the grass in the yard :RM

Pseudocode
BEGIN
DECLARE

float length,width,house,yard,cost,area_yard, x,y;

12
INPUT

length,width,x,y
PROCESS

area_yard=(length*width)-(x*y),
cost=(area_yard*2.5),

DISPLAY

total cost to cut the grass in the yard :RM

END

C++ Program
#include <iostream>
using namespace std;

int main (){


float length,width,house,yard,cost,area_yard, x,y;

cout<<"Enter the length of the yard:";


cin>>length;
cout<<"Enter the width of the yard:";
cin>>width;
cout<<"Enter the length of house:";
cin>>x;
cout<<"Enter the width of house:";
cin>>y;

area_yard=(length*width)-(x*y),
cost=(area_yard*2.5),

cout<<"total cost to cut the grass in the yard :RM"<<cost<<endl;


return 0;
}

Screen Output

13
14

You might also like