You are on page 1of 9

University of

Engineering Technology &

Taxila

2020

[Computer Programming]

Department of Computer Engineering

Assignment No 5

SUBMITTED BY:
SHEIKH ABUZAR AMJAD
REG NO:
19-CP-6
SUBMITTED TO:
SIR SHEHRYAR KHAN
Task No 01:-
Make a simple calculator which can perform addition,
subtraction, multiplication and division operations
using switch statement.
Note:-
In the above questions, the character constants has to
be used in cases. The operations will be ‘+’, ‘-’, ‘*’
and ‘/’.
CODE
# include <iostream>
using namespace std;

int main()
{
char op;
float num1, num2;

cout << "Enter operator either + or - or * or /: ";


cin >> op;

cout << "Enter two operands: ";


cin >> num1 >> num2;

switch(op)
{
case '+':
cout << num1+num2;
break;

case '-':
cout << num1-num2;
break;

case '*':
cout << num1*num2;
break;

case '/':
cout << num1/num2;
break;

default:
// If the operator is other than +, -, * or /, error message is shown
cout << "Error! operator is not correct";

1|Page
break;
}

return 0;
}

OUTPUT

Task No 02:-
Write a program to print first ten natural numbers. Use “goto” statements.
Algorithm
#include <iostream>

using namespace std;

// function to print numbers from 1 to 10

int main()

int n = 1;

print:

cout << n << " ";

n++;

if (n <= 10)

goto print;

return 0;

2|Page
}

OUTPUT

Task No 03:-

Write a program in C++ to input a single character and print a message “It is a
vowel” if it is a vowel otherwise print a message “It is a consonant”. Use if-else
structure and OR(||) operator.
ALGORITHM

#include <iostream>

#include <cstdlib>

using namespace std;

int main()

char c;

cout<<"Enter your character:";

cin>>c;

if (c=='a'|| c=='e'|| c=='i'|| c=='o'|| c=='u')

cout<<"It is a Vowel"<<endl;

3|Page
else

cout<<"It is a Consonent"<<endl;

return 0;

}
OUTPUT

Task No 04:-

Write a program in C++ to input a single digit from 0 to 9 and print the
input value in words. For example if the input value is 0, then print “zero”.
Use switch statement.
ALGORITHM
#include <iostream>
using namespace std;
int main()
{
int n;
cout << "\n\n Print a number in words:\n";
cout << "-----------------------------\n";
cout << " Input any number: ";
cin >> n;
switch (n) {
case 0:
cout << "Zero ";
4|Page
break;
case 1:
cout << "One ";
break;
case 2:
cout << "Two ";
break;
case 3:
cout << "Three ";
break;
case 4:
cout << "Four ";
break;
case 5:
cout << "Five ";
break;
case 6:
cout << "Six ";
break;
case 7:
cout << "Seven ";
break;
case 8:
cout << "Eight ";
break;
case 9:
cout << "Nine ";
break;
}
cout << endl;
}

5|Page
OUTPUT

Task No 05:-

Write a program that allows the user to enter the grade scored in
a programming class (0-100). If the user scored a 100 then notify
the user that they got a perfect score. Modify the program so that
it will notify the user of their letter grade.
0-59 F
60-69 D
70-79 C
80-89 B
90-100 A.
ALGORITHM
#include<iostream>
using namespace std;
int main()
{
int score;
cout<<"Enter your Scores:";
cin>>score;
if (score==100)
{
cout<<"You got a Perfect Score"<<endl;
cout<<"score=A"<<endl;
}
else if(score>=90)
{
cout<<"grade=A"<<endl;
}
else if(score<90 && score>=80)
{
cout<<"grade=B"<<endl;
}
6|Page
else if(score<80 && score>=70)
{
cout<<"grade=C"<<endl;
}
else if (score<70 && score>=60)
{
cout<<"grade=D"<<endl;
}
else
{
cout<<"grade=F"<<endl;
}
return 0;
}
OUTPUT

Task No 06:-
Write a program that presents the user w/ a choice of your 5 favorite beverages
(Coke, Water, Sprite,..., Whatever).Then allow the user to choose a beverage by
entering a number 1-5. Output which beverage they chose. Modify the program so
that if the user enters a ch
oice other than 1-5 then it will output "Error. Choice was not valid”, here is your
money back."

CODE

#include <iostream>

using namespace std;

int main()
{
int input;

7|Page
cout<<"1 Coke\t\t 2 Water\t\t 3 Sprite\t\t 4 Fanta\t 5 Sting\n";
cout<<"Enter your choice:";
cin>> input;
switch (input) {
case 1:
cout<<"Your Coke is ready!";
break;
case 2:
cout<<"Your Water is ready!";
break;
case 3:
cout<<"Your Sprite is ready!";
break;
case 4:
cout<<"Your Fanta is ready!";
break;
case 5:
cout<<"Your Sting is ready!";
break;
default:
cout <<"Error. choice was not valid, here is your money back.\n";
break;
}
cin.get();

}
OUTPUT

THE end

8|Page

You might also like