Sum of Non-Prime Number
Gourav Kadu
Posted on October 11, 2021
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
Output
9
Input
467
Output
10
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);
}
}
💖 💪 🙅 🚩
Gourav Kadu
Posted on October 11, 2021
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.