want to print numbers: 1 1 2 1 2 4 1 2 4 8 using a for loop in java. so far i have the following code: for(int i = 1;i<=4;i++) for(int j =0; j<=3; j++) { j=j+1; system.out.print(j); } need some help
Why you all are in the habit of making such easy programs so complex? The program will be simply:-
class pattern { public void disp(int rows) { int c=1; for(int i=1;i<=rows;i++) { for(int j=1;j<=c;j*=2) { System.out.print(j); } System.out.println(); c*=2; } } } If you are asked to user input the number of rows then this is the program.If not then cut int rows only(not the two brackets)and in the sixth line in place of "i<=rows" write the number of rows after "i<=". Do not write the quotes(" ") in the program.Better if you copy paste the program.Please give a reply.
Comments
for(int i = 1;i<=4;i++)
for(int j =0; j<=3; j++)
j=j+1;
{
system.out.println(j);
}
}
for (int i = 1; i <= 4; i++) {
int lastNbr = 1;
for (int j = 1, k=1; j < i + 1; j++) {
System.out.print(k + " ");
lastNbr = k;
k = 2 * lastNbr;
}
System.out.println("");
}
g.l.
public class Numara {
public static void main(String[] args) {
String j = "";
for (int i = 1; i <= 8; i += i) {
j = j + " " + i;
System.out.println(j);
}
}
}
[/code]
Try this. Keep me informed if it's working.
The program will be simply:-
class pattern
{
public void disp(int rows)
{
int c=1;
for(int i=1;i<=rows;i++)
{
for(int j=1;j<=c;j*=2)
{
System.out.print(j);
}
System.out.println();
c*=2;
}
}
}
If you are asked to user input the number of rows then this is the program.If not then cut int rows only(not the two brackets)and in the sixth line in place of "i<=rows" write the number of rows after "i<=".
Do not write the quotes(" ") in the program.Better if you copy paste the program.Please give a reply.
public class Pattern {
public static void main(String[] args) {
for(int i=1;i<=4;i++){
for(int j=0;j<=i-1;j++){
System.out.print((int)Math.pow(2.0,(double)j));
}
System.out.println();
}
}
}
I hope this works too.
Thanks.