November 28th, 2012, 05:13 PM

C function problem
Hello! I'm new in c programming and the functions are already a headache. I have to make a function that returns all the prime numbers smaller than a number given. How can i return all the values in this function? Thank you.
November 28th, 2012, 05:20 PM

A function can only return one single value (in special cases the value can be a composite). You probably misinterpreted the requirements.
I suggest you write a function that prints all prime numbers smaller than a given number.
November 28th, 2012, 05:51 PM

Originally Posted by bdb
A function can only return one single value (in special cases the value can be a composite). You probably misinterpreted the requirements.
I suggest you write a function that prints all prime numbers smaller than a given number.
How can i do that? Can you post an example?
November 28th, 2012, 06:09 PM

Originally Posted by adi_290
Can you post an example?
Here's a complete program that prints all numbers less than 1000 that can be written as a product of 3 consecutive integers
Code:
#include <stdio.h>
int isvalid(int n) {
int k = 1;
int product;
do {
product = k * (k + 1) * (k + 2);
k++;
} while (product < n);
return (product == n);
}
void printall(int n) {
int k;
printf("Numbers:");
for (k = 1; k < n; k++) {
if (isvalid(k)) printf(" %d", k);
}
printf("\n");
}
int main(void) {
printall(1000);
return 0;
}
November 28th, 2012, 06:17 PM

Originally Posted by bdb
Here's a complete program that prints all numbers less than 1000 that can be written as a product of 3 consecutive integers
Code:
#include <stdio.h>
int isvalid(int n) {
int k = 1;
int product;
do {
product = k * (k + 1) * (k + 2);
k++;
} while (product < n);
return (product == n);
}
void printall(int n) {
int k;
printf("Numbers:");
for (k = 1; k < n; k++) {
if (isvalid(k)) printf(" %d", k);
}
printf("\n");
}
int main(void) {
printall(1000);
return 0;
}
Got it! Thanks.