write a c program to check
whether the given
given number is prime or not
Answers
Check prime number by C Language
Output:
Enter number to check prime number: 7
7 is a prime number.
Explanation:
//set header file
#include<stdio.h>
//set main function
int main()
{//set integer type variables
int num, a;
//print message for input
printf("Enter number to check prime number: ");
//get input from the user
scanf("%d", &num);
//set for loop
for (a = 2; a <= num/2; a++)
{//check condition for not a prime number
if (num%a == 0)
{ //print message if not a prime number
printf("%d is not a prime number.\n", num);
//break the loop
break;
}
}
//check condition for a prime number
if (a == num/2 + 1)
//print message if prime number
printf("%d is a prime number.\n", num);
return 0;
}
Following are the description of the program:
- Set required header file and define main method, inside the main method.
- Set two integer data type variables that is 'num' and 'a'.
- Print message to input and get input from the user in the variable 'num'.
- Set the for loop and inside the loop set if conditional statement to check that the given number is not a prime then, print message and break loop.
- Finally, set if conditional statement to check that the number is prime and print message.
Learn More:
brainly.in/question/636655