BOARD PROGRAM class 9-10 icse programs PROGRAMMING
Lavi Thakur  

Write a program to input a number and check whether it is prime or not

Write a program to input a number and check whether it is prime or not. Today we are going to discuss about the above mentioned topic. We have provided important details along with suitable example.

What is prime number?

Prime numbers are natural numbers greater than 1 that have no positive divisors other than 1 and themselves. They are the building blocks of the natural numbers. As every natural number greater than 1 can be uniquely represented as a product of prime numbers. A concept known as prime factorization.

Prime numbers play a crucial role in various areas of mathematics and computer science:

  1. Cryptography: Prime numbers are extensively used in cryptographic algorithms, such as RSA encryption, where the security of the system relies on the difficulty of factoring large prime numbers.
  2. Number Theory: Prime numbers are fundamental to number theory, the branch of mathematics that deals with the properties and relationships of numbers. Many famous conjectures and theorems in number theory revolve around prime numbers, including the Goldbach Conjecture and the Riemann Hypothesis.
  3. Computational Complexity: Prime numbers are central to the study of computational complexity theory. Problems involving prime numbers, such as primality testing and integer factorization, are used to analyze the computational difficulty of algorithms and problems.
  4. Data Structures and Algorithms: Prime numbers are used in various algorithms and data structures, such as hashing functions and sieve algorithms, for their efficiency and properties.
  5. Random Number Generation: Prime numbers are often used in random number generation algorithms to ensure randomness and avoid patterns.
Write a program to input a number and check whether it is prime or not

Understanding and efficiently identifying prime numbers is essential in various computational tasks and mathematical investigations. Algorithms for checking primality, like the Sieve of Eratosthenes and probabilistic primality tests, are continuously researched and optimized for improved performance and accuracy.

The code for: Write a program to input a number and check whether it is prime or not

import java.io.*;
public class test
{
public static void main(String args[])throws IOException
{
int num;
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter a Number");
num = Integer.parseInt(br.readLine());
int prime = 1;
for (int x = 2;x < num; x++)
{
if(num % x==0)
{
prime = 0;
break;
}
}
if(prime == 1)
System.out.println("Number is prime number.");
else 
System.out.println("Number is not a prime number.");
}
}

In the above post we have discussed a program which is mostly asked by computer science students. In the program we have discussed how to check whether number is prime or not. For more such related posts please visit pour blog regularly.

Thank you!!

Leave A Comment