September 29, 2018

# Write a java program to calculate the sum of all the non prime positive numbers

Write a program to calculate the sum of all the non prime positive numbers less than or equal to the given number.

Note: prime is a natural number greater than 1 that has no positive divisors other than 1 and itself

Example:

input = 9

Prime numbers = 2,3,5 and 7

output = 1+4+6+8+9=28

**Input and Output Format:**

Input consists of an integer.

Output consists of an integer.

**Sample Input:**

9

**Sample Output:**

28

## java program to calculate the sum of all the non prime positive numbers

package com.demo; import java.util.*; public class Main { public static void main(String[] args) { Scanner s = new Scanner(System.in); int n = s.nextInt(); System.out.println(func(n)); } public static int func(int n) { int sum = 0; int k = 0; for (int i = 1; i <= n; i++) { k = 0; for (int j = 1; j <= i; j++) { if (i % j == 0) k++; } if (k != 2) { sum = sum + i; } } return sum; } }