1.

Rewrite the following using nested switch construct.#include<iostream>using namespace std; int main() { int a,b;cout<<"Enter value for a and b";cin>>a>>b;if(b==0)    cout<<"Divide by zero error";else    if(a==0)    cout<<"The result is zero";else   cout<<"The result is "<<(float)a/b;}

Answer»

#include<iostream>

using namespace std; 

int main() 

int a,b;

cout<<"Enter value for a and b";

cin>>a>>b;

switch(b)

{

case 0 : cout<<"Divide by zero error";

break;

default:

switch(a)

{

case 0:cout<<"The result is zero";

break;

default:

cout<<"The result is "<<(float)a/b;

}

}

}



Discussion

No Comment Found

Related InterviewSolutions