write a program to input an integer and check whether it is a prime number or not
Answers
Answered by
3
#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; }
Output
Enter a positive integer: 29 29 is a prime number.
Output
Enter a positive integer: 29 29 is a prime number.
Answered by
12
CODE
import java.util.*;
class prime
{
public void main()
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter a number to check whether it is prime or not");
int n=sc.nextInt();
int c=0;
for(int i=1;i<=n;i++)
{
if(n%i==0)
{
c++;
}
}
if(c==2)
{
System.out.println("Prime number");
}
else
{
System.out.println("Not a prime numer");
}
}
}
----------------------------------------------------------
Hope it helps :-)
____________________________________________________________________
Similar questions
Hindi,
7 months ago
Business Studies,
1 year ago