- - - - - - - - - * 
- - - - - - - - * * 
- - - - - - - * * * 
- - - - - - * * * * 
- - - - - * * * * * 
- - - - * * * * * * 
- - - * * * * * * * 
- - * * * * * * * * 
- * * * * * * * * * 
* * * * * * * * * 

Can anybody help me out? actually i want * to be printed at the end blank space.

    public static void main(String[] args) {
   int x=10, a=0;


        for (int i = 1;  i<=  x; i++) {
             for (int k = 1; k<=x-i ; k++) {
                    System.out.print("- ");
                      a =k;
             }
//             System.out.println("");
            for (int j = a+1; j <=x; j++) {

                System.out.print("*");
                System.out.print(" ");

            }
            System.out.println("");

        }
}

You could change Line 17 of your code to System.out.println("*"); to add that '*' you want.

Sorry necrovore, that would add an asterisk to every line. Between lines 19 and 20 would be better.

The problem is that on the last line the loop lines 6-9 is not executed, so the variable a does not get set, so it keeps its previous value of 1, which is why you only get 9 asterisks. The correct fix is to make sure a is zero for the last line. I'll leave you to think about that.

static void Main(string[] args)
        {
                int x=10, a=0;
            for (int i = 1;  i<=  x; i++) {
                Console.Write("\n");
                 for (int k = 1; k<=x-i ; k++) {
                     Console.Write("- ");
                          a =k;
                 }
    //             System.out.println("");
                for (int j = a+1; j <=x; j++) {
                    Console.Write("*");
                    Console.Write(" ");
                }
            }
            Console.WriteLine("*");
            Console.ReadKey();
        }

I guess i added one more like of code. the code is in c#. (sorry, dont have java in system)

@necrovore,

Your solution is not really correct but rather a patch job. What JamesCherrill said that the OP should reset the a value at either the begining or ending of each loop i. Because the current code let variable a be assigned by the for-loop k, the variable is not modified in the last loop in loop i which causes the number of loop j to be off by 1.

I remember this example! Frustrated the hell out of me too back in the day! Am thinking to myself would probably be easy now, to just make an array of chars, and a single for loop starting at the right side, the end of the array, switching values to asterisks as it goes, and printing the whole array each time through the loop. Probably not the textbook version, but now I can see all sorts of clever ways of doing it.

I'm not sure whether creating arrays for it should be considered a 'clever way' to do this.
Usually, less resources used is better. Creating an array you can do without isn't necessarily clever.

hi all,
why dont we try like this?
all you know , i took i variable for number of rows we want and j variable for print symbol in which column
as shown in the picture we need to print empty space where the bellow condition is met

if(j  <= total_no_rows - i)

in remaining places we need to print '* ' symbol [instead of taking extra for loop]

// see this sample java code 
for (int i = 1; i <= total_lines; i++) {
            for (int j = 1; j <= total_lines; j++) { 
                if (j <= total_lines - i) {
                    System.out.print(" " + "\t");// for space
                } else {
                    System.out.print("*" + "\t");
                }
            }
            System.out.println("");
        }

advice me if i am worng

Your code works @IHELPYOU...
but i wanted to alter the code i have written to get the same output on screen.
Thank you all.

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.