C program to Calculate sum of digits of given Number

Spread the love

Description :

This program accepts one Integer from user and calculates the Sum of the individual digits of given number. 
Ex: if the number is 1234 then sum of digits is 1+2+3+4 that is 10.

Program :

  1. #include<stdio.h>
  2. int main()
  3. {
  4.         int num,sum,remainder,temp;
  5.         printf(“Enter any Number : “);
  6.         scanf(“%d”,&num);
  7.  
  8.         temp = num;
  9.         sum = 0;
  10.  
  11.         while(num > 0)
  12.         {
  13.                 remainder = num % 10;
  14.                 sum = sum + remainder;
  15.                 num = num / 10;
  16.         }
  17.         printf(“Sum of the Digits of Number %d is : %d n,temp, sum);
  18.         return 0;
  19. }

Output :

Venkatesh Macha

Hi Guys, I am Venkatesh Macha. I am a programmer and an Open Source enthusiast, Presently working as a Software Engineer. Apart from that, I love to explore new technologies and things.

You may also like...

Leave a Reply