In this article while loop programs we give the while loop program with output, do-while loop programs with output and for loop programs with output.

while loop programs:

// 1. while loop- Write a C Program to show 1st 10 natural numbers.

#include<stdio.h>

#include<conio.h>

void main()

{

int i=1;

clrscr();

printf(“\n 1st Ten natural numbers.: \n”);

while(i<=10)

{

printf(“\t %d”,i);

i++;

}

getch();

}

Output:

1st Ten natural numbers.

1          2          3          4          5          6          7          8          9          10

// 2. Write a C Program to show 1st 10 natural numbers sum.

#include<stdio.h>

#include<conio.h>

void main()

{

int i=1,sum=0;

clrscr();

printf(“\n 1st Ten natural numbers.: \n”);

while(i<=10)

{

printf(“\t %d”,i);

i++;

sum=sum+i;

}

printf(“\ Sum of 1st Ten natural numbers: %d”, sum);

getch();

}

Output:

1st Ten natural numbers.

1          2          3          4          5          6          7          8          9          10

Sum of 1st Ten natural numbers: 55

// 3. Write a C Program to show multiplication table for given number.

#include<stdio.h>

#include<conio.h>

void main()

{

int i=1,n=0;

clrscr();

printf(“\n Enter the number: \n”);

scanf(“%d”,&n);  //4

while(i<=10)

{

printf(“\t %d”,i*n);  // 4  8  12….

i++;

}

getch();

}

Output:

Enter the number: 4

4          8          12        16        20        24        28        32        36        40

// 4. Write a C Program to Show of odd and even numbers between 1 to 10.

#include<stdio.h>

#include<conio.h>

void main()

{

int i=1;

clrscr();

printf(“\n\n Odd numbers: “);

printf(“\t Even numbers: “);

while(i<=10)

{

if(i%2==0)

{

printf(“\t\t %d”,i);

}

else

{

printf(“\n %d”,i);

}

i++;

}

getch();

}

Output:

Odd numbers:                       Even numbers:

1                                              2

3                                              4

5                                              6

7                                              8

9                                              10

// 5. Write a C Program to sum of odd and even numbers between 1 to 10

#include<stdio.h>

#include<conio.h>

void main()

{

int i=1,oddsum=0,evensum=0;

clrscr();

printf(“\n\n Odd numbers: “);

printf(“\t Even numbers: “);

while(i<=10)

{

if(i%2==0)

{

printf(“\t\t %d”,i);

evensum=evensum+i;

}

else

{

printf(“\n %d”,i);

oddsum=oddsum+i;

}

i++;

}

printf(“\n Sum of even numbers: %d”,evensum);

printf(“\n Sum of Odd numbers: %d”,oddsum);

getch();

}

Output:

Odd numbers:                       Even numbers:

1                                              2

3                                              4

5                                              6

7                                              8

9                                              10

Sum of even numbers: 30

Sum of Odd numbers: 25

while loop programs:

/* 6. do-while loop – Write a C Program to show even number square and odd numbers cub between 1 to 10 numbers */

#include<stdio.h>

#include<conio.h>

void main()

{

int i=1;

clrscr();

printf(“\n\n Odd Numbers Cube:”);

printf(“\t Even number Square:”);

do

{

if(i%2==0)

{

printf(“\t\t\t %d”,i*i);

}

else

{

printf(“\n %d”,i*i*i);

}

i++;

}while(i<=10);

getch();

}

Output:

Odd Numbers Cube:                       Even number Square:

1                                                          4

27                                                        16

125                                                     36

343                                                     64

729                                                     100

// 7. Write a C Program to show ASCII numbers

// 48, 49, 50, ….  – 0, 1, 2, —, 9

// 65, 66, 67, …   – A, B, C, —, Z

// 97, 98, 99, …   – a,b,c, —-,z

#include<stdio.h>

#include<conio.h>

void main()

{

int i;

clrscr();

printf(“\n\n Digits: \n”);

for(i=48;i<=57;i++)

{

printf(“\t %c”,i);

}

printf(“\n\n Capital Letters: \n”);

for(i=65;i<=90;i++)

{

printf(“\t %c”,i);

}

printf(“\n\n Small Letters: \n”);

for(i=97;i<=122;i++)

{

printf(“\t %c”,i);

}

getch();

}

Output:

Digits: 0  1  2  3  4  5  6  7  8  9

Capital Letters: A      B         C         D         E         F          G         H         I           J          K         L          M         N            O         P         Q         R         S         T          U         V         W        X         Y         Z

Small Letters: a         b          c          d          e          f           g          h          I           j           k          l           m         n            o          p          q          r           s          t           u          v          w         x          y          z

/* 8. Write a C Program To find factorial for the given number

     0!=1    1!=1   2!=2*1=2     3!=3*2*1=6     4!=4*3*2*1=24 */

#include<stdio.h>

#include<conio.h>

void main()

{

long int n, fact=1;

clrscr();

printf(“\n\n Enter the number: “);

scanf(“%ld”,&n);  //3!=6

while(n>0)          // 3>0   2>0  1>0  0>0

{

fact=fact*n;   // 3  6  6

n–;           // 2  1  0

}

printf(“\n\n Factorial of given number: %ld”,fact);

getch();

}

Output:

Enter the number: 5

Factorial of given number: 120

// 9. Write a C Program to show Fibonacci Series

#include<stdio.h>

#include<conio.h>

void main()

{

int f=0,s=1,t=0,n=0;

clrscr();

printf(“\n\n Enter the number of terms:\n “);

scanf(“%d”,&n);

printf(“\n Fibonacci Series: \n ”)

while(n>0)

{

printf(“\t %d\t%d”,f,s);

t=f+s;

printf(“\t %d”,t);

f=s;

s=t;

n–;

}

getch();

}

Output:

Enter the number of terms: 5

Fibonacci Series:

0          1          1          2          3          5          8

// 10. while loop- Write a C Program to show sum of digit for ex. 123= 6, 471=12

#include<stdio.h>

#include<conio.h>

void main()

{

int n,dig=0,sum=0;

clrscr();

printf(“\n\n Enter the number: “);

scanf(“%d”,&n); // 123

while(n>0)      // 123>0    12>0   1>0  0>0

{

dig=n%10;    // 3   2    1

sum=sum+dig;   // 3   5    6

n=n/10;        // 12  1    0

}

printf(“\n\n Sum of digit for givan number: %d”,sum);

getch();

}

Output:

Enter the number: 123

Sum of digit for given number: 6

// 11. Write a C Program Armstrong numbers between 1 to 1000

#include<stdio.h>

#include<conio.h>

void main()

{

int i,dig,temp,sum;

clrscr();

printf(“\n\n Armstrong numbers between 1 to 1000: \n\n”);

for(i=1;i<=1000;i++)

{

temp=i;

sum=0;

dig=0;

while(temp>0)

{

dig=temp%10;

sum=sum+(dig*dig*dig);

temp=temp/10;

}

if(i==sum)

{

printf(“\t %d”,sum);

}

}

getch();

}

Output:

Armstrong numbers between 1 to 1000:

1    153     370    371     407

/* 12. To find given number is Armstrong or not

   153     1    5     3

                1 + 125 + 27 =153

                           */

#include<stdio.h>

#include<conio.h>

void main()

{

int n=0,dig=0,sum=0,temp=0;

clrscr();

printf(“\n Enter the number: “);

scanf(“%d”,&n);  //153

temp=n;        //153

while(n>0)       //153>0            15>0     1>0  0>0

{

dig=n%10;               // 3    5    1

sum=(dig*dig*dig)+sum;  // 27   152  153

n=n/10;                 // 15   1    0

}

if(temp==sum)

printf(“\n\n %d is Armstrong number.”, temp);

else

printf(“\n\n %d is not Armstrong number.”, temp);

getch();

}

Output:

Enter the number: 153

153 is Armstrong number.

/* 13. Write a C Program to show Palindrome number for the given number.

Palindrome number ex. 121=121     44=44    565=565  */

#include<stdio.h>

#include<conio.h>

void main()

{

int n=0,dig=0,rev=0,temp=0;

clrscr();

printf(“\n\n Enter the number: “);

scanf(“%d”,&n);

temp=n;

while(n>0)

{

dig=n%10;

rev=(rev*10)+dig;

n=n/10;

}

if(temp==rev)

printf(“\n %d is Palindrome number.”, rev);

else

printf(“\n %d is Not Palindrome number: %d”,temp,rev);

getch();

}

Output:

Enter the number: 121

121 Palindrome number.

// 14. while loop – Write a C Program to show to find given number is Prime or not

// prime number – 2,3,5,7, 11, ….

#include<stdio.h>

#include<conio.h>

void main()

{

int n,d=2;

clrscr();

printf(“\n\n Enter the number: “);

scanf(“%d”,&n);

while(n>d)

{

if(n%d==0)

{

printf(“\n\n %d is not Prime number…”,n);

break;

}

else

{

d++;

}

}

if(n==d)

printf(“\n %d is Prime number.”, n);

getch();

}

Output:

Enter the number: 13

13 is Prime number.

// 15. Write a C Program to find prime numbers between 1 to 100.

#include<stdio.h>

#include<conio.h>

void main()

{

int i=0,d=0,cnt=0,sum=0;

clrscr();

printf(“\n\n Prime numbers from 1 to 100: \n”);

for(i=1;i<=100;i++)

{

d=2;

while(i>d)

{

if(i%d==0)

{

break;

}

else

{

d++;

}

}

if(i==d)

printf(“\t %d”,i);

}

getch();

}

Output:

Prime numbers from 1 to 100:

2   3   5   7   11   13   17   19   23   29   31   37   41   43   47   53   59   61   67   71   73  79   83   89    97

/* 16. Write a C Program to show given number in reverse order.  ex. 123=321      467=764 */

#include<stdio.h>

#include<conio.h>

void main()

{

int n=0,dig=0,rev=0;

clrscr();

printf(“\n\n Enter the number: “);

scanf(“%d”,&n);  // 123

while(n>0)      // 123>0       12>0       1>0   0>0

{

dig=n%10;          //  3     2     1

rev=(rev*10)+dig;  //  3     32    321

n=n/10;            //  12    1     0

}

printf(“\n Reverse number: %d”,rev);

getch();

}

Output:

Enter the number: 123

Reverse number: 321

Some More: 

POP- Introduction to Programming Using ‘C’

DS – Data structure Using C

OOP – Object Oriented Programming 

Java Programming

DBMS – Database Management System

RDBMS – Relational Database Management System

Join Now: Data Warehousing and Data Mining 

Leave a Reply

Your email address will not be published. Required fields are marked *