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
Click For Summary

Discussion Overview

The discussion revolves around creating a C program to output prime numbers in a specific format as illustrated in an image shared by the original poster. Participants explore coding techniques, algorithms for identifying prime numbers, and troubleshooting issues with the provided code.

Discussion Character

  • Homework-related
  • Technical explanation
  • Debate/contested

Main Points Raised

  • The original poster shares an incomplete C program and requests help to achieve a specific output format for prime numbers.
  • Some participants point out the need to initialize the primes array with specific prime values and question the original poster's understanding of how to determine if a number is prime.
  • There is a suggestion to develop a basic algorithm for identifying prime numbers before implementing it in C code.
  • A participant provides a revised code snippet that includes a new variable, prime_count, to count the number of primes found and suggests using a tab character for formatting output.
  • Another participant clarifies that the suggested code should work in both C and C++ but notes that variable declarations must be made before executable statements in C.
  • The original poster expresses confusion about why the code does not work when saved as a .c file but does work as a .cpp file, leading to further discussion about differences between C and C++.

Areas of Agreement / Disagreement

Participants do not reach a consensus on the original poster's implementation, as there are multiple suggestions and corrections provided. The discussion remains unresolved regarding the exact implementation details and the original poster's understanding of the concepts.

Contextual Notes

There are limitations in the original poster's code regarding the initialization of the primes array and the placement of variable declarations, which may affect compilation in C. The discussion also highlights the need for a clear algorithm before coding.

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

  • · Replies 9 ·
Replies
9
Views
2K
  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 12 ·
Replies
12
Views
3K
  • · Replies 3 ·
Replies
3
Views
1K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 1 ·
Replies
1
Views
11K
Replies
9
Views
2K
  • · Replies 19 ·
Replies
19
Views
3K
  • · Replies 17 ·
Replies
17
Views
3K
  • · Replies 9 ·
Replies
9
Views
4K