Computer Science, asked by ltata1549, 1 month ago

1. WAP in java to display all odd numbers and their sum in the range 50 to 100 (java)

Answers

Answered by anindyaadhikari13
7

\textsf{\large{\underline{Solution}:}}

The given problem is solved using language - Java.

public class Java{

   public static void main(String s[]){

       int i,sum=0;

       System.out.println("Odd numbers in the range 50 to 100 are as follows...");

       for(i=51;i<=100;sum+=i,i+=2)

           System.out.print(i+" ");

       System.out.println("\nSum of the numbers: "+sum);

   }

}

\textsf{\large{\underline{Logic}:}}

  1. Initialize sum = 0.
  2. Repeat i = 51 to 100.
  3. Add 'i' to sum.
  4. Display the value of 'i'.
  5. Increment the value of 'i' by 2.
  6. If i > 100, terminate the loop.
  7. Display the 'sum'.

See attachment for output.

Attachments:
Similar questions
Math, 1 month ago