Practice Writing Loops (Repetitive Blocks)
Topics
Practice Exercises
Exercise 1: Sum of Natural Numbers
Problem: Write a for loop to calculate the sum of the first n natural numbers.
Solution
int sum = 0;
for (int i = 1; i <= n; i++) {
sum += i;
}
System.out.println("Sum: " + sum);
Variants
- Sum of Even Numbers
Problem: Write aforloop to calculate the sum of the firstneven numbers. - Sum of Odd Numbers
Problem: Write aforloop to calculate the sum of the firstnodd numbers. - Sum of Multiples of 3
Problem: Write aforloop to calculate the sum of the firstnmultiples of 3. - Sum of Squares of Numbers
Problem: Write aforloop to calculate the sum of the squares of the firstnnatural numbers.
Exercise 2: Factorial of a Number
Problem: Write a while loop to calculate the factorial of a number n.
Solution
int factorial = 1;
int i = n;
while (i > 0) {
factorial *= i;
i--;
}
System.out.println("Factorial: " + factorial);
Variants
- Product of Even Numbers
Problem: Write awhileloop to calculate the product of the firstneven numbers. - Product of Odd Numbers
Problem: Write awhileloop to calculate the product of the firstnodd numbers. - Double Factorial
Problem: Write awhileloop to calculate the double factorial of a numbern, which is the product of all integers of the same parity as n.
Ifnis odd, calculaten * (n - 2) * (n - 4) * ... * 1.
Ifnis even, calculaten * (n - 2) * (n - 4) * ... * 2.
Exercise 3: Find Maximum Element in Array
Problem: Write a for loop to find the maximum value in an array.
Solution
int[] arr = {10, 50, 30, 70, 20};
int max = arr[0];
for (int i = 1; i < arr.length; i++) {
if (arr[i] > max) {
max = arr[i];
}
}
System.out.println("Max value: " + max);
Variants
- Find Minimum Element in Array
Problem: Write aforloop to find the minimum value in an array. - Find Second Maximum Element in Array
Problem: Write aforloop to find the second maximum value in an array. - Find Index of Maximum Element
Problem: Write aforloop to find the index of the maximum value in an array.
Exercise 4: Count Even Numbers in Array
Problem: Write an enhanced for loop (for-each) to count how many even numbers are in an array.
Solution
int[] arr = {1, 2, 3, 4, 5, 6};
int evenCount = 0;
for (int num : arr) {
if (num % 2 == 0) {
evenCount++;
}
}
System.out.println("Even numbers count: " + evenCount);
Variants
- Count Odd Numbers in Array
Problem: Write an enhancedforloop to count how many odd numbers are in an array. - Count Prime Numbers in Array
Problem: Write an enhancedforloop to count how many prime numbers are in an array. - Count Numbers Greater Than a Threshold
Problem: Write an enhancedforloop to count how many numbers in an array are greater than a given threshold valuek.
Exercise 5: Reverse an Array
Problem: Write a for loop to reverse the elements of an array.
Solution
int[] arr = {1, 2, 3, 4, 5};
for (int i = 0; i < arr.length / 2; i++) {
int temp = arr[i];
arr[i] = arr[arr.length - i - 1];
arr[arr.length - i - 1] = temp;
}
System.out.println(Arrays.toString(arr)); // Prints reversed array
Variants
- Reverse a String
Problem: Write a for loop to reverse a string. - Reverse Digits of a Number
Problem: Write awhileloop to reverse the digits of a number. - Reverse Every Word in a Sentence
Problem: Write aforloop to reverse every word in a given sentence.
Summary
Loops are essential for repeating tasks, traversing data structures, and automating calculations. By understanding the different types of loops (for, while, do-while, enhanced for), their syntax, and common use cases, you can efficiently solve a variety of problems in programming. These practice exercises help reinforce these concepts.
Next Topic:
Go to Arrays to learn more with examples.