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

Cin A B Name

The document contains 10 programming questions related to conditional statements and loops in C++. It includes questions to trace code output, identify syntax errors, convert between if-else and switch statements, and write complete programs to calculate laundry prices and discounts using conditional logic. Pseudocode and full programs are provided as solutions for problems that involve writing code, such as calculating total price and discount for laundry services based on material type, weight, and price tables.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
107 views

Cin A B Name

The document contains 10 programming questions related to conditional statements and loops in C++. It includes questions to trace code output, identify syntax errors, convert between if-else and switch statements, and write complete programs to calculate laundry prices and discounts using conditional logic. Pseudocode and full programs are provided as solutions for problems that involve writing code, such as calculating total price and discount for laundry services based on material type, weight, and price tables.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 8

Exercise W7

No 1 and 2 revision

1. Trace and write the output for the below program


no =1;
n1 = 2;
If ( no != n1-1)
{
no=n1;
n1=no;
}
else
{
n1=no;
no=n1;
}
cout << no<<n1;

Answer
11

2. Analyze the below program segment and find the syntax error.

/* Tracing The Error*/


cin >> a,b,name;
cin >> a>>b>>name;
a*b = j;
j = a*b;
cout << salary << name << is << j;
cout << “salary “ << name << “ is ” << j;

3. Trace output for the following if else if statement :


(a)
x=8;
y=7;
if (y>x)
temp =y;
else if (y<x)
temp = x;
else if (x==y)
temp=0;
else
temp=-100;
cout<<”temp = “<<temp;
temp = 8
(b)

int x,y,z;
float a = 5.0;

x = y = z = 10;
x = 9%2*3 + 54;
y = +2+z;
y = a/y + z%5;
if(y>z)
cout <<"\n value of y is greater than z";
else if(y<z)
cout <<"\n value of y is less than z";
else
cout <<"\n value of y and z are same";
value of y is less than z

4. Write if else statement to do the following:

Display “excellent” if mark is more than 90 and attendance is more than 80,
otherwise display “moderate”;

if (marks >90 && attendance >80)


cout<”excellent”;
else
cout<<”moderate”;

5. Trace the output for the program segment bellow

int number = 4;
float alpha = -1.0;
if (alpha < 0.0)
if (number > 0)
cout << "Here I am!" << endl;
else
cout << "No, I’m here!" << endl;
cout << "No, actually, I’m here!" << endl;
Here I am!
No, actually, I’m here!

6. Convert the following program segment to switch statement.

int x=50;
int y=4;
if (x%y==0 || x%y==1) y++;
else if (x%y == 2)y--;
else y+=2;
cout << y << endl;
int x=50;
int y=4;
switch(x%y)
{
case 0:
case 1:
y++;
break;
case 2:
y--;
break;
default:
y+=2;
}
cout << y << endl;

TIPS:How to convert expression with OR ( || ) to case


if (x==2 || x==3)
cout<<” 2 or 3”;

case 2:
case 3:
cout<<”2 or 3”;
break;
Question 7 until 10 you have to create complete programs.
7. Clean Fast laundry shop is charging for the laundry based on a weight. Rate that imposed is
RM2.5 for one kilogram. If the total payment is RM30 and above, 10% discount would be
given to the customer every day except for the weekend (Sunday).
a. Write a pseudocode.
Start
input weight, status
total_price = weight*2.5
if (total_price >= 30 && status == 'y')
actual_price = total_price * 0.9;
discount = total_price - actual_price;
print total_price, discount and actual_price
End

b.Write a program based on the pseudocode.

#include <iostream.h>
void main()
{

float weight, total_price, actual_price=0;


char status;
float discount=0;

cout<<"Enter the weight : ";


cin>>weight;
cout<<"Is it weekday ? [y[yes] , n[no]] :";
cin>>status;
total_price = weight * 2.5;

if (total_price >= 30 && status == 'y')


{
actual_price = total_price * 0.9;
discount = total_price - actual_price;
}

cout<<"\n\nTotal price : "<<total_price<<endl;


cout<<"Discount given : "<<discount<<endl;
cout<<"You have to pay : "<<actual_price<<endl;
}

8. ABC Fast laundry shop is charging for the laundry based on a type of material. Price rate that
imposed for one kilogram of each type are as follows :
Type Price
clothes RM 15 for 1 kilogram
blankets / towels RM 6 for 1 kilogram
comforter RM 10 for 1 kilogram
You may use if else if statement or switch statement to create a program.

#include <iostream.h>
void main()
{
char type;
float weight, price=0;
cout<<"Enter the type of material : ";
cin>>type;
cout<<"Enter the weight of the material : ";
cin>>weight;
switch (type)
{
case 'C':
price = weight * 15;
break;
case 'B':
case 'T':
price = weight * 6;
break;
case 'F':
price = weight * 10;
break;
default:
cout<<"invalid type \n";
}
cout<<"price = "<<price;
}

9. ABC laundry shop provides discounts to its customers as follow:

Total price Discount


>= RM30 10% from total price
>= RM20 dan <RM30 5% from total price
>= RM15 dan <RM20 3% from total price
< RM15 0% from total price

Calculate the price that the customer should pay after taking into account the discount
given.

#include <iostream.h>

void main()
{
float total_price;
float price_to_pay;

cout<<"Enter total price : ";


cin>>total_price;

if (total_price >= 30)


price_to_pay = total_price *0.9;
else if (total_price >= 20)
price_to_pay = total_price *0.95;
else if (total_price >= 15)
price_to_pay = total_price *0.97;
else
price_to_pay = total_price;
cout<<"Price to pay : "<<price_to_pay;
}

10 Shamira Laundry has designed a new calculation system for their shop. The price for each
type is as follows:

Type Price
Clothes RM 15 for 1 kilogram
blankets / towels RM 6 for 1 kilogram
comforter RM 10 for 1 kilogram

Only one type can be washed at a time. This laundry shop also provides discounts to its
customers as follow:

Total price Discount


>= RM30 10% from total price
>= RM20 dan <RM30 5% from total price
>= RM15 dan <RM20 3% from total price
< RM15 0% from total price

Calculate the price that the customer should pay after taking into account the discount
given. Display the output as below:

BILL FOR ABC LAUNDRY

Customer Number : _________________________


Total Price : _________________________
Discount : _________________________
Amount Paid : _________________________

Write a coding to solve this problem.

#include <iostream.h>

void main()
{
char type;
int customerNo;
float weight, price=0;
float price_to_pay;

cout<<"Enter your customer Number : ";


cin>>customerNo;
cout<<"\n============================================"<<endl;
cout<<"Type of material :"<<endl;
cout<<"[C] clothes"<<endl;
cout<<"[B] blankets"<<endl;
cout<<"[T] towels"<<endl;
cout<<"[F] comforter"<<endl;
cout<<"============================================"<<endl;
cout<<"Enter the type of material : ";
cin>>type;
cout<<"Enter the weight of the material : ";
cin>>weight;
cout<<"============================================"<<endl;

switch (type)
{
case 'C':
price = weight * 15;
break;
case 'B':
case 'T':
price = weight * 6;
break;
case 'F':
price = weight * 10;
break;
default:
cout<<"invalid type \n";
}

if (price >= 30)


price_to_pay = price *0.9;
else if (price >= 20)
price_to_pay = price *0.95;
else if (price >= 15)
price_to_pay = price *0.97;
else
price_to_pay = price;

cout<<"\n++++++++++++++++++++++++++++++++++++++++++++"<<endl;

cout<<"BILL FOR ABC LAUNDRY "<<endl;


cout<<"++++++++++++++++++++++++++++++++++++++++++++"<<endl;
cout<<"\nCustomer Number is : "<<customerNo<<endl;
cout<<"Total price : "<<price<<endl;
cout<<"Discount : "<<price - price_to_pay<<endl;
cout<<"Amount paid : "<<price_to_pay<<endl;
cout<<"++++++++++++++++++++++++++++++++++++++++++++"<<endl;

You might also like