DEV Community

Cover image for Sum of Non-Prime Number
Gourav Kadu
Gourav Kadu

Posted on

4 2

Sum of Non-Prime Number

We are taking a number N as a input which and we have to find the sum of all non prime numbers in digits of N i.e. if N=789 then 8 and 9 are not prime so print 8+9.

Example:

Input

579
Enter fullscreen mode Exit fullscreen mode

Output

9
Enter fullscreen mode Exit fullscreen mode

Input

467
Enter fullscreen mode Exit fullscreen mode

Output

10
Enter fullscreen mode Exit fullscreen mode

Solution :- https://onlinegdb.com/4wofh-j8y

import java.util.*;
public class Main
{

static int countDigit (long n)
  {
    int count = 0;
    while (n != 0)
      {
 n = n / 10;
 ++count;
      }
    return count;
  }

public static int prime (int n)
  {

if (n == 1 || n == 0)
      return 0;

else if (n == 2)
      return 0;

else if (n % 2 == 0)
      return n;

for (int i = 3; i <= Math.sqrt (n); i += 2)
      {
 if (n % i == 0)
   return n;
      }
    return 0;
  }

public static void main (String[]args)
  {
    int sum = 0;
    int arr[] = new int[100];
    Scanner sc = new Scanner (System.in);
    int n = sc.nextInt ();
    int size = countDigit (n);
    for (int i = 0; i < size; i++)
      {
 if (n > 0)
   {
     arr[i] = n % 10;
     n = n / 10;
   }
      }
    for (int i = 0; i < size; i++)
      {
 sum += prime (arr[i]);
      }
    System.out.println (sum);
  }
}
Enter fullscreen mode Exit fullscreen mode

Heroku

Simplify your DevOps and maximize your time.

Since 2007, Heroku has been the go-to platform for developers as it monitors uptime, performance, and infrastructure concerns, allowing you to focus on writing code.

Learn More

Top comments (0)

Image of Docusign

🛠️ Bring your solution into Docusign. Reach over 1.6M customers.

Docusign is now extensible. Overcome challenges with disconnected products and inaccessible data by bringing your solutions into Docusign and publishing to 1.6M customers in the App Center.

Learn more