1. No Profile Picture
Registered User
Devshed Newbie (0 - 499 posts)

Join Date
Aug 2012
Posts
3
Rep Power
0

#### Prime fibonacci program

A C program that inputs a number and check if it is prime and also belongs to fibonacci

You can't just wander in and start issuing demands for answers on a plate with no effort on your part.
homework

And when you do post some code, please make sure it has [code][/code] tags.
3. No Profile Picture
Registered User
Devshed Newbie (0 - 499 posts)

Join Date
Feb 2010
Location
Stockholm
Posts
9
Rep Power
0
Originally Posted by balvinder87
A C program that inputs a number and check if it is prime and also belongs to fibonacci

Maybe you could use these links for creating one yourself? :

# Check if number is prime or not
http://www.programmingsimplified.com/c/source-code/c-program-for-prime-number

# Function for creating N fibonacci numbers
http://www.zacharyfox.com/blog/fibonacci-project/fibonacci-in-c

For checking if the number is NOT a fibonacci number you could just run the sequence until the fibonacci number is more than the number you are checking.

May be a good idea to cache the numbers too, maybe to a file, so you don't have to calculate the numbers every time you're checking a number.

Cheers,
Artheus
4. No Profile Picture
Registered User
Devshed Newbie (0 - 499 posts)

Join Date
Aug 2012
Posts
3
Rep Power
0

#### my code is like this can you give the error

#include<stdio.h>
main()
{
int n, c = 2,k,result;

printf("Enter a number to check if it is prime and fibonacci\n");
scanf("%d",&n);

for ( c = 2 ; c <= n - 1 ; c++ )
{
if ( n%c == 0 )
{
printf("%d is not prime.\n", n);
break;
}
}
if ( c == n )
k=n;
;
fibonacci(k);

return 0;
}

int fibonacci(int n)
{
int a = 0;
int b = 1;
int sum;
int i;
while(sum<n)
{
sum = a + b;
a = b;
b = sum;
if (sum==n)
printf("%d",sum);
}
return 0;
}
5. Your first error is that you did not use code tags as you had been instructed to do!

Code:
```#include<stdio.h>
main()
{
int n, c = 2,k,result;

printf("Enter a number to check if it is prime and fibonacci\n");
scanf("%d",&n);

for ( c = 2 ; c <= n - 1 ; c++ )
{
if ( n%c == 0 )
{
printf("%d is not prime.\n", n);
break;
}
}
if ( c == n )
k=n;
;
fibonacci(k);

return 0;
}

int fibonacci(int n)
{
int a = 0;
int b = 1;
int sum;
int i;
while(sum<n)
{
sum = a + b;
a = b;
b = sum;
if (sum==n)
printf("%d",sum);
}
return 0;
}```
Your second error is that you did not tell us why you think that there's any error. If you get sick and go you see a doctor, do you refuse to describe your symptoms to him? Of course you wouldn't, so why play that stupid guessing game with us?

Are you getting error messages when you compile? What about warnings, which are even more important than error messages? For that matter, if you have not turned warnings on, then you need to do so immediately and always. And you need to not only tell us that that's what happens, but also you need to tell us what the messages are.

Or does the program compile but you get unexpected results when you run it? In that case, you need to tell us what happens. Does the program crash? Does it ignore your input and/or produce no output? Is the output different than what you expected? In that case, you need to tell us what it had output, what you expected, and what input(s) you had given the program to produce that output.

A third obvious error is that you failed to provide a prototype for the fibonacci function. Also, since that function does not provide any meaningful return value and main ignores it anyway, there is no need for fibonacci to be declared as returning int. Explain why you did that.
6. Here are some prime numbers also Fibonacci computed in j http://www.jsoftware.com
Code:
```   (1 p: ])Filter (, [: +/ _2&{.)^:40(0 1)
2 3 5 13 89 233 1597 28657 514229```
Use them as test values---also use numbers that are different than these but between---like 90 or 28000---as test cases. Oh---

Filter=: (#~`)(`:6)

(in plain language: "copy those that satisfy the condition")