Unformatted text preview:

Example 1 Sum the numbers from 1 to 10 Suppose we want to calculate the sum of the numbers from 1 to 10 We could do the following sum 1 2 3 4 5 6 7 8 9 10 or we could recognize that this is really the set of steps 1 sum 1 2 sum sum 2 3 sum sum 3 4 sum sum 4 5 sum sum 5 6 sum sum 6 7 sum sum 7 8 sum sum 8 9 sum sum 9 10 sum sum 10 So to write our program we will need 1 A variable that will be used to accumulate our sum called sum 2 A variable that will represent the set of numbers that will be added to sum 1 2 3 4 5 which we will call x 3 Both should be integers 4 Our loop will exit when x is greater than 10 We will loop while x 10 public class Sumto10 public static void main String args int sum x sum 0 x 1 This prints while x 10 sum sum x x x 1 The sum of the numbers from 1 to 10 is 55 System out println The sum of the numbers from 1 to 10 is sum Example 2 What if we needed to do a more generalized type of calculation Instead of just the numbers from 1 to 10 but sum the numbers from 1 to N where N is a number input by the user The steps are the same sum sum 1 sum sum 2 sum sum n We would do the following alterations to our program 1 We would create a new variable n and prompt the user to input an integer value for n 2 We would loop as long as x n public class Sumton public static void main String args int sum x n System out println Please enter an integer number N and we will calculate the sum from 1 to N n int ReadItem getLong sum 0 x 1 while x n sum sum x x x 1 System out println The sum of the numbers from 1 to n is sum home hayhurst java java Sumton Please enter an integer number N and we will calculate the sum from 1 to N 20 PRE TEST LOOPS The sum of the numbers from 1 to 20 is 210 Example 3 As another example lets take the calculator program we created in a previous class using a switch statement and modify it so that it can allow the user to enter multiple calculations Originally it was implemented as import java util public class switchEx2 public static void main String args Scanner in new Scanner System in int a b char ch String input System out println Do you want to Add Subtract Multiply or Divide System out print Please enter the first letter of what you would like to do ch in next toUpperCase charAt 0 System out println Enter first number a in nextInt System out println Enter second number b in nextInt switch ch case A System out println Answer is a b break case S System out println Answer is a b break case M System out println Answer is a b break case D if b 0 System out println Answer is a b else System out println Division by zero break default System out println Invalid Operation end of switch System out println Good bye As originally implemented it only allowed to user to enter a single calculation to change to multiple our program will need to be able to Input the desired operation multiple times Input 2 integers for each operation Determine the operation entered and perform the calculation for each operation and print the result We will need to allow the user to enter something special to indicate when they are finished performing calculations Perhaps Q The steps of our program will look like 1 Prompt for and get the first operation from the user 2 If the operation entered is q exit the program and do nothing 3 If the operation is not q then enter the loop a Prompt for the 2 integers to be used in the calculation b Identify the operation entered and perform the calculation c Print the results d Prompt for and get the NEXT operation e Return to step 2 import java util public class switchEx2 public static void main String args Scanner in new Scanner System in int a b char ch String input System out println Do you want to Add Subtract Multiply or Divide System out print Please enter the first letter of what you would like to do or Q to quit 1 ch in next toUpperCase charAt 0 read the input while ch Q System out println Enter first number a in nextInt System out println Enter second number b in nextInt 2 3 switch ch case A 4 System out println Answer is a b break case S System out println Answer is a b break case M System out println Answer is a b break case D if b 0 System out println Answer is a b else System out println Division by zero break default System out println Invalid Operation end of switch 5 System out println Do you want to Add Subtract Multiply or Divide System out print Please enter the first letter of what you would like to do or Q to quit ch in next toUpperCase charAt 0 read the input end of the loop body System out println Good bye 1 2 3 4 5 Get the initial operation to perform Loop while the operation is not quit Get the numbers used in the calculation Perform the calculation Get the next operation and return to the top of the loop Example 4 Sum the numbers from 1 to 10 public class Sumto10 public static void main String args int sum x sum 0 x 1 do sum sum x x x 1 while x 10 System out println The sum of the numbers from 1 to 10 is sum home hayhurst java java Sumto10 The sum of the numbers from 1 to 10 is 55 Example 5 public class Sumto10 public static void main String args int sum x sum 0 x 20 do sum sum x x x 1 while x 10 System out println The sum of the numbers from 1 to 10 is sum home hayhurst java java Sumto10 The sum of the numbers from 1 to 10 is 20 Example 6 For loops Sum the numbers from 1 to n public class Sumto10for public static void main String args int sum x sum 0 for x 1 x 10 x sum sum x System out println The sum of the numbers from 1 to 10 is sum home hayhurst java java Sumto10for The sum of the numbers from 1 to 10 is 55 Example 7 public class Sumto10for public static void main String args int sum x 1 sum 0 for x 10 x sum sum x System out println The sum of the numbers from 1 to …


View Full Document

WVU CS 110 - Sum the numbers from 1 to 10

Loading Unlocking...
Login

Join to view Sum the numbers from 1 to 10 and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Sum the numbers from 1 to 10 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?