0% found this document useful (0 votes)
4 views

Java Exercises Loops

Hehehdgyehheudhr

Uploaded by

grmpp9m4h9
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Java Exercises Loops

Hehehdgyehheudhr

Uploaded by

grmpp9m4h9
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Java Loop Tutorial

1. Calculate Average, Highest, Lowest Grades


Write a Java program that prompts the user to enter student grades. The program should
calculate the average grade, the highest grade, the lowest grade, and the total number of
students. The program will stop accepting input when the user enters -1 as a grade.

2. Reverse a String
Write a program that takes a string input from the user and reverses it using a `for` loop.
For example:
Input: "Hello"
Output: "olleH"

3. Sum of Entered Numbers


Write a Java program that prompts the user to enter numbers. The program should
calculate the sum of all entered numbers. The program will stop when the user enters `0` as
a number.

4. Remove Non-Letters and Sort


Write a Java program that asks the user to input a string. The program should remove any
character that is not a letter and then sort the letters in alphabetical order.
For example:
Input: "dabc fe"
Output: "abcdef"

5. Calculate Sum, Highest, and Lowest Numbers


Write a Java program that prompts the user to enter a series of positive numbers. The
program should calculate the sum, the highest number, and the lowest number. The input
process stops when the user enters a negative number.

6. Count Vowels in a String


Write a program that takes a string and counts the number of vowels (a, e, i, o, u) using a
loop and `charAt`.
For example:
Input: "Education"
Output: 5
7. Calculate Average of Entered Numbers
Write a Java program that asks the user to input a series of numbers. The program should
calculate the average of the numbers entered. Input stops when the user enters `1`.

8. Count Words in a Sentence (No split method)


Write a program that counts the number of words in a sentence. Do not use the split
method, StringTokenizer, or any library methods to process each word. Only use a loop and
the isLetterOrDigit method.
For example:
Input: "I love programming"
Output: 3

9. Sum Odd Numbers


Write a Java program that prompts the user to enter a positive number, then sums all the
odd numbers from 1 up to that number.

10. Sum Prime Numbers


Write a Java program that prompts the user to enter a positive number, then sums all the
prime numbers from 1 up to that number.

11. Replace Characters in a String


Write a program that takes a string and replaces all occurrences of a given character with
another character using a loop and `charAt`.
For example:
Input: "banana", replace 'a' with 'o'
Output: "bonono"

12. Remove All White Spaces


Write a program that removes all white spaces from a string using a loop and `charAt`.
Example:
Input: "Hello World"
Output: "HelloWorld"

13. Find Unique Characters in a String


Write a program that prints the characters that appear only once in a string using nested
loops and `charAt`.
Example:
Input: "programming"
Output: "p o g i n"

14. Convert Sentence to CamelCase


Write a program that takes a sentence and converts it to CamelCase format where each
word starts with a capital letter, and spaces are removed.
Example:
Input: "hello world from java"
Output: "HelloWorldFromJava"

15. Generate Subsequences of a String


Write a program that generates all possible subsequences of a string using a loop and the
`substring` method.
Example:
Input: "abc"
Output: ["a", "b", "c", "ab", "bc", "abc"]

16. Find the Longest Word in a Sentence


Write a program that takes a sentence and finds the longest word. Do not use the split
method or StringTokenizer to process each word. Use a loop to search for the longest word.
Example:
Input: "I love programming"
Output: "programming"

17. Build a Simple Calculator


Write a Java program that prompts the user to create a calculator. The calculator should
support the following operations: multiplication, division, subtraction, addition, modulus
(remainder), and power. The program should display these operations as a menu to the
user and continue performing calculations until the user selects the letter 'E' to exit.

You might also like