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
Answered By
2 Likes
Related Questions
Write a program to input n number of integers and find out:
i. Number of positive integers
ii. Number of negative integers
iii. Sum of positive numbers
iv. Product of negative numbers
v. Average of positive numbers
Write three different programs using for, while, and do-while loops to find the product of series 3, 9, 12,… 30.
Write a program to convert kilograms to pounds in the following tabular format (1 kilogram is 2.2 pounds):
Kilograms Pounds 1 2.2 2 4.4 20 44.0 What is the output produced by the following code?
int num = 10; while (num > 0) { num = num - 2; if (num == 2) continue; System.out.println(num); } System.out.println("Finished");