Write a program to identify if the number is Prime number or not

15 May 2022

Write a program to identify if the number is Prime number or not


If you are from 2023 batch student, Join our Telegram group for placement preparation and coming placement drive updates : https://t.me/talentbattle2023

Description

Get a number as input from the user and check whether that number is prime or not.

A prime number is a number with factors as 1 and that number itself.

Input

1

Output

1 is not a prime number

Input

5

Output

5 is a prime number

C Program

Method 1

#include <stdio.h>

int main()

{

    int i,count=0;

    int num;

    printf("Enter a number: ");

    scanf("%d",&num);

    for(i=1;i<=num;i++) 

    { 

        if(num%i==0) 

            count++; 

    }

    if(num<=1)

        printf("%d is not a prime number", num);

    else if(count > 2) 

        printf("%d is not a prime number", num);

    else

        printf("%d is a prime number", num);

    return 0;

}

Method 2

#include <stdio.h>

#include<stdlib.h>

int main()

{

    int i,flag=0;

    int num;

    printf("Enter a number: ");

    scanf("%d",&num);

    if(num<=1)

    {

        printf("%d is not a prime number", num);

        exit(1);

    }

    for(int i=2;i<=num/2;i++)

    {

        if(num%i==0)

        {

            flag=1;

            break;

        }

    }

    if(flag==1) 

        printf("%d is not a prime number", num);

    else

        printf("%d is a prime number", num);

    return 0;

}

Method 3

#include <stdio.h>

#include<stdlib.h>

#include<math.h>

int main()

{

    int i,flag=0;

    int num;

    printf("Enter a number: ");

    scanf("%d",&num);

    if(num<=1)

    {

        printf("%d is not a prime number", num);

        exit(1);

    }

    for(int i=2;i<=sqrt(num);i++)

    {

        if(num%i==0)

        {

            flag=1;

            break;

        }

    }

    if(flag==1) 

        printf("%d is not a prime number", num);

    else

        printf("%d is a prime number", num);

    return 0;

}

Method 4

#include <stdio.h>

#include<stdlib.h>

#include<math.h>

int main()

{

    int i,flag=0;

    int num;

    printf("Enter a number: ");

    scanf("%d",&num);

    if(num<=1)

    {

        printf("%d is not a prime number", num);

        exit(1);

    }

    for(int i=2;i<=sqrt(num);i=i+2)

    {

        if(num%i==0)

        {

            flag=1;

            break;

        }

    }

    if(flag==1) 

        printf("%d is not a prime number", num);

    else

        printf("%d is a prime number", num);

    return 0;

}

C++ Program

Method 1

#include <iostream>

using namespace std;

int main()

{

    int i,count=0;

    int num;

    cout<<"Enter a number: ";

    cin>>num;

    for(i=1;i<=num;i++) 

    { 

        if(num%i==0) 

            count++; 

    }

    if(num<=1)

        cout<<num<<" is not a prime number";

    else if(count > 2) 

        cout<<num<<" is not a prime number";

    else

        cout<<num<<" is a prime number";

    return 0;

}

Method 2

#include <iostream>

using namespace std;

int main()

{

    int i,flag=0;

    int num;

    cout<<"Enter a number: ";

    cin>>num;

    if(num<=1)

    {

        cout<<num<<" is not a prime number";

        exit(1);

    }

    for(int i=2;i<=num/2;i++)

    {

        if(num%i==0)

        {

            flag=1;

            break;

        }

    }

    if(flag==1) 

        cout<<num<<" is not a prime number";

    else

        cout<<num<<" is a prime number";

    return 0;

 

}

Method 3

#include <iostream>

#include<math.h>

using namespace std;

int main()

{

    int i,flag=0;

    int num;

    cout<<"Enter a number: ";

    cin>>num;

    if(num<=1)

    {

        cout<<num<<" is not a prime number";

        exit(1);

    }

    for(int i=2;i<=sqrt(num);i++)

    {

        if(num%i==0)

        {

            flag=1;

            break;

        }

    }

    if(flag==1) 

        cout<<num<<" is not a prime number";

    else

        cout<<num<<" is a prime number";

    return 0;

 

}

Method 4

#include <iostream>

#include<math.h>

using namespace std;

int main()

{

    int i,flag=0;

    int num;

    cout<<"Enter a number: ";

    cin>>num;

    if(num<=1)

    {

        cout<<num<<" is not a prime number";

        exit(1);

    }

    for(int i=2;i<=sqrt(num);i=i+2)

    {

        if(num%i==0)

        {

            flag=1;

            break;

        }

    }

    if(flag==1) 

        cout<<num<<" is not a prime number";

    else

        cout<<num<<" is a prime number";

    return 0;

 

}

Java Program

Method 1

import java.util.Scanner;

public class Main

{

public static void main(String[] args) {

Scanner sc=new Scanner(System.in); 

System.out.println("Enter a number");

int num=sc.nextInt();

int count=0;

if(num<=1)

System.out.println(num+" is not a prime number");

else

{

    for(int i=2;i<=num;i++)

    {

        if(num%i==0)

        {

            count++;

        }

    }

    if(count>2)

    System.out.println(num+" is not a prime number");

    else

    System.out.println(num+" is a prime number");

}

}

}

Method 2

import java.util.Scanner;

public class Main

{

public static void main(String[] args) {

Scanner sc=new Scanner(System.in); 

System.out.println("Enter a number");

int num=sc.nextInt();

int flag=0;

if(num<=1)

System.out.println(num+" is not a prime number");

else

{

    for(int i=2;i<num;i++)

    {

        if(num%i==0)

        {

            flag=1;

            break;

        }

    }

    if(flag==1)

    System.out.println(num+" is not a prime number");

    else

    System.out.println(num+" is a prime number");

}

}

}

Method 3

import java.util.Scanner;

public class Main

{

public static void main(String[] args) {

Scanner sc=new Scanner(System.in); 

System.out.println("Enter a number");

int num=sc.nextInt();

int flag=0;

if(num<=1)

System.out.println(num+" is not a prime number");

else

{

    for(int i=2;i<=num/2;i++)

    {

        if(num%i==0)

        {

            flag=1;

            break;

        }

    }

    if(flag==1)

    System.out.println(num+" is not a prime number");

    else

    System.out.println(num+" is a prime number");

}

}

}

Python

num=int(input("Enter a number: "))

flag=0

for i in range(2,num):

    if num%i==0:

        flag=1

        break

if(num<=1):

    print("Not a prime number")

elif(flag==1):

    print("Not a prime number")

else:

    print("Is a prime number")


If you are from 2023 batch student, Join our Telegram group for placement preparation and coming placement drive updates : https://t.me/talentbattle2023
Related Articles

Ask Us Anything !