Develop an algorithm and a program in C that asks the user to enter a positive integer between 2 and 100,000. The program will then print to the screen the 10 prime numbers that are greater than or equal to the user's input.
Please find the required C script as the following:
//=================================================================
#include <stdio.h>
int ifprime(int num) // Function to check a prime number
{
int i,check=1;
for(i=2;i<=num/2;i++)
{
if(num%i==0)
{
check = 0;
}
}
return check;
}
int main(void) {
int input,print_count=0;
printf("Enter a positive number between 2 and 100000: ");
scanf("%d",&input);
while(input<2 || input>100000) // check the validity of an input
{
printf("Incorrect input!\nEnter a positive number between 2 and 100000: ");
scanf("%d",&input);
}
printf("The required 10 prime numbers are: ");
while(print_count<10) // Condition to print only 10 prime numbers
{
if(ifprime(input))
{
printf("%d ",input);
print_count++;
}
input++;
}
return 0;
}
//=================================================================
sample output:
Enter a positive number between 2 and 100000: -1
Incorrect input!
Enter a positive number between 2 and 100000: 4
The required 10 prime numbers are: 5 7 11 13 17 19 23 29 31
37
Get Answers For Free
Most questions answered within 1 hours.