Program:
Write the program to generate following triangle.
1
1 3
1 3 5
1 3 5 7
Code:
package com.ranga;
class TriangleDemo
{
public static void main (String[] args)
{
int temp = 0;
for(int i=0; i< 4; i++) {
for(int j = 0; j<=i; j++) {
if(j == 0) {
temp = j + 1;
} else {
temp = temp + 2;
}
System.out.print(temp+" ");
}
System.out.println();
}
}
}
Output:
Write the program to generate following triangle.
1
1 3
1 3 5
1 3 5 7
Code:
package com.ranga;
class TriangleDemo
{
public static void main (String[] args)
{
int temp = 0;
for(int i=0; i< 4; i++) {
for(int j = 0; j<=i; j++) {
if(j == 0) {
temp = j + 1;
} else {
temp = temp + 2;
}
System.out.print(temp+" ");
}
System.out.println();
}
}
}
Output:
1
1 3
1 3 5
1 3 5 7
You can also see live example in ideone.
http://ideone.com/ie8lmD
0 comments:
Post a Comment