#1
  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
  2. #2
  3. Contributed User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jun 2005
    Posts
    4,379
    Rep Power
    1871
    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.
    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
  4. #3
  5. 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
    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/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
  6. #4
  7. 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;
    }
  8. #5
  9. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,145
    Rep Power
    2222
    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.
  10. #6
  11. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,840
    Rep Power
    480
    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 and Makefiles!

IMN logo majestic logo threadwatch logo seochat tools logo