Sunday, January 11, 2026

Java Reverse Increasing Half Triangle pattern

//Solution I

 import java.util.Scanner;


class ReverseIncreasingHalfTriangle

{

static void reverseIncreasingHalfTriangle()

{

int height = 10;

//count the number of odd numbers in the string.

int odds =0;

String num = "";

//Run the loop for half of height because only half are odd numbers.

for(int i = 0; i<=height/2; i++)

{

odds =0;

num = "";

for(int j = height ; j >= 1 ; j --)

{

//Check if j is odd, if yes append it to the string. 

if(j%2 != 0)

{

odds++;


num = num + ""+j+"";

}

//If number of odds in the string is equal to the row number, exit the inner for loop.

if(odds == i)

{

break;

}

}

//Reverse and print the number string.

System.out.println(new StringBuilder(num).reverse().toString());

}

}

public static void main(String[] args)

{

reverseIncreasingHalfTriangle();

}

}


//Solution II

class ReverseIncreasingHalfTriangleNew

{

static void reverseIncreasingHalfTriangleNew()

{

for(int i = 9; i>=1; i -=2)

{

//Starting j from current value of i, and increasing maximum up to  9.

for(int j = i; j <= 9; j += 2)

{

System.out.print(j);

}

System.out.println();

}

}

public static void main(String[] args)

{

reverseIncreasingHalfTriangleNew();

}

}

Output:



No comments:

Post a Comment

Maturity value at Compound Interest