You are on page 1of 10

Assignment 3

Task 1:
Using switch statement Write a C program to input marks of five
subjects Physics,
Chemistry, Biology, Mathematics and Computer. Calculate percentage
and grade
according to following:
Percentage>=90%:GradeA
Percentage>=80%:GradeB
Percentage>=70%:GradeC
Percentage>=60%:GradeD
Percentage>=40%:GradeE
Percentage < 40% : Grade F
Program::
#include <iostream>
using namespace std;
int main()
{
int phy,chem,bio,math,comp,total_marks,sum ;
long int per;
cout << "enter the total marks of the subjects\n";
cin >> total_marks;
cout << "Enter the marks of physics \n";
cin >> phy;
cout << "Enter the marks of chemistry\n";
cin >> chem;
cout << "Enter the marks of biology\n";
cin >> bio ;
cout << "Enter the marks of math\n";
cin >> math ;
cout << "Enter the marks of computer\n";
cin >> comp ;
sum = phy + chem + bio + math + comp;
per = (sum * 100)/total_marks;
switch (per/10 )
{
case 10:
case 9:
cout << "GRADE A";
break;
case 8:
cout << "GRADE B";
break;
case 7:
cout << "GRADE C";
break;
case 6:
cout << "GRADE D";
break;
case 5:
case 4:
cout << "GRADE E";
break;
default :
cout << "GRADE F";
break;
}

}
Output::
enter the total marks of the subjects
500
Enter the marks of physics
55
Enter the marks of chemistry
66
Enter the marks of biology
88
Enter the marks of math
77
Enter the marks of computer
96
GRADE C
E:\programs\ConsoleApplication5\x64\Debug\ConsoleApplication5.exe
(process 4196) exited with code 0.
To automatically close the console when debugging stops, enable Tools-
>Options->Debugging->Automatically close the console when
debugging stops.
Press any key to close this window . . .
Task 2
C++ Program to find the maximum between two numbers. Using the
switch statement.
Program:

#include <iostream>
using namespace std;
int main()
{
int num1, num2;
cout << "Enter first number"<<endl;
cin >> num1;
cout << "Enter second number"<<endl;
cin >> num2;
switch (num1 > num2)
{
case 1:
cout << "First number is largest";
break;
case 0:
cout << "Second number is largest";
break;
}
}
Output:
Enter first number
33
Enter second number
44
Second number is largest
E:\programs\ConsoleApplication6\x64\Debug\ConsoleApplication6.exe
(process 12408) exited with code 0.
To automatically close the console when debugging stops, enable Tools-
>Options->Debugging->Automatically close the console when
debugging stops.
Press any key to close this window . . .
Task 3
C++ Program to check whether a year is a leap year or not. Using switch
statement
Program:
#include <iostream>
using namespace std;
int main()
{
int year,r;
cout << "enter a year";
cin >> year;
r = year % 400 == 0 || year % 4 == 0 || year % 100 == 0;
switch (r)
{
case 1:
cout << "It is a leap year";
break;
case 0:
cout << "It is not a leap year ";
break;
}
}
Output:
enter a year2022
It is not a leap year
E:\programs\ConsoleApplication7\x64\Debug\ConsoleApplication7.exe
(process 4968) exited with code 0.
To automatically close the console when debugging stops, enable Tools-
>Options->Debugging->Automatically close the console when
debugging stops.
Press any key to close this window . . .
Task 4
C++ Program to check whether a character is an alphabet or not. Using
the switch statement.
Program:
#include <iostream>
using namespace std;
int main()
{
char x;
cout << "enter any alphabet";
cin >> x;
switch (x >= 'a' && x <= 'z' || x >= 'A' && x <= 'Z') {
case 1:
cout << "it is an alphabet";
break;
case 0:
cout << "it is not an alphabet";
break;
}
}
Output:
enter any alphabetF
it is an alphabet
E:\programs\ConsoleApplication8\x64\Debug\ConsoleApplication8.exe
(process 10156) exited with code 0.
To automatically close the console when debugging stops, enable Tools-
>Options->Debugging->Automatically close the console when
debugging stops.
Press any key to close this window . . .
Task 5
Write a program that performs the arithmetic calculations of two
numbers entered by user. Use
switch statement to perform arithmetic operations.
Program:
#include <iostream>
using namespace std;
int main()
{
char x;
int num1, num2;
cout << "enter any mathematical operation +,-,/,*"<<endl;
cin >> x;
switch (x)
{
case '+':
cout << " enter the two numbers";
cin >> num1 >> num2;
cout << "sum=" << num1 + num2;
break;
case '-':
cout << " enter the two numbers";
cin >> num1 >> num2;
cout << "minus=" << num1 - num2;
break;
case '*':
cout << " enter the two numbers";
cin >> num1 >> num2;
cout << "multiple=" << num1 * num2;
break;
case '/':
cout << " enter the two numbers";
cin >> num1 >> num2;
cout << "divide=" << num1 / num2;
break;
}
}

Output:
enter any mathematical operation +,-,/,*
+
enter the two numbers33
55
sum=88
E:\programs\ConsoleApplication9\x64\Debug\ConsoleApplication9.exe
(process 11692) exited with code 0.
To automatically close the console when debugging stops, enable Tools-
>Options->Debugging->Automatically close the console when
debugging stops.
Press any key to close this window . . .

You might also like