Lab Week3&4 Abdullah Emam-1-26
Lab Week3&4 Abdullah Emam-1-26
______________________
Course Instructor / Lab Engineer
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
No of Checks
SUB TOTAL
TOTAL SCORE
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
test
expression?
True
Two-way Branching
Lab Manual for Programming in C Lab by. Abdullah Emam IT program Page 3
Programming with C - Lab
Lab Manual for Programming in C Lab by. Abdullah Emam IT program Page 4
Programming with C - Lab
Output:
(1) Enter student marks: 55
Student Passed
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;
Lab Manual for Programming in C Lab by. Abdullah Emam IT program Page 5
Programming with C - Lab
Output:
(1) Enter number: 53
53 is odd number
(2) Enter student marks: 42
42 is even number
Lab Manual for Programming in C Lab by. Abdullah Emam IT program Page 6
Programming with C - Lab
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;
Lab Manual for Programming in C Lab by. Abdullah Emam IT program Page 8
Programming with C - Lab
}
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.
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.
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
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)
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
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
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
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.
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;
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”.
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.
#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.
#include<stdio.h>
int main()
{
int x=1;
labelx:
printf(“\n%d”,x);
x=x+1;
if(x<=10)
goto labelx;
}
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)
#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+
#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:
}
}
Page 26
Lab Manual for Programming in C Lab by. Abdullah Emam IT program