0% found this document useful (0 votes)
11 views7 pages

C++ Assighnment 3

The document provides explanations and code examples in C++ for various programming concepts including conditional statements, relational and logical operators, and the use of the switch statement. It includes code snippets for finding the largest of three numbers, checking if a number is even or odd, and a menu-driven program. Additionally, it evaluates expressions and discusses the differences between various programming constructs.

Uploaded by

Sahil
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views7 pages

C++ Assighnment 3

The document provides explanations and code examples in C++ for various programming concepts including conditional statements, relational and logical operators, and the use of the switch statement. It includes code snippets for finding the largest of three numbers, checking if a number is even or odd, and a menu-driven program. Additionally, it evaluates expressions and discusses the differences between various programming constructs.

Uploaded by

Sahil
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

6.

1] Differentiate between the following:

a. “if” statement and “if-else” statement

b. Relational operators and Logical operators

c. AND operator and OR operator

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.

Whereas if the Boolean expression is true, then statement/s 1


will be executed and then the control will move to statement 3. otherwise, if the Boolean expression
is false then the statement/s 2 will be executed and then the control will move to statement 3.

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.

6.2 Write a program in C++ to find the largest of three numbers.

Ans]

#include <iostream>

using namespace std;

int main()

int a,b,c,mx;

cout<<"enter the 1st number"<<endl;

cin>>a;

cout<<"enter the 2nd number"<<endl;

cin>>b;

cout<<"enter the 3rd number"<<endl;

cin>>c;

mx=(a>b)?((a>c)?a:c):((b>c)?b:c);

cout << "largest of three numbers is:" << mx<< endl;

return 0;

}
6.3] Write a program in C++ to check whether a number is even or odd.

Ans]

#include <iostream>

using namespace std;

int main()

int a,b,c,mx;

cout<<"enter the 1st number"<<endl;

cin>>a;

cout<<"enter the 2nd number"<<endl;

cin>>b;

cout<<"enter the 3rd number"<<endl;

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;

cout << "largest of three numbers is:" << mx<< endl;

return 0;

6.4 Evaluate the following expression for the given values of x, y and z:

(x>=y)||(!z==y)&&(z<x)

1. x=5, y=8, z=9

2. x=7, y=10, z=2

3. x=9, y=9, z=9


Ans] a. 0

b. 0

c. 1

6.5 What will be displayed when the following code is executed?

#include <iostream>

using namespace std;

int main()

int i=0;

if (i=5)

cout<<i;

cout<<i;

return 0;

Ans] 55
7.1 Differentiate between the following:

a. Conditional operator and “if-else” construct

b. “if-else” and “switch”

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>

using namespace std;

int main()

int num;

cout<<"enter the number"<<endl;

cin>>num;

(num%2==0)?(cout<<"number is even"):(cout<<"number is odd");

return 0;

7.3 Write a program in C++ to find the largest of three numbers using conditional operator.

Ans]

#include <iostream>

using namespace std;

int main()

int a,b,c,mx;
cout<<"enter the 1st number"<<endl;

cin>>a;

cout<<"enter the 2nd number"<<endl;

cin>>b;

cout<<"enter the 3rd number"<<endl;

cin>>c;

mx=(a>b)?((a>c)?a:c):((b>c)?b:c);

cout << "largest of three numbers is:" << mx<< endl;

return 0;

7.4 Write a menu driven program in C++ using “switch” statement. The

choices are:

a. Largest of two numbers

b. Largest of three numbers

c. Number is even/odd

Ans]

#include <iostream>

using namespace std;

int main()

int choice;

cout<<"1] FIND LARGEST OF TWO NUMBERS"<<endl;

cout<<"2] FIND THE LARGEST OF THREE NUMBERS"<<endl;

cout<<"3] FIND IF A NUMBER IS EVEN OR ODD"<<endl;

cout<<"Enter your choice"<<endl;

cin>>choice;

switch(choice)

case 1: int a,b,mx;

cout <<"ENTER THE 1st NUMBER"<< endl;


cin>>a;

cout<<"ENTER THE 2nd NUMBER"<<endl;

cin>>b;

mx=(a>b)?a:b;

cout<<"LARGEST NUMBER IS:"<< mx<<endl;

break;

case 2: int a1,b1,c1,mx1;

cout<<"enter the 1st number"<<endl;

cin>>a1;

cout<<"enter the 2nd number"<<endl;

cin>>b1;

cout<<"enter the 3rd number"<<endl;

cin>>c1;

mx1=(a1>b1)?((a1>c1)?a1:c1):((b1>c1)?b1:c1);

cout << "largest of three numbers is:" << mx1<< endl;

break;

case 3: int num;

cout<<"enter the number"<<endl;

cin>>num;

(num%2==0)?(cout<<"number is even"<<endl):(cout<<"number is odd"<<endl);

break;

default:cout<<"ENTER CORRECT CHOICE"<<endl;

cout<<"VISIT AGAIN! THANK YOU!"<<endl;

}
7.5 What will be displayed when the following code is executed?

#include <iostream>

using namespace std;

int main()

int i, j=5;

i=(j!=5)?5,0;

cout<<i<<j;

return 0;

Ans] 05

You might also like