Check if sum of digits of a number exceeds the product of digits of that number

Last Updated : 14 Dec, 2022

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++

 `// C++ program for the above approach` `#include ` `using` `namespace` `std;`   `// Function to check if the sum of the digits of N is` `// strictly greater than the product of the digits of N or` `// not` `void` `check(``int` `n)` `{` `    ``// Stores the sum and the product of the digits of N` `    ``int` `sumOfDigit = 0;` `    ``int` `prodOfDigit = 1;` `    ``while` `(n > 0) {` `        ``// Stores the last digit if N` `        ``int` `rem;` `        ``rem = n % 10;` `        ``// Increment the value of sumOfDigits` `        ``sumOfDigit += rem;` `        ``// Update the prodOfDigit` `        ``prodOfDigit *= rem;` `        ``// Divide N by 10` `        ``n /= 10;` `    ``}`   `    ``// Print the result` `    ``if` `(sumOfDigit > prodOfDigit)` `        ``cout << ``"Yes"``;` `    ``else` `        ``cout << ``"No"``;` `}`   `// Driver Code` `int` `main()` `{` `    ``int` `N = 1234;` `    ``check(N);` `    ``return` `0;` `}`

C

 `// C program for the above approach` `#include `   `// Function to check if the sum of the digits of N is` `// strictly greater than the product of the digits of N or` `// not` `void` `check(``int` `n)` `{` `    ``// Stores the sum and the product of the digits of N` `    ``int` `sumOfDigit = 0;` `    ``int` `prodOfDigit = 1;` `    ``while` `(n > 0) {` `        ``// Stores the last digit if N` `        ``int` `rem;` `        ``rem = n % 10;` `        ``// Increment the value of sumOfDigits` `        ``sumOfDigit += rem;` `        ``// Update the prodOfDigit` `        ``prodOfDigit *= rem;` `        ``// Divide N by 10` `        ``n /= 10;` `    ``}`   `    ``// Print the result` `    ``if` `(sumOfDigit > prodOfDigit)` `        ``printf``(``"Yes"``);` `    ``else` `        ``printf``(``"No"``);` `}`   `// Driver Code` `int` `main()` `{` `    ``int` `N = 1234;` `    ``check(N);` `    ``return` `0;` `}`   `// This code is contributed by Sania Kumari Gupta`

Java

 `// Java program for the above approach` `import` `java.io.*;` `public` `class` `GFG{`   `// Function to check if the sum of the` `// digits of N is strictly greater than` `// the product of the digits of N or not` `static` `void` `check(``int` `n)` `{` `    `  `    ``// Stores the sum and the product of` `    ``// the digits of N` `    ``int` `sumOfDigit = ``0``;` `    ``int` `prodOfDigit = ``1``;`   `    ``while` `(n > ``0``) ` `    ``{` `        `  `        ``// Stores the last digit if N` `        ``int` `rem;` `        ``rem = n % ``10``;`   `        ``// Increment the value of` `        ``// sumOfDigits` `        ``sumOfDigit += rem;`   `        ``// Update the prodOfDigit` `        ``prodOfDigit *= rem;`   `        ``// Divide N by 10` `        ``n /= ``10``;` `    ``}`   `    ``// Print the result` `    ``if` `(sumOfDigit > prodOfDigit)` `        ``System.out.println(``"Yes"``);` `    ``else` `        ``System.out.println(``"No"``);` `}`   `// Driver code` `public` `static` `void` `main(String[] args)` `{` `    ``int` `N = ``1234``;` `    `  `    ``check(N);` `}` `}`   `// This code is contributed by abhinavjain194`

Python3

 `# Python3 program for the above approach`   `# Function to check if the sum of the` `# digits of N is strictly greater than` `# the product of the digits of N or not` `def` `check(n):` `    `  `    ``# Stores the sum and the product of` `    ``# the digits of N` `    ``sumOfDigit ``=` `0` `    ``prodOfDigit ``=` `1`   `    ``while` `n > ``0``:` `        `  `        ``# Stores the last digit if N` `        ``rem ``=` `n ``%` `10`   `        ``# Increment the value of` `        ``# sumOfDigits` `        ``sumOfDigit ``+``=` `rem`   `        ``# Update the prodOfDigit` `        ``prodOfDigit ``*``=` `rem`   `        ``# Divide N by 10` `        ``n ``=` `n ``/``/` `10`   `    ``# Print the result` `    ``if` `sumOfDigit > prodOfDigit:` `        ``print``(``"Yes"``)` `    ``else``:` `        ``print``(``"No"``)`   `# Driver Code` `N ``=` `1234` `    `  `check(N)`   `# This code is contributed by jana_sayantan`

C#

 `// C# program for the above approach` `using` `System;` ` `  `class` `GFG{` ` `  `// Function to check if the sum of the` `// digits of N is strictly greater than` `// the product of the digits of N or not` `static` `void` `check(``int` `n)` `{` `     `  `    ``// Stores the sum and the product of` `    ``// the digits of N` `    ``int` `sumOfDigit = 0;` `    ``int` `prodOfDigit = 1;` ` `  `    ``while` `(n > 0)` `    ``{` `         `  `        ``// Stores the last digit if N` `        ``int` `rem;` `        ``rem = n % 10;` ` `  `        ``// Increment the value of` `        ``// sumOfDigits` `        ``sumOfDigit += rem;` ` `  `        ``// Update the prodOfDigit` `        ``prodOfDigit *= rem;` ` `  `        ``// Divide N by 10` `        ``n /= 10;` `    ``}` ` `  `    ``// Print the result` `    ``if` `(sumOfDigit > prodOfDigit)` `        ``Console.WriteLine(``"Yes"``);` `    ``else` `        ``Console.WriteLine(``"No"``);` `}`   ` `  `// Driver Code` `public` `static` `void` `Main()` `{` `    ``int` `N = 1234;` `     `  `    ``check(N);` `     `  `}` `}`   `// This code is contributed by code_hunt.`

Javascript

 ``

Output:

`No`

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.