Page 90 - Trackpad_ipro 4.1_Class8
P. 90

}
                      }
                  }

                  In the preceding program, we need to provide the value of variable N from runtime.

                  Program 2: Write a Java program to print the sum of all the odd numbers up to 10 by using the
                  'for' loop.
                  public class Program2
                  {
                     public static void main(String args[])
                     {
                          int i, sum=0;
                         for(i=1; i<=10; i++)
                       {
                              if(i%2 != 0)
                              {
                                   sum+=i;
                              }
                       }
                       System.out.println("The sum of all the odd numbers up to 10 is: " + sum);
                     }
                  }













                  Program 3: Write a Java program to calculate the factorial of an entered number. The number
                  should be taken as input from the user.

                  public class Program3
                  {
                      public static void main(int number)
                      {
                          int i,fact=1;
                          for(i=1;i<=number;i++)
                          {
                              fact=fact*i;
                          }
                          System.out.println("Factorial of "+number+" is: "+ fact);
                   }
                  }





                    88     iPro (Ver. 4.1)-VIII
   85   86   87   88   89   90   91   92   93   94   95