codescracker


c

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



« C Tutorial C Examples »

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. Here is the program:

// 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
// -----------codescracker.com-----------

#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:


« C Tutorial C Examples »






Tools
Calculator

Quick Links
Signup - Login - Give Online Test