Tuesday, 10 November 2020

Java Program to find Prime Number

import java.util.*; //importing the package

public class Prime // creating the class

{

  public static void main(String args[])//creating the main Function

  {

    Scanner in = new Scanner(System.in); //creating the scanner object

    int n,i,c=0; //declaring and initializing the variables

    System.out.println("Enter a Number");

    n=in.nextInt();// Taking input from the user

    for(i=1;i<=n;i++)// loop which runs from 0 till the entered number

      if(n%i==0)//checking whether the number is divisible or not

      c++; //counter variable to count the number of factors

      if(c==2)//checking whether there are only 2 factors

      System.out.println(n+" is a Prime Number");

      else 

      System.out.println(n+" is not a Prime Number");

  }

}

    

No comments:

Post a Comment

Java Program to count the total number of characters in a string

 //Java Program to count the total number of characters in a string import java.util.*; public class CountChars {     public static void mai...