0

i hove a problem about hot to write a program to determine if a number entered by a user is a prime number.

Program Input is : Prompt user for a number between 1 and 1000.

the probable Program Output is:
Echo the user input, display whether the number is a prime
number or not and the first smaller prime number and the first
bigger prime number.


my attempt to the code determine only weather the number is or not a prime number, please help me:


#include<stdio.h>
#include<math.h>

main()
{
long int a,b,c;
printf("Enter the number: ");
scanf("%ld",&a);
for (b=2;b<a;++b)
{
c= a%b;
if (c==0)
goto cool;
}
printf("\nThe number you enter is a prime number!");
goto fool;
cool: printf("\nThe number you enter is not a prime number!");
fool: printf("\n");

getch();
}

3
Contributors
2
Replies
4
Views
5 Years
Discussion Span
Last Post by -[xxxxxxx]-
0

What a brave man of using a goto statement....
You may try to use another counter to add/minus by 1 if the current number is not a prime number. Then keep doing the procedure of determining a prime number until there is a nearest prime number found.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.