Click here to Skip to main content
15,894,106 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi Team,

i have the code as follows:Its working fine .i want to implement one more logic in the code
i want the output as follows:
0
.1
..1
...2
....3
.....5
......8
just ignore the dots .the output should look like this.
i want the space before the numbers .What changes i need to make .please guide me.
C#
namespace Triangle
{
    class Program
    {
        public static int Fibonacci(int n)
        {
            int a = 0;
            int b = 1;
            for (int i = 0; i < n; i++)
            {
                int temp = a;
                a = b;
                b = temp + b;
            }
            return a;
        }

        static void Main()
        {
            for (int i = 0; i < 15; i++)
            {
                Console.WriteLine(Fibonacci(i));
                Console.ReadLine();
            }
        }
    }
}


thanks
harshal
Posted
Updated 7-May-14 22:51pm
v3

If I have understood you correctly you need to change the line Console.WriteLine(Fibonacci(i)); to display a space before each number.

You need "Composite Formatting"[^]

For example
Console.WriteLine(" {0}", Fibonacci(i));


[EDIT] - The "ignore the dots" confused me. The simplest way is to change the line Console.WriteLine(Fibonacci(i)); to
Console.WriteLine(Fibonacci(i).ToString().PadLeft(i + 1,' '));
 
Share this answer
 
v2
Comments
R Harshal 8-May-14 5:02am    
Thanks for yur reply but i want the space like this : ignore the dots<br>
0<br>
.1<br>
..1<br>
...2<br>
not like this:<br>
0<br>
1<br>
1<br>
2<br>
3<br>
please guide me .<br>
Thanks <br>
Harshal
R Harshal 8-May-14 5:27am    
Please anyone can help me.
CHill60 8-May-14 6:26am    
I've updated my solution now that the question is clearer
R Harshal 8-May-14 6:34am    
its working for me .<br>
thank you so much .<br>
but this want i want to do .I am not clever in logic ,so how can i improve it.<br>
 <br>
Thanks<br>
harshal
CHill60 8-May-14 6:46am    
The logic of your calculation is fine - as long as you understand it. The iterative approach works well here because you are storing the previous result as you go through the loop. An alternative approach would be to use "recursion" - but this will be less performant in this case (and is overkill). There is a nice example here Recursive methods using C#[^]

Be aware with your current solution that you will get overflow problems eventually if you choose a big enough number for n. Try n = 48 and see what happens - you might be surprised :-)
C#
class Program
       {
           public static int Fibonacci(int n)
           {
               int a = 0;
               int b = 1;
               for (int i = 0; i < n; i++)
               {
                   int temp = a;
                   a = b;
                   b = temp + b;
               }
               return a;
           }

           static void Main()
           {
               for (int i = 0; i < 15; i++)
               {
                   for (int j = 0; j < i; j++)
                   {
                       Console.Write(" ");
                   }
                   Console.WriteLine(Fibonacci(i));
                   Console.ReadLine();
               }
           }
 
Share this answer
 
Comments
CHill60 8-May-14 6:27am    
There is a simpler way without the second loop - see my amended solution
R Harshal 8-May-14 6:29am    
Where it is amended Solution?? .Please let me know.

Thankful
harshal
R Harshal 8-May-14 6:29am    
yes i got it ...
thank you Sir ...
R Harshal 8-May-14 6:27am    
Thank you MR.Swarup Chavan.
Its working .
one more question please,this what i want to build my logic .i want to improve my logic .
how can it possible .
please any reference ..let me know.
Thanks
harshal
SwarupDChavan 8-May-14 6:38am    
Dude logic builds up slowly as you start solving certain complex problems on your own no one can teach logic thats for sure

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900