Download as pdf or txt
Download as pdf or txt
You are on page 1of 7

Pakistan Institute of Engineering and

Applied Sciences

Computing Fundamentals
Laboratory Exercise-03

Date: Mar 26, 2021

Computing Fundamentals Lab 2


Topics Covered
1. Relational operators
2. Logical Operators
3. If‐else statements

Relational Operators

When programming, the aim of the program will often require checking of one value stored by a variable against
another value to determine whether one is larger, smaller, or equal to the other. There are a number of operators
that allow these checks. When a comparison is performed by relation operator the result is either true or false.
Here are some of the relational operators, along with examples:

Relational Operator Purpose Example

> Greater than 5 > 4 is TRUE, 3 > 4 is FALSE


< Less than 4 < 5 is TRUE, 4 < 4 is FALSE
>= Greater than or equal 4 >= 4 is TRUE, 3 >= 4 is FALSE
<= Less than or equal 3 <= 4 is TRUE, 5 <= 4 is FALSE
== Equal to 5 == 5 is TRUE, 3==6 is FALSE
!= Not equal to 5 != 4 is TRUE, 5!=5 is FALSE

Logical Operators

Logical operators are used to combine expressions containing relation operators. In C, there are 3 logical
operators:

Operator Meaning of Operator Example

&& Logical AND If c=5 and d=2 then, ((c==5) && (d>5)) returns false.

|| Logical OR If c=5 and d=2 then, ((c==5) || (d>5)) returns true.

! Logical NOT If c=5 then, !(c==5) returns false.

Explanation

For expression, ((c==5) && (d>5)) to be true, both c==5 and d>5 should be true but, (d>5) is false in the given
example. So, the expression is false. For expression ((c==5) || (d>5)) to be true, either the expression should be
true. Since, (c==5) is true. So, the expression is true. Since, expression (c==5) is true, !(c==5) is false.

Basic if,else Statement

if
The structure of an if statement is as follows:

if (Comparison)
Line of code

The line of code will execute only if the comparison is true, if there are multiple line of code to be executed
when a comparison is true, the structure will be as follows

if (Comparison){
Line of code-1
Line of code-2
Line of code-3
Line of code-.
Line of code-.
Line of code-N

Computing Fundamentals Lab 2


}
else
Sometimes when the condition in an if statement evaluates to false, it would be nice to execute some code instead
of the code executed when the statement evaluates to true. The "else" statement effectively says that whatever
code after it (whether a single line or code between brackets) is executed if the if statement is FALSE. It can look
like this:

if ( TRUE ) {
/* Execute these statements if TRUE */
}
else {
/* Execute these statements if FALSE */
}

Self-Test‐01
#include <stdio.h>

int main(){
int age;
printf( "Please enter your age" ); //Asks for age
scanf( "%d", &age ); //The input is put in age

if ( age < 100 ) { //If the age is less than 100


printf ("\nYou are pretty young!" ); //Executed if comparison is true
}
else {
printf( "\nYou are really old\n" ); //Executed if comparison is false
}

getchar();
retrun 0;
}

Ternary Conditional Operators

There is also a short-hand if else, which is known as the ternary operator because it consists of
three operands. It can be used to replace multiple lines of code with a single line. It is often used
to replace simple if else statements:

Self-Test-2
#include<stdio.h>

int main()

{
int time = 20;
string result = (time < 18) ? "Good day." : "Good evening.";
printf(“%d”, result);

getch();
return 0;
}

Computing Fundamentals Lab 2


Tasks Section

Task 1

Write a program that lets user enter an integer value between 1 and 10, the program validates the input, if the
value entered is between 1 and 10 the program prints the message “Valid Number” and value entered
otherwise the program should print message “Invalid Number” and value. For better understanding of the
problem following are two sample outputs of the program

Sample Output-1

Enter a number between 1 and 10 -->56


Invalid Number 56

Sample Output-2

Enter a number between 1 and 10 -->6


Valid Number 6

Task 2

Write a program that gets a three digit integer input and prints the sum of its digits. For example if user enters
123 the program should calculate 1+2+3 = 6 as answer. To get an idea about the solution see the program
below

#include<stdio.h>
#include<conio.h>
void main(){
int num=12;
int digit1,digit2;
digit1=num%10;
digit2=num/10;

printf(“First digit is = %d ”,digit1);


printf(“\nSecond digit is =%d’,digit2);
}
Sample Output
Enter a number: 457
Sum of digits is = 16

Task 3

Write a C program to read the age of a candidate and determine whether it is eligible for

casting his/her own vote. Sample Output is given below:

Sam

Computing Fundamentals Lab 2


Task 4

Armstrong Numbers: An Armstrong number is an n-digit number that is equal to the sum of the nth powers of
its digits. For example 153 is an Armstrong number as it is a three digit number and if each digit is raised to
the 3rd power and summed the result will be 153.

13+53+33=153

Write a program that gets a three digit number from the user and checks whether the number is an
Armstrong number or not. Output of your program should be as under.

Hint: To check that a number is an Armstrong or not you first need to separate its digits as done in last activity

Sample Output
Enter a number 342
Not an Armstrong number

Enter a number 371


The number is an Armstrong

Task 5

Write a program t to check whether the triangle is valid or not if sides are given. Implement this code using ternary operator.Let’s say that a, b, c
is the sides of the triangle. So it must satisfy the below criteria:

1. a + b > c
2. a + c > b
3. b + c > a

Sample Output is given below:

Task 6

Write a C program to read temperature in centigrade and display a suitable message according to temperature state below:
Temp < 0 then Freezing weather
Temp 0-10 then Very Cold weather
Temp 10-20 then Cold weather
Temp 20-30 then Normal in Temp
Temp 30-40 then Its Hot
Temp >=40 then Its Very Hot
Implement this code using ternary operator.

Computing Fundamentals Lab 2


Computing Fundamentals Lab 2
Computing Fundamentals Lab 2

You might also like