i need a program that asks for one number and output like this:
1234
123
12
1
12
123
1234

NOTE:: in the example above the input given is number 4.
requirement doing this in using nest FOR loops.

i started coding it bu i dont get the algorithm or shall i say the concept help me please
add my codes :((((((((((( pleaseeeeeee as your xmas gift to meeee.

import java.io.*;

public class CaseStudy{
    public static InputStreamReader sti = new InputStreamReader(System.in);
    public static BufferedReader input = new BufferedReader(sti);
    
    public static void main(String[]args) throws IOException{
        
        Double x,y;
        System.out.println("Enter a positive integer: ");
        x=Double.parseDouble(input.readLine());
        
        
        for(x=0;x<y;y++ ){ \* i dont have really the concept to start with i tried making few
                                       codes but all i get is 44444 or  something like that help please*/      
        }
        
        
        

    }
}

Recommended Answers

All 4 Replies

I have forgotten all my Java, so the code may give you compile errors and you will have to fix them on your own. However the algorithm will be the same

Try a loop like this.

int x,y; \* Make these integers */
System.out.println("Enter a positive integer: ");
x=Interger.parseInt(input.readLine()); \* Get the user input to x. Used ParseInt to read it from the console. */

if ( x > 0) 
{
	for(y=-x+1;y<x;y++ )/* See the limits used for the loop.*/  
	{ 
        int i ; 
        for (  i = 0; i <= Math.abs(y) ; i++ ){
		System.out.println( i+1 );         
	}
        System.out.println( '\n' );  /* You need a newline at the end of the loop or you will get everything in one line */
	}
}

I guess the above should work. At least the algorithm should.

commented: Huh? A wolf in Java land! ;-) +20

yes its working, and it has many compiler errors but i fixed it already.

but since i dont want to leach because i want to learn, i want to ask what Math.abs part explain it pls

but thanks anyway iloveyou

~sara

The Math.abs function returns the absolute value of the number passed in as a parameter. Check out the java API for more information.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.