Hey, Today we will learn how to check the number is prime or not. First of all we need to know what is a prime number ? The simplest answer is the number can divide by 1 and the number itself then it’s a prime number , Example 1 and 7. here divide means no reminder value. so for 7 we can divide this number with 1 and 7 only . other number also can divide but there will be reminder value. So let’s check the program.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
package practice.house.com; import java.util.Scanner; public class PrimeNumber { public static void main(String args[]) { int n, i, res; boolean flag = true; Scanner sc = new Scanner(System.in); System.out.println("Please Enter a No."); n = sc.nextInt(); for (i = 2; i <= n / 2; i++) { res = n % i; if (res == 0) { flag = false; break; } } if (flag) System.out.println(n + " is Prime Number"); else System.out.println(n + " is not Prime Number"); } } |
OUTPUT:
1 2 3 4 5 6 7 8 |
Please Enter a No. 7 7 is Prime Number ------ ***** ----- Please Enter a No. 9 9 is not Prime Number |
here in the output you can see that 9 is not a prime number because 9 can divide by 3 with a reminder 0. So number 9 can divide by 1,3,9 So it’s not a prime number.
Thanks for reading this post. Stay tune with us to get more practices programs.
Happy Coding Happy Living…

Java – PHP – Python – Dart – Flutter – MySql – Spring – Hibernate – JavaScript – jQuery – BootStrap 4 – CSS – HTML
I love to explore new technologies. If you like my tutorials please share your thoughts about my work.
Check out my YouTube Channel For Video Tutorials.
To Hire Me Please Contact Me Through Social Media or https:www.amialif.com
If you want to help others by doing donation please Help Them.
You also can connect with me via social media. Thanks
Leave a Reply