Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -8,36 +8,65 @@
*/
public class LoopExercises {

public static void main(String[] args) {
static void main(String[] args) {

// TODO: 1 - Write a for loop to print numbers 1 to 10
// Hint: for (int i = 1; i <= 10; i++) { ... }
System.out.println("TODO 1");
for (int i = 1; i <= 10; i++) {
System.out.println(i);
}


// TODO: 2 - Write a while loop to print numbers 10 down to 1 (countdown)
// Hint: Declare a variable before the loop, use a condition, and decrement inside the loop.

System.out.println("TODO 2");
int num = 10;
while (num >= 1) {
System.out.println(num--);
}

// TODO: 3 - Write a do-while loop that runs at least once
// Print "This runs at least once!" inside the loop.
// Use a condition that is false so the loop only runs once.
// Hint: do { ... } while (condition);
System.out.println("TODO 3");
num = 20;
do {
System.out.println("This runs at least once!");
num = 5;
}while (num == 10);


// TODO: 4 - Write a for loop to print only even numbers from 1 to 20
// Hint: Use an if statement with the modulus operator (%) inside the loop,
// or increment by 2 starting from 2.

System.out.println("TODO 4");
for (int i = 1; i <= 20; i++) {
if (i % 2 == 0) {
System.out.println(i);
}
}

// TODO: 5 - Write a for loop to calculate the factorial of 5 (5! = 120)
// Declare a variable called factorial and initialize it to 1.
// Multiply factorial by each number from 1 to 5.
// Print the result.
System.out.println("TODO 5");
int factorial = 1;
for (int i = 1; i <= 5; i++) {
System.out.println(factorial *= i);
}


// TODO: 6 - Use an enhanced for loop (for-each) to iterate over a String array
// Declare a String array called fruits with at least 4 fruit names.
// Use an enhanced for loop to print each fruit.
System.out.println("TODO 6");
String[] fruits = {"Apples", "Blueberries", "Blackberries", "Oranges"};
for (String fruit : fruits) {
System.out.println(fruit);
}


// TODO: 7 - Write a nested for loop to print a 3x3 multiplication table
Expand All @@ -47,13 +76,30 @@ public static void main(String[] args) {
// 3 6 9
// Hint: Use System.out.print() for values on the same row,
// and System.out.println() to move to the next row.
System.out.println("TODO 7");
for (int i = 1; i <= 3; i++) {
for (int j = 1; j <= 3; j++) {
System.out.print(i * j + " ");
}
System.out.println(" ");
}


// TODO: 8 - Use break and continue in a loop
// Write a for loop from 1 to 10:
// - Use 'continue' to skip the number 5 (do not print it)
// - Use 'break' to stop the loop when you reach 8
// Print each number that is not skipped.

System.out.println("TODO 8");
for (int i = 1; i <= 10; i++) {
if (i == 5) {
continue;
}
if (i == 8) {
System.out.println(i);
break;
}
System.out.println(i);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,27 +11,33 @@ public class Variables {
public static void main(String[] args) {

// TODO: 1 - Declare an int variable called age and assign it the value 25

int age = 25;

// TODO: 2 - Declare a double variable called price and assign it the value 9.99

double price = 9.99;

// TODO: 3 - Declare a boolean variable called isJavaFun and assign it the value true

boolean isJavaFun = true;

// TODO: 4 - Declare a String variable called name and assign it your name

String name = "Jose";

// TODO: 5 - Declare a char variable called grade and assign it the value 'A'

char grade = 'A';

// TODO: 6 - Print all the variables above using System.out.println
// Hint: You can print each variable on its own line, e.g.:
// System.out.println("Age: " + age);

System.out.println("Age: " + age);
System.out.println("Price: " + price);
System.out.println("isJavaFun: " + isJavaFun);
System.out.println("Name: " + name);
System.out.println("Grade: " + grade);

// TODO: 7 - Declare a final (constant) variable called MAX_SCORE, set it to 100, and print it
// Hint: Use the 'final' keyword before the type to make a constant
final int MAX_SCORE = 100;
System.out.println("MAX_SCORE: " + MAX_SCORE);

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,12 @@ public static void printMultiplicationTable(int n) {
// TODO: 1 - Use nested for loops to print an n x n multiplication table.
// Outer loop iterates rows 1..n, inner loop iterates columns 1..n.
// Print each product followed by a tab, and a newline after each row.
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
System.out.print(i * j + "\t");
}
System.out.println(" ");
}

}

Expand All @@ -35,7 +41,15 @@ public static int sumWithSkipAndStop(int n) {
int sum = 0;
// TODO: 2 - Loop from 1 to n. Use 'continue' to skip multiples of 3.
// Use 'break' to stop if sum exceeds 100. Add the current number to sum otherwise.

for (int i = 1; i <= n;i++) {
if (i%3 == 0) {
continue;
}
if (sum > 100) {
break;
}
sum+=i;
}
return sum;
}

Expand All @@ -53,7 +67,17 @@ public static String findInMatrix(int[][] matrix, int target) {
// Use nested loops to iterate through the matrix.
// When the target is found, set result to "Found at [row][col]" and
// use 'break search;' to exit both loops.

// [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
System.out.println("search: " + target);
for (int i = 0; i < matrix.length; i++) {
for (int j = 0; j < matrix.length; j++) {
if (target == matrix[i][j]) {
result = "Found at [" + j +"][" + i + "]";
System.out.println("break search;");
break;
}
}
}
return result;
}

Expand All @@ -68,7 +92,9 @@ public static long factorial(int n) {
long accumulator = 1;
// TODO: 4 - Use a for loop from 1 to n (inclusive), multiplying accumulator
// by the loop variable each iteration. Return the result.

for (int i = 1; i <= n; i++) {
accumulator *= i;
}
return accumulator;
}

Expand All @@ -86,7 +112,16 @@ public static void printPyramid(int rows) {
// TODO: 5 - Use nested loops to print a centered pyramid of stars.
// For each row i (0-based), print (rows - i - 1) spaces followed by (2 * i + 1) stars.
// Print a newline after each row.

// [[ * ], [ *** ], [******], [*******], [*********]]
for (int i = 0; i < rows; i++) {
for (int j = 0; j < (rows - i - 1); j++) {
System.out.print(" ");
}
for (int j = 0; j < (2 * i + 1); j++) {
System.out.print("*");
}
System.out.println();
}
}

/**
Expand All @@ -100,7 +135,13 @@ public static String iterateBackwards(int[] arr) {
StringBuilder sb = new StringBuilder();
// TODO: 6 - Use a for loop starting from the last index down to 0.
// Append each element to sb. Add ", " between elements but not after the last one.
for (int i = arr.length; i >= 0; i--) {
sb.append(i);
if (i != 0) {
sb.append(", ");
}

}
return sb.toString();
}

Expand All @@ -118,7 +159,12 @@ public static int countUntilMatch(int target) {
// TODO: 7 - Use while(true) to create an infinite loop.
// Each iteration: increment attempts, generate a random int between 1 and 100,
// and break if it matches the target.

while(true) {
attempts += 1;
if(target == random.nextInt(100) + 1) {
break;
}
}
return attempts;
}

Expand Down