Q:

C program to calculate Sum and Product of all digits of an integer number

0

C program to calculate Sum and Product of all digits of an integer number

This program will read an integer number from the user and calculate the Sum and Product of all digits, in this program we will extract each digit by dividing and getting remainder with 10, add digits in Sum and Multiply the digit in Product.

 

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Sum and Product of all digits of a Number using C program

/* program to find sum and product of all digits of a number.*/
 
#include <stdio.h>
  
int main()
{
    int n;
    int dig, sum,pro;
     
    printf("\nEnter an integer number :");
    scanf("%d",&n);
  
    /*Calculating Sum and Product*/
    sum=0;
    pro=1;
     
    while(n>0)
    {
        dig=n%10; /*get digit*/
        sum+=dig;
        pro*=dig;
        n=n/10;
    }
     
    printf("\nSUM of all Digits is : %d",sum);
    printf("\nPRODUCT of all digits: %d",pro);
     
    return 0;
}

Using User Define Function

/* program to find sum and product of all digits of a number.*/
 
#include <stdio.h>
 
/*  function:   sumDigits
    to calculate sum of all digits.
*/
 
int sumDigits(int num)
{
    int sum=0,rem;
    while(num > 0)
    {
        rem=num%10;
        sum+=rem;
        num=num/10;
    }
 
    return sum;
}
 
/*  function:   proDigits
    to calculate product of all digits.
*/
 
int proDigits(int num)
{
    int pro=1,rem;
    while(num > 0)
    {
        rem=num%10;
        pro*=rem;
        num=num/10;
    }
 
    return pro;
 
}
int main()
{
    int n;
    printf("\nEnter an integer number :");
    scanf("%d",&n);
 
    printf("\nSUM of all Digits is : %d",sumDigits(n));
    printf("\nPRODUCT of all digits: %d",proDigits(n));
    return 0;
}

Output

Enter an integer number :1234
SUM of all Digits is : 10
PRODUCT of all digits: 24

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now