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();
}

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 developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.