EXAMPLES OF LOOP
Write a program to find factorial of a given no
#include<stdio.h>
void main()
{
int n,f=1;
printf(“enter any no”);
scanf(“%d”, &n);
Rahul:
F= f*n;
n--;
if(n>1)
goto Rahul;
printf(“factorial = %d”, f);
}
WAP to read a month number. Print it in words.
#include<stdio.h>
void main()
{
int no;
printf(“enter a month number :”);
scanf(“%d”,&no);
switch( no )
{
case 1:
printf(“JAN”);
break;
case 2:
printf(“FEB”);
break;
case 3:
printf(“MAR”);
break;
case 4:
printf(“APR”);
break;
case 5:
printf(“MAY”);
break;
case 6:
printf(“JUN”);
break;
case 7:
printf(“JUL”);
break;
case 8:
printf(“AUG”);
break;
case 9:
printf(“SEP”);
break;
case 10:
printf(“OCT”);
break;
case 11:
printf(“NOV”);
break;
case 12:
printf(“DEC”);
break;
default :
printf(“Invalid input”);
}
}
Output1 : Enter a month number : 2 FEB
OUTPUT 2: Enter a month number : 12 DEC
Output3: Enter a month number : 16 Invalid input
WAP to read a week number. Print it in words.
#include<stdio.h>
void main()
{
int no;
printf(“enter a week number :”);
scanf(“%d”,&no);
switch( no )
{
case 1:
printf(“SUN”);
break;
case 2:
printf(“MON”);
break;
case 3:
printf(“TUE”);
break;
case 4:
printf(“WED”);
break;
case 5:
printf(“THU”);
break;
case 6:
printf(“FRI”);
break;
case 7:
printf(“SAT”);
break;
default :
printf(“Invalid input”);
}
}
Output 1 : Enter a week number : 2 Mon
Output 2 : Enter a week number : 1 Sun
Output 3 : Enter a week number : 10 Invalid input.
WAP to print natural numbers from 1 to 100.
#include<stdio.h>
void main()
{
int no=1;
while( no<=100 )
{
printf(“%6d”,no);
no++;
}
}
Output : 1 2 3 4 5 6 … 100
WAP to print odd numbers from 1 to 100.
#include<stdio.h>
void main()
{
int no=1;
while( no<=100 )
{
printf(“%5d”,no);
no+=2;
}
}
Output : 1 3 5 7 9 … 99
WAP to print even numbers from 1 to 100.
#include<stdio.h>
void main()
{
int no=2;
while( no<=100 )
{
printf(“%5d”,no);
no+=2;
}
}
Output : 2 4 6 8 10 … 100
WAP to print natural number those are divisible by 5 from 1 to 100.
#include<stdio.h>
void main()
{
int no=1;
while( no<=100 )
{
if( no%5==0 )
printf(“%6d”,no);
no++;
}
}
Output : 5 10 15 20 25 … 100
WAP to print natural numbers those are divisible by 7 or 11.
#include<stdio.h>
void main()
{
int no=1;
while( no<=100 )
{
if( no%7==0 || no%11==0 )
printf(“%6d”,no);
no++;
}
}
Output : 7 11 14 21 22 28 33 35 96 99
WAP to print hail stone series.
#include<stdio.h>
void main()
{
int no;
printf(“Enter a number :”);
scanf(“%d”,&no);
printf(“%5d”,no);
while( no!=1 )
{
if( no%2==0 )
no=no/2;
else
no=no*3+1;
printf(“%5d”,no);
}
}
Output : Enter a number : 22 22 11 34 17 52 26 13 40 20 10 5 16 8 4 2 1
WAP to read a number. Find first and last digits of a number. No Fd ld 1527 7 7 152 2 15 5 1 1
#include<stdio.h>
void main()
{
int no,fd,ld;
printf(“Enter a number :”);
scanf(“%d”,&no);
id=no%10;
while( no!=0 )
{
fd=no%10;
no=no/10;
}
printf(“First digit=%d”,fd);
printf(“\nLast digit=%d”,ld);
}
Output: Enter a number : 1527 First digit : 1 Last digit : 7
WAP to read a number. Find the factorial value. 5!= 120 6!= 720 7!= 5040 8!= 40320
#include<stdio.h>
void main()
{
int no,i;
long int f;
printf(“Enter a number :”);
scanf(“%d”,&no);
for( i=1,f=1;i<=no;i++ )
f=f*i;
printf(“Factorial=%ld”,f);
}
Output : Enter a number : 4 Factorial=24
WAP to solve following problem. There are some goats and hens. When I count their eyes the count is 60. When I count their legs the count is 84. Print how many goats are there and hens are there. G H Legs 1 29 4+58=62 2 28 8+56=64 … 12 18 48+36=84
#include<stdio.h>
void main()
{
int g,h;
for( g=1,h=29;g<=30;g++,h--)
if( g*4+h*2==84 )
break;
printf(“Goats=%d”,g);
printf(“\nHens=%d”,h);
}
Output : Goats=12 Hens=18
WAP to solve the following problem. Rita has a money pouch. In that equal number of 1 Rupee coins, 50 paise coins, 25 paise coins. The total money is 700 rupees. Then find how many 1 rupee coins , 50 paise coins, 25 paise coins are there. 1 rupee 50 paise 25 paise Total 1 1 1 1.75 2 2 2 3.50 3 3 3 5.25 … 400 400 400 400+200+100=700
#include<stdio.h>
void main()
{
int n;
for( n=1;n<=700; n++ )
if( n*1+n*0.5+n*0.25==700 )
break;
printf(“1 Rupee coins=%d”,n);
printf(“\n50 paise coins=%d”,n);
printf(“\n25 paise coins=%d”,n);
}
Output :- 1 Rupee coins=400
WAP to print following pattern.
#include<stdio.h>
void main()
{
int i,j,s=4;
for( i=1;i<=5;i++ ) {
for( j=1;j<=s;j++)
printf(“ ”);
for( j=1;j<=i;j++ )
printf(“* ”);
printf(“\n”);
s--;
}
}
WAP to print following pattern.
#include<stdio.h>
void main()
{
int i,j,s=0;
for( i=5;i>=1;i--) {
for( j=1;j<=s;j++ )
printf(“ ”);
for( j=1;j<=i;j++)
printf(“* ”);
printf(“\n”); s++;
}
}
WAP to print following pattern.
#include<stdio.h>
void main()
{
int i,j;
for( i=1;i<=5;i++ ) {
for( j=1;j<=i;j++ ) {
if( j==1 || j==i || i==5 )
printf(“* ”);
else printf(“ ”);
}
printf(“\n”);
}
}
Output
*
* *
* *
* *
* * * * *
WAP to print following.
#include<stdio.h>
void main()
{
int i,j;
for( i=1;i<=5;i++ ) {
for( j=1;j<=5;j++ ) {
if( i==1 || j==3 )
printf(“* ”);
else printf(“ ”);
}
printf(“\n”);
}
}
Output
* * * * *
*
*
*
*
WAP to find count of odd elements and even elements entered by user.
#include<stdio.h>
void main()
{
int no,oc=0,ec=0;
char ch;
do
{
printf(“\nEnter a number :”);
scanf(“%d”,&no);
if( no%2==0 )
ec++;
else
oc++;
printf(“Do you want to enter one more number?”);
scanf(“%c”,&ch);
}
while( ch!='n');
printf(“Even count=%d”,ec);
printf(“\nOdd count=%d”,oc);
}
Enter a number : 1 Do you want to enter one more number?
Enter a number : 2 Do you want to enter one more number? y
Enter a number : 3 Do you want to enter one more number? y
Enter a number : 4 Do you want to enter one more number? y
Enter a number : 5 Do you want to enter one more number? n
Even count=2 Odd count=3
WAP to develop EVM application.
#include<stdio.h>
void main()
{
int tc=0,cc=0,pc=0,ch;
do
{
printf(“\n1. Taj Mahal”);
printf(“\n2. China Wall”);
printf(“\n3. Pyramid”);
printf(“\n4. Exit”);
printf(“\n\nEnter your vote :”);
scanf(“%d”,&ch);
switch( ch )
{
case 1: tc++;
break;
case 2: cc++;
break;
case 3: pc++;
break;
case 4:
break;
default: printf(“Invalid Vote”);
}
}
while( ch!=4 );
printf(“\nTaj Mahal votes=%d”,tc);
printf(“\nChina wall votes=%d”,cc);
printf(“\nPyramid votes=%d”,pc);
}
Output:
1. Taj Mahal
2. China Wall
3. Pyramid
4. Exit
Enter your vote : 3
1. Taj Mahal
2. China Wall
3. Pyramid
4. Exit
Enter your vote : 1
1. Taj Mahal
2. China Wall
3. Pyramid
4. Exit
Enter your vote : 2
1. Taj Mahal
2. China Wall
3. Pyramid
4. Exit
Enter your vote : 1
1. Taj Mahal
2. China Wall
3. Pyramid
4. Exit
Enter your vote : 2
1. Taj Mahal
2. China Wall
3. Pyramid
4. Exit
Enter your vote : 1
1. Taj Mahal
2. China Wall
3. Pyramid
4. Exit
Enter your vote : 4
Taj Mahal votes=3
China wall votes=2
Pyramid votes=1
Write a C program to print all two digit numbers, where the second digit is smaller than its first digit by 4, and if the number was divided by the digit’s sum, the quotient would be 7.
#include<stdio.h>
void main()
{
int no,fd,sd,diff,sum;
for( no=10;no<=99;no++) {
sd=no%10;
fd=no/10;
diff=fd-sd;
sum=fd+sd;
if( diff==4 && no/sum==7 )
printf(“%5d”,no);
}
}
Output: 62 73 84
WAP to print 1 to 100 without using loop.
#include<stdio.h>
void main()
{
int no=1;
ss:
printf(“%5d”,no);
no++;
if( no<=100)
goto ss;
}
No 1 2 3 4 ...101 output 1 2 3 ... 100
WAP to print “Welcome” message without using semi colon.
#include<stdio.h>
void main()
{
if( printf(“Welcome”) ) {
}
}
Output: Welcome
Explanation :
If( printf(“Welcome”))
If( 7 )
If( True )
WAP to execute If and Else both.
#include<stdio.h>
void main()
{
int no=1;
if( no==1 ) {
printf(“If statement executed”);
goto india;
}
else
{
india:
printf(“\nElse statement also executed”);
}
}
Output : If statement executed Else statement also executed