You are on page 1of 13

Programming Fundamentals Assignment

Name : Muhammad Ali Shair


Roll No. : bsf23005049

Task 1: Write a program to that can take three numbers as input


from the user and find the maximum number among them.
Code:

int num1, num2, num3;

cout << "Enter three numbers: ";

if (cin >> num1 >> num2 >> num3) {

if ((num1 > num2) && (num1 > num3)) {

cout << "The greater number is: " << num1 << endl;

else if ((num2 > num1) && (num2 > num3)) {

cout << "The greater number is: " << num2 << endl;

else if ((num3 > num1) && (num3 > num2)) {

cout << "The greater number is: " << num3 << endl;

else if ((num1 == num2) && (num2 == num3)) {

cout << "All numbers are equal and the number is: " << num1 << endl;

else {

cout << "Please enter valid numbers." << endl;

}
else {

cout << "Please enter valid numbers." << endl;

Output:
Task 2: Write a program which ask the user to enter a positive
integer and then calculate its factorial.

Code:

int num;

int factorial = 1;

cout<<"Enter a positive integer "<<endl;

cin>>num;

if(num>=0){

int i = 1;

while(i <= num){

factorial = factorial * i;

i++;

cout<<"The factorial of "<<num<<" is "<<factorial<<endl;

}else{

cout<<"Please Enter a positive integer "<<endl;

}
Output:

Task 3: Write a program to count total number of currency notes of


5000, 1000, 500, 100, 50, 20 and 10 in a given amount.

Code:

int amount, note5000, note1000, note500, note100, note50, note20, note10;

cout<<"Enter your amount"<<endl;

cin>>amount;

if(amount < 0){

cout<<"Your cannot enter a negative(-ve) amount"<<endl;

return 1;
}else{

note5000 = amount/5000;

amount%=5000;

note1000 = amount/1000;

amount%=1000;

note500 = amount/500;

amount%=500;

note100 = amount/100;

amount%=100;

note50 = amount/50;

amount%=50;

note20 = amount/20;

amount%=20;

note10 = amount/10;

amount%=10;

cout<<"Amount has "<<note5000<<" 5000 note"<<endl;

cout<<"Amount has "<<note1000<<" 1000 note"<<endl;

cout<<"Amount has "<<note500<<" 500 note"<<endl;

cout<<"Amount has "<<note100<<" 100 note"<<endl;

cout<<"Amount has "<<note50<<" 50 note"<<endl;

cout<<"Amount has "<<note20<<" 20 note"<<endl;

cout<<"Amount has "<<note10<<" 10 note"<<endl;


Output:
Task 4: Write a program to input electricity unit consumed and
calculate total electricity bill according to the given condition:
If number of units are up to 100, the bill will be charged at the rate
of Rs. 5 per unit
If number of units are up to 200, the bill will be charged at the rate
of Rs. 10 per unit
If number of units are above 200, the bill will be charged at the rate
of Rs. 20 per unit

Code:

int unitsConsumed, Bill;

cout<<"Enter Electricity Units Cosumed "<<endl;

cin>>unitsConsumed;

if(unitsConsumed > 0){

if(unitsConsumed <= 100){

Bill = unitsConsumed*5;

}else if(unitsConsumed > 100 && unitsConsumed <= 200){

Bill = unitsConsumed*10;

}else if(unitsConsumed > 200){

Bill = unitsConsumed*20;

cout<<"Your electricity bill is: "<<Bill;

}else{

cout<<"Please enter a positive value"<<endl;

}
Output:
Task 5: Write a program to design ATM interface to perform the
following transactions. You may initialize the balance with zero.
After each transaction, the program should ask the user to perform
another transaction. Based on the input (y/n) the program either
redirects to the main interface or exit.
Press B to check account Balance
Press D to cash Deposit
Press W to cash Withdraw

Code:

cout<<"Program started!!!!!!!"<<endl;

char options;

double balance, deposit, withdraw;

balance = 0;

char tranAgain;

do{

cout<<"Press B to check account Balance"<<endl;

cout<<"Press D to cash Deposit"<<endl;

cout<<"Press W to cash Withdraw"<<endl;

cin>>options;

switch(options){

case 'B':

case 'b':

cout<<"your account balance is: "<<balance<<endl;

break;

case 'D':

case 'd':
cout<<"Enter amount to deposit ";

cin>>deposit;

if(deposit > 0){

balance+=deposit;

cout<<"your account balance becomes: "<<balance<<endl;

}else{

cout<<"you cannot deposit -ve amount"<<endl;

break;

case 'W':

case 'w':

if(balance > 0){

cout<<"Enter amount to withdraw ";

cin>>withdraw;

if(withdraw > 0){

balance-=withdraw;

cout<<"your remaining account balance is:


"<<balance<<endl;

}else{

cout<<"you cannot deposit -ve amount"<<endl;

}else{

cout<<"Your account balance is: "<<balance<<" you cannot


withdraw"<<endl;

break;

default:

cout<<"This is invalid input"<<endl;


}

cout<<"Do you want to do transaction again "<<endl<<"press y if


yes"<<endl<<"press n if no"<<endl;

cin>>tranAgain;

if(tranAgain == 'n' || tranAgain == 'N'){

cout<<"Program exited!!!!!!!"<<endl;

}else if(tranAgain == 'y' || tranAgain == 'Y'){

}else{

cout<<"This is invalid input"<<endl;

cout<<"Do you want to do transaction again "<<endl<<"press y if


yes"<<endl<<"press n if no"<<endl;

cin>>tranAgain;

}while(tranAgain == 'y' || tranAgain == 'Y');


Output:

You might also like