Write a program to calculate the sum of all the non-prime positive numbers less than or equal to the given number.
Note: A 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
Following is the step to calculate the sum of all non-prime positive numbers in Java:
- Input a number from the user.
- Call fun() method with the input number.
- Inside the method, declare and initialize variable sum and k with 0.
- Use for loop, and start the index from 1 and go till the input number. Use the inner loop and traverse it till it is less than the outer loop index. Inside the loop check for the prime number. If it is a prime number then increment k value. And, add each index to the sum variable if k!=2.
- At last, return the value of the sum variable.
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; } }
Output
Check Prime Number Program
Write a program in Java to check whether a number is prime or not. Let 3 be a prime number. Then the program should return “3 is a prime number”.
Input and Output Format
- Input consists of an integer.
- Output consists of string.
Sample Input:
3
Sample Output:
3 is a prime number
Program to check for prime number in Java
Following are the step to check if a number is prime or not:
- Input number from the user.
- Call checkPrime() method with the input number.
- Inside the method, declare and initialize a flag value to false.
- First, check if the input number is 1, then return is not a prime number. Next, inside the loop check if the number is divisible by any number in the range 2 to n/2.
- If it is divisible, then set a flag to true and break from the loop, and then return it is not a prime number.
- Else, If the flag value is false then return “is a prime number”.
package com.testcases; import java.util.Scanner; public class CheckPrimeNumber { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int n = scanner.nextInt(); System.out.println(checkPrime(n)); } private static String checkPrime(int n) { boolean flag = false; if(n==1){ return n+ " is not prime number"; } for(int i = 2 ; i <= n/2 ; i++ ){ if(n % i==0){ flag = true; break; } } if(!flag){ return n+" is a prime number"; } return n+" is not a prime number"; } }
Output
Prime Numbers in the range 1 to 100
Write a program to print all the prime numbers between 1 to 100.
Java Program to print prime numbers in the range 1 to 100
Following are the step to display prime numbers:
- Call the printPrimeNumber() method.
- Inside the method, declare and initialize k and pn variables.
- Now, use a loop that starts with 1 and goes till 100.
- Use another loop, and set the start to index variable and inside check if the number is divisible then increment k value.
- If the k is exactly equal to two that means if the numbers have exactly two factors which is one and itself the number is set to a prime number.
- At last, it is printed.
package com.testcases; import java.util.Scanner; public class PrintPrime { public static void main(String[] args) { printPrimeNumber(); } private static void printPrimeNumber() { int n = 0; String pn = ""; for(int i = 1;i<=100;i++){ int k = 0; for(n=i;n>=1;n--){ if(i%n==0){ k = k + 1; } } if(k==2){ pn = pn + i + " "; } } System.out.print(pn); } }
Output
Thus, in this way, we learn How to calculate the sum of all non-prime numbers in Java.