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

  • Thread starter ming2194
  • Start date
In summary: N]; int prime_count = 0; /* rest of your program goes here */ 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); return 0;}
  • #1
ming2194
32
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
  • #2
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?
 
  • #3
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
 
  • #4
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.
 
  • #5
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??
 
  • #6
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.
 
  • #7
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:
  • #8
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);
}
 

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

What is a basic C program?

A basic C program is a set of instructions written in the C programming language that can be compiled and executed by a computer. It typically includes functions, variables, and control structures to perform a specific task.

How do I write a basic C program?

To write a basic C program, you will need a text editor and a compiler. First, you will need to write your code using the correct syntax and structure. Then, you will need to save the file with a .c extension. Finally, you can use a compiler to convert your code into a language that the computer can understand and execute.

What are the essential components of a basic C program?

A basic C program typically includes three main components: the header, the main function, and any additional functions or statements needed to complete the desired task. The header contains any necessary declarations and includes, the main function is where the program starts and ends, and additional functions and statements provide the functionality of the program.

How do I compile and execute a basic C program?

To compile a basic C program, you will need to use a compiler specific to your operating system. Once you have the compiler set up, you can use the command line to navigate to the directory where your C program is saved and run the command to compile the code. To execute the program, you can run the generated executable file.

What are some common errors in basic C programs?

Some common errors in basic C programs include syntax errors, such as missing semicolons or incorrect variable names, logical errors, which occur when the program does not produce the expected output, and runtime errors, such as division by zero or trying to access memory that is not allocated. It is essential to carefully debug and test your code to catch and correct these errors.

Similar threads

Replies
9
Views
2K
Replies
12
Views
2K
Replies
3
Views
926
Replies
3
Views
1K
Replies
1
Views
9K
Replies
9
Views
4K
Back
Top