Monday, 30 October 2017

java full important program



Problem: 1.6
                            Write a program that displays the result of
             1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9.
Discripition:
                 Their are two ways of solutions simple way and by using formula

Solution 01:
                     public class Message {

    public static void main(String[] args) {
         
             System.out.println("The result is "+(1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9));
    }
   
}



Solution 2:
                public class Message {

                public static void main(String[] args) {
          int sum=0;
          sum=9*(9+1)/2;
             System.out.println("The result is "+sum);
    }
   
}

 OutPut 

            

No comments:

Post a Comment