I am trying to make a c program that displays prime numbers 2 through the entered number, show runtime when 0 is entered and when a one is entered show the runtime plus display the prime numbers.
I can get it to display the runtime but for some reason I can't have it output the prime numbers. I have supplied my code for reference and any input would be greatly appreciated thank you so much.
Code:
#include <stdio.h>
#include <stdlib.h>
#include <sys/time.h>
int main(int argc, char **argv)
{
int max, i, show, prime, j;
struct timeval time_start; // starting time
struct timeval time_end; // ending time
if (argc < 3) //if arguments are less $
{
fprintf(stderr, "Not Enough Arguments!\n");
exit(1);
}
int arg1 = atoi(argv[1]);
max = atoi(argv[1]); //shows max
show = atoi(argv[2]); //SHOWS PRIMES
gettimeofday(&time_start, 0);
if (show==1) //if show ='s 1 then
{
for (i = argc + 1; i < argc; i++)
{
prime = 0;
for (j = 2; j <= i/2; j++)
{
if(i%j==0)
{
prime=1;
break;
}
}
if(prime==0)
printf("%d\n", i);
fprintf(stderr, "%10.6lf\n", time_end.tv_sec - time_start.tv_sec + ((time_end.tv_usec - time_start.tv_usec) / 1000000.0));
}
}
gettimeofday(&time_end, 0);
if (show == 0)
{
fprintf(stderr, "%10.6lf\n", time_end.tv_sec - time_start.tv_sec + ((time_end.tv_usec - time_start.tv_usec) / 1000000.0));
}
return 0;
}