Click here to Skip to main content
15,909,039 members
Please Sign up or sign in to vote.
1.00/5 (7 votes)
See more:
how much value variable j in last(four) for loop ? why??


C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace csharp
{
    class Program
    {
        static void Main(string[] args)
        {

            Console.WriteLine("Enter the number");
            int n = Convert.ToInt32(Console.ReadLine());
            for (int i = 1; i <= n; i++)
            {
                int j;
                for (int k = 1; k <= n - i; k++)
                {
                    Console.Write(" ");
                }
                for (j = 1; j <= i; j++)
                {
                    Console.Write("" + j);
                }
                for (int s = j - 2; s >= 1; s--)
                {
                    Console.Write("" + s);
                }


                Console.WriteLine("");
            }


            Console.ReadLine();
        }
    }
}
Posted
Updated 15-Nov-11 0:54am
v2
Comments
RaisKazi 15-Nov-11 6:58am    
Please stop posting your Home-Work Questions.
http://www.codeproject.com/script/Answers/MemberPosts.aspx?tab=questions&mid=7509390

Try to solve them by your self, which may help you to become a programmer one day.
nagendrathecoder 15-Nov-11 7:05am    
Debug and check the answer.
Shubham Vijay 15-Nov-11 7:21am    
its not my home work.m only try to make program, but when m run then many error are generate.so give me solution and remove my error,,
Philippe Mori 15-Nov-11 15:54pm    
Which error... That program compile without error if I paste it in a new C# Console project and also execute correctly if I enter a number like 3.
Shubham Vijay 15-Nov-11 7:23am    
nagendrathecoer... m already run and give me answer but i can't understand answer how to print,,so m ask

Using a debugger it the easiest way to understand the program. You just have to execute it step by step and inspect the variables.

Anyway for a simple program like this one, if you execute it once, it should then become obvious what each line does.

And it is pretty easy to figure out what this program do only by looking at the code. Last loop simply print number backward starting with the number just before last printed number. In fact after the previous loop, j is 1 more than the last printed value and substracting 2 from that number give the number just before the last printed value.

Try it on paper with a small number (for exemple, an input of 3).
 
Share this answer
 
v3
Comments
Sergey Alexandrovich Kryukov 15-Nov-11 15:42pm    
Good advice, a 5.
--SA
put it in Visual studio Editor and debug it. or if you have a 'C' version of this code then you can debug overthere too. by pressing F8
 
Share this answer
 

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