{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

InClass0924

# InClass0924 - sum Problem 8 p.229 Write a loop that will...

This preview shows page 1. Sign up to view the full content.

/* * In-class assignment 09/24, COMP 110 * Name: Xin Chi * Section: 004 */ import java.util.Scanner; public class InClass0924 { public static void main(String[] args) { /* Problem 4, p.229: Write a fragment of code that will compute the sum of the first n POSITIVE ODD integers (Store the result in the variable sum) */ System.out.print("Enter a number: "); Scanner keyboard = new Scanner(System.in); keyboard.useDelimiter(System.getProperty("line.separator")); int n = keyboard.nextInt(); int sum = 0; int count = 1; for (count = 1; count <= n; count = count + 1) { sum = sum + (2*count - 1); } System.out.println("The sum of the first " + n + " positive odd integers is: " +
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: sum); } /* Problem 8, p.229: Write a loop that will count the number of blank characters in a given string (Store the number of blanks in the variable numBlanks) */ System.out.print("Enter a string: "); String myString = keyboard.next(); int numBlanks = 0; /* Problem 8: Put your code here */ /* Note: myString.length() gives the number of characters in myString myString.charAt(i) gives the character at index i of myString */ System.out.println("The number of blank characters in that string was " + numBlanks); } }...
View Full Document

{[ snackBarMessage ]}

Ask a homework question - tutors are online