Forums Register Login

Problem with number arrangement :(

+Pie Number of slices to send: Send
i am trying to execute a program with the following number arrangement.

3 2 1
3 2
3

i have used "i" for rows and j for columns.



The output i got is

D:\Amit\Java\New folder>javac Demo.java

D:\Amit\Java\New folder>java Demo
321
21
 1

i spoke to my trainer regarding the same, and he told me to swap j with space and vice versa.

}

The new output i got is:

D:\Amit\Java\New folder>javac Demo.java

D:\Amit\Java\New folder>java Demo

3
32

The second code doesn't make much of a sense, since i have used the if condition to print a integer when i+j<=4,  or else use space.

Would really appreciate your help, thank you.

+Pie Number of slices to send: Send
Inner for loop needs to be checked.

It is more to do with logic than Java.



Of course, you can think of how to remove extra space appended at the end of each line.  
+Pie Number of slices to send: Send
 

Omkar Shetkar wrote:Inner for loop needs to be checked.

It is more to do with logic than Java.



Of course, you can think of how to remove extra space appended at the end of each line.  




Thanks for the reply, can you please explain the meaning of " for (int j = 3; j >= i; j--) " and S.O.P(j + " ") , there are few program executed in my class with j>=i , having hard time understanding the logic.

I have asked couple of my friends , to make me understand the j>=i , all failed to make me understand the logic
+Pie Number of slices to send: Send
To understand the meaning of this program, better take pen and paper.
Write down the control flow of the program.

For eg,
------------------
i                  j
------------------
1                 3
                  2
                  1
                      <-- Now j >=i is TRUE, hence skips inner loops
2                 3
                  2
                     <-- Now j >=i is TRUE, hence skips inner loops
3                 3
                     <-- Now j >=i is TRUE, hence skips inner loops
                 


Console
-----------
3 2 1
3 2
3


This way you can understand a computer program and its logic.

+Pie Number of slices to send: Send
 

Omkar Shetkar wrote:To understand the meaning of this program, better take pen and paper.
Write down the control flow of the program.

For eg,
------------------
i                  j
------------------
1                 3
                  2
                  1
                      <-- Now j >=i is TRUE, hence skips inner loops
2                 3
                  2
                     <-- Now j >=i is TRUE, hence skips inner loops
3                 3
                     <-- Now j >=i is TRUE, hence skips inner loops
                 


Console
-----------
3 2 1
3 2
3


This way you can understand a computer program and its logic.



WoW thanks a lot Omkar, you made it look so easy. Now i know what exactly it means ( kinda slow learner) .
However, when i tried to execute the code which you have given, it worked like a charm. But when i tried to give spacing, i hit a road block again. Can you tell me what am i doing wrong. And sorry for being such a pain



this is the output i am getting

D:\Amit\Java\New folder>java Demo
3 2 1
2
+Pie Number of slices to send: Send
I would say write the logic again on pen and paper to figure out the issue.
This will surely will give you a clue.
To give you some hint, you want to have SPACE only when j is less than i.



Did you miss me? Did you miss this tiny ad?
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 326 times.
Similar Threads
Why the result of printing out the not-letter chars to console is different from to applets
Break statement Question
Exercise i m having trouble with
Displaying output for the given format
Run more than one code?
More...

All times above are in ranch (not your local) time.
The current ranch time is
Apr 15, 2024 21:50:22.