I have everything right in this program except for the lack of one line of output. The first line of the output should have the range 1-9, but I can't figure out how to do that and have the rest print out as well. Here's the code and the output.
Code:
void showOutput(int range[], int input)
{
int primeCount = 0;
// print the headers for the table
printf("\n%11s","Range of\n");
printf("%9s%25s%19s\n", "Primes","Prime Numbers","Quantity");
printf("%11s%25s%17s\n", "------------","-------------------","----------");
for(int i = 1; i < input; i++)
{
// separate the values in blocks of 10
if(i % 10 == 0)
{
// show the current range
printf("%4d%2s%5d%6c",(i)," -", (i+9), ' ');
// find the primes within the range and print them
for(int j = i; j <= (i+9); j++)
if(range[j]==1)
{
printf("%5d",j);
primeCount++;
}
// to make sure that the quantity column's value are lined up,
// blank spaces are added based on how many primes were printed
// for that set of 10 integers
for(int k = primeCount; k < 4; k++)
printf("%5c",' ');
// print the number of primes in the set of 10
// and reset the counter for the next group of 10
printf("%12d", primeCount);
primeCount = 0;
// since all of the above data was on one line,
// print a new line for the next set
printf("\n");
}
}
}
int main( )
{
int input = 0, currentSpace = 0;
printf("Enter the value of N: ");
scanf("%d",&input);
// define a large range of integers to test for primes
int range[500];
// set all elements to 1
for(int i = 0; i <= input; i++)
range[i] = 1;
range[0] = range[1] = 0;
for(int i=2;i <=input; i++)
{
if(range[i] == 1)
{
// if the element contains a 1
// remove all multiples of the number
// and make sure the multiples
// don't go above the user's input
currentSpace = i+i;
while(currentSpace < input)
{
// set the value to zero
range[currentSpace] = 0;
// set the variable to remove the next multiple
currentSpace += i;
}
}
}
// call the function to print the formatted output
showOutput(range,input);
return (EXIT_SUCCESS);
}
Code:
bash-2.05$ ./primes
Enter the value of N: 200
Range of
Primes Prime Numbers Quantity
------------ ------------------- ----------
10 - 19 11 13 17 19 4
20 - 29 23 29 2
30 - 39 31 37 2
40 - 49 41 43 47 3
50 - 59 53 59 2
60 - 69 61 67 2
70 - 79 71 73 79 3
80 - 89 83 89 2
90 - 99 97 1
100 - 109 101 103 107 109 4
110 - 119 113 1
120 - 129 127 1
130 - 139 131 137 139 3
140 - 149 149 1
150 - 159 151 157 2
160 - 169 163 167 2
170 - 179 173 179 2
180 - 189 181 1
190 - 199 191 193 197 199 4