Lab-02
Lab-02
PROGRAMMING
Lab-02
SCHEDULE
LAB MANUAL
CFP Laboratory sessions will be conducted in Computer Center on each Thursday
1. Introduc on from 8:25 to 11:30 a.m.
LAB INSTRUCTORS
Afaq Ahmed
Amen Wadood
Nouman Shamim
EMAIL
[email protected]
Read this sec on if you need to revise your concepts related to rela onal and logical operators otherwise
you should skip this sec on
RELATIONAL OPERATORS
When programming, the aim of the program will o en 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 rela on operator the result is either true or false. Here are some of
the rela onal operators, along with examples:
Demonstration [Execute the following program and try to understand the working of relational operators ]
#include <stdio.h>
int main()
{
int a=10,b=5, result;
printf("Testing Conditional Operators\n");
result=(a<5);
printf(" a < 5\t = %d\n",result);
result=(a>5);
printf(" a > 5\t = %d\n",result);
result=(a<=5);
printf(" a <= 5\t = %d\n",result);
result=(a>=5);
printf(" a >= 5\t = %d\n",result);
result=(a==5);
printf(" a == 5\t = %d\n",result);
result=(a!=5);
printf(" a != 5\t = %d\n",result);
return 0;
}
Logical operators are used to combine expressions containing rela on operators. In C, there are 3 logical operators:
Explana on
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 .
Demonstration [Execute the following program and try to understand the working of logical operators]
#include <stdio.h>
int main()
{
int a=10,b=5, result;
result=(a>10) || (b<5);
printf(" a > 10 or b < 5 \t= %d\n",result);
result=(a>=10) || (b<=5);
printf(" a >= 5 or b =< 5 \t= %d\n",result);
return 0;
}
IF STATEMENT
if ( Comparison )
Line of code
The line of code will execute only if the comparison is true, if there are mul ple line of code to be executed when a
comparison is true, the structure will be as follows
if ( Comparison/condition ){
Line of code-1
Line of code-2
Line of code-3
Line of code-.
Line of code-.
Line of code-N
}
else statement
Some mes when the condi on 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 effec vely says that whatever code a er
it (whether a single line or code between brackets) is executed if the if statement is FALSE.
It can look like this: (In c programs anything wri en between /* */ is called comments and is ignored by compiler)
if ( TRUE ) {
/* Execute these statements if TRUE */
}
else {
/* Execute these statements if FALSE */
}
Example
#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 */
} return 0; }
In each of the following ac vi es you are provided with a sample problems statement along with
programming solu ons to them, you have to complete the ac vi es given following the sample problem/
solu on. In most of the cases you have to extend the solu on of given programming solu on according to
the direc ons.
Task-01
Just for exercise convert the statements given in the table-01 into conditional statements for A, B, C and D
Note: You do not need to write program for this, either do it verbally or write it down on your notebook.
Table-01
#include <stdio.h>
int main()
{
char ans;
printf("Is today your birthday ? (Y/N) : ");
scanf("%c",&ans);
if(ans=='y' || ans == 'Y')
printf("\n Happy Birthday To You ");
else
printf("\nHave a nice day");
return 0 ;
}
PROGRAM-02 (% OPERATOR)
Revision: The % is called the Modulus operator, it returns the remainder of a division. e.g.
We can check if a number is even or odd by using % (mod) operator if a number is even the remainder with 2 will be
zero for odd number it will be a non-zero number
3 %2 = 1 (3 is odd)
7 %2 = 1 (7 is odd)
6% 2 = 0 (6 is even)
#include<stdio.h>
int main()
{
int num;
printf("Enter a Number : ");
scanf("%d",&num);
if(num%2==0)
printf("\nEven Number");
else
printf("\nOdd Number");
return 0;
}
Ac vity-01
Re-write the program-2 and check that the number entered by the user is a multiple of 7 or not
Ac vity-02
Extend the program-2, get two numbers X and Y from the user, check that X completely divides Y or not. If X does not
divides Y print the remainder. The output of the program should be as follows
SAMPLE OUTPUT
C:\>
Remainder is = 1
The following program will get three integers from the user and will check whether the numbers are same or not
#include <stdio.h>
int main()
{
int a,b,c;
printf("Enter value for A : ");
scanf("%d",&a);
printf("\nEnter value for B : ");
scanf("%d",&b);
printf("\nEnter value for C : ");
scanf("%d",&c);
if(a==b && a==c)
printf(" \nThe numbers are same ");
else
printf("\nThe numbers are different");
return 0;
}
Ac vity-03
Re-write the program-03 and find the greatest number and print its value.
Sample Output-01
C:\>
Enter value of A: 5
Enter value of B: 8
Enter value of C: 9
Sample Output-02
C:\>
Enter value of A : 5
Enter value of B : 8
Enter value of C : 8
Sample Output-1
Sample Output-2
Ac vity-05
The criteria for the selection of players in a college is as under
Sample Output-1
Player Selector.
Enter Age: 19
Enter Height:5.7
Sample Output-2
Player Selector.
Enter Age: 19
Enter Height:5.7
Sample Output-3
Player Selector.
Enter Age: 23
Enter Height:5.7
Ac vity-06
Write a program that ask users to input marks of a subject and print the letter grade (See grading criteria below). For
details see the following sample execution of the program
Grading Criteria
Marks 51 – 60 D
Marks 61 – 70 C
Marks 71 – 80 B
Marks 81 – 100 A
Sample Output-1
Grade: A
Sample Output-2
Ac vity-07
Write a program that should calculate the salary of an employee, the program should ask the pay scale from the user,
the valid scales are 17,18,19 in case a wrong scale is entered the program should print a message “Invalid Pay Scale”,
after acquiring the pay scale the program should calculate the salary according to the following criteria.
17 30000
18 40000
19 60000
Expected Outcome: The program should display full pay summary as below
Scale 17
Ac vity-08
Write a program that gets no of units consumed by a consumer, by using the following billing criteria the program
should calculate the electricity bill of the user.
1 to 200 Rs. 20
1 to 300 Rs. 45
1 to 700 Rs. 60
Activity-09
Ac vity-09
Write a program that determines whether a student is admissible to certain according to the following selection
criteria.
The program should ask user to enter the marks of matriculation, intermediate, and graduation and print the one of
the two messages “Admissible” or “Rejected”
Activity-10
Ac vity-10
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>
int 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);
return 0;
}
Sample Output
13+53+33=153
Write a program that gets a three digit number from the user and checks whether the number is a 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-01
Sample Output-02