Wednesday 20 January 2016

Product of Digits (Solution to week 1 Assignment)

Write a C program that takes a positive number N and produces an output that is the product of its digits.

Explanation:

Take number 657.
Answer expected : 6 * 5 * 7 = 210

Constraint:
1<=N<=999999
Input: A single number
Output:  The value
Example 1:
Input: 657
Output: 210
Example 2:
Input: 7

Output: 7

Solution -:

#include<stdio.h> int main() {int n,d=1,i;
scanf("%d",&n);
 i=n; 
 while(i>0) 
 {d=d*(i%10);
 i=i/10;

 printf("%d",d); 
return 0;}

No comments:

Post a Comment