Showing posts with label while loop. Show all posts
Showing posts with label while loop. Show all posts

Monday, September 12, 2011

find the prime factor of the given number

find the prime factor of the given number */
#include<stdio.h>
#include<conio.h>
main()
{
    int i=2,j=0,k,n,arr[15];
    clrscr();
    printf("enter any integer greater than 1:");
    scanf("%d",&n);
    while(i<=n)
    {
        if(n%i==0)
        {
            do{
            arr[j]=i;
            n=n/i;
            j++;
            }while(n%i==0);
        }
        else
        i++;
    }
    printf("prime factors of the number are\n");
     /*    for(k=j-1;k>=0;k--)
    printf("%d\t",arr[k]);    */
    for(k=0;k<=j-1;k++)
    printf("%d\t",arr[k]);
    getch();
}


convert decimal number into binary

// to convert decimal  number into binary
#include<stdio.h>
#include<conio.h>
main()
{
    int n,a[25],i=0,j;
    clrscr();
    printf("\n Enter any integer : ");
    scanf("%d",&n);
    while(n>0)
    {
        a[i]=n%2;
        n=n/2;
        i++;
    }
    for(j=i-1;j>=0;j--)
      printf(" %d ",a[j]);
       getch();
}

find number of digits in the given integer

/* To find the no. of digits in the given integer */
#include<stdio.h>
#include<conio.h>
main()
{
    int x,i=0,a[10],k;
    long n;
    clrscr();
    printf("\n enter an integer :");
    scanf("%ld",&n);
    while(n>0)
    {
    x=n%10;
    a[i]=x;
    n=n/10;
    if(a[i]==0)
        break;
    else
        i++;
    }
    printf("\n The number of digits in the given number are:%d",i);
    printf("\n And the numbers are:");
    for(k=i-1;k>=0;k--)
        printf("\n%d",a[k]);

    getch();
    }

Calculate HCF of two numbers

main()
{
    int a,b;
    clrscr();
    printf("\n Enter any two nos.:");
    scanf("%d %d",&a,&b);
    printf("\n HCF of the two nos.:%d",gcd(a,b));
    getch();
}

    gcd(int n,int d)
    {
      int r;
      do{
         r=n%d;
         n=d;
         d=r; }while(r!=0);
      return(n);
      }

Reverse any given integer

/* to reverse any given integer */
#include<stdio.h>
#include<conio.h>
main()
{
    int n,m,x,sum=0;
    clrscr();
    printf("\ngive the number:");
    scanf("%d",&n);
    m=n;
    while(n>0)
    {
    x=n%10;
    sum=sum*10+x;
    n=n/10;}
    printf("\ngiven number is =%d",m);
    printf("\nreversed number is =%d",sum);
       getch();
       }

Sunday, September 11, 2011

find the sum of digits of any given integer

/* to find the sum of digits of  any given integer */
#include<stdio.h>
#include<conio.h>
main()
{
//declare int variable n for number for which have to find addition
//m is for temp variable to store rest value
//x is for the single digit/reminder from whole number
//sum which is initialized as 0 to store a sum of the number
    int n,m,x,sum=0;
//clears the screen
    clrscr();
//print msg to screen to enter a number
    printf("\ngive the number:");
//read input from user and store it to n
    scanf("%d",&n);
//store entered number to temp variable m
    m=n;
// start while number number is greater then 0
  while(n>0)
    {
//divide the number by 10 and store the reminder in x
    x=n%10;
//add x to sum
    sum=sum+x;
//again divide n by 10 and store it to n
    n=n/10;
//again check n>0 if yes go to top else exit while
}
//print the given/inputed digit
    printf("\ngiven number is =%d",m);
//print the sum of digit
    printf("\nsum of digits of given number is =%d",sum);
//wait for response
       getch();
       }

Popular 5 Posts Last 7 days