Jump to content

I'm having a small issue with a program from school. I need to use a nested for loop (A for loop inside another) to output the following.

 

Any help is greatly appreciated :D

 

                                           1

                                   1       2     1

                            1     2       4     2     1

                     1     2     4        8    4     2     1

               1    2     4     8      16    8     4     2    1

          1   2    4     8    16     32   16    8     4    2   1

     1   2   4    8    16   32     64   32   16    8    4   2   1

1   2   4   8   16   32   64   128   64   32   16   8   4   2   1

Link to comment
https://linustechtips.com/topic/228211-java-nest-for-loop-help/
Share on other sites

Link to post
Share on other sites

@SlaughterApollo 
 
This will print half of the triangle.

int numberOfLines = 8; //based on the image you postedif(numberOfLines >= 1){    for(int spaces=0; spaces < numberOfLines; spaces++){        System.out.print(" ");    }    System.out.print("1");    for(int spaces=0; spaces < numberOfLines; spaces++){       System.out.print(" ");    }    System.out.println();}for(int i=1; i<numberOfLines ; i++){    for(int k=0; k<numberOfLines-i-1; k++){        System.out.print(" ");    }     for(int j=1; j<=(Math.pow(2, i)) ; j*=2){        System.out.print(j + " ");     }    System.out.println();}
        1              1 2      1 2 4     1 2 4 8    1 2 4 8 16   1 2 4 8 16 32  1 2 4 8 16 32 64 1 2 4 8 16 32 64 128

PSU Tier List | CoC

Gaming Build | FreeNAS Server

Spoiler

i5-4690k || Seidon 240m || GTX780 ACX || MSI Z97s SLI Plus || 8GB 2400mhz || 250GB 840 Evo || 1TB WD Blue || H440 (Black/Blue) || Windows 10 Pro || Dell P2414H & BenQ XL2411Z || Ducky Shine Mini || Logitech G502 Proteus Core

Spoiler

FreeNAS 9.3 - Stable || Xeon E3 1230v2 || Supermicro X9SCM-F || 32GB Crucial ECC DDR3 || 3x4TB WD Red (JBOD) || SYBA SI-PEX40064 sata controller || Corsair CX500m || NZXT Source 210.

Link to comment
https://linustechtips.com/topic/228211-java-nest-for-loop-help/#findComment-3124252
Share on other sites

Link to post
Share on other sites

 

@SlaughterApollo 

 

This will print half of the triangle without the preceding spaces. 

int numberOfLines = 8; //based on the image you postedif (numberOfLines >=1) System.out.println("1"); for(int i=1; i<numberOfLines ; i++){      for(int j=1; j<=(Math.pow(2, i)) ; j*=2){          System.out.print(j + " ");       }       System.out.println();}
Output:11 2 1 2 4 1 2 4 8 1 2 4 8 16 1 2 4 8 16 32 1 2 4 8 16 32 64 1 2 4 8 16 32 64 128 

i got the math but the spacing is messing me up

public class Chp4_19 {	public static void main(String[] args) {		for(int x = 0; x < 8; x++)		{		for(int y = 8 - x; y> 0; y--)		{		System.out.print(" ");		}		int w = 1;		for(int z = 0; z < x; z++)		{		System.out.print(w + " ");		w = w * 2;		}		while(w>0)		{		System.out.print(w + " ");		w = w / 2;		}		System.out.println();		}	}}
Link to comment
https://linustechtips.com/topic/228211-java-nest-for-loop-help/#findComment-3124313
Share on other sites

Link to post
Share on other sites

 

i got the math but the spacing is messing me up

Add an extra space to the print statement on line 9, that fixes the spacing until double digit numbers come into play, so once you get a double digit number you have to reduce the spacing by one. 

 

for(int x = 0; x < 8; x++)

{

for(int y = 8 - x; y> 0; y--)

{

System.out.print("    ");

}

int w = 1;

for(int z = 0; z < x; z++)

{

if(w<10){

System.out.print(w + "   ");

w = w * 2;

}

else if(w<100){

System.out.print(w + "  ");

w = w * 2;

}

else{

System.out.print(w + " ");

w = w * 2;

}

}

while(w>0)

{

if(w<10){

System.out.print(w + "   ");

w = w / 2;

}

else if(w<100){

System.out.print(w + "  ");

w = w / 2;

}

else{

System.out.print(w + " ");

w = w / 2;

}

}

System.out.println();

}

PSU Tier List | CoC

Gaming Build | FreeNAS Server

Spoiler

i5-4690k || Seidon 240m || GTX780 ACX || MSI Z97s SLI Plus || 8GB 2400mhz || 250GB 840 Evo || 1TB WD Blue || H440 (Black/Blue) || Windows 10 Pro || Dell P2414H & BenQ XL2411Z || Ducky Shine Mini || Logitech G502 Proteus Core

Spoiler

FreeNAS 9.3 - Stable || Xeon E3 1230v2 || Supermicro X9SCM-F || 32GB Crucial ECC DDR3 || 3x4TB WD Red (JBOD) || SYBA SI-PEX40064 sata controller || Corsair CX500m || NZXT Source 210.

Link to comment
https://linustechtips.com/topic/228211-java-nest-for-loop-help/#findComment-3124438
Share on other sites

Link to post
Share on other sites

Something like this?

 

/** * Created by Tyler on 2014/10/05. */public class Test {    private static final int POWER_COUNT = 8;    private static final int MAX_SPACING = getNumberOfDigits(Math.pow(2, POWER_COUNT));    public static void main(final String... args) {        for (int line = 0; line < POWER_COUNT; line++) {            for(int i = line - POWER_COUNT + 1; i < line; i++) {                if (i >= 0) {                    print((int) Math.pow(2, i));                } else {                    print(null);                }            }            print((int) Math.pow(2, line));            for(int i = line - 1; i > line - POWER_COUNT; i--) {                if (i >= 0) {                    print((int) Math.pow(2, i));                } else {                    print(null);                }            }            System.out.println();        }    }    private static void print(Integer num) {        if (num == null) {            for (int i = 0; i < MAX_SPACING+1; i++) {                System.out.print(" ");            }        } else {            int numSize = getNumberOfDigits(num);            System.out.print(" " + num);            for (int i = 0; i < MAX_SPACING - numSize; i++) {                System.out.print(" ");            }        }    }    private static int getNumberOfDigits(double num) {        return (int) (Math.log10(num)) + 1;    }}
Output

                             1                                                       1   2   1                                               1   2   4   2   1                                       1   2   4   8   4   2   1                               1   2   4   8   16  8   4   2   1                       1   2   4   8   16  32  16  8   4   2   1               1   2   4   8   16  32  64  32  16  8   4   2   1       1   2   4   8   16  32  64  128 64  32  16  8   4   2   1  
Link to comment
https://linustechtips.com/topic/228211-java-nest-for-loop-help/#findComment-3124950
Share on other sites

Link to post
Share on other sites

Something like this?

 

/** * Created by Tyler on 2014/10/05. */public class Test {    private static final int POWER_COUNT = 8;    private static final int MAX_SPACING = getNumberOfDigits(Math.pow(2, POWER_COUNT));    public static void main(final String... args) {        for (int line = 0; line < POWER_COUNT; line++) {            for(int i = line - POWER_COUNT + 1; i < line; i++) {                if (i >= 0) {                    print((int) Math.pow(2, i));                } else {                    print(null);                }            }            print((int) Math.pow(2, line));            for(int i = line - 1; i > line - POWER_COUNT; i--) {                if (i >= 0) {                    print((int) Math.pow(2, i));                } else {                    print(null);                }            }            System.out.println();        }    }    private static void print(Integer num) {        if (num == null) {            for (int i = 0; i < MAX_SPACING+1; i++) {                System.out.print(" ");            }        } else {            int numSize = getNumberOfDigits(num);            System.out.print(" " + num);            for (int i = 0; i < MAX_SPACING - numSize; i++) {                System.out.print(" ");            }        }    }    private static int getNumberOfDigits(double num) {        return (int) (Math.log10(num)) + 1;    }}
Output

                             1                                                       1   2   1                                               1   2   4   2   1                                       1   2   4   8   4   2   1                               1   2   4   8   16  8   4   2   1                       1   2   4   8   16  32  16  8   4   2   1               1   2   4   8   16  32  64  32  16  8   4   2   1       1   2   4   8   16  32  64  128 64  32  16  8   4   2   1  

Clearly thats perfect but if i turned that in i would get cut for cheating by my teacher lol

Link to comment
https://linustechtips.com/topic/228211-java-nest-for-loop-help/#findComment-3125026
Share on other sites

Link to post
Share on other sites

 

Something like this?

 

/** * Created by Tyler on 2014/10/05. */public class Test {    private static final int POWER_COUNT = 8;    private static final int MAX_SPACING = getNumberOfDigits(Math.pow(2, POWER_COUNT));    public static void main(final String... args) {        for (int line = 0; line < POWER_COUNT; line++) {            for(int i = line - POWER_COUNT + 1; i < line; i++) {                if (i >= 0) {                    print((int) Math.pow(2, i));                } else {                    print(null);                }            }            print((int) Math.pow(2, line));            for(int i = line - 1; i > line - POWER_COUNT; i--) {                if (i >= 0) {                    print((int) Math.pow(2, i));                } else {                    print(null);                }            }            System.out.println();        }    }    private static void print(Integer num) {        if (num == null) {            for (int i = 0; i < MAX_SPACING+1; i++) {                System.out.print(" ");            }        } else {            int numSize = getNumberOfDigits(num);            System.out.print(" " + num);            for (int i = 0; i < MAX_SPACING - numSize; i++) {                System.out.print(" ");            }        }    }    private static int getNumberOfDigits(double num) {        return (int) (Math.log10(num)) + 1;    }}
Output

                             1                                                       1   2   1                                               1   2   4   2   1                                       1   2   4   8   4   2   1                               1   2   4   8   16  8   4   2   1                       1   2   4   8   16  32  16  8   4   2   1               1   2   4   8   16  32  64  32  16  8   4   2   1       1   2   4   8   16  32  64  128 64  32  16  8   4   2   1  

Clearly thats perfect but if i turned that in i would get cut for cheating by my teacher lol

 

Asking for help is not cheating lol, as long as you understood what happened.

Link to comment
https://linustechtips.com/topic/228211-java-nest-for-loop-help/#findComment-3125037
Share on other sites

Link to post
Share on other sites

the whitespace can be simply handled with tabs

import java.io.*;class pyramid{	private static final int ROWS = 8;	private static int power(int number, int exp){			int res = 1;			while(exp-- > 0)					res *= number;			return res;	}	public static void main(String[] args){			for(int i = 1; i <= ROWS; i++){					int j, blanks = ROWS - i;					for(j = 0; j < blanks; j++)							System.out.print("\t");					int max = power(2, i - 1);					for(j = 1; j <= max; j *= 2)							System.out.print(j + "\t");					for(j = j / 4; j > 0; j /= 2)							System.out.print(j + "\t");					System.out.println();			}	}}
Link to comment
https://linustechtips.com/topic/228211-java-nest-for-loop-help/#findComment-3126623
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×