Prime Numbers From 1 To 100 in Java: Display 1 to 100 in Java

Introduction

First of all, let’s get started with the prime number definition. A number is said to be a prime number if it is divisible by only 1 and itself. If we were asked to say prime numbers from 1 to 100 then it would be a tedious task to check each number on paper and say whether it is a prime number or not. Never mind we can write a code to do that and java makes things easy.

Java is a popular and one of the most used languages, and the reason for its sunny day spotlight is providing features like object-oriented programming, platform independency, predefined libraries, etc.

Let’s build a code for printing prime numbers from 1 to 100 and walk through it. Let’s start!

Java Program

Before jumping to the code, we’ll understand the algorithm to check if a number is a prime number or not. At first, we need to loop over all the numbers from 1 to N and maintain a count of numbers that properly divides the given number. If the count is 2 then we can conclude that the given number is a prime, else it is not a prime. Here’s the code to do that.

int n = 5;
int c = 0;
for(int i=1;i<=n;i++)
  if(n%i==0)
      c++;
if(c==2)
    System.out.println(n+” is a prime number”);
else
    System.out.println(n+” is not a prime number”);

In the above snippet n is the number which is to be checked if it is a prime number or not, c is a variable that stores the count of proper divisors. And we are looping over the range 1 to n and incrementing the count if we found a proper divisor.

And after coming out of the loop, we are checking if the count is 2 i.e.; there are only two proper divisors (1 and itself). If yes concluding it as a prime number, else a non-prime number. Talking about the time complexity of the above code it is a linear one, so it’s an O(n) complexity code.

Now that we were asked to print prime numbers from 1 to 100, we need to run this same algorithm for each number between 1 to 100 and store the prime number. And here’s the code to do that.

ArrayList<Integer> a=new ArrayList<>();
  for(int n=1; n<=100; n++){
  int c = 0;
  for (int i = 1; i <= n; i++)
      if (n % i == 0)
          c++;
  if (c == 2)
      a.add(n);
  else
      continue;
  }
  System.out.println(a);

In the above code, we’ve declared an ArrayList that stores all the prime numbers in the range of 1 to 100. Now we have two for loops first for loop is for looping over all the numbers between 1 to 100 and the second for loop is our previous prime number algorithm. After running the prime number algorithm for each number we are pushing it into ArrayList if it is a prime number.

Check out: Java Developer Salary in India

And after completing the loops we are printing our ArrayList which displays all the prime numbers between 1 to 100. Talking about the time complexity of the above code, we can see that there are two for loops. So it is an O(n²) complexity code.

We have hardcoded the range in the above code, what if we want to print prime numbers in the range given by the user input?

Prime Numbers in Given Input Range

The whole algorithm will be almost similar to the above code, the only difference we make is taking user input for the lower limit and upper limit of the range.

Let’s build the code now!

Scanner sc=new Scanner(System.in);
int lower=sc.nextInt();
int upper=sc.nextInt();
ArrayList<Integer> a=new ArrayList<>();
for(int n=lower; n<=upper; n++){
int c = 0;
for (int i = 1; i <= n; i++)
if (n % i == 0)
c++;
if (c == 2)
a.add(n);
else
continue;
}
System.out.println(a);

In the above code, we are initializing a scanner for reading user input. We’ve declared two variables lower and upper and assigning those variables with user input. What we have to do is print all the prime numbers between the range [lower, upper]. Our previous algorithm does this task and appends all the prime numbers to the ArrayList.

Also Read: Java Project Ideas & Topics

Conclusion

We’ve understood the definition of prime numbers, walked through an algorithm to find whether a number is prime or not, extended that algorithm to find prime numbers from 1 to 100. And later we’ve used a java scanner to read user input for tweaking the range of numbers that are to be checked if they are prime or not, built an algorithm for printing prime numbers in a range given by the user.

Now that you are aware of how to check if a number is prime or not, how to print prime numbers in a given range. Try implementing few challenges like implementing it using functions, implementing it using objects, building the algorithm using recursion, try using few other collections in java, try optimizing the code a little bit if possible, etc. Because practice helps you in mastering a programming language, helps you in overcoming ambiguity with syntaxes and implementation.

If you wish to improve your Java skills, you need to get your hands on these java projects. If you’re interested to learn more about Java, full-stack development, check out upGrad & IIIT-B’s PG Diploma in Full-stack Software Development which is designed for working professionals and offers 500+ hours of rigorous training, 9+ projects, and assignments, IIIT-B Alumni status, practical hands-on capstone projects & job assistance with top firms.

Become a Full Stack Developer

UPGRAD AND IIIT-BANGALORE'S PG DIPLOMA IN SOFTWARE DEVELOPMENT
ENROLL NOW @ UPGRAD

Leave a comment

Your email address will not be published.

×