Saturday 26 January 2019

Compare of core java nested loop

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;
       Scanner sc=new Scanner(System.in);
       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);
    }
}

Next Question

No comments:

Post a Comment