main_1.c corresponds to the main.c in your textbook. After you finish requiremen
ID: 3532840 • Letter: M
Question
main_1.c corresponds to the main.c in your textbook. After you finish requirements in the textbook, rename your main.c file as main_1.c and then modify it to make files main_2.c, main_3.c and main_4.c, each of which implements a different functionality (see examples).
? Given an integer N, main_1.c outputs the first N primes starting from 2.
? Given an integer N, main_2.c outputs the all primes numbers less than N.
? Given an integer N and an integer X, main_3.c outputs primes with index X+1, X+2, ..., X+N in
prime number sequence starting from 2.
? Given an integer N and an integer Y, main_4.c outputs N primes starting from integer Y.
While testing main_1.c, compile using the following command (replacing main_1.c with main_2.c when testing main_2.c. same for other main files):
Example 1(output of program compiled from main_1.c, is_primes.c and primes.h):
5: 11
6: 13
7: 17
Example 2(output of program compiled from main_2.c, is_primes.c and primes.h):
5: 11
6: 13
7: 17
8: 19
Example 3(output of program compiled from main_3.c, is_primes.c and primes.h):
700: 5279
701: 5281
702: 5297
703: 5303
704: 5309
705: 5323
706: 5333
707: 5347
......
723: 5477
724: 5479
725: 5483
726: 5501
727: 5503
728: 5507
729: 5519
730: 5521
731: 5527
732: 5531
Example 4(output of program compiled from main_4.c, is_primes.c and primes.h):
700: 5279
701: 5281
702: 5297
703: 5303
704: 5309
705: 5323
706: 5333
707: 5347
708: 5351
......
5278
33
722: 5471
723: 5477
724: 5479
725: 5483
726: 5501
727: 5503
728: 5507
729: 5519
730: 5521
731: 5527
732: 5531
- main_1.c or main_2.c doesn
Explanation / Answer
//main_1.c
#include <stdio.h>
int main()
{
int n,i=1,j,c;
int k=0;
printf(" PRIMES WILL BE PRINTED How many do you want to see? ");
scanf("%d",&n);
while(k<n)
{
c=0;
for(j=1;j<=i;j++)
{
if(i%j==0)
c++;
}
//the above for-loop can be put
// in a is_prime(int) function in is_prime.c
//with i as the input argument,i.e, is_prime(i)
// which will replace the check condition of c==2
if(c==2)
printf("%d: %d ",++k,i);
i++;
}
return 0;
}
//-----------------------------------------------------------------
//main_2.c
#include <stdio.h>
int main()
{
int n,i=1,j,c;
int k=0;
printf(" Primes between 1 and N will be printed Please input the value of N:");
scanf("%d",&n);
while(i<n)
{
c=0;
for(j=1;j<=i;j++)
{
if(i%j==0)
c++;
}
//the above for-loop can be put
// in a is_prime(int) function in is_prime.c
//with i as the input argument,i.e, is_prime(i)
// which will replace the check condition of c==2
if(c==2)
printf("%d: %d ",++k,i);
i++;
}
return 0;
}
//-----------------------------------------------------------------
//main_3.c
#include <stdio.h>
int main()
{
int n,i=1,j,c,s;
int k=0;
printf(" PRIMES WILL BE PRINTED How many do you want to see? ");
scanf("%d",&n);
printf("Beginning at what index? ");
scanf("%d",&s);
while(k< (s+n-1))
{
c=0;
for(j=1;j<=i;j++)
{
if(i%j==0)
c++;
}
//the above for-loop can be put
// in a is_prime(int) function in is_prime.c
//with i as the input argument,i.e, is_prime(i)
// which will replace the check condition of c==2
if(c==2)
{ k++;
if(k>=s)
printf("%d: %d ",k,i);
}
i++;
}
return 0;
}
//-----------------------------------------------------------------
//main_4.c
#include <stdio.h>
int main()
{
int n,i=1,j,c,s;
int k=0;
printf(" PRIMES WILL BE PRINTED How many do you want to see? ");
scanf("%d",&n);
printf("Beginning at what number? ");
scanf("%d",&s);
while(n)
{
c=0;
for(j=1;j<=i;j++)
{
if(i%j==0)
c++;
}
//the above for-loop can be put
// in a is_prime(int) function in is_prime.c
//with i as the input argument,i.e, is_prime(i)
// which will replace the check condition of c==2
if(c==2)
{ k++;
if(i>=s)
{
n--;
printf("%d: %d ",k,i);
}
}
i++;
}
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.