You are on page 1of 11

UNIVERSITI TEKNOLOGI MARA

ASSIGNMENT 1

SEMESTER : MARCH 2022 – AUGUST 2022


COURSE : PROGRAMMING I
COURSE CODE : CSC402

Name : MUHAMMAD HARITH IKHWAN BIN ROSMAEINI


Student ID : 220753195
Group : M3CS2531A

QUESTION FULL MARKS MARKS


Question 1
Question 2
Question 3
Question 4
TOTAL MARKS
2 CS/20222/CSC402/A1

Question 1

Write pseudocode and complete C++ program to ask the user to enter the required data to
calculate the area of the circle and square. The formulas to calculate the respective areas
are as follows:

Area of circle = pi x radius


Area of square = length2
(10 marks)

Question 2

Write the pseudocode to enter a number from the user. Then, display the Fibonacci series of
the number. For example, the Fibonacci series are as below:

Input, n Fibonacci series


0 0
1 0
2 0 1
3 0 1 1
4 0 1 1 2
5 0 1 1 2 3
6 0 1 1 2 3 5
7 0 1 1 2 3 5 8

(10 marks)

Question 3

A XOX finance company will give a loan to a customer for buying a car based on the amount
of the loan. If the loan is RM100,000 or more, the interest rate will be 3.5%, otherwise, it will
be 5% annually. Given the amount of the loan and the period of repayment, write the
pseudocode to calculate the amount of interest and the monthly payment or installment.

(10 marks)

Question 4

The ABC telephone company gives its customers two types of packages, package A and
package B. For each package, there is a basic charge and the customer is allowed to make
free phone calls for up to a certain number of minutes. Any additional call will be charged 10
cents per minute. The customers are also given a certain number of free SMS. Additional
SMS will be charged 15 cents per message.

The charges for each package are as follows:

Package Basic Charge (RM) Free Call (minutes) Free SMS


A 100.00 200 20
B 125.00 250 35

The charge can be determined using the formula:

charge = basic charge + (total minutes – free call) x 0.10 + (total sms – free sms) x 0.15
3 CS/20222/CSC402/A1

a. Define the input, process, and output of this task.


b. Draw the flowchart to find the charge amount based on the type of package.
c. Write the pseudocode.
d. Write a full C++ program.
(30 marks)
4 CS/20222/CSC402/A1

Question 1

1. Start
2. Initialize constant pi=3.142
3. User enter radius and length
4. Calculate area_circle=pi x radius
5. Calculate area_square= length x length
6. Print area_circle and area_square
7. End

#include<iostream>
#include<cmath>
using namespace std;

int main(){
float const pi=3.142;
float areacircle,areasquare,length,radius;

cout<<"Enter the length of square(cm)= ";


cin>>length;
cout<<"Enter the radius of circle(cm)= ";
cin>>radius;

areacircle= pi*radius*radius;
areasquare= pow(length,2);

cout<<"Area of Circle is= "<<areacircle<<"square cm"<<endl;


cout<<"Area of Square is= "<<areasquare<<"square cm"<<endl;
return 0;
}
5 CS/20222/CSC402/A1

Question 2

1. Start
2. User enter n
3. Initialize int arrFab[n], term1=0, term2=1, nextT
4. Set arrFab[0]=term1, arrFab[1]=term2
5. For j=2, j<n+1 //j=2 because two slot in array occupied
5.1. next equal to sum of term1 and term2
5.2. term1 =term2, term2=nextT, arrFab[ j ]= next
5.3. j increment by 1
6. loop
7. For a=0, a<n+1 //a as the displayed term n number
7.1. Print a
7.1.1. For b=0, b<a //b as address of fabonacci series in array arrFab[ ]
7.1.2. Print arrFab[b]
7.1.3. b increment by 1
7.1.4. loop
7.2. a increment by 1
7.3. loop
8. end
6 CS/20222/CSC402/A1

#include<iostream>

using namespace std;

void printFabo(int i){


int j,a,b,term1=0,term2=1,nextT;
int arrFab[i];

arrFab[0]=term1;
arrFab[1]=term2;

for(j=2;j<i+1;j++){
nextT=term1+term2;
term1=term2;
term2=nextT;
arrFab[j]=nextT;

}
for(a=0;a<i+1;a++){ //display fabonacci
cout<<a<<"\t\t\t ";

for(b=0;b<=a;b++){
cout<<arrFab[b]<<" ";
}

cout<<endl;
}

int main(){

int n,j,next_t;

cout<<"Enter the numbers of terms= ";


cin>>n;
cout<<"_______________________________________________________
___________________________________\n";
cout<<"\t\tInput(n) \t\t\t\tFibonacci series\n";
cout<<"_______________________________________________________
____________________________________\n\n";

printFabo(n);
cout<<"_______________________________________________________
_____________________________________"<<endl;
return 0;
}
7 CS/20222/CSC402/A1

Question 3

1. start
2. user enter loan, period(in year)
3. if loan<100 000
3.1. rate=0.05
4. else
4.1. rate=0.035
5. calculate amount_interest=loan x rate x period
6. calculate monthly_installment=(loan + amount_interest) / (period x 12)
7. print amount_interest, monthly_installment
8. end
8 CS/20222/CSC402/A1

Question 4

a. input: package
output: charge
process: charge= basic charge+ minute_charge + sms_charge
9 CS/20222/CSC402/A1

b. Flowchart
10 CS/20222/CSC402/A1

c. Pseudocode

1. start
2. user enter package, total min, total sms
3. if package is ‘A’
3.1. basic charge=100
3.2. free call=200
3.3. free sms=20
4. else if package is ‘B’
4.1. basic charge=125
4.2. free call=250
4.3. free sms=35
5. else
5.1. user enter package
6. calculate excess_minute=total minute-free call
7. calculate excess_sms=total sms-free sms
8. if excess_minute>0
8.1. minute_charge = excess_minute x 0.10
9. else
9.1. minute_charge= 0
10. if excess_sms>0
10.1. sms_charge=excess_sms x 0.15
11. else
11.1. sms_charge=0
12. calculate charge=basic charge + minute_charge + sms_charge
13. print charge
14. end
11 CS/20222/CSC402/A1

d. Coding

#include<iostream>
using namespace std;

int main(){

float
charge,total_min,total_sms,basic_charge,excess_minute,excess_s
ms,min_charge,sms_charge;
int free_call,free_sms;
char package;
do{

cout<<"Please Enter Your Package: ";


cin>>package;
cout<<"Enter the Total call minute used: ";
cin>>total_min;
cout<<"Enter the Total sms used: ";
cin>>total_sms;
cout<<endl<<endl;
}
while(package!='A'&&package!='B');
if(package='A'){
basic_charge=100;
free_call=200;
free_sms=20;
}
if(package='B'){
basic_charge=125;
free_call=250;
free_sms=35;}

excess_minute=total_min-free_call;
excess_sms=total_sms - free_sms;

if(excess_minute>0){
min_charge=excess_sms*0.10;}
else{
min_charge=0;}

if(excess_sms>0){
sms_charge=excess_sms*0.15;}
else{
sms_charge=0;}

charge=basic_charge+min_charge+sms_charge;

cout<<"\nThe Charge is RM"<<charge;

return 0;
}

You might also like