Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Created file PrintPrime.java #173

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 56 additions & 0 deletions PrintPrime.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

/**
* The intention of this class is to perform following operations
*
* 1. Take any number from 1-100 as Input from Console 2. Find if Input number
* is a Prime number 3. If Prime, print two numbers as Output where the sum of
* these two numbers will result in Input number
*
*/
public class PrintPrime {

public static void main(String[] args) throws IOException {
readAndPrint();
}

private static void readAndPrint() throws IOException {
System.out.println("Enter any number between 1-100 in Console:");
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
String input = reader.readLine();
int number = Integer.parseInt(input);
System.out.println("Input Number: " + number);
processNumber(number);
}

private static void processNumber(int num) {
if (!isPrimeNumber(num)) {
return;
}

for (int i = 1; i <= 100; i++) {
for (int j = 1; j <= 100; j++) {
if (i < j && i + j == num) {
System.out.println(num + " is Sum of " + i + " and " + j);
}
}
}
}

private static boolean isPrimeNumber(int num) {
if (0 == num || 1 == num) {
return false;
}

for (int i = 2; i <= num / 2; ++i) {
if (num % i == 0) {
System.out.println(num + " is not a Prime Number");
return false;
}
}
System.out.println(num + " is a Prime number");
return true;
}
}