C program to accept an integer find the sum of the digits in it

by Nideesh C on April 13, 2011 · 0 comments

in C Programming




/* C program to accept an integer find the sum of the digits in it */

#include <stdio.h>

void main()
{
long num, temp, digit, sum = 0;

printf(“Enter the number\n”);
scanf(“%ld”, &num);

temp = num;

while(num > 0)
{
digit = num % 10;
sum  = sum + digit;
num /= 10;
}

printf(“Given number =%ld\n”, temp);
printf(“Sum of the digits %ld =%ld\n”, temp, sum);
}              /* End of main()*/

/*—————————————
Enter the number
123456
Given number =123456
Sum of the digits 123456 =21
—————————————-*/

Not Satisfied ? Just search & get the result

Related Posts Plugin for WordPress, Blogger...
Be Sociable, Share!

Related posts:

  1. C program to accept an integer and reverse it
  2. C program to accept a string and find the sum of all digits present in the string
  3. C program to find the sum of first 50 natural numbers using for loop
  4. C program to reverse a given integer number and check whether it is a palindrome. Output the given numbers with suitable message
  5. C program to find the number of integers divisible by 5 between the given range N1 and N2, where N1 < N2 and are integers. Also find the sum of all these integer numbers that divisible by 5 and output the computed results

Leave a Comment

Previous post:

Next post: