Page 166 - CA_Blue( J )_Class10
P. 166

for (initialization; condition for testing; increment or decrement)
                            {
                                 // job performed by the body of the loop;
                            }





                            initialization;
                            while (condition for testing)
                                   {
                                       // job performed by the body of the loop;
                                       increment or decrement;
                                   }




                            initialization;
                            do
                            {
                                // job performed by the body of the loop;
                                increment or decrement;
                            } while (condition for testing);


                Program 5    To input a number and print the factorial of the number using for loop.


                 1  import java.util.*;

                 2  class for_loop
                 3  {

                 4      public static void main()

                 5      {
                 6          Scanner sc= new Scanner(System.in);

                 7          int i,n,f=1;
                 8          System.out.print("Enter a number : ");

                 9          n=sc.nextInt();

                10          for(i=1;i<=n;i++)
                11              f=f*i;

                12          System.out.println("Factorial of a number : "+f);
                13      }}












                164164  Touchpad Computer Applications-X
   161   162   163   164   165   166   167   168   169   170   171