ICSE Class 10 Computer Applications Question 16 of 69

Iterative Constructs in Java — Question 16

Back to all questions
16
Question

Question 12

Write a program using do-while loop to compute the sum of the first 500 positive odd integers.

public class KboatSumOdd
{
    public static void main(String args[]) {
        long sumOdd = 0;  
        int num = 1;
        do  {
            sumOdd += num;
            num += 2;
        }while(num <= 500);
                    
        System.out.println("Sum of 500 odd positive numbers = "
                            + sumOdd);        
    }
}
Output
BlueJ output of KboatSumOdd.java
Answer