Related questions

0 votes
1 answer
0 votes
1 answer
0 votes
1 answer
asked Apr 20, 2017 in C program by anonymous
0 votes
1 answer
BJSHUB - is a place to gain and share knowledge. It's a platform to ask questions and connect with people who contribute unique insights and quality answers.

C Program to Compute the Sum of Digits in a given Integer

0 votes
asked Apr 20, 2017 in C program by anonymous

1 Answer

0 votes

This C Program computes the sum of digits in a given integer. This program m accepts integer. Then adds all the digits of a given integer, that becomes the sum of digits of integer.

Here is source code of the C program to compute the sum of digits in a given integer. The C program is successfully compiled and run on a Linux system. The program output is also shown below.

  1. /*
  2.  * C program to accept an integer & find the sum of its digits
  3.  */
  4. #include <stdio.h>
  5. void main()
  6. {
  7.     long num, temp, digit, sum = 0;
  8.     printf("Enter the number \n");
  9.     scanf("%ld", &num);
  10.     temp = num;
  11.     while (num > 0)
  12.     {
  13.         digit = num % 10;
  14.         sum  = sum + digit;
  15.         num /= 10;
  16.     }
  17.     printf("Given number = %ld\n", temp);
  18.     printf("Sum of the digits %ld = %ld\n", temp, sum);
  19. }

$ cc pgm81.c
$ a.out
Enter the number
300
Given number = 300
Sum of the digits 300 = 3
 
$ a.out
Enter the number
16789
Given number = 16789
Sum of the digits 16789 = 31
answered Apr 20, 2017 by anonymous
...