Saturday, 7 July 2012

c program sum of digits of a number

For beginners
{I am going write the c program but before you need a c compiler(software).there are a lot of compilers(software). like dev c++  Download dev c++ its free and install it in your computer.open  dev c++ go to file-->new-->source file.} 


#include<stdio.h>
#include<conio.h>
main()
{
      int a,ra,y=0;
      printf("enter a number\n");
      scanf("%d",&a);
      do
      {
                    ra=a%10;
                    a=a/10;
                    y=y+ra;
                    }while(a!=0);
                    printf("the sum of digits is %d",y);
                    getch();
                    }

or


#include<stdio.h>
#include<conio.h>
main()
{
    int rn,n,y=0;
    printf("enter a number\n");
    scanf("%d",&n);
    for(n=n;n!=0;n=n/10)
    {
                  rn=n%10;
                  y=y+rn;
                  }
                  printf("sum of digits is %d",y);
                  getch();
                  }



For beginners
{After typing this program go to execute-->compile & run-->a window will pop-up-->give file name:(anything u like)-->save as type: c source files-->click save.}

IS IT HELPFIL? comment.

No comments:

Post a Comment