C++ Assighnment 3
C++ Assighnment 3
Ans] a. If the Boolean output of the expression is true, then the block of statements will be executed
and then the control will go to statement 2. Otherwise these statements will be skipped and the
control will directly go to statement 2.
B. Relational operator is used to compare two values and give output as true/false. Whereas logical
operator is used to relational/ logical expression and gives output as true/false.
C. In case of AND operator, the output will be true only when all the input are true. Whereas In case
of OR operator, the output will be false only when all the inputs are false.
Ans]
#include <iostream>
int main()
int a,b,c,mx;
cin>>a;
cin>>b;
cin>>c;
mx=(a>b)?((a>c)?a:c):((b>c)?b:c);
return 0;
}
6.3] Write a program in C++ to check whether a number is even or odd.
Ans]
#include <iostream>
int main()
int a,b,c,mx;
cin>>a;
cin>>b;
cin>>c;
//mx=(a>b)?((a>c)?a:c):((b>c)?b:c);
if(a>b)
if(a>c)
mx=a;
else
mx=c;
else
if(b>c)
mx=b;
}
else
mx=c;
return 0;
6.4 Evaluate the following expression for the given values of x, y and z:
(x>=y)||(!z==y)&&(z<x)
b. 0
c. 1
#include <iostream>
int main()
int i=0;
if (i=5)
cout<<i;
cout<<i;
return 0;
Ans] 55
7.1 Differentiate between the following:
Ans] a. conditional operator is a single statement construct whereas if else contains an if block and
else block and is a multiple line construct.
b. if else construct is slower and contains two blocks of statements which is to be executed
according to thecondition whereas switch statement is faster and can contain multiple number of
cases which is to be executed according to the condition.
7.2 Write a program in C++ to check whether a number is even/odd using conditional operator.
Ans]
#include <iostream>
int main()
int num;
cin>>num;
return 0;
7.3 Write a program in C++ to find the largest of three numbers using conditional operator.
Ans]
#include <iostream>
int main()
int a,b,c,mx;
cout<<"enter the 1st number"<<endl;
cin>>a;
cin>>b;
cin>>c;
mx=(a>b)?((a>c)?a:c):((b>c)?b:c);
return 0;
7.4 Write a menu driven program in C++ using “switch” statement. The
choices are:
c. Number is even/odd
Ans]
#include <iostream>
int main()
int choice;
cin>>choice;
switch(choice)
cin>>b;
mx=(a>b)?a:b;
break;
cin>>a1;
cin>>b1;
cin>>c1;
mx1=(a1>b1)?((a1>c1)?a1:c1):((b1>c1)?b1:c1);
break;
cin>>num;
break;
}
7.5 What will be displayed when the following code is executed?
#include <iostream>
int main()
int i, j=5;
i=(j!=5)?5,0;
cout<<i<<j;
return 0;
Ans] 05