Computer Science, asked by kushwahasumit940, 1 year ago

write a program to check number is prime or not in computer​

Answers

Answered by shagunnag37
2

Heeyy are u in class 8th dude..

TUTORIALS

EXAMPLES

search

C Program to Check Whether a Number is Prime or Not

Example to check whether an integer (entered by the user) is a prime number or not using for loop and if...else statement.

Prime Numbers

To understand this example, you should have the knowledge of following C programming topics:

C if...else Statement

C for Loop

C break and continue

A prime number is a positive integer which is divisible only by 1 and itself. For example: 2, 3, 5, 7, 11, 13

Example: Program to Check Prime Number

#include <stdio.h>

int main()

{

int n, i, flag = 0;

printf("Enter a positive integer: ");

scanf("%d", &n);

for(i = 2; i <= n/2; ++i)

{

// condition for nonprime number

if(n%i == 0)

{

flag = 1;

break;

}

}

if (n == 1)

{

printf("1 is neither a prime nor a composite number.");

}

else

{

if (flag == 0)

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

else

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

}

return 0;

}

Answered by Anonymous
1

Answer:

import java.util.*;

class Prime_no

{

   public static void main()

   {

       Scanner sc=new Scanner(System.in);

       System.out.println("Enter number of elements to be entered");

       int n=sc.nextInt();

       int a[]=new int[n];

       int f;

       System.out.println("Enter "+n+" elements");

       for(int i=0;i<n;i++)

       a[i]=sc.nextInt();

       System.out.println("Prime numbers:");

       for(int i=0;i<n;i++)

       {

           f=0;

           for(int j=1;j<=a[i];j++)

           {

               if(a[i]%j==0)

               f++;

           }

       if(f==2)

           System.out.println(a[i]);

       }

   }

}

Similar questions