tech Prime Number Java Program - 1 to 100 & 1 to N | Programs
Latest :

Prime Number Java Program – 1 to 100 & 1 to N | Programs

Prime Number Java Program –  Java Program to Check Whether a Number is Prime or Not using different methods. The compiler has also been added so that you can execute the programs yourself, alongside suitable examples and sample outputs added for each program. Also, we will see the method of how to find out prime numbers between 1 to n, 1 to 100 The methods used aforementioned are:

  • Using Static Method
  • Using While Loop
  • Using For Loop
  • Using Recursion

A PRIME NUMBER  is any number that is divisible by 1 and itself only. Certain examples of prime numbers are 2, 3, 5, 7, 11 etc. However, 1 is neither a prime nor composite number.

Using Static Method

1) A prime number is a number which has no positive divisors other than 1 and itself.

2) We are finding the given number is prime or not using the static method primeCal(int num). For loop iterates from i=0 to i=given number, if the remainder of number/i =0 then increases the count by 1. After all the iterations, if count=2, then that number is a prime number.

Output:

Find Prime Numbers Between 1 to n

1) We are finding the prime numbers within the limit.

2) Read the “n” value using scanner object  sc.nextInt()and store it in the variable n.

3) The for loop iterates from j=2 to j=given number. then count assigned to 0, the inner loop finds the divisors of each j value, count value represents no.of divisors. If count=2, then that number is a prime number.

Output:

Prime Number Java Program – Using While Loop

1) In this program, the while loop is present in the constructor. If we instantiate the class then automatically constructor will be executed.

2) Read the “n” value using scanner class object sc.nextInt(). FindPrime class is initiated in the class Prime as new FindPrime(n); then the constructor of FindPrime will be executed.

The while loop iterates until i<=num is false. The remainder of number/i=0 then count will be increased by 1, “i” value increased by 1. If count=2, then print “number is a prime number”.

Output:

Using For Loop 

1) To find  divisors of the  given number

  • for loop iterates from i=1 to n.
  • If remainder of n,i is 0 then count value increased by 1. Count represents  total no of divisors.
  • if count=2 then the given number is prime.

2) Example n=53, i=1, 53%1=0,count=1: i=2,53%2=1!=0, count=1:i=3, 53%3=2!=0: repeat until i=53 ,53%53=0,count=2. Count=2 so 53 is prime number.

Output:

Using Recursion

1) Read the entered number n.

2) The object for Prime class will be created in the main method. Call the method primeOrNot(n) using the object as  p.primeOrNot(n);

3) The method primeOrNot(int num) will be executed and calls itself as primeOrNot(num); until the condition if(i<=num) is false. If the condition is false then this method returns the count and assigned to the variable c. If  count=2, then print “prime number” otherwise print “not a prime number”.

Output:

If you have any doubts while solving the 1 to 100 or 1 To N Prime number program leave a comment here.

More Java Programs:

tech
x

Check Also

Java Program To Reverse An Array | Programs

Java program to reverse an array – We will discuss the various methods to reverse ...