Thread: Code Not working

  1. #1
    Registered User
    Join Date
    Oct 2008
    Posts
    13

    Code Not working

    Can someone tell me how the series is to be implemented in c
    1,1,1 2,1 2 3,1 2 3 4 5, 1 2 3 4 5 6 7 8
    here is my code
    but i am not able to get the answer
    Code:
    #include<stdio.h>
    int i,n1=0,n2=1,n3,n;
    void main()
    {
    printf("\n Enter Value Of N  \n");
    scanf("%d",&n);
    printf("%-2d",n1);
    for(i=0;i<=n;i++)
    fib(n);
    }
    
    int fib(int n)
    {
    n1=0,n2=1;
    printf("%-2d",n1);
     for(i=0;i<n;i++)
      {
       n3=n1+n2;
       n1=n2;
       n2=n3;
       printf("%-2d",n3);
      }
    }

  2. #2
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Code:
    #include <stdio.h>
    
    int main () {
    	int n,i,ii,x=0;
    	puts("Value of N");
    	scanf("%d",&n);
    	for (i=0;i<n;i++) {
    		for (ii=0; ii<=x; ii++) {
    			printf("%d ",1+ii);
    		}
    		x++;
    	}
    }
    output if n=5: 1 1 2 1 2 3 1 2 3 4 1 2 3 4 5

    Which is not exactly the series in your example, but perhaps it will help.

    [edit: Sorry, I'm not a math guy and it just occurred to me that "fib" refers to Fibonacci -- see my next post]
    Last edited by MK27; 10-12-2008 at 08:43 AM.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    The result of fib(n) needs to be used to control the length of another loop.
    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. #4
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    The Fibonacci numbers (what are they for, anyway?):

    Code:
    #include <stdio.h>
    
    int main () {
    	int a=0, b=1, n=a+b,i,ii;
    	puts("Number of iterations");
    	scanf("%d",&ii);
    	printf("%d %d ",a,b);
    	for (i=0;i<ii;i++) {
    		n=a+b;
    		printf("%d ",n);
    		a=b;b=n;
    	}
    }
    output if ii=12: 0 1 1 2 3 5 8 13 21 34 55 89 144 233
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  5. #5
    Technical Lead QuantumPete's Avatar
    Join Date
    Aug 2007
    Location
    London, UK
    Posts
    894
    Quote Originally Posted by MK27 View Post
    The Fibonacci numbers (what are they for, anyway?):
    You will find fibonacci numbers a lot in nature, from the way that leaves are arranged on some plants to capture the maximum amount of sunlight, to the stripes of a tiger. More recently fibonacci numbers have found application in financial trading. Like the article says, mostly the numbers themselves aren't as interesting as the ratios between them.

    QuantumPete
    "No-one else has reported this problem, you're either crazy or a liar" - Dogbert Technical Support
    "Have you tried turning it off and on again?" - The IT Crowd

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Code not working?
    By Elysia in forum C++ Programming
    Replies: 12
    Last Post: 04-06-2009, 01:57 AM
  2. Replies: 3
    Last Post: 02-24-2009, 08:49 PM
  3. C code not working
    By D3ciph3r in forum C Programming
    Replies: 2
    Last Post: 05-27-2005, 04:13 PM
  4. Trying to eject D drive using code, but not working... :(
    By snowfrog in forum C++ Programming
    Replies: 3
    Last Post: 05-07-2005, 07:47 PM
  5. Linked List Working Code
    By Linette in forum C++ Programming
    Replies: 9
    Last Post: 01-24-2002, 12:00 PM