How Can I Create a C Program to Output Prime Numbers as Shown in the Image?

  • Thread starter Thread starter ming2194
  • Start date Start date
AI Thread Summary
The discussion centers on creating a C program to output prime numbers as specified in an image. A user attempts to write the program but encounters issues with output and initialization of the prime numbers array. Suggestions include defining a variable to count primes and using a tab character for formatting output. The conversation highlights the importance of correctly declaring variables and initializing arrays to ensure the program compiles and runs successfully. Ultimately, the user is guided to a corrected version of the code that addresses these issues.
ming2194
Messages
31
Reaction score
0

Homework Statement


http://u1.imgupload.co.uk/1257465600/6baa_prime1.jpg

how can i write a C programe that can give a output exactly the same as the pic above?

Homework Equations


above.


The Attempt at a Solution



PHP:
        printf("The prime numbers less than or equal to %d are:\n", number);
        int pos = 0;
        int pcount = 0;
        while(primes[pos] != -1)
           {
           if(primes[pos] != -2)
               {
               printf("\t%d\n", primes[pos]);
               pcount++;
               }
           pos++;
           }
        printf("The total count of prime numbers is: %d\n", pcount);

I have tried my best to attempt the required programe and it is shown above.
but when i try to run it in codeblock it shows nothing.
is there any critia code i missed or some fautal error in my attempt?
thx for help.
 
Physics news on Phys.org
The code you show uses your primes array, but doesn't show how you have initialized this array. Somewhere in your code you are going to need to put the values 2, 3, 5, 7, 11, 13, and 17 in your array. Do you know how to tell if a number is prime?
 
Mark44 said:
The code you show uses your primes array, but doesn't show how you have initialized this array. Somewhere in your code you are going to need to put the values 2, 3, 5, 7, 11, 13, and 17 in your array. Do you know how to tell if a number is prime?

i don't know how to express in c program and i only know it should be something related to the code "while...loop..ect.."

poor me
 
ming2194 said:
i don't know how to express in c program and i only know it should be something related to the code "while...loop..ect.."
That's not going to help you any. I'm not asking for C code. I'm asking for your basic algorithm for determining which of the numbers from 2 through, say 20, are prime. After you figure out your algorithm, then you can write an implementation using C.
 
Mark44 said:
That's not going to help you any. I'm not asking for C code. I'm asking for your basic algorithm for determining which of the numbers from 2 through, say 20, are prime. After you figure out your algorithm, then you can write an implementation using C.

i know wt u mean.i just revise some concept about prime number and my attempt is listed below:

PHP:
#include <stdio.h>
#include <math.h>

#define N 20

int main () 
	
{
	int i, j, s;
	int is_prime[N]; /* if is_prime[i] == 0, then i isn't prime */

/* initially, all are presumed prime */

	printf ("The prime numbers less than of equal to %d are:\n", N);

	for (i=0; i<N; i++) is_prime[i] = 1;

/* find the limit of the numbers to count up to */

	s = (int) sqrt (N) + 1;

/* for each i in 2..sqrt(N), mark out multiples of i */

	for (i=2; i<s; i++) {

/* mark out 2i, 3i, 4i, ... */

	for (j=2*i; j<N; j+=i) is_prime[j] = 0;
}

/* print out the numbers that are left */

	for (i=2; i<N; i++)
	if (is_prime[i]) printf ("%d\n", i);
	printf("There total count of prime numbers is: %d\n",  );  
}

output:
http://u1.imgupload.co.uk/1257552000/bb4b_2009_11_07_114949.png

http://u1.imgupload.co.uk/1257552000/e2f9_prime11.jpg
but how can i make the yellow region happen?
printf("There total count of prime numbers is: %d\n" ,?? ) <-- what shoud i put in "??" ?
and how can i make the each line had some space at the beginning??
 
Try this. I have defined a new variable, prime_count. It should go up near the top with the other variable declarations.
Code:
int i, j, s;
int is_prime[N]; 
int prime_count = 0;
...
for (i=2; i<N; i++)
{
    if (is_prime[i]) printf ("\t%d\n", i);
    prime_count++;
}

printf("The total count of prime numbers is: %d\n",  prime_count);

There is also a new line in your for loop, that increments prime_count each time a prime is found in the array (that is, each time an element in the array has a value of 1).

I have also added a '\t' control character in your first printf() statement. This is the TAB character. When it is printed, it moves the cursor over to the first tab position.
 
Mark44 said:
Try this. I have defined a new variable, prime_count. It should go up near the top with the other variable declarations.
Code:
int i, j, s;
int is_prime[N]; 
int prime_count = 0;
...
for (i=2; i<N; i++)
{
    if (is_prime[i]) printf ("\t%d\n", i);
    prime_count++;
}

printf("The total count of prime numbers is: %d\n",  prime_count);

There is also a new line in your for loop, that increments prime_count each time a prime is found in the array (that is, each time an element in the array has a value of 1).

I have also added a '\t' control character in your first printf() statement. This is the TAB character. When it is printed, it moves the cursor over to the first tab position.

sorry..why seems not work?
i saved the name file as xxx.c and doesn't work.
and i tried to save in .cpp and it works.

your suggested code is C++ code?
 
Last edited:
No, my suggested code was straight C, and would work as either C or C++. I think I remember that one difference between C and C++ is that in C, variables have to be declared before any executable statements, so if you put the line int prime_count = 0; in the wrong place, the revised code might not compile as C but would compile as C++. That's all I can think of.

Your code should look like the following.
Code:
#include <stdio.h>
#include <math.h>

#define N 20

int main () 
{
    int i, j, s;
    int is_prime[N]; /* if is_prime[i] == 0, then i isn't prime */
    int prime_count = 0;

    /* initially, all are presumed prime */

    printf ("The prime numbers less than of equal to %d are:\n", N);

    for (i=0; i<N; i++) is_prime[i] = 1;

    /* find the limit of the numbers to count up to */

    s = (int) sqrt (N) + 1;

    /* for each i in 2..sqrt(N), mark out multiples of i */

    for (i=2; i<s; i++) {

        /* mark out 2i, 3i, 4i, ... */

        for (j=2*i; j<N; j+=i) is_prime[j] = 0;
    }

    /* print out the numbers that are left */

    for (i=2; i<N; i++)
    {
        if (is_prime[i]) printf ("\t%d\n", i);
        prime_count++;
    }

    printf("The total count of prime numbers is: %d\n",  prime_count);
}
 

Similar threads

Back
Top