0% found this document useful (0 votes)
11 views

Lab-02

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 views

Lab-02

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/ 13

COMPUTER FUNDAMENTALS AND

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.

2. Condi onal Statement Where can I get lab handouts?


A: All material related to laboratory sessions will be available on Zambeel and
3.
Canvas under CFP-2024 course folder / page.

Is it mandatory to submit the work done in a laboratory session?


A: Not necessarily, submission of work related to laboratory session varies from
among laboratory sessions. Submission if required will be communicated via the
laboratory handout along with deadline and when and how to submit details.

Will there be a laboratory exam?


A: Laboratory performance is graded based (a) comple on and quality of assigned
work (b) number of laboratory sessions a ended (c) laboratory quizzes (d) course
project.

The lab work of a session should be completed with


all ques ons resolved before the next lab session.

LAB INSTRUCTORS
Afaq Ahmed
Amen Wadood
Nouman Shamim

EMAIL
[email protected]

Let’s Talk to Computer

CF-Lab-02 prepared by Dr. Nouman Shamim


Sec on -01 Reading

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 ]

Operator Purpose Examples

> 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

List of rela onal operators with example

#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;
}

CF-Lab-02 prepared by Dr. Nouman Shamim


LOGICAL OPERATORS

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

Operator Meaning of Operator Example


&& Logial 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.

Logical operators and examples to demonstrate their working principle

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;

printf("Testing relational operators\n");

printf(" a=%d , b=%d\n",a,b);

result=(a<0) && (b<0);


printf(" a > 0 and b > 0 \t= %d \n",result);

result=(a<10) && (b<5);


printf(" a < 10 and b < 5 \t= %d\n",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;
}

CF-Lab-02 prepared by Dr. Nouman Shamim


Sec on-02 Basic if, else statement
Carefully read this sec on, as it introduces another part of if statement i.e. else and provides examples to
demonstrate how if and else work

IF STATEMENT

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 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; }

CF-Lab-02 prepared by Dr. Nouman Shamim


Sec on-03 Ac vi es

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.

To better understand the task see the examples.


Given that A, B, C, D are all integer variables having some initial value (which is not known), see the conversion of
regular logical statements to C-program statements using relational operator.

Example (Single condition)

Regular Statement In C-Program


A is a positive number A>0
B is not zero B!=0
A and B are positive numbers A>0 && B>0
B and C are non-zero numbers B!=0 && C!=0

Single Condition Multiple conditions

1) A and B are equal 1) C and D are negative numbers


2) A is an even number 2) A is greater than B and C is greater than D
3) 5 C is a factor of C 3) A is even or B is odd number

4) B divides C and there is no remainder 4) A or B or C is a positive number


5) C is greater than D 5) A and B are greater than 20 or C and D are less than 100
6) B is an odd number 6) A and B are even numbers

7) Sum of A and B is less than C 7) A is even and B is odd number


8) C is greater than 100 and less than 200 8) C is positive and D is negative
9) A or B is even and C is odd

Table-01

CF-Lab-02 prepared by Dr. Nouman Shamim


PROGRAM-01 (CHARACTER INPUT)

#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.

13%5 = 13 / 5 = 2 (quotient) + 3 (remainder) = 3

17%5 = 17 / 5 = 3 (quotient) + 2 (remainder) = 2

56%13= 4 (quotient) + 4 (remainder) = 4

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)

CF-Lab-02 prepared by Dr. Nouman Shamim


The following program will get an integer from the user and will check whether the number is even or odd

#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:\>

Enter value for X: 3

Enter value for Y: 10

X does not completely Divides Y

Remainder is = 1

Press any key to terminate the program. . .

CF-Lab-02 prepared by Dr. Nouman Shamim


Program-03 (Same or different numbers)

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

Result: 9 is greatest among 5, 8 and 9

Sample Output-02

C:\>

Enter value of A : 5

Enter value of B : 8

Enter value of C : 8

Result: 8 is greatest among 5,8 and 8

CF-Lab-02 prepared by Dr. Nouman Shamim


Ac vity-04
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 the
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

Ac vity-05
The criteria for the selection of players in a college is as under

1- The age of player should be between 16 to 20


2- The minimum height should be 5.6’’
3- 60% marks in matriculation
Write a program that asks user to enter data for the selection procedure, the program should inform the user about the
selection or rejection of a player, if a player is rejected the program should also display the reason of rejection. For
better understanding see sample output of such program.

Sample Output-1

Player Selector.

Enter Age: 19

Enter Height:5.7

Enter %age marks: 72

Result: Congratulation Your are Selected

Sample Output-2

Player Selector.

Enter Age: 19

Enter Height:5.7

Enter %age marks: 52

Result : Sorry, You are not selected,

CF-Lab-02 prepared by Dr. Nouman Shamim


Reason: You do not meet the educational requirement

Sample Output-3

Player Selector.

Enter Age: 23

Enter Height:5.7

Enter %age marks: 52

Result : Sorry, You are not selected,

Reason: You do not meet the age requirement

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

Enter the marks: 80

Grade: A

Sample Output-2

(if user enters invalid marks)

Enter the marks: 125

Invalid marks, no grades can be assigned

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.

CF-Lab-02 prepared by Dr. Nouman Shamim


Pay Scale Calculation of allowance / taxes

17,18,19 Tax = 10% of Basic Salary

17,18,19 Medical Allowance = 15% of Basic Salary

18,19 Special Allowance1= 10% of Basic Salary

19 Special Allowance2 = 15% of Basic Salary

Data to be used as input to your program

Pay Scale Basic Salary

17 30000

18 40000

19 60000

Expected Outcome: The program should display full pay summary as below

Scale 17

Tax (Tax Calculated)

Medical (Medical Calculated)

Special Allowance1 (As Calculated)

Special Allowance2 (As Calculated)

Gross Salary Actual Salary

Net Payable Salary After Deduction

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.

No of Units Unit Price

1 to 200 Rs. 20

1 to 300 Rs. 45

1 to 700 Rs. 60

More than 700 Rs. 90

CF-Lab-02 prepared by Dr. Nouman Shamim


For units greater than 200, first 200 units should be charged @ Rs. 20 per unit while remaining should be charged
according to the criteria provided above.

Activity-09
Ac vity-09
Write a program that determines whether a student is admissible to certain according to the following selection
criteria.

Marks Matriculation > 65%

Marks Intermediate > 70 %

Marks Graduation > 65 %

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;
}

The output of your program should be as under

Sample Output

Enter a number : 457

Sum of digits is in 457 is =16

CF-Lab-02 prepared by Dr. Nouman Shamim


Activity-11
Ac vity-11
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 3 rd
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 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

Enter a number 342

Not a Armstrong number

Sample Output-02

Enter a number 371

The number is an Armstrong

CF-Lab-02 prepared by Dr. Nouman Shamim

You might also like