Tuesday 10 January 2012

Add digits of number in c

C program to add digits of a number: Here we are using modulus operator to extract individual digits of number and adding them.

#include<stdio.h>
#include<conio.h>

void main()
{
    int n, sum = 0, remainder;
    clrscr();
    printf("Enter an integer\n");
    scanf("%d",&n);
    while( n != 0 )
    {
        remainder = n % 10;
        sum = sum + remainder;
        n = n / 10;
    }
    printf("Sum of digits of entered number = %d\n",sum);
    getch();
}



Output of program:
digit program output

No comments:

Post a Comment