Professional Documents
Culture Documents
Pakistan Institute of Engineering and Applied Sciences: Computing Fundamentals
Pakistan Institute of Engineering and Applied Sciences: Computing Fundamentals
Applied Sciences
Computing Fundamentals
Laboratory Exercise-03
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:
Logical Operators
Logical operators are used to combine expressions containing relation operators. In C, there are 3 logical
operators:
&& Logical AND If c=5 and d=2 then, ((c==5) && (d>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.
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
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
getchar();
retrun 0;
}
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;
}
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
Sample Output-2
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;
Task 3
Write a C program to read the age of a candidate and determine whether it is eligible for
Sam
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
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
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.