Question

This is an exercise using a while loop Input an integer n from the keyboard. Calculate...

This is an exercise using a while loop Input an integer n from the keyboard. Calculate the sum of the first n integers. So if n=10, the answer should be 55 (because 1+2+3+4+5+6+7+8+9+10=55)

Code language: Java

Homework Answers

Answer #1

Java program to find the sum of first n numbers using while loop:
Step-1: Declare and initialize the required variables- n, sum=0, i=1.
Step-2: Ask the user to enter a integer number and store it in the variable n.
Step-3: Use the while loop to check if i (i.e. 1) is less than or equal to n,
3.1: If true, then add the value of i to sum, and increment the value of i by 1, then again check the condition i<=n, if true repeat the step-3.1.
3.2: Once it becomes false, then come out of the while loop and execute the next statement following the while loop i.e. the print the sum statement.

The complete program is shown below:

import java.util.Scanner;
public class Main
{
   public static void main(String[] args)
   {
   int n, i=1, sum=0;
       System.out.print("Enter the integer: ");
       Scanner scan=new Scanner(System.in);
       n= scan.nextInt();
       while(i<=n)
       {
       sum=sum+i;
       i++;
       }
       System.out.println("Sum of the first " + n + " numbers is: " +sum);
   }
}

The screenshot of the complete program along with the output and explanation is shown below:

Output-1:

Output-2:

Know the answer?
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
Similar Questions
Ask user to input any 5-digit positive integer value. Calculate the sum of digits of that...
Ask user to input any 5-digit positive integer value. Calculate the sum of digits of that number. Assume that the number is positive, integer, 5-digit. Example: 29107 should calculate 2+9+1+0+7 and get 19 Hint: Use the % operator to extract a digit from a number. Use loop(s) Must be in Java
Step 2 Exercise - Using while loop:    Write a Java program (using while loop) to display...
Step 2 Exercise - Using while loop:    Write a Java program (using while loop) to display 5 lines of smiley faces. The following would be the output of your program: Note: Sample program is in the "Important Documents", Module 6 folder) Hello:                          Bye!
1) Write a java programming using a while loop where you will add numbers and when...
1) Write a java programming using a while loop where you will add numbers and when you press number 0 it will add all your numbers and display the results. Use Scanner object. Steps: 1) Declare variables integer called sum which is equal to zero   2) Declare Scanner object   3) Declare while condition where is true   4) Inside of that condition prompt the user to enter the numbers   5) Declare variable integer called number and input the number statement   6)...
Java Code: Console IO Practice Exercise The purpose of this exercise is to practice console input...
Java Code: Console IO Practice Exercise The purpose of this exercise is to practice console input and output with the Java console. Setup: Create a class called ConsolePractice with a main method. Create a static field in your class that stores a scanner object. You will use this scanner for all user input. private static Scanner scanner = new Scanner(System.in); Part A: Create a static method called “divide”. Your method should do the following: Accept two integers as parameters, a...
IN C LANGUAGE This program initially reads two integers from standard input: (1) an integer T...
IN C LANGUAGE This program initially reads two integers from standard input: (1) an integer T and (2) a positive integer N. It then reads N integers and counts the numbers that are greater than T and the numbers than are less than T. It then prints out these two counts. Example What is the threshold value? 7 How many values? 5 Enter 5 values: 6 7 9 9 8 3 values are greater than 7 1 values are less...
The Java program should sum the odd integers from 1 to the integer that the user...
The Java program should sum the odd integers from 1 to the integer that the user enters. For example, if the user enters 30, the program should sum the odd integers from 1 to 30.   Print "Please enter a positive nonzero integer." Declare a Scanner and obtain an integer from the user.   Use a Do...while statement and calculate the sum Use the integer that the obtained from the previous step for the loop-continuation condition. Display the sum Print the sum...
Please code in Java and please implement constarints Digital Root and Iterations Given a non-negative integer,...
Please code in Java and please implement constarints Digital Root and Iterations Given a non-negative integer, print out its digital root and the number of iterations required to reach it. The digital root is the single digit number obtained by an iterative process of finding the sum of digits. In the next iteration, the sum of the digits in the previous iteration is computed, and the process repeated until a single digit value is obtained. Input Format The first line...
4.2.2 Basic while loop with user input. JAVA Write an expression that executes the loop while...
4.2.2 Basic while loop with user input. JAVA Write an expression that executes the loop while the user enters a number greater than or equal to 0. Note: These activities may test code with different test values. This activity will perform three tests, with user input of 9, 5, 2, -1, then with user input of 0, -17, then with user input of 0 1 0 -1. See "How to Use zyBooks". Also note: If the submitted code has an...
USING PYTHON do all the he problems using while loop , continue and break 1-This problem...
USING PYTHON do all the he problems using while loop , continue and break 1-This problem provides practice using a while True loop.write a function named twoWords that gets and returns two words from a user. The first word is of a specified length, and the second word begins with a specified letter.The function twoWords takes two parameters: an integer, length, that is the length of the first word and a character, firstLetter, that is the first letter of the...
Using a while loop, write a program that reads 10 integer numbers. The program shall count...
Using a while loop, write a program that reads 10 integer numbers. The program shall count how many of them are multiples of 2, how many are multiples of 3, and how many are NOT multiples of either 2 or 3. The output should be similar to the one shown below. c++ language
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT