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

Lab Week3&4 Abdullah Emam-1-26

The document is a lab manual for Programming with C at Fayoum International Technological University, detailing lab performance criteria, control statements, and decision-making constructs in C programming. It includes examples of if statements, switch-case statements, and algorithms for various programming tasks. The manual is prepared by Abdullah Emam for first-year students in the Information Technology program for the academic year 2024/2025.

Uploaded by

hossamarpro
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)
2 views

Lab Week3&4 Abdullah Emam-1-26

The document is a lab manual for Programming with C at Fayoum International Technological University, detailing lab performance criteria, control statements, and decision-making constructs in C programming. It includes examples of if statements, switch-case statements, and algorithms for various programming tasks. The manual is prepared by Abdullah Emam for first-year students in the Information Technology program for the academic year 2024/2025.

Uploaded by

hossamarpro
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/ 26

Fayoum International Technological University

Student Name: ……………………………………………………………….…………


Student Group: …………………………………………………………………………

Lab Instructor : Abdullah Emam

Lab Performed In: School Lab

Programming with C - Lab


Semester - II

Fayoum International Technological University


Faculty of Technology
Information Technology Program
First Year – Essentials in C programming
Academic Year (2024/2025)

______________________
Course Instructor / Lab Engineer

Prepared by Abdullah Emam

Lab Manual for Programming in C Lab by. Abdullah Emam IT program Page 1
LAB POINTS SCORE
Lab Performance [10] 0 1 2 3 4 5
Lab Participation
Lab Activity Completion on the Same Day

Lab Submission [10] 0 1 2 3 4 5


Completeness & Correctness
Required Conclusion & Results

No of Checks
SUB TOTAL
TOTAL SCORE

LAB POINTS SCORE


Lab Performance [10] 0 1 2 3 4 5
Lab Participation
Lab Activity Completion on the Same Day

Lab Submission [10] 0 1 2 3 4 5


Completeness & Correctness
Required Conclusion & Results

No of Checks
SUB TOTAL
TOTAL SCORE

Lab Manual for Programming in C Lab by. Abdullah Emam IT program Page 2
WEEK – 4
1 Control Statements (Conditional): If and its Variants
2 Switch (Break)
3 Sample C Programs
------------------
Control Statements (Conditional – Decision Making)
We have a number of situations where we may have to change the order of execution of
statements based on certain conditions, or repeat a group of statements until certain
specified conditions are met. This involves a kind of decision making to see whether a
particular condition has occurred or not and then direct the computer to execute certain
statements accordingly.
C language possesses such decision-making capabilities and supports the following
statements known as control or decision-making statements.
1. if statement
2. switch statement
3. conditional operator statement
4. goto statement

Decision making with ‘if’ statement


The if statement is a powerful decision-making statement and is used to control the flow
of execution of statements. It is basically a two-way decision statement and is used in
conjunction with an expression. It takes the following form:
𝑖𝑓(𝑡𝑒𝑠𝑡 𝑒𝑥𝑝𝑟𝑒𝑠𝑠𝑖𝑜𝑛)
It allows the computer to evaluate the expression first and then depending on whether
the value of expression (or condition) is true (1) or false (0), it transfers the control to a
particular statement. This point of program has two paths to follow, one for the true
condition and the other for the false condition.
Entry

test
expression?

True

Two-way Branching

Lab Manual for Programming in C Lab by. Abdullah Emam IT program Page 3
Programming with C - Lab

Examples of decision making, using if statement are


1. if (bank balance is zero) borrow money
2. if(age is more than 60) person retires

The if statement may be implemented in different forms depending on the complexity of


conditions to be tested.
1. Simple if statement
2. if…else statement
3. Nested if…else statement
4. else if ladder
Simple ‘if’ statement
The general form of a ‘simple if’ statement is
Syntax:
if(test_expression)
{
statement_block;
}
statement_x;
‘statement_block’ may be a single statement or a group of statements. If the test
expression is true the ‘statement_block’ will be executed, otherwise the ‘statement_block’
will be skipped and the execution will jump to ‘statement_x’.

Flowchart for Simple If

Example: To check whether student is passed or failed.


/*Program to check whether student is passed or failed*/
#include<stdio.h>
main()
{
int marks;

Lab Manual for Programming in C Lab by. Abdullah Emam IT program Page 4
Programming with C - Lab

printf("Enter student marks: ");


scanf("%d",&marks);
if(marks>50)
printf("Student Passed");
if(marks<50)
printf("Student Failed");
}

Output:
(1) Enter student marks: 55
Student Passed

(2) Enter student marks: 40


Student Failed

If-Else Statement
The if-else statement is an extension of the ‘simple if’ statement. The general form is
Syntax:
if(test_expression)
{
true-block-statements;
}
else
{
false-block-statements;
}
statement_x;

If the test_expression is true, then the true-block-statement(s), immediately following the


if statement are executed; otherwise the false-block-statement(s) are executed. In either
case, either true-block-statements or false-block-statements will be executed, not both.

Flowchart for If Else

Lab Manual for Programming in C Lab by. Abdullah Emam IT program Page 5
Programming with C - Lab

Example: Program to check whether given number is even or odd


/*Program to check whether given number is even or odd*/
#include<stdio.h>
#include<conio.h>
main()
{
int num; =
printf("Enter number: ");
scanf("%d",&num);
if(num%2 == 0)
printf("%d is even number",num);
else
printf("%d is odd number",num);
}

Output:
(1) Enter number: 53
53 is odd number
(2) Enter student marks: 42
42 is even number

Nested If… Else Statement


When a series of decisions are involved, we may have to use more than one if…else
statement in nested form as follows:
Syntax:
if(test_condition1)
{
if(test_condition2)
{
statement-1;
}
else
{
statement-2;
}
}
else
{
statement-3;
}
statement-x;

Lab Manual for Programming in C Lab by. Abdullah Emam IT program Page 6
Programming with C - Lab

If the test_condition1 is false, the statement-3 will be executed; otherwise it continues to


perform the second test. If the test_condition2 is true, the statement-1 will be executed
otherwise statement-2 will be evaluated and then the control is transferred to the
statement-x;
Flowchart for Nested If…Else

Example: Program to find the largest of three numbers


/*Program to find the largest of three numbers*/
#include<stdio.h>
main()
{
int a,b,c;
printf("Enter the three values: ");
scanf("%d %d %d",&a,&b,&c);
if(a>b && a>c)
printf("%d is largest",a);
else
if(b>a && b>c)
printf("%d is largest",b);
else
printf("%d is largest",c);

Lab Manual for Programming in C Lab by. Abdullah Emam IT program Page 7
Programming with C - Lab

Output:
Enter number: 5 6 7
7 is largest

Else If Ladder
There is another way of putting if’s together when multipath decisions are involved. A
multipath decision is a chain of if’s in which the statement associated with each else is an
if. It takes the following general form:

Syntax:
if(condition1)
statement-1;
else if(condition-2)
statement-2;
else if(condition-3)
statement-3;
...
...
...
else if(condition-n)
statement-n;
else
default-statement;
statement-x;

This construct is known as else if


ladder. The conditions are evaluated
from the top downwards. As soon as
a true condition is found, the
statement associated with it is
executed and the control is
transferred to statement-x. When all
the n conditions become false, then
the final else containing the default-
statement will be executed.

The logic of execution for ‘else if


ladder statements’ is shown in the
flowchart below.

Lab Manual for Programming in C Lab by. Abdullah Emam IT program Page 8
Programming with C - Lab

Example: Program to illustrate concept of else-if ladder to select color


/*Program to select color*/
#include<stdio.h> main()
{
int n;
printf("Enter any number between 1 & 4 to select color: \n");
scanf("%d",&n);
if(n==1)
{
printf("You selected Red color");
}
else if(n==2)
{
printf("You selected Green color");
}
else if(n==3)
{
printf("You selected yellow color");
}
else if(n==4)
{
printf("You selected Blue color");
}
else
{
printf("No color selected");
}

}
Output:
(1) Enter any value between 1 & 4 to select color: 4
You selected Blue Color
(2) Enter any value between 1 & 4 to select color: 1
You selected Red Color
(3) Enter any value between 1 & 4 to select color: 5
No color selected

Lab Manual for Programming in C Lab by. Abdullah Emam IT program Page 9
Programming with C - Lab

SWITCH-CASE STATEMENT
When one of many alternatives is to be selected we can design a program using 'if'
statement, to control the selection. However, the complexity of such programs in C
number of alternatives increases. The program becomes difficult to read and follow.

The switch test or checks the values of given variable (or expression) against a list of case
values and when a match is found a block of statements associated with that case is
executed. The switch makes one selection when there are several choices to be made.

The general form of switch statement is

Syntax:
switch(expression)
{
case value-1: block-1;
break;
case value-2: block-2;
break;
:
:
default: default-block;
break;
}
statement-x;

The expression is an integer expression or characters. Value-1, value-2, ... are constants or
constant expressions and are known as case labels. Each of these values should be unique
within a switch statement.

Block-1, block-2, ... are statements lists and may contain 0 or more statements. There is
no need to put braces ({ }) around these blocks. Case labels end with a colon(:).

When a switch is executed the value of the expression is compared against the value
(value-1, value-2, ...). If a case is found whose value of expression then block of
statements that follows the case are executed.

The break statement at the end of each block signals the end of a particular case and
causes an exit from the switch statement, transferring the control to the statement-x
following the switch statement.

The default is an optional case, when present, it will be executed if the value of the
expression does not match with any of the case values.

Lab Manual for Programming in C Lab by. Abdullah Emam IT program Page 10
Programming with C - Lab

If not present, no action takes place and if all matches fail; the control goes to the
statement-x.

The selection process of switch statement is illustrated in flow chart:

Example 1: Program to print words corresponding numbers below 9


/*Print words corresponding numbers below 9*/
#include<stdio.h>
main()
{
int n;
printf("Enter a number(0-9): ");
scanf("%d",&n);
switch(n)
{
case 0: printf("Zero");
break;
case 1: printf("One");
break;
case 2: printf("Two");
break;
case 3: printf("Three");
break;
case 4: printf("Four");
break;
case 5: printf("Five");
break;

Lab Manual for Programming in C Lab by. Abdullah Emam IT program Page 11
Programming with C - Lab
case 6: printf("Six");
break;
case 7: printf("Seven");
break;
case 8: printf("Eight");
break;
case 9: printf("Nine");
break;
default: printf("More than 9");
}
}
Output:
Enter a number (0-9): 3
Three
Enter a number (0-9): 10
More than 9

Algorithms for Conditional and Case Control

Example: Write an algorithm to find the largest of three numbers.


Step 1. Read the numbers a, b, c
Step 2. If a>b AND a>c then
Step 3. Print ''a is the largest number"
Step 4. Else If b>a AND b>c then
Step 5. Print ''b is the largest number"
Step 6. Else Print ''c is the largest number"
Step 7. End of program.

Example: Write an algorithm to calculate pay salary with overtime.


[Salary depends on the pay rate and the number of hours worked per week. However, if you work more
than 40 hours, you get paid time-and-a-half for all hours worked over 40.]
Step 1. Read hours and rate
Step 2. If hours ≤ 40 then
Step 3. Set salary as hours * rate
Step 4. Else
Step 5. Set salary as [40 * rate + (hours – 40) * rate * 1.5]
[salary = pay rate times 40 plus 1.5 times pay rate times (hours worked - 40)]
Step 6. Print salary
Step 7. End of program.

Lab Manual for Programming in C Lab by. Abdullah Emam IT program Page 12
Programming with C - Lab

Lab Manual for Programming in C Lab by. Abdullah Emam IT program Page 13
Programming with C - Lab

Sample C Programs
1. To check whether number is +ve, -ve or zero
/*Program to check number is positive, negative or zero*/
#include<stdio.h>
main()
{
int n;
printf("Enter a number: ");
scanf("%d",&n);
if(n>0)
printf("Number is Positive");
if(n<0)

printf("Number is Negative"); if(n==0)


printf("Number is Zero");
}
Output:
Enter a number: -2
Number is Negative
Enter a number: 0
Number is Zero
Enter a number: 6
Number is Positive

2. To check two numbers are equal


/*Program to check whether the given numbers are equal*/
#include<stdio.h>
#include<conio.h>
main()
{
int num1,num2;
printf("Enter 2 numbers: ");
scanf("%d %d",&num1,&num2);
if(num1==num2)
printf("Both the numbers are equal");
}
Output:
Enter 2 numbers: 6 6
Both the numbers are equal
Enter 2 numbers: 3 2

Lab Manual for Programming in C Lab by. Abdullah Emam IT program Page 14
Programming with C - Lab
3. Check whether given character is vowel or consonant.
/*Program to check whether the given character is vowel or consonant */
#include<stdio.h>
main()
{
char x;
printf("Enter letter: ");
x=getchar();
if(x=='a'||x=='A'||x=='e'||x=='E'||x=='i'||x=='I'||x=='o'||x=='O'
||x=='u'||x=='U')
printf("The character %c is a vowel",x);

else
printf("The character %c is a consonant",x);
}
Output:
Enter letter: p
The character p is a consonant
Enter letter: a
The character a is a vowel
4. Program to calculate square of numbers whose least significant digit is 5.
/*Program to calculate square of numbers whose LSD is 5 */
#include<stdio.h>
main()
{
int s,d;
printf("Enter a Number: ");
scanf("%d",&s);
d=s%10;
if(d==5)
{
s=s/10;
printf("Square = %d %d",s*s++,d*d);
}
else
printf("\nInvalid Number");
}

Lab Manual for Programming in C Lab by. Abdullah Emam IT program Page 15
Programming with C - Lab
Output:
Enter a Number: 25
Square = 625

Enter a Number: 32
Invalid Number

5. To obtain the electric bill as per the charges below


No of Units Consumed Rates (In Rs.)
500 and above 5.50
200-500 3.50
100-200 2.50
Less than 100 1.50

/*Program to obtain electric bill as per meter reading*/


#include<stdio.h>

main()
{
int initial,final,consumed;
float total;
printf("Initial & Final Readings: ");
scanf("%d %d",&initial,&final);
consumed = final-initial;
if(consumed>500)
total=consumed*5.50;
else if(consumed>=200 && consumed<=500)
total=consumed*3.50;
else if(consumed>=100 && consumed<=199)
total=consumed*2.50;
else if(consumed<100)
total=consumed*1.50;
printf("Total bill for %d units is %f",consumed,total);
}
Output:
Initial & Final Readings: 1200 1500
Total bill for 300 units is 1050.000000

Initial & Final Readings: 800 900


Total bill for 100 units is 250.000000

Lab Manual for Programming in C Lab by. Abdullah Emam IT program Page 16
Programming with C - Lab
6. To check whether letter is small, capital, digit or special symbol.
/*Program to check small,capital,digit or special*/
#include<stdio.h>
#include<conio.h>
main()
{
char x;
printf("Enter character: ");
scanf("%c",&x);
if(x>='a' && x<='z')
printf("Small letter");
else if(x>='A' && x<='Z')
printf("Capital letter");
else if(x>='0' && x<='9')
printf("Digit");
else
printf("Special Symbol");
}
Output:
Enter character: *
Special Symbol
Enter character: T
Capital letter

Enter character: a
small letter
Enter character: 6
Digit

7: Program to check whether a letter is vowel or consonant


/*Program to check whether given letter is vowel or consonant*/
#include<stdio.h>
#include<conio.h>
main()
{
char ch;
printf("Enter Character: ");
ch = getch();
switch(ch)
{
case 'a':
case 'A':
case 'e':
case 'E':
case 'i':
case 'I':
Lab Manual for Programming in C Lab by. Abdullah Emam IT program Page 17
Programming with C - Lab
case 'o':
case 'O':
case 'u':
case 'U': printf("Character %c is Vowel",ch);
break;
default: printf("Character %c is Consonant",ch);
}
}

Output:
Enter Character: a
Character a is Vowel

Enter Character: B
Character B is Consonant
8: Program to calculate Arithmetic Operations depending on operator.
/*Print words corresponding numbers below 9*/
#include<stdio.h>
main()
{
int a,b; ;
printf("Enter any arithmetic operator: ");
scanf("%c",&ch);
printf("Enter two numbers: ");
scanf("%d %d",&a,&b);
switch(ch)
{
case '+': printf("\nThe sum is %d",a+b);
break;
case '-': printf("\nThe difference is %d",a-b);
break;
case '*': printf("\nThe product is %d",a*b);
break;
case '/': printf("\nThe quotient is %d",a/b);
break;
case '%': printf("\nThe remainder is %d",a%b);
break;
default: printf("Not Valid Operator");
}
}
Output:
Enter any arithmetic operator: *
Enter two numbers: 8 4
The product is 32

Lab Manual for Programming in C Lab by. Abdullah Emam IT program Page 18
Programming with C - Lab
1-Write a program to check whether number is negative or positive
using if….else.

Logic : if number >=0 then positive else negative


#include<stdio.h>
void main()
{
int x;
printf (“Enter any number :- “);
scanf (“%d”,&x); //input value of x
if (x>=0)
//check for positive number
printf (“%d is a positive number”,x);
else
printf (“%d is negative number”,x);
}

2-Write a program to enter a number and display whether the number is positive or
negative using ternary operator.
Ternary operator is a control statement similar to if…else statement: Syntax of
ternary operator is: (Condition)?true statement :false statement ; Where condition is
formed using variable and operator (relational operator), true statement is the
statement to be executed when the condition is true and false statement will be
executed when the condition is false (just like else statement). It is used when logic
is very simple in conditional statement.

Program:
Page 19
Lab Manual for Programming in C Lab by. Abdullah Emam IT program
Programming with C - Lab
#include<stdio.h>
void main()
{
int x;

printf (“Enter any number :- “);


scanf (“%d”,&x); //input value of x
(x>=0) ? printf (“Number is positive”):printf(“Number is negative”);
}

3- Write a program to display greatest number out of two numbers using ternary
operator.

4- Write a program to enter your age, and display the message whether “eligible to
vote” or “not eligible to vote”.

5- Write a program to check whether number is even or odd.

6-Write a program to enter a number and display whether number is divisible by 5


or not.

7- Write a program to enter a number and check number is divisible by


2 and 4 both.

8- Write a program to enter a number and check whether number is divisible by 3


and 7.

9- Write a program to check character is vowel or consonant .(Example


Page 20
Lab Manual for Programming in C Lab by. Abdullah Emam IT program
Programming with C - Lab
of nested if…else)

10- Write a program to enter two numbers and find the greatest number.

11- Write a program to enter three numbers and find the smallest number.

12-Write a program to enter number and check whether divisible by 4, 12 or both.

13-Write a program to enter a number between (1 to 7) and display accordingly


Sunday to Saturday using switch case. Use goto statement to continue program till
user’s press Y(Yes)

12- Write the output of the following program :

#include<stdio.h>
void main()
{
int n;
printf(“Enter no. between 1 and 3 “);
scanf(“%d”,&n);
switch(n)
{
case 1: printf(“\n1”);
break;
case 2: printf(“\n2”);
break;
case 3: printf(“\n3”);
break;
default: printf(“\nWrong choice”);
break;
}
}

13- Write a program to enter two numbers x and y and also enter an operator (+,-
Page 21
Lab Manual for Programming in C Lab by. Abdullah Emam IT program
Programming with C - Lab
,*,/,%). Find out the result using switch case.Continue your program till the user
press ‘Y’ to continue.

14- Write a program to display numbers from 1 to 10 using goto statement.


Note: goto can be used to repeat statement for number of times. Goto is controlled
by if…else statement. Remember when we use goto statement in program the speed
of a program is getting slow. It’s better to use loop instead of goto statement. Goto
is not considered as good programming practice.

#include<stdio.h>

int main()
{
int x=1;

labelx:
printf(“\n%d”,x);
x=x+1;
if(x<=10)
goto labelx;
}

14-Predict the output of the following program.


#include<stdio.h>
void main()
{
int x=1;
A:
/*to clear screen*/
printf("\n%d",x);
x=x+1;
B:
printf("\n%d",x+1);
x=x+2;
if(x<=10)
goto A;
Page 22
Lab Manual for Programming in C Lab by. Abdullah Emam IT program
Programming with C - Lab
else if(x<=20)
goto B;
else
return 0;
}

15- Predict the output of the following program:


#include<stdio.h>
void main()
{
int a=10;
Label1:
printf(“\n%d”,a);
a=a+10;
Label2:
printf(“\n%d”,a);
a=a+20;
if(a<=100)
goto Label1;
else if(a>=100 && a<=300)
goto Label2;
return 0;
}

Page 23
Lab Manual for Programming in C Lab by. Abdullah Emam IT program
Programming with C - Lab
LAB EXERCISE #3

Objective(s):
To understand the programming knowledge using Decision Statements (if, if-else,
if-else-if ladder, switch and GOTO)

Program: Write a program to print whether a given number is even or odd.

#include<stdio.h>
Int main()
{
int num;
printf("Enter the number: ");
scanf(“%d”,&num);
if(num%2==0)
printf(“\n %d is even”, num);
else
printf(“\n %d is odd”, num);
return 0;
}

Output:
Enter the number: 6
6 is even

Page 24
Lab Manual for Programming in C Lab by. Abdullah Emam IT program
Programming with C - Lab
Programs List :
1. Write a Program to Check Whether a Number is Prime or not.
2. Write a program to find the largest and smallest among three entered numbers
and also display whether the identified largest/smallest number is even or odd.
3. Write a program to compute grade of students using if else adder. The grades are
assigned as followed:
Marks Grade
b. marks<50 F
c. 50≤marks< 60 C
d. 60≤marks<70 B
e. 70≤marks<80 B+
f. 80≤marks<90 A
g. 90≤mars≤ 100 A+

4.Write a program to find whether a character is consonant or vowel using


switch statement.

#include <stdio.h>
int main()
{
char ch;
printf(“Enter any alphabet:”); //input alphabet from user
scanf(“%c”, &ch);
switch(ch)
{
case „a‟:
case „A‟:
printf(“Vowel”);
break;
case „e‟:
case „E‟:
printf(“Vowel”);
break;
case „i‟:
case „I‟:
Page 25
Lab Manual for Programming in C Lab by. Abdullah Emam IT program
Programming with C - Lab
printf(“Vowel”);
break;
case „o‟:
case „O‟:
printf(“Vowel”);
break;
case „u‟:
case „U‟:
printf(“Vowel”);
break;
default:
}
}

5.Write a program to print day name using switch case.


6.Write a program to determine whether the input character is capital or small
letter, digits or special symbol.
7.Write a program to check whether a date is valid or not.
8. Write a program to check whether a number is positive, negative or zero
using switch case.

Page 26
Lab Manual for Programming in C Lab by. Abdullah Emam IT program

You might also like