Write a program that prompts to enter a number and check whether the number is (i) prime or not
Answers
Answer:
#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 non-prime if (n % i == 0) { flag = 1; break; } } if (n == 1) { printf("1 is neither prime nor composite."); } 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.
Answer:
hello,
its using python
Explanation:
# program to enter a number and check whether its prime or not
n=int(input("enter the number"))
if n==2:
print(" 2 is a prime number")
else:
for i in range(2,n):
if n%i==0:
print(n,"is not a prime number")
break
else:
print(n,"is a prime number")