this is the code that i did:

import javax.swing.JOptionPane;


public class MidtermExam {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub

	int x;
	int y;
	int z;
	
	
	int count = Integer.parseInt(JOptionPane.showInputDialog(null,"Enter any Number:"));
	
	for(x=1;x<=count;x++){
		
			for(y=x;y<=count;y++){
					
			
				
		
		System.out.print(x+"");
		} 
		
	
		System.out.println();
		}
	 }
	
	}

it should output a triangle like this when i input 3:

1
2 2
3 3 3

but it doesnt and i dont know how to do that..please help thanks!

what does it print?

what does it print?

this:
111
22
3
but it should print a triangle like this:


1
2 2
3 3 3

just spotted the problem:
replace your inner for-loop signature with:

for (y = 1; y <= x; y++) {

this:
111
22
3
but it should print a triangle like this:


1
2 2
3 3 3

no not like that but this: (the "_" means space)

_1_
_2_2_
3_3_3

there!

a real triangle u know what i mean wahah!

well, you know that your base will be count*2 (if count = odd) or (count*2) - 1 (if count = even)

you can calculate the length your line must be using that.

just spotted the problem:
replace your inner for-loop signature with:

for (y = 1; y <= x; y++) {

yes yes thats right but thats a right triangle.. im trying to do a triangle


i TRIAngle..not a right triangle:(

yes, I've seen. you can do that by implementing what I've said in my previous post.
just tried it here and it's not that difficult:
you set 3 for loops, an outer, to loop over the number of lines you have to print ('count' times, that is)
and two for loops in that one:
the first one, to print the leading spaces for that line, that you can calculate based on the base of the triangle (I've shown you above how to find that one)
the second one to print the actual line of numbers with spaces in between.

yes, I've seen. you can do that by implementing what I've said in my previous post.
just tried it here and it's not that difficult:
you set 3 for loops, an outer, to loop over the number of lines you have to print ('count' times, that is)
and two for loops in that one:
the first one, to print the leading spaces for that line, that you can calculate based on the base of the triangle (I've shown you above how to find that one)
the second one to print the actual line of numbers with spaces in between.

ok ok i'll try to do this...

well, I have a meeting now, but if you have any questions, I'll check on them when I get back. it's pretty easy to program once you know what logic to follow.

yes, I've seen. you can do that by implementing what I've said in my previous post.
just tried it here and it's not that difficult:
you set 3 for loops, an outer, to loop over the number of lines you have to print ('count' times, that is)
and two for loops in that one:
the first one, to print the leading spaces for that line, that you can calculate based on the base of the triangle (I've shown you above how to find that one)
the second one to print the actual line of numbers with spaces in between.

hey sir this is what i come up to

int x;
	int y;
	int z;
	
	
	int count = Integer.parseInt(JOptionPane.showInputDialog(null,"Enter any Number:"));
	 
	int spaces = count-1;

	for(x=1;x<=count;x++){
		
		
		z=2*x+1;
		
			for(y=1;y<=spaces+z;y++){
				
				if (y<=spaces)
					System.out.print("");
					
					else
						System.out.print("*");
						
				}
					
			
				
		
		
	
		System.out.println();
		 spaces --;
		}
	 }
}

i got some of the idea from the other codes i put it all together with my code..and as u have said i tried to do an equation for the spaces..multiplied it by 2

but doesnt change a thing..i have some errors in the logic..doesnt show the right output

hey sir this is what i come up to

int x;
	int y;
	int z;
	
	
	int count = Integer.parseInt(JOptionPane.showInputDialog(null,"Enter any Number:"));
	 
	int spaces = count-1;

	for(x=1;x<=count;x++){
		
		
		z=2*x+1;
		
			for(y=1;y<=spaces+z;y++){
				
				if (y<=spaces)
					System.out.print("");
					
					else
						System.out.print("*");
						
				}
					
			
				
		
		
	
		System.out.println();
		 spaces --;
		}
	 }
}

i got some of the idea from the other codes i put it all together with my code..and as u have said i tried to do an equation for the spaces..multiplied it by 2

but doesnt change a thing..i have some errors in the logic..doesnt show the right output

There was a previous question similar to this, but i cant find it now, its somewhere on the forum but here was the code that came from the answer maybe you can use it:

int n=7;
     for (int i = 1; i < n; i +=2) {
            for (int j = 0; j < n-1-i / 2; j++)
                System.out.print(" ");
            for (int j = 0; j < i; j++)
                System.out.print("*");
            System.out.print("\n");
        }

well, I have a meeting now, but if you have any questions, I'll check on them when I get back. it's pretty easy to program once you know what logic to follow.

sir i did some changes here but i cant understand how it went right please explain to me what happened here and it worked right,...:

package mgaSample;




import javax.swing.JOptionPane;

public class SampleTriangle {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
	
	
		
	
		 int n =Integer.parseInt(JOptionPane.showInputDialog(null,"Enter any number:"));
		 
		          int spaces = n-1;
		 
		          int ast;                
		 
		          for(int i = 0; i < n; i++)
		 
		          { 
		 
		             ast = 2*i+1;
		 
		             for(int j = 1; j <= spaces+ast; j++)
		 
		             {
		 
		                 if(j <= spaces)
		 
		                     System.out.print(' ');
		 
		                 else
		 
		                     System.out.print('*');
		 
		             }
		 
		           System.out.println();
		 
		             spaces--;
		 
		         }   
		 
		     }
		 
		 }

thanks!

I had about the same:

public static void main(String[] args) {

		int count = Integer.parseInt(JOptionPane.showInputDialog(null, "Enter any Number:"));
		// this calculation of base is based on the assumption that count
		// is an odd value
		int base = count * 2;
		// if count is an even value, we'll have to subtract one.
		if (count % 2 == 0)
			base -= 1;

		int nrLine = 1;
		for (int tel = 0; tel < count; tel++) {
			// this for loop adds up the spaces before every line
			// just change the space in any character to see the difference.
			for (int test = nrLine - 1; test < base / 2; test++) {
				System.out.print(" ");
			}
			// this for loop adds up the numbers that have to be printed
			// we need to print the nr of the line (which is automatically augmented
			// every loop, and which was originally set to value 1, so 1 is not only
			// the first line, but will also print '1' '1' times.
			for (int test = 0; test < nrLine; test++) {
				System.out.print(nrLine + " ");
			}
			// when all the printing is done, just make sure you go to a next line
			// and augment nrLine, otherwise you would get a list of '1' s printed
			// right beneath each other.
			System.out.println();
			nrLine += 1;
		}
	}

There was a previous question similar to this, but i cant find it now, its somewhere on the forum but here was the code that came from the answer maybe you can use it:

int n=7;
     for (int i = 1; i < n; i +=2) {
            for (int j = 0; j < n-1-i / 2; j++)
                System.out.print(" ");
            for (int j = 0; j < i; j++)
                System.out.print("*");
            System.out.print("\n");
        }

yes..thts right but what i have to do is something like this:

__1__
_2_2_
3_3_3


thats also the output of my program and i cant do something like that

well, look at my snippet, it prints just that and has comments to explain.

yes..thts right but what i have to do is something like this:

__1__
_2_2_
3_3_3


thats also the output of my program and i cant do something like that

what must the correct output ressemble?
this?
__1__
_2_2_
3_3_3

with _ being a space, is what she said earlier.

well, look at my snippet, it prints just that and has comments to explain.

sir yes.. i have took a look at it..
i have inputted 22 in your program but there seems to be an error..
suppose this is the triangle that i am talking about! (because when i paste the triangle here it doesnt follow the format haha)
this is the output of ur program when i inputted 22:


1
2 2
3 3 3
4 4 4 4
5 5 5 5 5
6 6 6 6 6 6
7 7 7 7 7 7 7
8 8 8 8 8 8 8 8
9 9 9 9 9 9 9 9 9
10 10 10 10 10 10 10 10 10 10
11 11 11 11 11 11 11 11 11 11 11
12 12 12 12 12 12 12 12 12 12 12 12
13 13 13 13 13 13 13 13 13 13 13 13 13
14 14 14 14 14 14 14 14 14 14 14 14 14 14
15 15 15 15 15 15 15 15 15 15 15 15 15 15 15
16 16 16 16 16 16 16 16 16 16 16 16 16 16 16 16
17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17
18 18 18 18 18 18 18 18 18 18 18 18 18 18 18 18 18 18
19 19 19 19 19 19 19 19 19 19 19 19 19 19 19 19 19 19 19
20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
21 21 21 21 21 21 21 21 21 21 21 21 21 21 21 21 21 21 21 21 21
22 22 22 22 22 22 22 22 22 22 22 22 22 22 22 22 22 22 22 22 22 22


that 10 onwards shouldnt look like that..if it is "ten" then it should be ten :)

with _ being a space, is what she said earlier.

sir i thought u have to attend a meeting u can answer my questions when ur free.. i just want to know how did this happen..that was our midterm exam i am just trying to figure out how to do that because i wasnt able to do that haha

yes, I only intended for small numbers, with all the same length.
well, it was only a half-hour meeting, so I'm already done.

if you want it for longer numbers, you'll need extra validation on the length of base, and the width of the nrLine you're printing.

with _ being a space, is what she said earlier.

int count = Integer.parseInt(JOptionPane.showInputDialog(null, "Enter any Number:"));
		// this calculation of base is based on the assumption that count
		// is an odd value
		int base = count * 2;
		// if count is an even value, we'll have to subtract one.
		if (count % 2 == 0)
			base -= 1;

int base=count*2

sir why do i have to multiply count by 2?

Check my edited code to add in what i think you wanted:

int n=10;
        int numtmp=n;
     for (int i = 1; i < n; i +=2) {
            for (int j = 0; j < n-i / 2; j++)
                if(numtmp>=10)//handles double digit vals
                System.out.print("   ");//3spaces for double digit
                else 
                System.out.print("  ");//2spaces for single digit
            for (int j = 0; j < i; j++)
                System.out.print(n+" ");//adds blank space between
            System.out.print("\n");
        }
int count = Integer.parseInt(JOptionPane.showInputDialog(null, "Enter any Number:"));
		// this calculation of base is based on the assumption that count
		// is an odd value
		int base = count * 2;
		// if count is an even value, we'll have to subtract one.
		if (count % 2 == 0)
			base -= 1;

int base=count*2

sir why do i have to multiply count by 2?

let's say that count = 5
"5_"
your 5 and your space, that are two characters, which each take space in your line.
hence, the 2
you will print this five (with the space) five times after each other, that's why you get the complete length by multiplying them.

but, as I said, I only look at numbers with only one digit.

Check my eidted code to add in what i think you wanted:

int n=7;
        int numtmp=n;
     for (int i = 1; i < n; i +=2) {
            for (int j = 0; j < n-1-i / 2; j++)
                if(numtmp>=10)
                System.out.print("  ");//2spaces for double digit
                else 
                System.out.print(" ");//1spaces for single digit
            for (int j = 0; j < i; j++)
                System.out.print(n);
            System.out.print("\n");
        }

here:

for (int i = 1; i < n; i +=2) {
	            for (int j = 0; j < n-1-i / 2; j++)

why do i have to minus "n-1-i" on each other..what does that do?? and also divide it by 2?

let's say that count = 5
"5_"
your 5 and your space, that are two characters, which each take space in your line.
hence, the 2
you will print this five (with the space) five times after each other, that's why you get the complete length by multiplying them.

but, as I said, I only look at numbers with only one digit.

ok ok now i understand!!! so what i have to do..is to figure out how it will work on 2 digit numbers..

if a single digit needs one space and that u decided to multiply it by 2..
does that mean. that i need to do an if else

(if count > 9)
int base=count*3
??

is that right?

because 2 digits are two and i need a sing space.. that makes it 3?

here:

for (int i = 1; i < n; i +=2) {
	            for (int j = 0; j < n-1-i / 2; j++)

why do i have to minus "n-1-i" on each other..what does that do?? and also divide it by 2?

lol sorry you can take away the -1 dont know why that was there! you can just have:n-i / 2

ok ok now i understand!!! so what i have to do..is to figure out how it will work on 2 digit numbers..

if a single digit needs one space and that u decided to multiply it by 2..
does that mean. that i need to do an if else

(if count > 9)
int base=count*3
??

is that right?

because 2 digits are two and i need a sing space.. that makes it 3?

well sort off. the tricky part will be to calculate how to change the one digit lines so they will be in line with the two (or three) digit lines, because you'll have them both, and they can't use the same calculations for everything.

one way to go, is not only taking in account the length of the baseline, but also the length of the last number, and as such, format the printing of the nrLine with additional spaces, but I don't think you'll have to line them up above each other, instead of getting them perfect in the middle.

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.