Home Uncategorized Sum of Digits in c

Sum of Digits in c

SHARE

Write a C Program to Add Digits of a number in C.

#include<stdio.h>
int main()
{
  int a,sum=0,temp; //’a’ for user defined number, ‘sum’ for store the result, ‘temp’ is temporary variable

  printf(“Enter a number :”);
  scanf(“%d”,&a);
  for(temp=a;temp>0;temp=temp/10)//removing last digit from the temp variable for the next iteration.

  {
    sum=sum+(temp%10);//adding last digit of the temp to the sum.

  }
  printf(“Addtion digits of given number is : %d”,sum);
  return 0;
}

Output :
i) Enter a number : 25461
Addition digits of given number is : 18

Back to the programs.