0% found this document useful (0 votes)
13 views16 pages

24F0820 Lab08

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views16 pages

24F0820 Lab08

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 16

CL-1002

Programming
Fundamentals
Lab # 08

Muhammad Subhan Yousaf


Q1: Counter-controlled while loop:
Write a C++ program that generates a multiplication table for a given number. The program
should utilize a counter-controlled while loop to multiply the input number by integers from 1 to
10, displaying each result in a formatted table. For example, if the user inputs the number 5, the
output should display: 5 x 1 = 5, 5 x 2 = 10, up to 5 x 10 = 50. Your program should also keep
track of the number of iterations and print the total count of multiplications performed.
#include <iostream>
int main (void)
{
//Declaration of variables
int n,c=1;

//Input and Output: Number


std::cout<<"Enter a number to generate its multiplication table: ";
std::cin>>n;
std::cout<<"Multiplication Table for "<<n<<"\n";
std::cout<<"-------------------------------------------"<<"\n";

//Table formation
while (c<=10)
{
std::cout<<"\n"<<n<< " X " <<c<< " = " <<n*c<<"\n";
++c;
}
std::cout<<"-------------------------------------------"<<"\n";
std::cout<<"\nTotal multiplications performed: "<<c-1<<"\n";
std::cout<<"-------------------------------------------"<<"\n";
system("pause>0");
return 0;
}

Q2. Sentinel-controlled while loop:


Write a C++ program for a gym that collects and processes members&#39; weights. Your
program should prompt the user to enter each member&#39;s weight and continue to accept
input until the user enters a sentinel value of -99, which signifies that no more weights will be
provided. As each weight is entered, the program should calculate and display the average weight
of the members. Additionally, ensure that the program handles invalid inputs, such as negative
weights (other than the sentinel), gracefully by prompting the user to enter a valid weight.
#include <iostream>
int main (void)
{
//Declaration of variables
float weight=1,c=1,Avg,sum=0;

//Input and Output:Weight of gym members


std::cout<<"Enter the weights of gym members (enter -99 to stop): "<<"\n";
while (weight!=-99)
{
std::cout<<"Weight: ";
std::cin>>weight;
if (weight != -99)
{
if (weight<0 && weight != -99)
{
std::cout<<"Enter a vaid value: ";
std::cin>>weight;
}
sum=sum+weight;
Avg=sum/c;
std::cout<<"Current average weight: "<<Avg<<"\n";
++c;
}
}

//Final average weight


std::cout<<"Final average weight of memebers: "<<Avg;
system("pause>0");
return 0;
}

Q3. Condition/Event-Controlled while loop:


Write a C++ program for a simple banking application where users can withdraw money from
their accounts. Write a program that prompts the user to enter their account balance and the
amount they wish to withdraw. The program should use a condition/event-controlled while loop
to repeatedly check if the requested withdrawal amount is less than or equal to the available
balance. If the condition is met, the withdrawal should be processed, and the new balance
displayed. If not, the user should be informed that they cannot withdraw more than their balance,
and the program should prompt them to enter a different amount until a valid transaction occurs.
#include <iostream>
int main (void)
{
//Declaration of variables
float balance,withdraw=1,new_balance;

//Output and Input:Balance


std::cout<<"Enter your account balance: ";
std::cin>>balance;

//check
while (balance<0){
std::cout<<"Invalid amount. Enter a valid amount.";
std::cin>>balance;
}

//Withdrawal amount
while (withdraw!=0)
{
std::cout<<"\nEnter the amount you wish to withdraw: ";
std::cin>>withdraw;
if (withdraw>balance){
std::cout<<"Insufficient funds. You cannot withdraw more than your balance of
"<<balance<<"."<<"\n";
std::cout<<"\nEnter the amount you wish to withdraw: ";
std::cin>>withdraw;

}
if (withdraw<0)
{
std::cout<<"Invalid amount. Please enter a positive amount to withdraw."<<"\n";
std::cout<<"\nEnter the amount you wish to withdraw: ";
std::cin>>withdraw;
}

//New balance
new_balance = balance - withdraw;
std::cout<<"Withdraw successful! Your new balance is: "<<new_balance;

system("pause>0");
return 0;
}
}

Q4. Flag-controlled while loop:


Write a program that presents the user&#39;s choice of 5 favorite beverages (Coke, Fanta,
Sprite, Pepsi, mineral water) then allow the user to choose a beverage by entering a number 1-5.
Output which beverage they chose. Users can give choices until he/she has money in his/her
account and display the message “out of money” with the remaining balance when the user does
not have enough money to buy a minimum-cost drink (You must use the switch statement).
#include <iostream>
int main (void)
{
//Declaration of variables
int Coke=100, Fanta=95, Sprite=85, Pepsi=85, Mineral_water=60,Balance,Opt,n=0;
bool ans;
std::cout<<"Choose your favourite beverage: "<<"\n"<<"1. Coke
($"<<Coke<<")"<<"\n"<<"2. Fanta ($"<<Fanta<<")"<<"\n"<<"3. Sprite
($"<<Sprite<<")"<<"\n"<<"4. pepsi ($"<<Pepsi<<")"<<"\n"<<"5. Mineral Water
($"<<Mineral_water<<")"<<"\n";

//Output and Input: Balance


std::cout<<"Enter your account balance: $";
std::cin>>Balance;

//Check
while (Balance<=0){
std::cout<<"Invalid value. Enter again."<<"\nEnter your account balance: $";
std::cin>>Balance;
}

//Buying of drinks
while (Balance>Coke || Balance>Fanta || Balance>Sprite || Balance>Pepsi ||
Balance>Mineral_water){
std::cout<<"\nEnter the number of beverage you want to buy(1-5) or 0 to exit:";
std::cin>>Opt;
switch(Opt){
case 1:{
std::cout<<"Do you want to buy this drink? (1 for yes, 0 for no):";
std::cin>>ans;
if (ans==1){
++n;
Balance=Balance-Coke;
std::cout<<"You have bought a drink. you have bought "<<n<<" drinks so
far."<<"Remaining balance: $"<<Balance<<"\n";
}
else {
Balance=Balance-0;
std::cout<<"You chose not to buy the drink. you have bought "<<n<<"
drinks so far."<<"Remaining balance: $"<<Balance<<"\n";
}
break;
}
case 2:{
std::cout<<"Do you want to buy this drink? (1 for yes, 0 for no):";
std::cin>>ans;
if (ans==1){
++n;
Balance=Balance-Fanta;
std::cout<<"You have bought a drink. you have bought "<<n<<" drinks so
far."<<"Remaining balance: $"<<Balance<<"\n";
}
else {
Balance=Balance-0;
std::cout<<"You chose not to buy the drink. you have bought "<<n<<"
drinks so far."<<"Remaining balance: $"<<Balance<<"\n";
}
break;
}
case 3:{
std::cout<<"Do you want to buy this drink? (1 for yes, 0 for no):";
std::cin>>ans;
if (ans==1){
++n;
Balance=Balance-Sprite;
std::cout<<"You have bought a drink. you have bought "<<n<<" drinks so
far."<<"Remaining balance: $"<<Balance<<"\n";
}
else {
Balance=Balance-0;
std::cout<<"You chose not to buy the drink. you have bought "<<n<<"
drinks so far."<<"Remaining balance: $"<<Balance<<"\n";
}
break;
}
case 4:{
std::cout<<"Do you want to buy this drink? (1 for yes, 0 for no):";
std::cin>>ans;
if (ans==1){
++n;
Balance=Balance-Pepsi;
std::cout<<"You have bought a drink. you have bought "<<n<<" drinks so
far."<<"Remaining balance: $"<<Balance<<"\n";
}
else {
Balance=Balance-0;
std::cout<<"You chose not to buy the drink. you have bought "<<n<<"
drinks so far."<<"Remaining balance: $"<<Balance<<"\n";
}
break;
}
case 5:{
std::cout<<"Do you want to buy this drink? (1 for yes, 0 for no):";
std::cin>>ans;
if (ans==1){
++n;
Balance=Balance-Mineral_water;
std::cout<<"You have bought a drink. you have bought "<<n<<" drinks so
far."<<"Remaining balance: $"<<Balance<<"\n";
}
else {
Balance=Balance-0;
std::cout<<"You chose not to buy the drink. you have bought "<<n<<"
drinks so far."<<"Remaining balance: $"<<Balance<<"\n";
}
break;
}
case 0:{
return 0;
break;
}
default:{
std::cout<<"Invalid choice. Plaese enter a number between 1 and 5, or 0 to
exit."<<"\n";
break;
}
}
}
std::cout<<"Thnak you for using the beverage selection program!";
system("pause>0");
return 0;
}

Q5: For Loop.


a) Write a C++ program that takes a positive integer from the user and uses a for loop to
calculate the factorial of n.
#include <iostream>
int main (void)
{
//Declaration of variables
int n,fact=1,n1;

//Input and Output: number


std::cout<<"Enter the number?";
std::cin>>n;

//check
while (n<0){
std::cout<<"Invalid value. Please enter a valid value."<<"\n";
std::cin>>n;
}
n1=n;

//Factorial calculation
for (n;n>=1;n-=1)
{
fact=fact*n;
}

//Display factorial
std::cout<<"Factorial of "<<n1<<" = "<<fact;
system("pause>0");
return 0;
}

b) Write a C++ program that generates a multiplication table for a given number.
The program should utilize a for loop to multiply the input number by integers
from 1 to 10, displaying each result in a formatted table. For example, if the user
inputs the number 5, the output should display: 5 x 1 = 5, 5 x 2 = 10, up to 5 x 10
= 50.
#include <iostream>
int main (void)
{
//Declaration of variable
int n,c=1;
//Input and Output: number
std::cout<<"Enter the multiplication table number: ";
std::cin>>n;

//Multiplicative table formation


for(c;c<=10;c+=1)
{
std::cout<<n<<" X "<<c<<" = "<<n*c<<"\n";
}
system("pause>0");
return 0;
}

c) Write a C++ program that takes an integer input n from the user and uses a for
loop to print all prime numbers between 1 and n.
#include <iostream>
int main (void)
{
//Declaration of variables
int n, c, c1, sum=0;

//Input and Output: number


std::cout<<"Enter the number? ";
std::cin>>n;

//check
while (n<2)
{
std::cout<<"Invalid Value. Enter again! "<<"\n";
std::cin>>n;
}

//listing of Prime Numbers


for (c=1;c<=n;++c)
{
for (c1=1;c1<=n;++c1)
{
if (c%c1==0)
{
++sum;
}
}
if (sum==2)
{
std::cout<<c<<"\n";
}
sum=0;
}
system("pause>0");
return 0;
}

You might also like