Question

design a program using a while loop that displays the odd numbers from 1 to 30....

design a program using a while loop that displays the odd numbers from 1 to 30. Display the sum of all odd numbers at the end.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

I am Creating C++ Program

#include<iostream>
using namespace std;

int main()
{
   int sum=0; // Declare Integer variable sum=0
   int i=1; // Declare Integer variable i=1
  
   while(i<=30) // Create while loop until 30
   {
       cout<<i<<" "; // display odd numbers i
       sum+=i; // calculate sum of odd numbers
       i+=2; // increment i value by 2
   }
   cout<<"\nSum of Odd Numbers: "<<sum; // display sum of odd numbers
}

OUTPUT:

1 3 5 7 9 11 13 15 17 19 21 23 25 27 29
Sum of Odd Numbers: 225

I am Creating JAVA Program

class DisplayOdd // Create Main class DisplayOdd
{
public static void main(String [] args)
{
int i=1; // Declare Integer variable i=1
int sum=0; // Declare Integer vairable sum=0

while(i<=30) // Create while loop until 30 elements
{
System.out.print(i+" "); // display i value
sum+=i; // calculate sum of odd numbers i
i+=2; // increment i by 2
}
System.out.println("\nSum of Odd Numbers: "+sum); // display sum of odd numbers
}
}

OUTPUT


F:\>javac DisplayOdd.java

F:\>java DisplayOdd
1 3 5 7 9 11 13 15 17 19 21 23 25 27 29
Sum of Odd Numbers: 225

Add a comment
Know the answer?
Add Answer to:
design a program using a while loop that displays the odd numbers from 1 to 30....
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • english 4. Design a for loop that would add all the odd numbers starting from 1...

    english 4. Design a for loop that would add all the odd numbers starting from 1 to 1001 and store in the variable sum (10 points) 5. [Programming Exercise) (20 POINTS) Write a Program that will use a function named Grade which will take three inputs: homeworks, midterm and final and compute average using 20% of homeworks, 30% from midterm and 50% from final. Then it will compute the letter Grade by using the following: If the average is greater...

  • Design the logic for a program that allows a user to enter 10 numbers, then displays...

    Design the logic for a program that allows a user to enter 10 numbers, then displays all of the numbers, the sum and average of the numbers. (Use array and loop.) the pseudocode in python please

  • In java, write a program that gets 10 integer numbers from the user using user input,...

    In java, write a program that gets 10 integer numbers from the user using user input, and then calculates and display the sum of the numbers that have been read.   Program Requirements: Write the program in three versions with three loops. Put all three loops in the main method of your source code. version1:  use a while loop. version2:  use a do-while loop. version 3:  use a for loop. For each version, use a loop to input 10 int numbers from the user...

  • In java, write a program that gets 10 integer numbers from the user using user input,...

    In java, write a program that gets 10 integer numbers from the user using user input, and then calculates and display the sum of the numbers that have been read.   Program Requirements: Write the program in three versions with three loops. Put all three loops in the main method of your source code. The program must be in one file. version1:  use a while loop. version2:  use a do-while loop. version 3:  use a for loop. For each version, use a loop to...

  • Design a For loop that displays the following set of numbers: 0, 10, 20, 30, 40,...

    Design a For loop that displays the following set of numbers: 0, 10, 20, 30, 40, 50,..., 1000

  • in c++ language 1.Re-write the following for loop statement by using a while loop statement: int...

    in c++ language 1.Re-write the following for loop statement by using a while loop statement: int sum = 0; for(int i=0;i<=1000;i++){                 sum+=i; } 1 (b). Following is the main () function of a program. The program asks a user to enter 150 integers and print the largest integer user entered. int main() {    int score, highest;             //missing portion of the program             return 0;    } 1(c). Find the missing portion of the following code, so the...

  • Please write a java program that inputs positive numbers using a while loop until a negative...

    Please write a java program that inputs positive numbers using a while loop until a negative number is input and finds the sum of the numbers input

  • Design a Do-While loop that asks the user to enter two numbers. The numbers should be...

    Design a Do-While loop that asks the user to enter two numbers. The numbers should be added and the sum displayed. The loop should ask the user whether he or she wishes to perform the operation again. If so, the loop should repeat; otherwise it should terminate. using visual basic

  • 1. Write a program that reads a sequence of numbers from the keyboard, and displays the...

    1. Write a program that reads a sequence of numbers from the keyboard, and displays the smallest number. The sequence has at least one number and is terminated by -999(-999 will not appear in the sequence other than as the end marker). 2. Write a program which requests an integer input n from the keyboard and computes the sum of square of k for all k from 1 to n(inclusive). use java

  • In Java, write a program using a loop that asks the user to enter a series...

    In Java, write a program using a loop that asks the user to enter a series of decimal numbers. The user must enter -88 to end the input of the decimal numbers. After the user enters all numbers, the program should display the sum of all numbers entered.

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT