C code to calculate product of even digits of a given number
Code Steps:
1. First input the number
2. Now initialize a variable =1, that stores the multiplication
3. Now start a while loop until number = 0
4. Inside while loop just find out all the digits by finding the remainder & check it if it is divisible by 2
5. If the number is divisible by 2 then multiply that number with the variable that was initialize = 1
6. At last return the multiplication storing variable.
C Code Snippet
#include<stdio.h>
int main (){
int num;
int product = 1;
printf("Enter the number: ");
scanf("%d",&num);
int temp = num;
while(num != 0){
int digit = num % 10;
num = num / 10;
if(digit%2==0){
product = digit*product;
}
}
printf("The product of even digits of %d is: %d", temp,product);
return 0;
}
Input Output
Enter the number: 78254
The product of even digits of 78254 is: 64
Explanation: 8*2*4 = 64
Enter the number: 589
The product of even digits of 589 is: 8
Explanation: 8*1 = 8
Comments
Post a Comment