Forums: » Register « |  Free Tools |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support |

New Free Tools on Dev Shed!

#1
September 2nd, 2012, 01:15 PM
 balvinder87
Registered User

Join Date: Aug 2012
Posts: 3
Time spent in forums: 1 h 35 m 54 sec
Reputation Power: 0
Prime fibonacci program

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

#2
September 2nd, 2012, 02:11 PM
 salem
Contributed User

Join Date: Jun 2005
Posts: 4,252
Time spent in forums: 2 Months 4 Weeks 1 Day 12 h 25 m 37 sec
Reputation Power: 1809

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.
__________________
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper

#3
September 2nd, 2012, 02:14 PM
 artheus
Registered User

Join Date: Feb 2010
Location: Stockholm
Posts: 9
Time spent in forums: 1 h 53 m 38 sec
Reputation Power: 0
Quote:
 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
September 2nd, 2012, 03:12 PM
 balvinder87
Registered User

Join Date: Aug 2012
Posts: 3
Time spent in forums: 1 h 35 m 54 sec
Reputation 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
September 2nd, 2012, 03:58 PM
 dwise1_aol
Contributing User

Join Date: Jan 2003
Location: USA
Posts: 6,842
Time spent in forums: 3 Months 23 h 39 m 57 sec
Reputation Power: 2199
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
September 2nd, 2012, 05:26 PM
 b49P23TIvg
Contributing User

Join Date: Aug 2011
Posts: 4,124
Time spent in forums: 1 Month 3 Weeks 2 Days 4 h 49 m 23 sec
Reputation Power: 455
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")
__________________
[code]Code tags[/code] are essential for python code!

 Viewing: Dev Shed Forums > Programming Languages > C Programming > Prime fibonacci program