II Semester - Programming in C Lab Manual
Experiment – 10
Exercise on if..else statement
Program 26: Write a C program to test whether given number is even or odd by using if
else statement.
#include<stdio.h>
#include<conio.h>
void main(){
int n ;
clrscr() ;
printf("Enter any integer number: ") ;
scanf("%d", &n) ;
if ( n%2 == 0 )
printf("Given number is EVEN\n") ;
else
printf("Given number is ODD\n") ;
Output: Enter an integer number: 24
Given number is EVEN
Prepared by B. Naresh Page 20
II Semester - Programming in C Lab Manual
Experiment – 11
Exercise on else..if ladder statement
Program 27: Write a C program to find the largest of three given numbers by using if
else ladder.
#include<stdio.h>
#include<conio.h>
main()
float a, b, c, max;
printf(“ Enter a, b, c:\n);
scanf(“%f%f%f”, &a, &b, &c);
max= a;
if(b>max)max=b;
if(c>max)max=c;
printf(“largest of three given numbers = %f\n”, max);
Output
Enter a, b, c:
12.00 5.00 30.00
Largest of three given numbers = 30.00
Program 28: Write a C program to Test whether given number is divisible by 5.
#include<stdio.h>
#include<conio.h>
void main(){
int n ;
clrscr() ;
printf("Enter any integer number: ") ;
Prepared by B. Naresh Page 21
II Semester - Programming in C Lab Manual
scanf("%d", &n) ;
if ( n%5 == 0 )
printf("Given number is divisible by 5\n") ;
printf("statement does not belong to if!!!") ;
Output: Enter any integer number: 32
statement does not belong to if!!!
Prepared by B. Naresh Page 22
II Semester - Programming in C Lab Manual
Experiment – 12
Exercise on switch statement
Program 29: Write a C program to Display pressed digit in words by using switch
statement.
#include<stdio.h>
#include<conio.h>
void main(){
int n ;
clrscr() ;
printf("Enter any digit: ") ;
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 ;
case 6: printf("SIX") ;
break ;
case 7: printf("SEVEN") ;
break ;
case 8: printf("EIGHT") ;
break ;
case 9: printf("NINE") ;
break ;
default: printf("Not a Digit") ;
}
getch() ;
Prepared by B. Naresh Page 23
II Semester - Programming in C Lab Manual
}
Output
Enter any digit: 7
Seven
Program 30: Write a C program to read a value in range 1 to 12 and print the name of
that month by using switch statement.
#include<stdio.h>
#include<conio.h>
main()
int month;
printf(“Enter a number between 1 to 12\n:”);
scanf(“%d”, &month);
switch(month)
case 1:
printf(“January\n”);
break;
case 2:
printf(“February\n”);
break;
case 3:
printf(“March\n”);
break;
case 4:
printf(“April\n”);
Prepared by B. Naresh Page 24
II Semester - Programming in C Lab Manual
break;
case 5:
printf(“May\n”);
break;
case 6:
printf(“June\n”);
break;
case 7:
printf(“July\n”);
break;
case 8:
printf(“August\n”);
break;
case 9:
printf(“September\n”);
break;
case 10:
printf(“October\n”);
break;
case 11:
printf(“November\n”);
break;
case 12:
printf(“December\n”);
Prepared by B. Naresh Page 25
II Semester - Programming in C Lab Manual
break;
default:
printf(“Unrecognized number”);
Output:
Enter a number between 1 to 12: 6
June
Prepared by B. Naresh Page 26
II Semester - Programming in C Lab Manual
Experiment – 13
Exercise on conditional operator
Program 31: Write a C program to check whether given number is odd or even
#include<stdio.h>
int main()
int num;
printf(“Enter the number : “);
scanf(“%d”, &num);
(num%2==0)?printf(“Even”):printf(“Odd”);
Output:
Enter the number: 7
Odd
Program 32: Write a C program to find the eligible for voting by age.
#include <stdio.h>
int main()
{
int age; // variable declaration
printf("Enter your age");
scanf("%d",&age); // taking user input for age variable
(age>=18)? (printf("eligible for voting")) : (printf("not eligible for
voting")); // conditional operator
return 0;
}
Output: Enter your age23
eligible for voting
Prepared by B. Naresh Page 27
II Semester - Programming in C Lab Manual
Experiment – 14
Exercise on while statement
Program 33: Write a C program to display even numbers upto 10 by using while
statement.
#include<stdio.h>
#include<conio.h>
void main(){
int n = 0;
clrscr() ;
printf("Even numbers upto 10\n");
while( n <= 10 )
{
if( n%2 == 0)
printf("%d\t", n) ;
n++ ;
}
getch() ;
}
Output:
Even numbers up to 10
0 2 4 6 8 10
Prepared by B. Naresh Page 28
II Semester - Programming in C Lab Manual
Experment – 15
Exercise on do...while statement
Program 34: Write a C program to display odd numbers upto 10 by using do while
loop.
#include<stdio.h>
#include<conio.h>
void main(){
int n = 0;
clrscr() ;
printf("Odd numbers upto 10\n");
do
{
if( n%2 == 1)
printf("%d\t", n) ;
n++ ;
}while( n <= 10 ) ;
getch() ;
}
Output
Enter odd numbers up to 10
1 3 5 9
Prepared by B. Naresh Page 29
II Semester - Programming in C Lab Manual
Experiment – 16
Exercise on for statement
Program 35: Write a C program display even numbers upto 10 by using for statement.
#include<stdio.h>
#include<conio.h>
void main(){
int n ;
clrscr() ;
printf("Even numbers upto 10\n");
for( n = 0 ; n <= 10 ; n++ )
{
if( n%2 == 0)
printf("%d\t", n) ;
}
getch() ;
}
Output
Even numbers upto 10
0 2 4 6 8 10
Program 36: Write a C program to display i and j values by using nested for loop.
#include<stdio.h>
void main()
{
int i, j;
for(i=0; i<2; i++)
{
for(j=0; j<4; j++)
{
printf("%d, %d\n", i,j);
}
printf("\n");
}
}
Output: 0, 0
0, 1
Prepared by B. Naresh Page 30
II Semester - Programming in C Lab Manual
0, 2
0, 3
1, 0
1, 1
1, 2
1, 3
Prepared by B. Naresh Page 31