PLEASE TYPE CODE IN C AND ALLOW IT TO COPY AND PASTE PLEASE! 3) Write a program
ID: 3585039 • Letter: P
Question
PLEASE TYPE CODE IN C AND ALLOW IT TO COPY AND PASTE PLEASE!
3) Write a program that indicates if a number input is a prime number or not. The program should repeat this until the user enters O to exit. For example, the input of 17 should print "prime", since 17 has no divisors other than 17 and 1 (hence 17 is a prime number) Input of 21 should print "not a prime" (3 and 7 are both divisors of 21, hence 21 is not a prime number) Run the program using your own Red ID number to determine if it is a prime numberExplanation / Answer
#include <stdio.h>
#include<conio.h>
void main()
{
int n, i, flag = 0;
printf("Enter a positive integer number");
scanf("%d",&n);
for(i=2; i<=n/2; ++i)
{
if(n%i==0) // condition for checking a nonprime number
{
flag=1;
break;
}
}
if (flag==0)
printf("%d is a prime number.",n);
else
printf("%d is not a prime number.",n);
getch();
}
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.