C Program to Print the Sum of Even and the Product of Odd Digits

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

The question is: write a program in C to print the sum of even digits and product of odd digits of any given number given by the 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 in the Code::Blocks IDE, here is the sample run after a successful build and run:

c program print sum of even position

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

print product of odd digits c

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

C Quiz


« Previous Program Next Program »


Liked this post? Share it!