1
2 3
4 5 6
7 8 9 10
11 12 13 14 15
and this
1 2 3 4
2 3 4 5
3 4 5 6
4 5 6 7
You'll probably use System.out.print() to print those.
To call System.out.print() and use nested loops as well.
It might help to create an int variable and increment it each line, to keep track of what number you start outputting from.
class extemer
{
public static void main(String args[])
{
int c=1;
int j=1;
for(int i=1;i<=10;i++)
{
for(int k=1;k<=c;k++){
System.out.print(j);
System.out.print(" ");
j++;
}
System.out.println();
c+=1;
}}
}
for(int i=0;i<=10;i++)
The first loop i used because for printing ten lines.
for(k=1;k<=c;k++)
The second loop i used is to print number in this format e.g
1
23
345
5678
how this work as i have taken c=1 and at end loop bracket i have written c+=1.this excatly increment the number to be print mean that in first line 1 is printed and in second line 23 is printed and in third line 234 is printed so as you can see that numbers are been increment in amount to be printed.
System.out.println();
i have used println method at the end of the loop so that when loop circles it print numbers from new line.what println do it take the cursor to the new line.
/////////////////////////
Second Question
class extemer
{
public static void main(String args[])
{
int c=4;
int j=1;
for(int i=1;i<=10;i++)
{
if(j>4){
j-=3;
}
for(int k=1;k<=c;k++){
System.out.print(j);
System.out.print(" ");
j+=1;
}
System.out.println();
}}
}
The second question you ask have the same logic as first question do.
The first loop print ten lines,same as in first question.
while second loop does the same thing as in first question but as in first question we want to print number but by incrementing them as enter in a new line e.g 1 and in second line 23 and third line 234 and so on.So that's why we have taken c=1 and incremented it with 1 e.g c+=1 at the end of the loop but in this second question we want to print 4 numbers in every line so i have taken c=4 and not increment it by 1.
if(j>4){
j-=3;
}
Major part of question was to print number in this order e.g 1234,2345,3456,4567
so that's why i put j>4 as first time when loop circles j=1 and so if statement became false and print 1234 and j get equals to 4,now j is incremented by 1 so j=5.The next time loop circles the if statement gets true and j value is decrement by 3 so it became 2 which prints 2345 and so on.
check mine second post its been edited for you by me
Hello everyone,
I am Ritz here,I am from India.
Can anyone help me to get the program for the pattern below as soon as possible.
___________________
54321
4321
321
21
1
___________________
You need to start your own thread for your problem. This thread is over one year old.
When you start your new thread, be sure to post the code that you currently have and ask specific questions about the problems you are having getting it to work.
hello Ritz ...
welcome to DaniWeb, but you might want to take a couple of seconds reading the rules of this forum:
just the heads-up on a few major ones:
1. 'as soon as possible' as you put it ... do remember, it's not urgent to us.
2. do not hijack old threads, start a new one
3. we don't make your homework for you, we're just willing to help you get the errors out of your code, so .. what have you done so far?
it's a very easy program, a simple for-loop and System.out.print() will do the trick.
NormR1 and Stultuske have it spot on.
Closing this thread.
hw 2 print d pattern:-
12345
22345
33345
44445
55555
plzzzzzz hlp!!
DaniWeb Member Rules (which you agreed to when you signed up) include:
"Do not hijack old threads by posting a new question as a reply to an old one"
http://www.daniweb.com/community/rules
Please start your own new thread for your question
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.