as

Wednesday 12 November 2014

While Loop in C


Program  1: Print 1 to 10 numbers.

#include<stdio.h>

void main()
{
  int n=1;
  while(n<=10)
   {
     printf("\n %d",n);
     n++;
   }
 getchar();
}



Program  2 :  Write a program to find out factorial of a number.

#include<stdio.h>

void main()
{
  int no,fact=1,i=1;
  system("cls");
  printf("\n Enter the number: ");
  scanf("%d",&no);
  while(i<=no)
  {
    fact=fact*i;
    i++;
  }
  printf("\n Factorial of a number is : %d",fact);

getchar();
}

Program  3 : Find whether number is prime or not .
#include<stdio.h>

void main()
{
  int no;
  system("cls");
  printf("\n Enter the number: ");
  scanf("%d",&no);
  if(no==1||no==2||no==3||no==5||no==7)
  {
    printf("\n %d is prime number",no);
  }
  else  if(no%2>0 && no%3>0 && no%5>0 && no%7>0)
  {
    printf("\n %d is prime number",no);
  }
  else
  {
    printf("\n %d is not prime",no);
  }
getchar();
}


Program 4: Write a program count the number of digit in the number .
#include<stdio.h>

void main()
{
  int no,r=0,count=0;
  system("cls");
  printf("\n Enter the number: ");
  scanf("%d",&no);
  while(no!=0)
  {
    r=no%10;
    count++;
    no=no/10;
  }
  printf("\n The number of digits are %d",count);

getchar();
}

Program  5 : Write a program to sum of digit the number.
#include<stdio.h>

void main()
{
  int no,r=0,sum=0;
  system("cls");
  printf("\n Enter the number: ");
  scanf("%d",&no);
  while(no!=0)
  {
    r=no%10;
    sum=sum+r;
    no=no/10;
  }
  printf("\n The number of digits are %d",sum);

getchar();
}

Program  6 :  Print 1 to 10 Fibonacci series  .
#include<stdio.h>

void main()
{
  int i=1;
  int no1=0,no2=1,fib=0;
  system("cls");
  while(i<=10)
  {
    fib=no1+no2;
    printf("\t %d",fib);
    no1=no2;
    no2=fib;
    i++;
  }

getchar();
}

Program  7 : Check whether number is Armstrong or not .
#include<stdio.h>

void main()
{
  int no,r=0,sum=0;

  system("cls");
  printf("\n Enter the number : ");
  scanf("%d",&no);
  int temp=no;
  while(no!=0)
  {
    r=no%10;
    sum=sum+(r*r*r);
    no=no/10;
  }

  if(temp==sum)
  {
     printf("%d is Armstrong number",temp);
  }
  else
  {
    printf("%d is not Armstrong number",temp);
  }

  getchar();

}


No comments:

Post a Comment

Leave your valuable feedback. Your thoughts do matter to us.

Sponsored Links

Popular Posts

Comments

ShareThis