KnowledgeBoat Logo

Computer Applications

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

Java

Java Iterative Stmts

4 Likes

Answer

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 Write a program using do-while loop to compute the sum of the first 500 positive odd integers.

Answered By

2 Likes


Related Questions