C Program to Print Sum of Even & Product of Odd Digit

In this tutorial, we will learn about how to create a program in C that will ask from user to enter any number as input to find and print sum of even digit and product of odd digit present in that given number.

The question is, write a program in C to print the sum of even digit and product of odd digit of any given Number from user at run-time. Here is the program:

#include<stdio.h>
#include<conio.h>
int main()
{
    int num, evenSum=0, oddProd=1, rem, temp;
    printf("Enter any number: ");
    scanf("%d", &num);
    while(num>0)
    {
        rem = num%10;
        if(rem%2==0)
            evenSum = evenSum + rem;
        else
            oddProd = oddProd * rem;
        num = num/10;
    }
    printf("\nSum of Even Digit = %d", evenSum);
    printf("\nProduct of Odd Digit = %d", oddProd);
    getch();
    return 0;
}

As the above program was written under Code::Blocks IDE, therefore after successful build and run, here is the sample run:

c program print sum of even position

Supply any number say 234879 and press ENTER key to see the sum of even digit (that is 2, 4, and 8) and the product of odd digit (that is 3, 7, and 9). As the digit 2, 4 and 8 are the three even numbers and 3, 7 and 9 are the three odd numbers. Therefore the program will calculate the sum of even and product of odd digit (number). Here is the second snapshot of the sample run:

print product of odd digits c

Here are some of the main steps used in above program:

  • Receive any number from user at run-time say 234879
  • Now create a while loop that runs until the number (say 234879) is greater than 0
  • Inside the while loop, initialize num%10 or 234879%10 or 9 to the variable say rem. Here we have calculated the digit (last digit) of the given number by using modulous or remainder operator (%)
  • Now we have to check that whether the remainder is an even number or an odd number
  • If it is an even number, then sum it up
  • Here we have used the variable say evenSum to store the summation result of even digit of the given number
  • Never forgot to initialize 0 to the variable say evenSum at start of the program
  • If the number is found to be an even number (using if block), just initialize evenSum+rem to evenSum variable
  • And if the number is found to be an odd number, then just initialize oddProd*rem to the variable say oddProd. Never forgot to initialize 1 to the variable say oddProd at start of the program
  • For example, if user has entered 234879 as input. Then at first run of the while loop, num%10 or 234879%10 or 9 will be initialized to rem. As 9 is an odd number, therefore oddProd*rem or 1*9 or 9 will be initialized to oddProd
  • After doing above steps, divide the number with 10, that is initialize num/10 or 234879/10 or 23487 to num
  • At second run of the while loop, num%10 or 23487%10 or 7 will be initialized to rem, as 7 is also an odd number, then oddProd*rem or 9*7 or 63 will be initialized to oddProd, and again divide the number with 10 and continue for the next check
  • Do the same operation until the number becomes 0
  • In this way, after coming out from the while loop, we have two variable say evenSum and oddProd that holds up the sum of even digit and product of odd digit of the given number say 234879
  • Finally print the value of both the variable

C Online Test


« Previous Program Next Program »

Follow/Like Us on Facebook




Subscribe Us on YouTube