Find the sum of the digits of a number

Input a number from the user and calculate and print the sum of the individual digits of the number.

/*
Calculate Sum of digits of a number
Shaswat Saxena
10/11/2017
*/
#include <stdio.h>
#include <conio.h>
void main()
  {
  int num,sum,temp;
  clrscr();
  sum=0;
  printf("Enter the Number \n");
  scanf("%d",&num);
  temp=num;
  //Chop Digits and Add
  while(temp>0){
    sum=sum+temp%10;
    temp=temp/10;
  }
  printf("The Sum of Digits of %d is %d\n",num,sum);
  getch();
}

Output

Join our list

Subscribe to our mailing list and get interesting stuff and updates to your email inbox.

Thank you for subscribing.

Something went wrong.

Recommended for You  Check for Armstrong Number

Leave a Reply

Close Menu