

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to find the product of given digits by using for loop in C language?
The user has to enter a number, then divide the given number into individual digits, and finally, find the product of those individual digits using for loop.
The logic to find the product of given digits is as follows −
for(product = 1; num > 0; num = num / 10){ rem = num % 10; product = product * rem; }
Example1
Following is the C program to find the product of digits of a given number by using for loop −
#include <stdio.h> int main(){ int num, rem, product; printf("enter the number : "); scanf("%d", & num); for(product = 1; num > 0; num = num / 10){ rem = num % 10; product = product * rem; } printf(" result = %d", product); return 0; }
Output
When the above program is executed, it produces the following result −
enter the number: 269 result = 108 enter the number: 12345 result = 120
Example2
Consider another example to find the product of digits of given number by using while loop.
#include <stdio.h> int main(){ int num, rem, product=1; printf("enter the number : "); scanf("%d", & num); while(num != 0){ rem = num % 10; product = product * rem; num = num /10; } printf(" result = %d", product); return 0; }
Output
When the above program is executed, it produces the following result −
enter the number: 257 result = 70 enter the number: 89 result = 72
- Related Questions & Answers
- How to separate even and odd numbers in an array by using for loop in C language?
- Find out the GCD of two numbers using while loop in C language
- C program to write all digits into words using for loop
- Find 2’c complements for given binary number using C language
- C Program for Print the pattern by using one loop
- Maximize the given number by replacing a segment of digits with the alternate digits given in C++
- How to find the number of digits in a given number using Python?
- C program to print multiplication table by using for Loop
- How to find the power of any given number by backtracking using C#?
- C program to find palindrome number by using while loop
- How to find the product of two binary numbers using C#?
- How to find the product of 2 numbers using recursion in C#?
- Find the Largest number with given number of digits and sum of digits in C++
- Largest Time for Given Digits in C++
- Find last k digits in product of an array numbers in C++
Advertisements