Prime Number Up to N Terms Program in Java

In this tutorial you will learn about the Prime Number Up to N Terms Program in Java and its application with practical example.

In this tutorial, we will learn to create a Java Prime Number Up to N Terms Program in Java.

Prerequisites

Before starting with this tutorial we assume that you are best aware of the following java programming topics:

  • Java Operators.
  • Basic Input and Output
  • Class and Object.
  • Basic Java programming.
  • String.
  • User define Function.

What is a Prime Number?

A number is called a prime number if it fully divisible by the number itself or 1 only.

For example :->  7 is a prime number, it is fully divisible by 7 only between 2-7  and “1” only.

Prime Number Up to N Terms Program in Java

In this program we will find prime n umber between 1-N. We would first declared and initialized the required variables. Next, we would ask user to enter value of N. let have a look at Code.

Output

N Prime Number

In the above program, we have first declared and initialized a set variables required in the program.

  • n = it will hold entered N size.
  • number = For iteration till N.
  • p= for condition of prime.

After that we declare variables and taking value from user.

Then we check condition for prime between 2 to N.

Here we iterate loop form two to given number and check the condition for every number  that is prime or not and last we will print list of all prime number between 1 to N as shown in above image.

In this tutorial we have learn about the Prime Number Up to N Terms Program in Java and its application with practical example. I hope you will like this tutorial.