+ Start a Discussion
apoorva pandeyapoorva pandey 

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

Best Answer chosen by apoorva pandey
Purushotham YellankiPurushotham 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 YellankiPurushotham 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 pandeyapoorva pandey
Thank you Purushotham. This works!