๐Ÿ” 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)

Java Roadmap for SDET Superstars!

Mastering XPath in Selenium 4 ๐Ÿš€ – Supported Functions & Axes Explained