Programs using while loop
Example-1 –
WAP in C to print the numbers from 1, 2, 3..... 10
#include < stdio.h >#include < conio.h >
void main ( )
{
// Variable Declaration
int i;
clrscr ( );
// Processing and Output
i = 1;
while ( i < = 10 )
{
printf ( “\n %d “, i);
i = i + 1;
}
getch ( );
}
Example-2 –
WAP in C to print the numbers from 10, 9, 8..... 1
#include < stdio.h >
#include < conio.h >
void main ( )
{
// Variable Declaration
int i;
clrscr ( );
// Processing and Output
i = 10;
while ( i > = 1 )
{
printf ( “\n %d “, i);
i = i - 1;
}
getch ( );
}
Example-3 –
WAP in C to print the numbers from N, N-1, N-2.....3, 2, 1
#include < stdio.h >
#include < conio.h >
void main ( )
{
// Variable Declaration
int i, n ;
clrscr ( );
// Input
printf ( “ \n Enter the value of N \n “ );
scanf ( “ %d “, &n );
// Processing and Output
i = n;
while ( i > = 1 )
{
printf ( “\n %d “, i);
i = i - 1;
}
getch ( );
}
Example-4 –
WAP in C to print the numbers from 1, 2, 3, ... N-2, N-1, N
#include < stdio.h >
#include < conio.h >
void main ( )
{
// Variable Declaration
int i, n ;
clrscr ( );
// Input
printf ( “ \n Enter the value of N \n “ );
scanf ( “ %d “, &n );
// Processing and Output
i = 1;
while ( i < = n )
{
printf ( “\n %d “, i);
i = i + 1;
}
getch ( );
}
Example-5 –
WAP in C to print the numbers from 2, 4, 6 ... N-2, N
#include < stdio.h >
#include < conio.h >
void main ( )
{
// Variable Declaration
int i, n ;
clrscr ( );
// Input
printf ( “ \n Enter the value of N \n “ );
scanf ( “ %d “, &n );
// Processing and Output
i = 2;
while ( i < = n )
{
printf ( “\n %d “, i);
i = i + 2;
}
getch ( );
}
Example-6 –
WAP in C to print the numbers from 200, 400, 600 ... N-200, N
#include < stdio.h >
#include < conio.h >
void main ( )
{
// Variable Declaration
int i, n ;
clrscr ( );
// Input
printf ( “ \n Enter the value of N \n “ );
scanf ( “ %d “, &n );
// Processing and Output
i = 200;
while ( i < = n )
{
printf ( “\n %d “, i);
i = i + 200;
}
getch ( );
}
Example-7 –
WAP in C to print the numbers from 2, 4, 8, 16 ... N/2, N
#include < stdio.h >
#include < conio.h >
void main ( )
{
// Variable Declaration
int i, n ;
clrscr ( );
// Input
printf ( “ \n Enter the value of N \n “ );
scanf ( “ %d “, &n );
// Processing and Output
i = 2;
while ( i < = n )
{
printf ( “\n %d “, i);
i = i * 2;
}
getch ( );
}
Example-8 –
WAP in C to print the numbers from N, N/2, ... 16, 8, 4, 2, 1
#include < stdio.h >
#include < conio.h >
void main ( )
{
// Variable Declaration
int i, n ;
clrscr ( );
// Input
printf ( “ \n Enter the value of N \n “ );
scanf ( “ %d “, &n );
// Processing and Output
i = n;
while ( i > = 1 )
{
printf ( “\n %d “, i);
i = i / 2;
}
getch ( );
}
Example-9 –
A number is input through the keyboard. WAP in C to print the table of that number.
#include < stdio.h >
#include < conio.h >
void main ( )
{
// Variable Declaration
int i, n, ans ;
clrscr ( );
// Input
printf ( “ \n Enter the value of N \n “ );
scanf ( “ %d “, &n );
// Processing and Output
i = 1;
while ( i < = 10 )
{
ans = n * i;
printf ( “\n %d “, ans);
i = i + 1;
}
getch ( );
}
Example-10 –
A number is input through the keyboard. WAP in C to print the sum of its digits. [Hint: n= 1257 ans = 1+2=5+7 = 15]
#include < stdio.h >
#include < conio.h >
void main ( )
{
// Variable Declaration
int n, ans, a ;
clrscr ( );
// Input
printf ( “ \n Enter the value of N \n “ );
scanf ( “ %d “, &n ); //1257
// Processing and Output
ans =0;
while ( n ! = 0 )
{
a = n % 10; //1
ans = ans + a; // 15
n = n / 10; //0
}
printf ( “\n %d “, ans); //15
getch ( );
}