0% found this document useful (0 votes)
3 views

Lab 8 task_solution

The document contains two programming tasks. The first task is to create a program that checks if a user-entered character is a vowel using a switch statement. The second task involves creating a menu-driven program that allows users to calculate the average, median, binary representation, or check if a number is even or odd, with the option to continue or quit after each operation.

Uploaded by

muhammad hasnain
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)
3 views

Lab 8 task_solution

The document contains two programming tasks. The first task is to create a program that checks if a user-entered character is a vowel using a switch statement. The second task involves creating a menu-driven program that allows users to calculate the average, median, binary representation, or check if a number is even or odd, with the option to continue or quit after each operation.

Uploaded by

muhammad hasnain
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/ 8

Lab 8 task

Q1: Write a program using witch to check the alphabet


whether its vowel or not. The alphabet should be entered by
the user at run time.
Solution:
#include <iostream>
using namespace std;

int main()

{
char ch;
cout<<"enter a cahracter to check it is vowel or not \n";
cin>>ch;
switch(ch)
{
case 'a': case'A':
cout<<ch<<"is vowel";
break;

case 'e':case'E':

cout<<ch<<"is vowel";
break;
case 'i':case'I':
cout<<ch<<"is vowel";
break;
case 'o':case 'O':
cout<<ch<<"is vowel";
break;
case 'u':case'U':
cout<<ch<<"is vowel";
break;
default:
cout<<"the character
entered is not a vowel \n";
}
return 0;
}

Q2: Write a program using any approach to display the


following menu. Select any one option and work accordingly.
Once it is done for a one time, the program should ask the user
to continue or quit.
1 - To Calculate average of 5 numbers.
2 - To calculate Median of 5 numbers
3 - To calculate Binary of a number entered by the user
4 – To find whether the number entered by the user is even or
odd
Solution:
#include <iostream>

using namespace std;

int main()

char selection;

char ch='y';

int a,b,c,d,e;

cout<<"\n Menu";

cout<<"\n========";

cout<<"\n A - To Calculate average";

cout<<"\n M - To calculate Median";

cout<<"\n B - To calculate Binary";

cout<<"\n O - To Find odd/even";

// read the input

while(ch=='y')

{
cout<<"\n Enter selection: ";

cin>>selection;

switch(selection)

case 'A' :

case 'a' :{cout<<"\n To Calculate average\n";

cout<<"enter 5 numbers\n";

//int a,b,c ,d,e;

float avg;

cin>>a>>b>>c>>d>>e;

avg=(a+b+c+d+e)/5;

cout<<"the average is \t"<<avg;

break;

case 'M' :

case 'm' :{cout<<"\n To find median of 5 numbers\n";

cout<<"enter 5 numbers\n";

int median;

//int a,b,c,d,e;
cin>>a>>b>>c>>d>>e;

median=(a+b+c+d+e)/2 +1;

cout<<"the median is \t"<<c;

break;

case 'B' :

case 'b' :

cout<<"\n To calculate Binary";

cout<<"enter the number \n";

int remain;

int sum=0,a=1;

int x;

cin>>x;

do

sum+=(x%2)*a;

x=x/2;

a=a*10;

}
while(x!=0);

cout<<"the binary of numberis \t"<<sum;

break;

case 'O' :

case 'o' :

cout<<"\n To Find odd/even";

int x;

cout<<"enter the number \n";

cin>>x;

if(x%2==0)

cout<<"the number \t "<<x<<"is even";

else

cout<<"the number \t"<<x <<"is odd";

break;

default : cout<<"\n Invalid selection";

cout<<"\n";
cout<<"Do you want to perform another ? press y if yes \n";

cin>>ch;

return 0;

You might also like