Programming For Problem Solving: Experiment 3 - 2
Programming For Problem Solving: Experiment 3 - 2
EXPERIMENT 3 - 2
Task 1: Identify the errors if any in the following codes without using
codeblock and Justify you answer.
a. #include <iostream>
void main() {
int choice = 2;
switch(choice);
{
case 1:
cout<<"Case 1\n";
break;
case 2:
cout<<"Case 2\n";
break;
case 3:
cout<<"Case 3\n";
break;
case 4:
cout<<"Case 4\n";
break;
default:
cout<<"Case default\n"; }
return 0;
}
ERROR:
Standard library is not mentioned
case statements are not within the switch statement and break statements are not
within the loop
b. #include <iostream>
void main()
{
int a=2;
int b=a;
switch(b)
{
case a:
cout<<"Case a \n"; break;
case 3:
cout<<"Case 3\n"; break;
default:
cout<<"No option\n"; break;
}
cout<<"Exit from switch";
}
ERROR:
Standard library not mentioned
As ‘a’ is not constant, the value of ‘a’ is not usable in a constant expression.
c. #include <iostream>
int main()
{
char ch='b';
switch (ch)
{
case 'd':
cout<<"Case D ";
break;
case 'b':
cout<<"Case B ";
break;
case 'c':
cout<<"Case C ";
break;
case 'z':
cout<<"CaseZ ";
break;
default:
cout<<"Default";
}
return 0;
}
OUTPUT: Case B
a. #include <iostream>
int main()
{
int num = 2;
switch (num + 2)
{
case 1:
cout<<"Case 1: ";
case 2:
cout<<"Case 2: ";
case 3:
cout<<"Case 3: ";
default:
cout<<"Default: ";
}
return 0;
}
OUTPUT: Default:
b. #include <iostream>
void main()
{
int a=10;
switch(a){
case 5+5:
cout<<"Hello\n";
default:
cout<<"OK\n";
}
}
OUTPUT: Hello
OK
c. #include<iostream>
int main()
{
switch(2/3)
{
case 1:
cout<<"case 1 executed ";
case 2:
cout<<"case 2 execcuted ";
break;
default:
cout<<"Default block executed";
}
return 0;
}
OUTPUT: Default block executed
b) if (grade == 'A')
cout<<"Very good";
else if (grade== 'B')
cout<<"Good";
else if (grade == 'C')
cout<<"Moderate";
else
cout<<"Try harder!";
ANSWER:
#include<iostream>
Using namespace std;
int main()
{
Char grade;
Cout<<”enter grade”;
Cin>>grade;
Switch(grade)
{
Case ‘A’: cout<<”very good”;
Break;
Case ‘B’:cout<<”good”;
Break;
Case ‘C’:cout<<”moderate”;
Break;
Default: cout<<”try harder”;
}
Return 0;
}
Task 4: Write a C++ program to print the day of the week using switch
case.
PROGRAM:
#include<iostream>
Using namespace std;
int main()
{
int day;
cout<<”enter the number of day(1-7)”;
cin>>day;
switch(day)
{
Case 1: cout<<”Sunday”;
Break;
Case 2: cout<<”Monday”;
Break;
Case 3: cout<<”Tuesday”;
Break;
Case 4: cout<<”Wednesday”;
Break;
Case 5: cout<<”Thursday”;
Break;
Case 6: cout<<”Friday”;
Break;
Case 7: cout<<”Saturday”;
Break;
Default: cout<<”enter valid no. from (1-7)”;
}
Return 0;
}
Task 5: Write a C++ program to implement a calculator (a|A for add, s|S
for subtract, m|M for multiply, d|D for division) using switch case.
PROGRAM:
#include<iostream>
Using namespace std;
int main()
{
int operators,a,b,r;
cout<<”enter the number of operators”(1-4);
cin>>operators;
switch(operators)
{
Case 1: cout<<”\n enter any 2 number to find their sum”;
Cin>>a>>b;
r=a+b;
cout<<”\n sum is”<<r;
break;
case 2: cout<<”\n enter any 2 numbers to find subtraction”;
cin>>a>>b;
r=a-b;
cout<<”\n subtraction is”<< r;
case 3: cout<<”\n enter any 2 numbers to find their product”;
cin>>a>>b;
r=a*b;
cout<<”\n multiplication is=”<<r;
break;
case 4: cout<<”\n enter any numbers to find their division”;
cin>>a>>b;
r=(a/b);
cout<<”\n division is=”<<r;
default: cout<<”enter valid number from(1-4)”;
}
return 0;
}
PROGRAM:
#include<iostream>
int main()
int a,st;”reply”;
do
Cin>>a;
St=sqrt(a);
Cin>>reply;
While(“reply”==”y”||”reply”==”Y”);
Return 0;
}
b. Finding a power of a number
PROGRAM:
#include<iostream,.h>
#include<conio.h>
#include<math.h>
Void main()
Clrscr();
Int a,n,p;
Cin>>a;
Cin>>n;
P=(a^n);