DOC PREVIEW
WVU CS 110 - Sum the numbers from 1 to 10

This preview shows page 1-2-3 out of 10 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 10 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 10 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 10 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 10 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

Example 1: Sum the numbers from 1 to 10Suppose 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 = 12. sum = sum + 23. sum = sum + 34. sum = sum + 45. sum = sum + 56. sum = sum + 67. sum = sum + 78. sum = sum + 89. sum = sum + 910. sum = sum + 10So 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 < = 10public class Sumto10 {public static void main (String args[]) {int sum, x;sum = 0;x = 1;while ( x <= 10) {sum = sum + x;x = x + 1;}System.out.println(“The sum of the numbers from 1 to 10 is: “ + sum);}}This prints:The sum of the numbers from 1 to 10 is: 55Example 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 + nWe would do the following alterations to our program:1. We would create a new variable “n”, and prompt the user to input an integer valuefor 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);}}PRE-TEST LOOPS:/home/hayhurst/java- java SumtonPlease enter an integer number, N, and we will calculate the sum from 1 to N20The sum of the numbers from 1 to 20 is: 210Example 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 switchSystem.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 calculationb. Identify the operation entered, and perform the calculationc. Print the resultsd. 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"); 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(); 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 ("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 bodySystem.out.println("Good bye"); }}123451. Get the initial operation to perform2. Loop while the operation is not quit3. Get the numbers used in the calculation4. Perform the calculation5. Get the next operation and return to the top of the loopExample 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); }}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 Sumto10The sum of the numbers from 1 to 10 is: 55/home/hayhurst/java- java Sumto10The sum of the numbers from 1 to 10 is: 20Example #6: For


View Full Document

WVU CS 110 - Sum the numbers from 1 to 10

Download Sum the numbers from 1 to 10
Our administrator received your request to download this document. We will send you the file to your email shortly.
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 2 2 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?