write a program to find if the given integer is a prime number or not.
Answers
/*
* C program to check whether a given number is prime or not
* and output the given number with suitable message.
*/
#include <stdio.h>
#include <stdlib.h>
void main()
{
int num, j, flag;
printf("Enter a number \n");
scanf("%d", &num);
if (num <= 1)
{
printf("%d is not a prime numbers \n", num);
exit(1);
}
flag = 0;
for (j = 2; j <= num / 2; j++)
{
if ((num % j) == 0)
{
flag = 1;
break;
}
}
if (flag ==0)
printf("%d is a prime number \n", num);
else
printf("%d is not a prime number \n", num);
}
Answer:
A positive integer greater than 1 which has no other factors except 1 and the number itself is called a prime number. 2, 3, 5, 7 etc. are prime numbers as they do not have any other factors. But 6 is not prime (it is composite) since, 2 x 3 = 6
Step-by-step explanation:
# Program to check if a number is prime or not
num = 407
# To take input from the user
#num = int(input("Enter a number: "))
# prime numbers are greater than 1
if num > 1:
# check for factors
for i in range(2,num):
if (num % i) == 0:
print(num,"is not a prime number")
print(i,"times",num//i,"is",num)
break
else:
print(num,"is a prime number")
# if input number is less than
# or equal to 1, it is not prime
else:
print(num,"is not a prime number")
Output
407 is not a prime number
11 times 37 is 407