Categories: Java

write a java program to input a number and test if it is a prime number

Today We will create a java program to take input of number and check weather it is a prime number or not using java So Lets Get Started

import java.util.Scanner; 


public class Prime {

public static void main(String[] args) {

int number;

Scanner sc=new Scanner(System.in);
System.out.print("Enter number: "); 

number=sc.nextInt();

int n=2, mid=number/2;

boolean primetest = true;

do {

if (number % n == 0) {
    
System.out.println("A factor "+n+" of "+number+" found!");
System.out.println("Hence NOT a prime number11");

primetest=false;
break;

}
n++;
}
 while(n<=mid);
if(primetest==true){
System.out.println(number+" is a Prime Number");
}
}}

Explaining The Above Program Step By Step

  • Firstly import scanner class to take input
  • then I have created a class
  • After that created a main function
  • Create a variable number
  • Create scanner object to take input
  • Then i have printed Enter number
  • After that i have checked the number is prime or not using do while loop

Today we have learned how to input a number and test if it is a prime number

Thank You

admin

Recent Posts

How to make money as a Beginner Coder in 2025

Making money as a coder in 2025 is not much of a big or tough…

1 year ago

Lloyds Technology Centre : A Concise Guide

Lloyds Technology Centre, A tech and data company located in Hyderabad, India. They are  part of…

1 year ago

How to Increase Height at Any Age

How to Increase Height at Any Age Rise Above: How to Increase Height at Any…

1 year ago

The Hidden Beauty of Karrukha Wood: A Rare Masterpiece from Nature

In the fast-paced digital world, we often forget the timeless beauty that nature offers. Today,…

1 year ago

Top 10 Deadliest Kicks in Martial Arts [2024]

Top 10 Deadliest Kicks in Martial Arts: Master the Art of Knockouts Martial arts is…

1 year ago

How to Optimize Battery Life of Your Smartphone

How to Optimize Battery Life of Your Smartphone: Tips and Tricks for Prolonging Battery Health…

1 year ago