InClass0924

InClass0924 - sum); } /* Problem 8, p.229: Write a loop...

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

View Full Document Right Arrow Icon
/* * 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: " +
Background image of page 1
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(&quot;Enter a string: &quot;); 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(&quot;The number of blank characters in that string was &quot; + numBlanks); } }...
View Full Document

Ask a homework question - tutors are online