function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
apoorva pandey

Can we write apex code to show reverse triangle (pattern)?

Best Answer chosen by apoorva pandey
Purushotham Yellanki
Hi Apoorva,

Try below Code in you Dev Console and see if this is what you are looking for. Basically reverse triangle logic is same in almost all OOP languages, it doesn't matter if it is Java or Apex all we are going to use is for loops!

String revTriangle = '';
Integer x = 1;
Integer y = 1;
for (x = 1; x <= 10; x++)
{
for (y = x; y <= 10; y++)
{
if (y<=x)
revTriangle +='*';
else
revTriangle+='*';
}
revTriangle+='\n';
}

System.debug('ReverserTriangle: \n' + revTriangle);

Thank you

All Answers

Purushotham Yellanki
Hi Apoorva,

Try below Code in you Dev Console and see if this is what you are looking for. Basically reverse triangle logic is same in almost all OOP languages, it doesn't matter if it is Java or Apex all we are going to use is for loops!

String revTriangle = '';
Integer x = 1;
Integer y = 1;
for (x = 1; x <= 10; x++)
{
for (y = x; y <= 10; y++)
{
if (y<=x)
revTriangle +='*';
else
revTriangle+='*';
}
revTriangle+='\n';
}

System.debug('ReverserTriangle: \n' + revTriangle);

Thank you
This was selected as the best answer
apoorva pandey
Thank you Purushotham. This works!