Codeblocks (C code) Create a program that queries the user for a range of intege
ID: 3666165 • Letter: C
Question
Codeblocks (C code)
Create a program that queries the user for a range of integer values (min and max). For this range, you are to find all of the prime numbers and print them to the screen. The user is then given the option to request that all of the non-prime numbers be printed to the screen with at least one factor pair that proves that they are not prime.For example, the user can request the program to find all prime numbers in the range of 900 to 1000. In this case, the output should be the following:
907: PRIME Number
911: PRIME Number
919: PRIME Number
929: PRIME Number
For a number that turns out not to be prime, your output should read
908 = 2 x 454
908: NOT A PRIME Number
Each number in the range needs to be sent to a Call by Reference Function. That function should return the flag showing whether or not it is prime and a factor pair if it is not prime.
Explanation / Answer
/*
* C program to find prime numbers in a given range.
* Also print the number of prime numbers.
*/
#include <stdio.h>
#include <stdlib.h>
void main()
{
int num1, num2, i, j, flag, temp, count = 0;
printf("Enter the value of num1 and num2 ");
scanf("%d %d", &num1, &num2);
if (num2 < 2)
{
printf("There are no primes upto %d ", num2);
exit(0);
}
printf("Prime numbers are ");
temp = num1;
if ( num1 % 2 == 0)
{
num1++;
}
for (i = num1; i <= num2; i = i + 2)
{
flag = 0;
for (j = 2; j <= i / 2; j++)
{
if ((i % j) == 0)
{
flag = 1;
break;
}
}
if (flag == 0)
{
printf("%d ", i);
count++;
}
}
printf("Number of primes between %d & %d = %d ", temp, num2, count);
}