Page 204 - CA_Blue( J )_Class9
P. 204

Step Loop: Here the increment or decrement operator changes by more than one.
                  For Example:
                                              Print all the multiples of 5 up to 100.
                                                  for (i=5; i<=100; i=i+5)
                                                 {
                                                     System.out.println(i);
                                                 }

                  Examples of programs based on loops:

                   Program 5      Write a program to input a number and print the number is composite number or not.

                     1     import java.util.*;

                     2     class composite_number
                     3     {

                     4         public static void main ()
                     5         {

                     6             Scanner sc= new Scanner (System.in);
                     7             int n,i,count=0;
                     8             System.out.print("Enter a number: ");

                     9             n=sc.nextInt();

                    10             for (i=1; i<=n;i++)
                    11             {
                    12                 if(n%i==0)

                    13                 {
                    14                     count++;

                    15                 }
                    16             }

                    17             if(count>2)
                    18                 System.out.println(n+" is Composite Number");

                    19             else
                    20                 System.out.println(n+" is not Composite Number");

                    21         }
                    22     }
                  You will get the following output:












                   202    Touchpad Computer Applications-IX
   199   200   201   202   203   204   205   206   207   208   209