Check if sum of digits of a number exceeds the product of digits of that number
Given a positive integer N, the task is to check if the sum of the digits of N is strictly greater than the product of the digits of N or not. If found to be true, then print “Yes”. Otherwise, print “No”.
Examples:
Input: N = 1234
Output: No
Explanation:
The sum of the digits of N(= 1234) is = 1 + 2 + 3 + 4 = 10.
The product of the digits of N(= 1234) is 1*2*3*4 = 24.
As the sum of the digits is smaller than the product of the array. Therefore, print No.
Input: N = 1024
Output: Yes
Approach: Follow the steps below to solve the given problem:
- Initialize two variables, say sumOfDigit as 0 and prodOfDigit as 1 that stores the sum and the product of the digits of N.
- Iterate until N is greater than 0 and perform the following steps:
- Find the last digit of N and store it in a variable, say rem.
- Increment the value of sumOfDigit by rem.
- Update the value of prodOfDigit as prodOfDigit*rem.
- After completing the above steps, if the value of sumOfDigit is greater than prodOfDigit then print “Yes”. Otherwise, print “No”.
Below is the implementation of the above approach:
C++
#include <iostream>
using namespace std;
void check( int n)
{
int sumOfDigit = 0;
int prodOfDigit = 1;
while (n > 0) {
int rem;
rem = n % 10;
sumOfDigit += rem;
prodOfDigit *= rem;
n /= 10;
}
if (sumOfDigit > prodOfDigit)
cout << "Yes" ;
else
cout << "No" ;
}
int main()
{
int N = 1234;
check(N);
return 0;
}
|
C
#include <stdio.h>
void check( int n)
{
int sumOfDigit = 0;
int prodOfDigit = 1;
while (n > 0) {
int rem;
rem = n % 10;
sumOfDigit += rem;
prodOfDigit *= rem;
n /= 10;
}
if (sumOfDigit > prodOfDigit)
printf ( "Yes" );
else
printf ( "No" );
}
int main()
{
int N = 1234;
check(N);
return 0;
}
|
Java
import java.io.*;
public class GFG{
static void check( int n)
{
int sumOfDigit = 0 ;
int prodOfDigit = 1 ;
while (n > 0 )
{
int rem;
rem = n % 10 ;
sumOfDigit += rem;
prodOfDigit *= rem;
n /= 10 ;
}
if (sumOfDigit > prodOfDigit)
System.out.println( "Yes" );
else
System.out.println( "No" );
}
public static void main(String[] args)
{
int N = 1234 ;
check(N);
}
}
|
Python3
def check(n):
sumOfDigit = 0
prodOfDigit = 1
while n > 0 :
rem = n % 10
sumOfDigit + = rem
prodOfDigit * = rem
n = n / / 10
if sumOfDigit > prodOfDigit:
print ( "Yes" )
else :
print ( "No" )
N = 1234
check(N)
|
C#
using System;
class GFG{
static void check( int n)
{
int sumOfDigit = 0;
int prodOfDigit = 1;
while (n > 0)
{
int rem;
rem = n % 10;
sumOfDigit += rem;
prodOfDigit *= rem;
n /= 10;
}
if (sumOfDigit > prodOfDigit)
Console.WriteLine( "Yes" );
else
Console.WriteLine( "No" );
}
public static void Main()
{
int N = 1234;
check(N);
}
}
|
Javascript
<script>
function check(n)
{
let sumOfDigit = 0;
let prodOfDigit = 1;
while (n > 0)
{
let rem;
rem = n % 10;
sumOfDigit += rem;
prodOfDigit *= rem;
n = Math.floor(n / 10);
}
if (sumOfDigit > prodOfDigit)
document.write( "Yes" );
else
document.write( "No" );
}
let N = 1234;
check(N);
</script>
|
Time Complexity: O(log10N), as we are traversing the digits which will cost log10N time.
Auxiliary Space: O(1), as we are not using any extra space.
Last Updated :
14 Dec, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...