created 08/04/99; revised 07/20/2017
Write a program that asks the user for a starting value and an ending value and then writes all the integers (inclusive) between those two values.
Enter Start: 5 Enter End: 9 5 6 7 8 9
Click here to go back to the main menu.
Write a program that asks the user for N and then sum all the integers (inclusive) between 1 and N.
Enter N: 10 Sum = 55
Do this in a loop that counts up 1 to N and in each iteration adds the current count to the sum.
Another way to calculate the same thing is through a formula:
sum = (n*(n+1))/2
Modify your program so that it calculates the sum both ways:
Enter N: 10 Loop Sum = 55 Formula Sum = 55
You might worry that the formula will lead to integer division problems. But it works as is. Why? (Hint: think about even and odd.)
The sum of sequential integers 1 to N is called a triangle number. See the exercises for chapter 25.
Click here to go back to the main menu.
Write a program that asks the user for two integers, low and high and then sums all the integers (inclusive) low through N.
Enter low: 5 Enter high: 10 Sum = 45
Do this by noticing that the sum from low to high is the same as the sum from one to high minus the sum from one to (low-1). Calculate each of these by using the formula:
sum = (n*(n+1))/2
Click here to go back to the main menu.
Write a program that asks the user to enter a word. The program will then repeat word for as many times as it has characters:
Enter a word: Hello Hello Hello Hello Hello Hello
To do this you will need to use the length()
method of String
that counts
the number of characters in a string:
String inputString; int times; . . . . times = inputString.length()
Click here to go back to the main menu.
Write a program that asks the user to enter two words. The program then prints out both words on one line. The words will be separated by enough dots so that the total line length is 30:
Enter first word: turtle Enter second word 153 turtle....................153
This could be used as part of an index for a book.
To print out the dots, use System.out.print(".")
inside a loop body.
Click here to go back to the main menu.
Write a program that asks the user to enter a word. The program then writes that word one character per line:
Enter a word: turtle t u r t l e
Use the length()
method that counts
the number of characters in a string
and the charAt( int index )
that returns the character at index
.
Recall that String
indexes start at zero.
Click here to go back to the main menu.
End of exercises.