Thread: Difficult C programs

  1. #1
    Registered User
    Join Date
    Apr 2008
    Posts
    2

    Difficult C programs

    how to print fibbonacci numbers diagonally ....
    i have written the code for fibonacci series .. & i am getting the Output ..

    Code:
    #include<stdio.h>
    #include<conio.h>
    void main()
    {
    	int a=0, b=1, c, i, num,j,k;
       clrscr();
       printf("Enter till whr u want fibbo : ");
       scanf("%d",&num);
       printf("\n %d",a);
       printf("\n\t%d",b);
       for(i=1; i<=num; i++)
       {
          c=a+b;
          a=b;
          b=c;
          printf("\n");
          printf("\t");
          for(k=i; k<=i; k++)
          {
    	printf("\n\t");
    	for(j=i; j<=i; j++)
    	{
    	  printf("\n\t");
    	  printf ("\n\t %d",c);
    	  printf("\t");
    	  if(i==j)
    	  {
    	    printf("\t");
    	    break;
    	  }
    	 }
          }
       }
       getch();
    }
    ..This is the Code ...
    But i want my output as ......

    1
    1 1
    1 1 2
    1 1 2 3
    1 1 2 3 5
    1 1 2 3 5 8 ... & so on ..

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Code:
    "Enter till whr u want fibbo:"
    Are some of your keys missing on your keyboard??

    What does this do:
    Code:
          printf("\n");
    And this:
    Code:
          printf("\t");
    If you understand what those do, then I presume you can format your output any way you like...

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Nub SWE
    Join Date
    Mar 2008
    Location
    Dallas, TX
    Posts
    133
    Code:
    #include <stdio.h>
    
    int main(void)
    {
    	int curr = 1;
    	int prev = 0;
    	int result[20];
    	int k;
    	
    	result [0] = 1;
    	
    	for (k = 1; k < 20; k++)
    	{
    		result[k] = curr + prev;
    		prev = curr;
    		curr = result[k];
    	}
    	
    	for (k = 0; k < 20; k++)
    	{
    		printf("&#37;d ", result[k]);
    	}
    	
    	return 0;
    }
    Make sure you are getting the correct results before you try to display them.
    Last edited by JDGATX; 04-15-2008 at 10:26 AM.

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by JDGATX View Post
    Code:
    int main(void)
    {
    	int curr = 1;
    	int prev = 0;
    	int result[200];
    	int k;
    	
    	result [0] = 1;
    	
    	for (k = 1; k < 200; k++)
    	{
    		result[k] = curr + prev;
    		prev = curr;
    		curr = result[k];
    	}
    	
    	for (k = 0; k < 200; k++)
    	{
    		printf("%d ", result[k]);
    	}
    	
    	return 0;
    }
    1. Don't post solutions to homework problems [yes, I'm sure that there are other uses of fibonacci series, but it's very unlikely that this is such a case].

    2. I don't think your code actually solves what the OP wanted.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    Registered User
    Join Date
    Apr 2008
    Posts
    2
    i know that \n is for next line & \t is for TAb space ...
    But i want to do this only through loops ...& whenever the counter comes inside any loop it goes to the First position ..

  6. #6
    Nub SWE
    Join Date
    Mar 2008
    Location
    Dallas, TX
    Posts
    133
    Quote Originally Posted by matsp View Post
    1. Don't post solutions to homework problems [yes, I'm sure that there are other uses of fibonacci series, but it's very unlikely that this is such a case].

    2. I don't think your code actually solves what the OP wanted.

    --
    Mats
    It sure doesn't, which is why I posted it. His code didn't find the correct sequence, let alone display it correctly. Consider it a building block.

  7. #7
    Nub SWE
    Join Date
    Mar 2008
    Location
    Dallas, TX
    Posts
    133
    Quote Originally Posted by aijaz View Post
    i know that \n is for next line & \t is for TAb space ...
    But i want to do this only through loops ...& whenever the counter comes inside any loop it goes to the First position ..
    The solution that I've written doesn't make use of any tabs.

    Since you're tasked with using for loops, I will tell you to use a pair of nested for loops. Now you have to decide what your loop conditions will be for the inner and outer loop.

    I would suggest creating the desired sequence first, storing it, and then worrying about displaying it.

  8. #8
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    See also here.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Recommend upgrade path for C programs
    By emanresu in forum C Programming
    Replies: 3
    Last Post: 11-22-2007, 07:32 AM
  2. Newbie question: pointers, other program's memory
    By xxxxme in forum C++ Programming
    Replies: 23
    Last Post: 11-25-2006, 01:00 PM
  3. Reroute where programs write to
    By willc0de4food in forum C Programming
    Replies: 7
    Last Post: 09-21-2005, 04:48 PM
  4. POSIX/DOS programs?
    By nickname_changed in forum C++ Programming
    Replies: 1
    Last Post: 02-28-2003, 05:42 AM
  5. executing c++ programs on the web
    By gulti01 in forum C++ Programming
    Replies: 4
    Last Post: 08-12-2002, 03:12 AM