You need to sign in to do that
Don't have an account?
Anoop 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
1
2 2
3 3 3
4 4 4 4
5 5 5 5 5
{
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
1
2 2
3 3 3
4 4 4 4
5 5 5 5 5
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);
}
}
}
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