24F0820 Lab08
24F0820 Lab08
Programming
Fundamentals
Lab # 08
//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;
}
//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;
}
}
//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;
}
//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;
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;
//check
while (n<2)
{
std::cout<<"Invalid Value. Enter again! "<<"\n";
std::cin>>n;
}