I have a long and confusing problem (to me) I'm new to C. I was wondering if anyone could help me with my problem.

File: Prime.h

#include <stdio.h>
#include <stdlib.h>

int is_prime(int n);

File: is_prime.c

#include "primes.h"

int is_prime(int n)
{
        int k, limit;

        if (n == 2)
                return 1;
        if (n % 2 == 0)
                return 0;
        limit = n /2;
        for (k = 3; k <= limit; k += 2)
                if (n % k == 0)
                return 0;
        return 1;
}

File: main.c

#include "primes.h"
int main(void)
{
        int n, prime, count;

        printf("PRIMES WILL BE PRINTED.\n");
        printf("\nHow many do you want to see?   ");
        scanf("%d,&n");
        n = 0;
        prime = 1;
        count += 1;
        while(prime < n)
        {
                if(is_prime(count))
                {
                        prime++;
                        printf("%d %d\n", prime, count);
                }
        count++;
        }
        return 0;
}

I know that there is something definetly wrong with main.c
I've played with the code for about 2 days now and i don't get ANY output.
Can someone please help

Recommended Answers

All 4 Replies

>scanf("%d,&n");
>n = 0;
This assignment is suspicious.

>prime = 1;
>while(prime < n)
1 is not less than 0; your loop will never execute.

haha! it's suspicious b/c it's the wrong problem...
This is the real main.c...

File: Main.c

#include "primes.h"
 int main(void) {

        int primes;
        int n;
        int i;
        int count;

        printf("\n%s", "PRIME NUMBER THEOREM:\n" "lim (pi(x) / (x/log(x))=1\n " "x-> inf");
        printf("\n%s", "where pi(x) is the number of primes\n" "less than or equal to x.");
        printf("\n%s", "How many primes do you want to consider?  ");
        scanf("%d", &n);


        primes =1000;
        i=0;
        while(primes <= n){
                if (is_prime(i)){
                        printf("%d\t%d\n",primes,i);
                        ++primes;
                  }
                i+= 1000;
                }
        return 0; }

I'm trying to make it look like this:

  x               pi(x)       pi(x) / (x / log(x))
-----         -------       -------------------
1000             ...           ..............
2000             ...           ..............
3000             ...           ..............
...
...

Do I need to set up another function?
Also, I don't know where to input the pi(x).

Ok fixed it up a little...

#include "primes.h"
int main(void)
 {
        int pi;
        int n;
        int i,x,y;
        int count;
        int mess(int);
        int n_of_pt(int);

        printf("\n%s", "PRIME NUMBER THEOREM:\n" "lim (pi(x) / (x/log(x))=1\n" "x-> inf");
        printf("\n%s", "where pi(x) is the number of primes\n" "less than or equal to x.");
        printf("\n%s", "How many primes do you want to consider?");
        scanf("%d", &n);
        printf("%s","x\t" "pi(x)\t" "(pi(x) / (x/log(x))\n");


        return;

        for(x=1000;pi<=n;x+=1000){
                pi=n_of_pt(x); // y=pi/log(x);
                printf("%d\t%d\n",x,pi);
                }
        return;
 }
        int n_of_pt(int a) {
        int prime;
        prime=0;
        int i;
        i=0;
                while(prime<=a){
                prime = is_prime(a);
                }
        return prime;
}

>haha! it's suspicious b/c it's the wrong problem...
>This is the real main.c...
What is this? Some sort of sick game? "Guess the problem." "Well, it's obviously <so and so>." "Wrong! I fooled you by giving you the wrong problem! Haha!"

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.