I
interpim
Im trying to teach myself programming, and I am doing exercises out of the book C by Dissection by Ira Pohl.
I am stuck on one of them and can't figure out how to fix it.
The exercise is for you to write a program that asks for a user inputted number and then print out that many prime numbers. The function that determines if a number is prime or not is provided.
My problem is that I can't figure out how to call the function repetitively and only print the numbers that return a true from the prime number function. What I have so far is this.
#include "primes.h"
main(void)
{
int x,y,n;
printf("PRIME NUMBERS WILL BE PRINTED\n");
printf("How many do you want to see? ");
scanf("%d",&x);
is_prime(n);
{
for(y=0;y<=x;++y)
printf("%5d: %5d\n",y,n)
}
}
here is 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;
}
here is primes.h
#include <stdio.h>
#include <stdlib.h>
int is_prime(int n);
Please help me.... this thing is really bugging the crap out of me.
I am stuck on one of them and can't figure out how to fix it.
The exercise is for you to write a program that asks for a user inputted number and then print out that many prime numbers. The function that determines if a number is prime or not is provided.
My problem is that I can't figure out how to call the function repetitively and only print the numbers that return a true from the prime number function. What I have so far is this.
#include "primes.h"
main(void)
{
int x,y,n;
printf("PRIME NUMBERS WILL BE PRINTED\n");
printf("How many do you want to see? ");
scanf("%d",&x);
is_prime(n);
{
for(y=0;y<=x;++y)
printf("%5d: %5d\n",y,n)
}
}
here is 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;
}
here is primes.h
#include <stdio.h>
#include <stdlib.h>
int is_prime(int n);
Please help me.... this thing is really bugging the crap out of me.