Programming in c Experiment 10-16
Programming in c Experiment 10-16
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() ;
scanf("%d", &n) ;
if ( n%2 == 0 )
else
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;
max= a;
if(b>max)max=b;
if(c>max)max=c;
Output
Enter a, b, c:
#include<stdio.h>
#include<conio.h>
void main(){
int n ;
clrscr() ;
scanf("%d", &n) ;
if ( n%5 == 0 )
Experiment – 12
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() ;
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() ;
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;
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”);
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”);
break;
default:
printf(“Unrecognized number”);
Output:
June
Experiment – 13
#include<stdio.h>
int main()
int num;
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
Experiment – 14
#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
Experment – 15
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
1 3 5 9
Experiment – 16
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");
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
0, 2
0, 3
1, 0
1, 1
1, 2
1, 3