0% found this document useful (0 votes)
13 views9 pages

Programming For Problem Solving: Experiment 3 - 2

The document outlines various programming tasks related to error identification in C++ code, switch-case implementations, and mathematical operations. It includes examples of incorrect code with explanations of errors, as well as tasks requiring the conversion of if-else statements to switch-case structures. Additionally, it provides sample programs for calculating square roots and powers of numbers.
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)
13 views9 pages

Programming For Problem Solving: Experiment 3 - 2

The document outlines various programming tasks related to error identification in C++ code, switch-case implementations, and mathematical operations. It includes examples of incorrect code with explanations of errors, as well as tasks requiring the conversion of if-else statements to switch-case structures. Additionally, it provides sample programs for calculating square roots and powers of numbers.
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/ 9

PROGRAMMING FOR PROBLEM SOLVING

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

Task2: Find the output of the following

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

Task 3: Rewrite the following code segment using switch case.


a) if ( node == 15)
{ curValue += 5;
Count++;
}
else if (node == 40)
{ curValue *= 1.5;
Count++;
}
else
curValue -= 2;
ANSWER:
#include<iostream>
Using namespace std;
int main()
{
int value, count;
cout<<”enter the current value”;
cin>>value;
switch(value)
{
Case 1: cout<<”enter current value+=5;
Break;
Case 2: cout<<”enter current value *=1.5;
Break;
Case 3: cout<<”enter current value-=2;
break;
default: cout<<”enter valid number”;
}
Return 0;
}

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;
}

Task 6: Write a program to give a choice to user to perform

below mathematical operations:

a. Finding a square root of number

PROGRAM:

#include<iostream>

using namespace std;

int main()

int a,st;”reply”;

do

Cout<<”enter +ve number”;

Cin>>a;

St=sqrt(a);

Cout<<”square root value is:”<<st;

Cout<<<”do you want to find sqrt of another number(y/n):”;

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;

Cout<<”enter any number to as base”;

Cin>>a;

Cout<<”enter any number to find its”;

Cin>>n;

P=(a^n);

Cout<<”nth power of a is”<<p

You might also like