Home C Language Basic C Programs Add digits of a number

Add digits of a number

Write a C Program to Add Digits & Multiplication of a number.

#include <stdio.h>

int main()

{

   int n,i,sum,product,temp;

   printf(“Enter a number: “);

   scanf(“%d”, &n);

   sum=0;

   product=1;

   while(n>0)

   {

      temp=n%10;

      sum+=temp;

      product*=temp;

      n=n/10;

   }

   printf(“Sum of digits of given number is: %d\n”,sum);

   printf(“Product of digits of given number is : %d\n”,product);

   return 0;

}

Output:

Enter a number: 627

Sum of digits of given number is: 15

Product of digits of given number is : 84