Program to Check Whether a Number is Prime or Not [C, C++, Python & Java]

Posted in

Program to Check Whether a Number is Prime or Not [C, C++, Python & Java]
vinaykhatri

Vinay Khatri
Last updated on December 14, 2024

    In this tutorial, we will learn how to write a program in C, C++, Python, and Java languages to check whether the entered number is a prime number or not.

    To write this program, the following are the prerequisites:

    • if...else statement
    • for loop
    • break statement

    What is a Prime Number?

    A natural number is called a prime number if it is only divisible by 1 and itself. In other others, a prime number is a natural number greater than 1 with exactly two factors - 1 and the number itself.

    For example, 3 is a prime number because it is only divisible by 1 and itself.

    Some prime number include 2, 3, 5, 7, 11, 13, 17, 19,...

    Note - 1 is neither prime nor composite. All prime number are odd number except 2. 2 is the only even prime number and is the smallest one.

    Steps to Write a Program Check a Prime Number

    Here are the steps required to check whether an entered number is prime or not.

    • First, we ask the user to enter a number
    • Then we create a loop from 2 to half of that number.
    • Using the if statement inside the loop, we will check whether the entered number is divisible by any of the numbers between 2 to half of the entered number.
    • If the number is divisible by any of the numbers between 2 to half of the entered number, we will break the loop and print the statement that the entered number is not a prime number.

    C Program to Check Whether a Number is Prime Number

    #include<stdio.h>
    #include<conio.h>
    void main()
    {
    int num, i;
    clrscr();
    printf("Enter a number:");
    scanf("%d",&num);
    if(num==1 ||num==2)
        printf("It is a Prime Number");
    else{
        for(i =2;i<=num/2;i++)
                    {
                    if(num%i==0)    //check whether num is divisible by i or not
                     {  printf("It is not a Prime Number");
                        break;
                      }
                    else
                        if(i==num/2)
                          printf("It is a prime number ");
                     }
         }
     getch();
    }

    Output:

    Enter a number:49
    It is not a Prime Number

    C++ Program to Check Whether a Number is Prime Number

    #include<iostream.h>
    #include< conio.h>
    #include<stdio.h>
    #include<math.h>
    void main()
    {
      clrscr();
      int num,k=0;
      cout<<"Enter a number: ";
      cin>>num;
      for(int i=2;i<=num/2;i++)
      {
         if(num%i==0)
           {
              cout<<"It is not a prime number";
              break;
           }
         else
               k++;
        }
    if(k>1)
    cout<<"It is a prime number";
    getch();
    }

    Output:

    Enter a Number: 17
    It is a prime number

    Python Program to Check Whether a Number is Prime Number

    num =int(input("Enter a Number: "))
    k=0
    for i in range(2,num//2):
        if num%i==0:
            print("It is not a prime number")
            break
        else:
            k+=1
    if k>0:
        print("It is a prime number")

    Output:

    Enter a Number: 17
    It is a prime number

    Java Program to Check Whether a Number is Prime Number

    import java.util.*;
    
    public class Main
    {
       public static void main(String[] args)
       {
          int num, i, count=0;
          Scanner sc = new Scanner(System.in);
          
          System.out.print("Enter a Number: ");
          num = sc.nextInt();
          
          for(i=2; i<num; i++)
          {
             if(num%i == 0)
             {
                count++;
                break;
             }
          }
          
          if(count==0)
             System.out.println("\nIt is a Prime Number");
          else
             System.out.println("\nIt is not a Prime Number");
       }
    }

    Output:

    Enter a Number: 4
    It is not a Prime Number

    Conclusion

    Have you tried this simple program on your system? If yes, you will understand, it is straightforward to implement. Simply you need to have knowledge of the programming language syntax, the prime number logic, and programming constructs. In the above programs, we used the for loop. You can try the same logic using the while loop.

    If you encounter any issues, comment below, and we would be glad to help you.

    People are also reading:

    Leave a Comment on this Post

    0 Comments