This is will show you to compare of all three types of loops (while,do-while and for) and print a triangle pattern.
import java.util.*;
public class star
{
static void sop(String ar)
{
System.out.println(ar);
}
public static void main(String args[])
{
int i,j,k;
sop("Enter triangle size");
i=sc.nextInt();
/*for(j=1;j<=i;j++)
{
for(k=1;k<=j;k++)
System.out.print(k);
sop("\n");
}*/
/*
j=1;
while(j<=i)
{
k=1;
while(k<=j)
{
System.out.print("*");
k++;
}
sop("");
j++;
}
*/
j=1;
do
{
k=1;
do
{
System.out.print("*");
k++;
}while(k<=j);
sop("");
j++;
}while(j<=i);
}
}
No comments:
Post a Comment