🔁 For Loop Based Programs

 

🔁 For Loop Based Programs

  1. Print all even numbers from 1 to N.
    💡 Hint: Start from 2, increment pointer by 2 until it reaches or exceeds N.

  2. Reverse a string using a for loop.
    💡 Hint: Use one pointer from end to start, append characters to result.

  3. Find the sum of the first N natural numbers.
    💡 Hint: One pointer moves from 1 to N, accumulate total.

  4. Print a right-angled triangle of stars with N rows.
    💡 Hint: Outer pointer for rows, inner pointer for stars in each row.

  5. Find all prime numbers up to N.
    💡 Hint: Outer loop for numbers, inner loop checks divisibility up to √N.


🔁 While Loop Based Programs

  1. Count the number of digits in an integer.
    💡 Hint: Use one pointer to divide number by 10 until it becomes 0.

  2. Check if a number is a palindrome.
    💡 Hint: Use two pointers—start and end digit comparision via math.

  3. Print the Fibonacci sequence up to N terms.
    💡 Hint: Two pointers hold last two values, loop until term count reached.

  4. Find the factorial of a number.
    💡 Hint: Use a pointer starting from 1, multiply until reaching the number.

  5. Check if an array is sorted in ascending order.
    💡 Hint: Use two pointers—current and next index to compare elements.

Comments

Popular posts from this blog

10 automation test cases for https://www.saucedemo.com/ (Sauce Demo)

Selenium WebDriver commands grouped by Browser Commands, WebElement Commands, and Navigation Commands.

Loops in Java