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:


long int a,b,c;
printf("Enter the number: ");
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");


Re: Help about prime number 80 80

1. Don't use goto statements
read this thread: http://www.daniweb.com/software-development/cpp/threads/414344/1768131#post1768131
2. I suggest a boolean like statement that if it is a prime number it would print this
printf("\nThe number you enter is a prime number!"); in an if else statement

Re: Help about prime number 80 80

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.18 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.