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.
+ Start a Discussion
Anoop Kumar 31Anoop Kumar 31 

i want to print pattern just like

public class Program1 
{
    public void print()
    {
         integer i, j;
            for (i = 1; i <= 5; i++)
            {
                for (j = 1; j <= i; j++)
                {
                    system.debug(i);
                }
                 
            }
    }
}

i am getting output but not in this shape User-added image
1
2 2
3 3 3
4 4 4 4
5 5 5 5 5 
 
Harish DHarish D
Hi Anoop,

Try this:

public class Program1 
{
    public void print()
    {
            integer i, j;
            for (i = 1; i <= 5; i++)
            {
                string output ='';
                for (j = 1; j <= i; j++)
                {
                    output = output+i;
                }
                 system.debug(output);
            }
    }
}
Ajay K DubediAjay K Dubedi
Hello Anoop,

In apex language system.debug()  function by default create a new line every time. If you don't want a new line to use string and concatenate your output with this string.
See this Example as your requirement.

public class pattern {
    public static void printPattern(){
        for(integer i=1;i<=5;i++){
            string ptn='';
            for(integer j=1;j<=i;j++){
                ptn=ptn+i;
            }
        system.debug(ptn);
        }
    }
}




Hope this will help you.

Thanks 
Ajay Dubedi