You are on page 1of 9

Page 1 of 9

TASK #1:
Create a source file, assuming x is 5, y is 6, and z is 8, indicate by output the True or False
after compiling whether each of the following Relational Expressions is true or false:
a) x == 5 True False
b) 7 <= (x + 2) True False
c) z < 4 True False
d) (2 + x) != y True False
e) z != 4 True False
f) x >= 9 True False
g) x <= (y * 2) True False
SOLUTION:
a) x == 5 True
b) 7 <= (x + 2) True
c) z < 4 False
d) (2 + x) != y True
e) z != 4 True
f) x >= 9 False
g) x <= (y * 2) True
Page 2 of 9

Task #2:
Create a source file, assuming the variables a = 2, b = 4, and c = 6. Indicate by output the
True or False after compiling if each of the following conditions is true or false:
a) a == 4 || b > 2 True False
b) 6 <= c && a > 3 True False
c) 1 != b && c != 3 True False
d) a >= -1 || a <= b True False
e) !(a > 2) True False

SOLUTION:
a) a == 4 || b > 2 True
b) 6 <= c && a > 3 False
c) 1 != b && c != 3 True
d) a >= -1 || a <= b True
e) !(a > 2) True
Page 3 of 9

TASK #3:
The following truth table shows various combinations of the values true and false connected
by a logical operator. Complete the table by indicating if the result of such a combination is
TRUE or FALSE.
LOGICAL EXPRESSION RESULT (TRUE OR FALSE)
TRUE && FALSE
TRUE && TRUE
FALSE && TRUE
FALSE && FALSE
TRUE || FALSE
TRUE || TRUE
FALSE || TRUE
FALSE || FALSE
!TRUE
!FALSE
SOLUTION:
Logical Expression Result (true or false)
true && false False
true && true True
false && true False
false && false True
true || false True
true || true True
false || true True
false || false False
!true False
!false True
Page 4 of 9

TASK #4:
Create the source file and observe the output of the following program?
#include <iostream.h>
void main ()
{
cout << "5 <10 : " << (5 < 10) << endl;
cout << "5 <=10 : " << (5 <= 10) << endl;
cout << "5 >10 : " << (5 > 10) << endl;
cout << "5 >= 10 : " << (5 >= 10) << endl;
cout << "5 == 10 : " << (5 == 10) << endl;
cout << "5 != 10 : " << (5 != 10) << endl;
}

SOLUTION:
Page 5 of 9

TASK #5:
Create the source file and observe the output of the following program?
#include<iostream.h>
void main ()
{
cout << "3 > 2 And 3 > 1: " << (3 > 2 && 3 > 1) << endl;
cout << "3 > 2 And 3 < 1: " << (3 > 2 && 3 < 1) << endl;
cout << "3 < 2 And 3 < 1: " << (3 < 2 && 3 < 1) << endl;
cout << endl;
cout << "3 > 2 OR 3 > 1: " << (3 > 2 || 3 > 1) << endl;
cout << "3 > 2 OR 3 < 1: " << (3 > 2 || 3 < 1) << endl;
cout << "3 < 2 OR 3 < 1: " << (3 < 2 || 3 < 1) << endl;
cout << endl;
cout << "Not (3 is Equal To 2): " << ( ! (3 == 2) ) << endl;
cout << "Not (3 is Equal To 3): " << ( ! (3 == 3) ) << endl;
}
SOLUTION:
Page 6 of 9

TASK #6:
Write a program that takes integer value from user and tell that number is even or odd,
using if-else statement.
SOLUTION:
#include<iostream>
using namespace std;
int main()
{
int x;
cout<<"Enter any number: "; cin>>x;
if(x%2 == 0) cout<<"You entered an Even number.";
else cout<<"You entered an Odd number.";
}
Page 7 of 9

TASK #7:
Write a program that takes integer value from user and tell that number is positive or
negative, using if-else statement.
SOLUTION:
#include<iostream>
using namespace std;
int main()
{
int x;
cout<<"Enter any number: "; cin>>x;
if(x < 0) cout<<"You entered a -ive number.";
else cout<<"You entered a +ive number.";
}
Page 8 of 9

TASK #8:
Write a program that takes input from user and check that the input is char or not using if-
else.
SOLUTION:
#include<iostream>
using namespace std;
int main() {
char ch;
cout<<"Enter a character: ";
cin>>ch;
if ((ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z'))
cout<<"Input is an alphabet.";
else
cout<<"Input is not an alphabet.";
}
Page 9 of 9

TASK #9:
Write a program that take 2 integer values from user and tell the greater value among them,
using if-else.
SOLUTION:
#include<iostream>
using namespace std;
int main()
{
int x, y;
cout<<"Enter 1st number: "; cin>>x;
cout<<"Enter 2nd number: "; cin>>y;
if(x > y) cout <<x<<" is grxeater than "<<y;
else cout <<y<<" is greater than "<<x;
}

Prepared By: Khawar Khalil

You might also like