In: Computer Science
In C:
Write a complete program that performs the following task:
Ask the user for the number of sequences to display.
For each sequence,
Ask the user for a starting value
Print out the value and double it (multiply by 2). Continue printing and doubling (all on the same line, separated by one space each) as long as the current number is less than 1000, or until 8 numbers have been printed on the line.
You may assume that the user will enter a positive integer less than 1000 for all inputs.
public class OddEvenSum { // Save as "OddEvenSum.java" public static void main(String[] args) { int lowerbound = 1, upperbound = 1000; int sumOdd = 0; // For accumulating odd numbers, init to 0 int sumEven = 0; // For accumulating even numbers, init to 0 int number = lowerbound; while (number <= upperbound) { if (number % 2 == 0) { // Even sumEven += number; // Same as sumEven = sumEven + number } else { // Odd sumOdd += number; // Same as sumOdd = sumOdd + number } ++number; // Next number } // Print the result System.out.println("The sum of odd numbers from " + lowerbound + " to " + upperbound + " is " + sumOdd); System.out.println("The sum of even numbers from " + lowerbound + " to " + upperbound + " is " + sumEven); System.out.println("The difference between the two sums is " + (sumOdd - sumEven)); } }