September 2nd, 2012, 01:15 PM

Prime fibonacci program
A C program that inputs a number and check if it is prime and also belongs to fibonacci
September 2nd, 2012, 02:11 PM

OK, so where is YOUR attempt at an answer?
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.
September 2nd, 2012, 02:14 PM

Originally Posted by balvinder87
A C program that inputs a number and check if it is prime and also belongs to fibonacci
Are you asking for one?
Maybe you could use these links for creating one yourself? :
# Check if number is prime or not
http://www.programmingsimplified.com/c/sourcecode/cprogramforprimenumber
# Function for creating N fibonacci numbers
http://www.zacharyfox.com/blog/fibonacciproject/fibonacciinc
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
September 2nd, 2012, 03:12 PM

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;
}
September 2nd, 2012, 03:58 PM

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.
September 2nd, 2012, 05:26 PM
