Computer Applications
Write a program using do-while loop to compute the sum of the first 50 positive odd integers.
Java
Java Iterative Stmts
11 Likes
Answer
public class KboatSumOdd
{
public static void main(String args[]) {
long sumOdd = 0;
for (int i = 1; i <= 50; i++)
if (i % 2 != 0)
sumOdd += i;
System.out.println("Sum of 50 odd positive numbers = " + sumOdd);
}
}
Output
Answered By
4 Likes
Related Questions
Write a program to calculate the value of Pi with the help of the following series:
Pi = (4/1) - (4/3) + (4/5) - (4/7) + (4/9) - (4/11) + (4/13) - (4/15) …
Hint: Use while loop with 100000 iterations.
How many times are the following loop bodies repeated? What is the final output in each case?
int z = 1; while (z < 10) if((z++) % 2 == 0) System.out.println(z);
Write a program to read the number n via the Scanner class and print the Tribonacci series:
0, 0, 1, 1, 2, 4, 7, 13, 24, 44, 81 …and so on.
Hint: The Tribonacci series is a generalisation of the Fibonacci sequence where each term is the sum of the three preceding terms.
Write a program to accept n number of input integers and find out:
i. Number of positive numbers
ii. Number of negative numbers
iii. Sum of positive numbers