Thursday, December 5, 2013

Loops – Example



    
//Print First 10 Natural No. using for()
    public class For_loop_Demo
        {
            public static void main(String args[])
                {
                    for(int i=1;i<=10;i++)
                        {
                            System.out.print(i+"  ");
                        }
                }
         }




    //Print First 10 Natural No. in reverse order using while()
    public class While_loop_Demo
        {
            public static void main(String args[])
                {
                    int i=11;
                    while(i-->1)
                        {
                            System.out.print(i+"  ");
                        }
                }
         }




    //Print First 20 even No using do-while()
    public class do_while_loop_Demo
        {
            public static void main(String args[])
                {
                    int i=2;
                    do
                        {
                            System.out.print(i+"  ");
                            i+=2;
                        }while(i<20)
                }
         }

No comments: