Best WAP to find whether a given number is prime or not. - codewithshiva

Latest

Search Bar

Tuesday, August 4, 2020

Best WAP to find whether a given number is prime or not.

package com.kapish;

import java.util.Scanner;

public class Main {
public static void main(String[] args) {
//Prime Number
Scanner sc= new Scanner(System.in); //System.in --> input from keyboard
System.out.print("Enter a Integer: ");
int a = sc.nextInt();//reads number

//For Making it Efficient
int b = (a/2)+1;
int notp = 1;
int f =0;

//for loop to find who is dividing it
for(int i = 2; i <= b; i++)
{
// condition for non Prime number
if(a % i == 0) {
f=i;
notp=1;
break;
}
else{
notp=0;
}

}
if (notp==0){
System.out.println(a+"ya Boy! It's Prime number : ");
}
else{
System.out.println("Not A Prime number since its divisble by "+f);
}



}
}

No comments:

Post a Comment