0% found this document useful (0 votes)
16 views6 pages

Task #1: Code

The document contains code snippets for 6 questions that use switch statements to output different results based on input values. The code snippets demonstrate using switch statements to: 1) determine the number of days in a month based on a number input, 2) check if a character is a vowel or consonant, 3) perform basic math operations based on operator character input, 4) classify a character as a digit, letter or operator, 5) calculate grade based on percentage score, and 6) determine if profit or loss based on cost and selling price comparison.

Uploaded by

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

Task #1: Code

The document contains code snippets for 6 questions that use switch statements to output different results based on input values. The code snippets demonstrate using switch statements to: 1) determine the number of days in a month based on a number input, 2) check if a character is a vowel or consonant, 3) perform basic math operations based on operator character input, 4) classify a character as a digit, letter or operator, 5) calculate grade based on percentage score, and 6) determine if profit or loss based on cost and selling price comparison.

Uploaded by

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

Task #1:

Code
#include <iostream>
using namespace std;
void main()
{
int month;
cout << "Enter the month";
cin >> month;
switch (month)
{
case 1: case 3: case 5: case 7: case 8: case 10: case 12:
cout << "This month has 31 days.";
break;
case 2:
cout << "This month has 28 days.";
break;
case 4: case 6: case 9: case 11:
cout << "This month has 30 days.";
break;

default:
cout << "Invalid";
}
}

Output

Question 2
Code
#include <iostream>
using namespace std;
void main()
{
char alphabet;
cout << "Enter a alphabet:";
cin >> alphabet;
switch (alphabet)
{
case 'a': case 'e': case 'i': case 'o': case 'u':
cout << "This alphabet is a vowel";
break;

default:
cout << "This alphabet is consonent.";
}
}

Output

Question 3
#include <iostream>
using namespace std;
void main()
{
float a, b;
char k;
cout << "Enter First Number:";
cin >> a;
cout << "Enter Second Number:";
cin >> b;
cout << "Which Function you want to perform:";
cin >> k;
switch (k)
{
case '+':
cout << "The Sum of Numbers is:";
cout << a+ b;
break;
case '-':
cout << "The Difference of Numbers is:";
cout << a - b;
break;
case '*':
cout << "The Product of Numbers is:";
cout << a * b;
break;
case '/':
cout << "The Division of Numbers is:";
cout << a / b;
break;

default:
cout << "Invalid command";
}
}
Output

Question 4
Code
#include <iostream>
using namespace std;
void main()
{
char op ;
cout << "Enter the character:";
cin >> op ;
switch (op)
{
case 0: case 1: case 2: case 3: case 4: case 5: case 6: case 7: case 8: case 9:
case 10:
cout << "This is a digit.";
break;
case 'a': case 'w': case 'i': case 's':
cout << "This is a alphabet.";
break;
case '+': case '-': case '*': case '/':
cout << "This is a special character";
break;
default:
cout << "Invalid command";
}
}

Output

Question 5
Code
#include <iostream>
using namespace std;
int main()
{
float Physics,Chemistry,Maths,Computer,Biology ;
cout << "Enter your marks in Physics:";
cin >> Physics ;
cout << "Enter your marks in Chemistry:";
cin >> Chemistry;
cout << "Enter your marks in Maths:";
cin >> Maths;
cout << "Enter your marks in Computer:";
cin >> Computer;
cout << "Enter your marks in Biology:";
cin >> Biology;
float ObtainedMarks = Physics + Chemistry + Maths + Computer + Biology;
float Percentage = (ObtainedMarks / 500) * 100;
char grade;
switch (int (Percentage)/10)
{
case 9:
grade = 'A';
break;
case 8:
grade = 'B';
break;
case 7:
grade = 'C';
break;
case 6:
grade = 'D';
break;
case 5: case 4:
grade = 'E';
break;
default:
grade = 'F';
}
cout << "Percentage"<<Percentage<<"%"<<endl;
cout << "Your grade is:";
cout << grade;
return 0;

Output

Question 6
Code
#include<iostream>
using namespace std;
void main()
{
int c,s;
cout << "Enter Cost Price:";
cin >> c;
cout << "Enter Selling Price:";
cin >> s;
switch (c > s)
{
case true:
cout << "Loss";
case false:
cout << "Profit";
break;
}

Output

You might also like