Episode 7 — DSA with JavaScript / 7.2 — Loops Nested Loops Pattern Programming
7.2.a — For, While, Do-While Loops
Why loops?
A loop repeats a block of code while a condition holds. Without loops, printing numbers 1 to 1000 would require 1000 lines of code.
┌──────────────────┐
│ Initialization │
└────────┬─────────┘
▼
┌──────────────────┐ false
│ Condition? │──────────▶ EXIT
└────────┬─────────┘
│ true
▼
┌──────────────────┐
│ Loop Body │
└────────┬─────────┘
▼
┌──────────────────┐
│ Update │
└────────┬─────────┘
│
└──────▶ back to Condition
The for loop
Best when you know the number of iterations in advance.
Syntax
for (initialization; condition; update) {
// body
}
JavaScript — print 1 to 10
for (let i = 1; i <= 10; i++) {
console.log(i);
}
C++
#include <iostream>
using namespace std;
int main() {
for (int i = 1; i <= 10; i++) {
cout << i << " ";
}
// Output: 1 2 3 4 5 6 7 8 9 10
return 0;
}
Dry-run trace
i=1 → 1<=10? true → print 1 → i=2
i=2 → 2<=10? true → print 2 → i=3
...
i=10 → 10<=10? true → print 10 → i=11
i=11 → 11<=10? false → EXIT
Counting backwards
for (let i = 10; i >= 1; i--) {
console.log(i);
}
Iterating with step
// Print even numbers 2 to 20
for (let i = 2; i <= 20; i += 2) {
console.log(i);
}
Iterating over arrays
const fruits = ["apple", "banana", "cherry"];
// Index-based
for (let i = 0; i < fruits.length; i++) {
console.log(`${i}: ${fruits[i]}`);
}
// for...of (preferred in modern JS)
for (const fruit of fruits) {
console.log(fruit);
}
#include <iostream>
#include <vector>
#include <string>
using namespace std;
int main() {
vector<string> fruits = {"apple", "banana", "cherry"};
// Index-based
for (int i = 0; i < fruits.size(); i++) {
cout << i << ": " << fruits[i] << endl;
}
// Range-based (C++11)
for (const auto& fruit : fruits) {
cout << fruit << endl;
}
return 0;
}
The while loop
Best when the number of iterations is unknown — depends on a condition.
Syntax
while (condition) {
// body
// update condition
}
JavaScript — sum digits of a number
function sumDigits(n) {
let sum = 0;
n = Math.abs(n);
while (n > 0) {
sum += n % 10;
n = Math.floor(n / 10);
}
return sum;
}
console.log(sumDigits(12345)); // 15
// Dry-run:
// n=12345, sum=0
// sum += 5 → 5, n = 1234
// sum += 4 → 9, n = 123
// sum += 3 → 12, n = 12
// sum += 2 → 14, n = 1
// sum += 1 → 15, n = 0
// n=0, exit → return 15
C++
int sumDigits(int n) {
int sum = 0;
if (n < 0) n = -n;
while (n > 0) {
sum += n % 10;
n /= 10;
}
return sum;
}
Common pattern: reading input until sentinel
const readline = require("readline");
const rl = readline.createInterface({ input: process.stdin, output: process.stdout });
let total = 0;
function ask() {
rl.question("Enter a number (0 to stop): ", (answer) => {
const num = parseInt(answer);
if (num === 0) {
console.log(`Total: ${total}`);
rl.close();
} else {
total += num;
ask();
}
});
}
ask();
#include <iostream>
using namespace std;
int main() {
int num, total = 0;
cout << "Enter numbers (0 to stop):" << endl;
while (cin >> num && num != 0) {
total += num;
}
cout << "Total: " << total << endl;
return 0;
}
The do-while loop
Executes the body at least once before checking the condition.
┌──────────────────┐
│ Loop Body │ ◀─── runs first
└────────┬─────────┘
▼
┌──────────────────┐
│ Condition? │──false──▶ EXIT
└────────┬─────────┘
│ true
└──────▶ back to Body
JavaScript — menu that runs at least once
function menu() {
let choice;
do {
console.log("1. Play 2. Settings 3. Quit");
choice = parseInt(prompt("Choose:"));
if (choice === 1) console.log("Playing...");
else if (choice === 2) console.log("Settings...");
} while (choice !== 3);
console.log("Goodbye!");
}
C++
#include <iostream>
using namespace std;
int main() {
int choice;
do {
cout << "1. Play 2. Settings 3. Quit" << endl;
cout << "Choose: ";
cin >> choice;
if (choice == 1) cout << "Playing..." << endl;
else if (choice == 2) cout << "Settings..." << endl;
} while (choice != 3);
cout << "Goodbye!" << endl;
return 0;
}
When to use do-while
- User menus (show options at least once)
- Input validation (prompt until valid)
- Game loops (run at least one frame)
Comparison: for vs while vs do-while
| Feature | for | while | do-while |
|---|---|---|---|
| Known iteration count | Best | OK | Rare |
| Unknown iteration count | Possible | Best | OK |
| Run at least once | Possible | Possible | Guaranteed |
| Most common use | Arrays, ranges | Conditions, streams | Menus, validation |
Common algorithms using loops
Factorial
function factorial(n) {
let result = 1;
for (let i = 2; i <= n; i++) {
result *= i;
}
return result;
}
// Dry-run: n=5
// i=2: result = 1*2 = 2
// i=3: result = 2*3 = 6
// i=4: result = 6*4 = 24
// i=5: result = 24*5 = 120
long long factorial(int n) {
long long result = 1;
for (int i = 2; i <= n; i++) {
result *= i;
}
return result;
}
Reverse a number
function reverseNumber(n) {
let reversed = 0;
let isNeg = n < 0;
n = Math.abs(n);
while (n > 0) {
reversed = reversed * 10 + (n % 10);
n = Math.floor(n / 10);
}
return isNeg ? -reversed : reversed;
}
console.log(reverseNumber(1234)); // 4321
console.log(reverseNumber(-567)); // -765
// Dry-run: n=1234
// reversed=0*10+4 = 4, n=123
// reversed=4*10+3 = 43, n=12
// reversed=43*10+2 = 432, n=1
// reversed=432*10+1 = 4321, n=0
int reverseNumber(int n) {
int reversed = 0;
bool isNeg = n < 0;
if (isNeg) n = -n;
while (n > 0) {
reversed = reversed * 10 + n % 10;
n /= 10;
}
return isNeg ? -reversed : reversed;
}
Check prime
function isPrime(n) {
if (n < 2) return false;
if (n === 2) return true;
if (n % 2 === 0) return false;
for (let i = 3; i * i <= n; i += 2) {
if (n % i === 0) return false;
}
return true;
}
bool isPrime(int n) {
if (n < 2) return false;
if (n == 2) return true;
if (n % 2 == 0) return false;
for (int i = 3; i * i <= n; i += 2) {
if (n % i == 0) return false;
}
return true;
}
GCD (Euclidean algorithm)
function gcd(a, b) {
while (b !== 0) {
[a, b] = [b, a % b];
}
return a;
}
// Dry-run: gcd(48, 18)
// a=48, b=18 → [18, 48%18=12]
// a=18, b=12 → [12, 18%12=6]
// a=12, b=6 → [6, 12%6=0]
// a=6, b=0 → return 6
int gcd(int a, int b) {
while (b != 0) {
int temp = b;
b = a % b;
a = temp;
}
return a;
}
Power (iterative)
function power(base, exp) {
let result = 1;
for (let i = 0; i < exp; i++) {
result *= base;
}
return result;
}
Fibonacci sequence
function fibonacci(n) {
if (n <= 0) return [];
if (n === 1) return [0];
const fib = [0, 1];
for (let i = 2; i < n; i++) {
fib.push(fib[i-1] + fib[i-2]);
}
return fib;
}
console.log(fibonacci(10));
// [0, 1, 1, 2, 3, 5, 8, 13, 21, 34]
#include <vector>
using namespace std;
vector<int> fibonacci(int n) {
if (n <= 0) return {};
if (n == 1) return {0};
vector<int> fib = {0, 1};
for (int i = 2; i < n; i++) {
fib.push_back(fib[i-1] + fib[i-2]);
}
return fib;
}
Performance considerations
| Pattern | Time | Why |
|---|---|---|
| Simple loop | O(n) | One pass |
| Loop with i*i <= n | O(√n) | Square root bound |
| Loop halving n each iteration | O(log n) | Binary pattern |
| Two nested loops | O(n²) | Each iteration of outer runs inner |
Scenario bank (loops)
7.2a-001 — Count digits in a number using while (variation 1)
- Level: Beginner
- Loop type: while
- Approach: Use the appropriate loop construct for the iteration pattern.
- Time: O(n) unless specified otherwise.
- Edge cases: empty input, single element, large values.
- JS implementation: Write a function.
- C++ implementation: Use equivalent C++ syntax.
7.2a-002 — Sum all elements in an array using for (variation 2)
- Level: Beginner
- Loop type: do-while
- Approach: Use the appropriate loop construct for the iteration pattern.
- Time: O(n) unless specified otherwise.
- Edge cases: empty input, single element, large values.
- JS implementation: Write a function.
- C++ implementation: Use equivalent C++ syntax.
7.2a-003 — Find the maximum element in an array (variation 3)
- Level: Beginner
- Loop type: for
- Approach: Use the appropriate loop construct for the iteration pattern.
- Time: O(n) unless specified otherwise.
- Edge cases: empty input, single element, large values.
- JS implementation: Write a function.
- C++ implementation: Use equivalent C++ syntax.
7.2a-004 — Count occurrences of a character in a string (variation 4)
- Level: Beginner
- Loop type: while
- Approach: Use the appropriate loop construct for the iteration pattern.
- Time: O(n) unless specified otherwise.
- Edge cases: empty input, single element, large values.
- JS implementation: Write a function.
- C++ implementation: Use equivalent C++ syntax.
7.2a-005 — Print multiplication table for a number (variation 5)
- Level: Beginner
- Loop type: do-while
- Approach: Use the appropriate loop construct for the iteration pattern.
- Time: O(n) unless specified otherwise.
- Edge cases: empty input, single element, large values.
- JS implementation: Write a function.
- C++ implementation: Use equivalent C++ syntax.
7.2a-006 — Generate first N Fibonacci numbers (variation 6)
- Level: Beginner
- Loop type: for
- Approach: Use the appropriate loop construct for the iteration pattern.
- Time: O(n) unless specified otherwise.
- Edge cases: empty input, single element, large values.
- JS implementation: Write a function.
- C++ implementation: Use equivalent C++ syntax.
7.2a-007 — Check if a number is an Armstrong number (variation 7)
- Level: Beginner
- Loop type: while
- Approach: Use the appropriate loop construct for the iteration pattern.
- Time: O(n) unless specified otherwise.
- Edge cases: empty input, single element, large values.
- JS implementation: Write a function.
- C++ implementation: Use equivalent C++ syntax.
7.2a-008 — Convert decimal to binary using while (variation 8)
- Level: Beginner
- Loop type: do-while
- Approach: Use the appropriate loop construct for the iteration pattern.
- Time: O(n) unless specified otherwise.
- Edge cases: empty input, single element, large values.
- JS implementation: Write a function.
- C++ implementation: Use equivalent C++ syntax.
7.2a-009 — Find all factors of a number (variation 9)
- Level: Beginner
- Loop type: for
- Approach: Use the appropriate loop construct for the iteration pattern.
- Time: O(n) unless specified otherwise.
- Edge cases: empty input, single element, large values.
- JS implementation: Write a function.
- C++ implementation: Use equivalent C++ syntax.
7.2a-010 — Compute power without Math.pow using loop (variation 10)
- Level: Beginner
- Loop type: while
- Approach: Use the appropriate loop construct for the iteration pattern.
- Time: O(n) unless specified otherwise.
- Edge cases: empty input, single element, large values.
- JS implementation: Write a function.
- C++ implementation: Use equivalent C++ syntax.
7.2a-011 — Check if a string is a palindrome using loop (variation 11)
- Level: Beginner
- Loop type: do-while
- Approach: Use the appropriate loop construct for the iteration pattern.
- Time: O(n) unless specified otherwise.
- Edge cases: empty input, single element, large values.
- JS implementation: Write a function.
- C++ implementation: Use equivalent C++ syntax.
7.2a-012 — Find the LCM of two numbers (variation 12)
- Level: Beginner
- Loop type: for
- Approach: Use the appropriate loop construct for the iteration pattern.
- Time: O(n) unless specified otherwise.
- Edge cases: empty input, single element, large values.
- JS implementation: Write a function.
- C++ implementation: Use equivalent C++ syntax.
7.2a-013 — Count vowels and consonants in a string (variation 13)
- Level: Beginner
- Loop type: while
- Approach: Use the appropriate loop construct for the iteration pattern.
- Time: O(n) unless specified otherwise.
- Edge cases: empty input, single element, large values.
- JS implementation: Write a function.
- C++ implementation: Use equivalent C++ syntax.
7.2a-014 — Reverse an array in-place using loop (variation 14)
- Level: Beginner
- Loop type: do-while
- Approach: Use the appropriate loop construct for the iteration pattern.
- Time: O(n) unless specified otherwise.
- Edge cases: empty input, single element, large values.
- JS implementation: Write a function.
- C++ implementation: Use equivalent C++ syntax.
7.2a-015 — Find the second largest element in an array (variation 15)
- Level: Beginner
- Loop type: for
- Approach: Use the appropriate loop construct for the iteration pattern.
- Time: O(n) unless specified otherwise.
- Edge cases: empty input, single element, large values.
- JS implementation: Write a function.
- C++ implementation: Use equivalent C++ syntax.
7.2a-016 — Sum of first N natural numbers using loop (variation 16)
- Level: Beginner
- Loop type: while
- Approach: Use the appropriate loop construct for the iteration pattern.
- Time: O(n) unless specified otherwise.
- Edge cases: empty input, single element, large values.
- JS implementation: Write a function.
- C++ implementation: Use equivalent C++ syntax.
7.2a-017 — Check if a number is a perfect number (variation 17)
- Level: Beginner
- Loop type: do-while
- Approach: Use the appropriate loop construct for the iteration pattern.
- Time: O(n) unless specified otherwise.
- Edge cases: empty input, single element, large values.
- JS implementation: Write a function.
- C++ implementation: Use equivalent C++ syntax.
7.2a-018 — Print all prime numbers up to N (Sieve approach) (variation 18)
- Level: Beginner
- Loop type: for
- Approach: Use the appropriate loop construct for the iteration pattern.
- Time: O(n) unless specified otherwise.
- Edge cases: empty input, single element, large values.
- JS implementation: Write a function.
- C++ implementation: Use equivalent C++ syntax.
7.2a-019 — Binary to decimal conversion using loop (variation 19)
- Level: Beginner
- Loop type: while
- Approach: Use the appropriate loop construct for the iteration pattern.
- Time: O(n) unless specified otherwise.
- Edge cases: empty input, single element, large values.
- JS implementation: Write a function.
- C++ implementation: Use equivalent C++ syntax.
7.2a-020 — Find missing number in 1 to N array (variation 20)
- Level: Beginner
- Loop type: do-while
- Approach: Use the appropriate loop construct for the iteration pattern.
- Time: O(n) unless specified otherwise.
- Edge cases: empty input, single element, large values.
- JS implementation: Write a function.
- C++ implementation: Use equivalent C++ syntax.
7.2a-021 — Count digits in a number using while (variation 21)
- Level: Beginner
- Loop type: for
- Approach: Use the appropriate loop construct for the iteration pattern.
- Time: O(n) unless specified otherwise.
- Edge cases: empty input, single element, large values.
- JS implementation: Write a function.
- C++ implementation: Use equivalent C++ syntax.
7.2a-022 — Sum all elements in an array using for (variation 22)
- Level: Beginner
- Loop type: while
- Approach: Use the appropriate loop construct for the iteration pattern.
- Time: O(n) unless specified otherwise.
- Edge cases: empty input, single element, large values.
- JS implementation: Write a function.
- C++ implementation: Use equivalent C++ syntax.
7.2a-023 — Find the maximum element in an array (variation 23)
- Level: Beginner
- Loop type: do-while
- Approach: Use the appropriate loop construct for the iteration pattern.
- Time: O(n) unless specified otherwise.
- Edge cases: empty input, single element, large values.
- JS implementation: Write a function.
- C++ implementation: Use equivalent C++ syntax.
7.2a-024 — Count occurrences of a character in a string (variation 24)
- Level: Beginner
- Loop type: for
- Approach: Use the appropriate loop construct for the iteration pattern.
- Time: O(n) unless specified otherwise.
- Edge cases: empty input, single element, large values.
- JS implementation: Write a function.
- C++ implementation: Use equivalent C++ syntax.
7.2a-025 — Print multiplication table for a number (variation 25)
- Level: Beginner
- Loop type: while
- Approach: Use the appropriate loop construct for the iteration pattern.
- Time: O(n) unless specified otherwise.
- Edge cases: empty input, single element, large values.
- JS implementation: Write a function.
- C++ implementation: Use equivalent C++ syntax.
7.2a-026 — Generate first N Fibonacci numbers (variation 26)
- Level: Beginner
- Loop type: do-while
- Approach: Use the appropriate loop construct for the iteration pattern.
- Time: O(n) unless specified otherwise.
- Edge cases: empty input, single element, large values.
- JS implementation: Write a function.
- C++ implementation: Use equivalent C++ syntax.
7.2a-027 — Check if a number is an Armstrong number (variation 27)
- Level: Beginner
- Loop type: for
- Approach: Use the appropriate loop construct for the iteration pattern.
- Time: O(n) unless specified otherwise.
- Edge cases: empty input, single element, large values.
- JS implementation: Write a function.
- C++ implementation: Use equivalent C++ syntax.
7.2a-028 — Convert decimal to binary using while (variation 28)
- Level: Beginner
- Loop type: while
- Approach: Use the appropriate loop construct for the iteration pattern.
- Time: O(n) unless specified otherwise.
- Edge cases: empty input, single element, large values.
- JS implementation: Write a function.
- C++ implementation: Use equivalent C++ syntax.
7.2a-029 — Find all factors of a number (variation 29)
- Level: Beginner
- Loop type: do-while
- Approach: Use the appropriate loop construct for the iteration pattern.
- Time: O(n) unless specified otherwise.
- Edge cases: empty input, single element, large values.
- JS implementation: Write a function.
- C++ implementation: Use equivalent C++ syntax.
7.2a-030 — Compute power without Math.pow using loop (variation 30)
- Level: Beginner
- Loop type: for
- Approach: Use the appropriate loop construct for the iteration pattern.
- Time: O(n) unless specified otherwise.
- Edge cases: empty input, single element, large values.
- JS implementation: Write a function.
- C++ implementation: Use equivalent C++ syntax.
7.2a-031 — Check if a string is a palindrome using loop (variation 31)
- Level: Beginner
- Loop type: while
- Approach: Use the appropriate loop construct for the iteration pattern.
- Time: O(n) unless specified otherwise.
- Edge cases: empty input, single element, large values.
- JS implementation: Write a function.
- C++ implementation: Use equivalent C++ syntax.
7.2a-032 — Find the LCM of two numbers (variation 32)
- Level: Beginner
- Loop type: do-while
- Approach: Use the appropriate loop construct for the iteration pattern.
- Time: O(n) unless specified otherwise.
- Edge cases: empty input, single element, large values.
- JS implementation: Write a function.
- C++ implementation: Use equivalent C++ syntax.
7.2a-033 — Count vowels and consonants in a string (variation 33)
- Level: Beginner
- Loop type: for
- Approach: Use the appropriate loop construct for the iteration pattern.
- Time: O(n) unless specified otherwise.
- Edge cases: empty input, single element, large values.
- JS implementation: Write a function.
- C++ implementation: Use equivalent C++ syntax.
7.2a-034 — Reverse an array in-place using loop (variation 34)
- Level: Beginner
- Loop type: while
- Approach: Use the appropriate loop construct for the iteration pattern.
- Time: O(n) unless specified otherwise.
- Edge cases: empty input, single element, large values.
- JS implementation: Write a function.
- C++ implementation: Use equivalent C++ syntax.
7.2a-035 — Find the second largest element in an array (variation 35)
- Level: Beginner
- Loop type: do-while
- Approach: Use the appropriate loop construct for the iteration pattern.
- Time: O(n) unless specified otherwise.
- Edge cases: empty input, single element, large values.
- JS implementation: Write a function.
- C++ implementation: Use equivalent C++ syntax.
7.2a-036 — Sum of first N natural numbers using loop (variation 36)
- Level: Beginner
- Loop type: for
- Approach: Use the appropriate loop construct for the iteration pattern.
- Time: O(n) unless specified otherwise.
- Edge cases: empty input, single element, large values.
- JS implementation: Write a function.
- C++ implementation: Use equivalent C++ syntax.
7.2a-037 — Check if a number is a perfect number (variation 37)
- Level: Beginner
- Loop type: while
- Approach: Use the appropriate loop construct for the iteration pattern.
- Time: O(n) unless specified otherwise.
- Edge cases: empty input, single element, large values.
- JS implementation: Write a function.
- C++ implementation: Use equivalent C++ syntax.
7.2a-038 — Print all prime numbers up to N (Sieve approach) (variation 38)
- Level: Beginner
- Loop type: do-while
- Approach: Use the appropriate loop construct for the iteration pattern.
- Time: O(n) unless specified otherwise.
- Edge cases: empty input, single element, large values.
- JS implementation: Write a function.
- C++ implementation: Use equivalent C++ syntax.
7.2a-039 — Binary to decimal conversion using loop (variation 39)
- Level: Beginner
- Loop type: for
- Approach: Use the appropriate loop construct for the iteration pattern.
- Time: O(n) unless specified otherwise.
- Edge cases: empty input, single element, large values.
- JS implementation: Write a function.
- C++ implementation: Use equivalent C++ syntax.
7.2a-040 — Find missing number in 1 to N array (variation 40)
- Level: Beginner
- Loop type: while
- Approach: Use the appropriate loop construct for the iteration pattern.
- Time: O(n) unless specified otherwise.
- Edge cases: empty input, single element, large values.
- JS implementation: Write a function.
- C++ implementation: Use equivalent C++ syntax.
7.2a-041 — Count digits in a number using while (variation 41)
- Level: Beginner
- Loop type: do-while
- Approach: Use the appropriate loop construct for the iteration pattern.
- Time: O(n) unless specified otherwise.
- Edge cases: empty input, single element, large values.
- JS implementation: Write a function.
- C++ implementation: Use equivalent C++ syntax.
7.2a-042 — Sum all elements in an array using for (variation 42)
- Level: Beginner
- Loop type: for
- Approach: Use the appropriate loop construct for the iteration pattern.
- Time: O(n) unless specified otherwise.
- Edge cases: empty input, single element, large values.
- JS implementation: Write a function.
- C++ implementation: Use equivalent C++ syntax.
7.2a-043 — Find the maximum element in an array (variation 43)
- Level: Beginner
- Loop type: while
- Approach: Use the appropriate loop construct for the iteration pattern.
- Time: O(n) unless specified otherwise.
- Edge cases: empty input, single element, large values.
- JS implementation: Write a function.
- C++ implementation: Use equivalent C++ syntax.
7.2a-044 — Count occurrences of a character in a string (variation 44)
- Level: Beginner
- Loop type: do-while
- Approach: Use the appropriate loop construct for the iteration pattern.
- Time: O(n) unless specified otherwise.
- Edge cases: empty input, single element, large values.
- JS implementation: Write a function.
- C++ implementation: Use equivalent C++ syntax.
7.2a-045 — Print multiplication table for a number (variation 45)
- Level: Beginner
- Loop type: for
- Approach: Use the appropriate loop construct for the iteration pattern.
- Time: O(n) unless specified otherwise.
- Edge cases: empty input, single element, large values.
- JS implementation: Write a function.
- C++ implementation: Use equivalent C++ syntax.
7.2a-046 — Generate first N Fibonacci numbers (variation 46)
- Level: Beginner
- Loop type: while
- Approach: Use the appropriate loop construct for the iteration pattern.
- Time: O(n) unless specified otherwise.
- Edge cases: empty input, single element, large values.
- JS implementation: Write a function.
- C++ implementation: Use equivalent C++ syntax.
7.2a-047 — Check if a number is an Armstrong number (variation 47)
- Level: Beginner
- Loop type: do-while
- Approach: Use the appropriate loop construct for the iteration pattern.
- Time: O(n) unless specified otherwise.
- Edge cases: empty input, single element, large values.
- JS implementation: Write a function.
- C++ implementation: Use equivalent C++ syntax.
7.2a-048 — Convert decimal to binary using while (variation 48)
- Level: Beginner
- Loop type: for
- Approach: Use the appropriate loop construct for the iteration pattern.
- Time: O(n) unless specified otherwise.
- Edge cases: empty input, single element, large values.
- JS implementation: Write a function.
- C++ implementation: Use equivalent C++ syntax.
7.2a-049 — Find all factors of a number (variation 49)
- Level: Beginner
- Loop type: while
- Approach: Use the appropriate loop construct for the iteration pattern.
- Time: O(n) unless specified otherwise.
- Edge cases: empty input, single element, large values.
- JS implementation: Write a function.
- C++ implementation: Use equivalent C++ syntax.
7.2a-050 — Compute power without Math.pow using loop (variation 50)
- Level: Beginner
- Loop type: do-while
- Approach: Use the appropriate loop construct for the iteration pattern.
- Time: O(n) unless specified otherwise.
- Edge cases: empty input, single element, large values.
- JS implementation: Write a function.
- C++ implementation: Use equivalent C++ syntax.
7.2a-051 — Check if a string is a palindrome using loop (variation 51)
- Level: Intermediate
- Loop type: for
- Approach: Use the appropriate loop construct for the iteration pattern.
- Time: O(n) unless specified otherwise.
- Edge cases: empty input, single element, large values.
- JS implementation: Write a function.
- C++ implementation: Use equivalent C++ syntax.
7.2a-052 — Find the LCM of two numbers (variation 52)
- Level: Intermediate
- Loop type: while
- Approach: Use the appropriate loop construct for the iteration pattern.
- Time: O(n) unless specified otherwise.
- Edge cases: empty input, single element, large values.
- JS implementation: Write a function.
- C++ implementation: Use equivalent C++ syntax.
7.2a-053 — Count vowels and consonants in a string (variation 53)
- Level: Intermediate
- Loop type: do-while
- Approach: Use the appropriate loop construct for the iteration pattern.
- Time: O(n) unless specified otherwise.
- Edge cases: empty input, single element, large values.
- JS implementation: Write a function.
- C++ implementation: Use equivalent C++ syntax.
7.2a-054 — Reverse an array in-place using loop (variation 54)
- Level: Intermediate
- Loop type: for
- Approach: Use the appropriate loop construct for the iteration pattern.
- Time: O(n) unless specified otherwise.
- Edge cases: empty input, single element, large values.
- JS implementation: Write a function.
- C++ implementation: Use equivalent C++ syntax.
7.2a-055 — Find the second largest element in an array (variation 55)
- Level: Intermediate
- Loop type: while
- Approach: Use the appropriate loop construct for the iteration pattern.
- Time: O(n) unless specified otherwise.
- Edge cases: empty input, single element, large values.
- JS implementation: Write a function.
- C++ implementation: Use equivalent C++ syntax.
7.2a-056 — Sum of first N natural numbers using loop (variation 56)
- Level: Intermediate
- Loop type: do-while
- Approach: Use the appropriate loop construct for the iteration pattern.
- Time: O(n) unless specified otherwise.
- Edge cases: empty input, single element, large values.
- JS implementation: Write a function.
- C++ implementation: Use equivalent C++ syntax.
7.2a-057 — Check if a number is a perfect number (variation 57)
- Level: Intermediate
- Loop type: for
- Approach: Use the appropriate loop construct for the iteration pattern.
- Time: O(n) unless specified otherwise.
- Edge cases: empty input, single element, large values.
- JS implementation: Write a function.
- C++ implementation: Use equivalent C++ syntax.
7.2a-058 — Print all prime numbers up to N (Sieve approach) (variation 58)
- Level: Intermediate
- Loop type: while
- Approach: Use the appropriate loop construct for the iteration pattern.
- Time: O(n) unless specified otherwise.
- Edge cases: empty input, single element, large values.
- JS implementation: Write a function.
- C++ implementation: Use equivalent C++ syntax.
7.2a-059 — Binary to decimal conversion using loop (variation 59)
- Level: Intermediate
- Loop type: do-while
- Approach: Use the appropriate loop construct for the iteration pattern.
- Time: O(n) unless specified otherwise.
- Edge cases: empty input, single element, large values.
- JS implementation: Write a function.
- C++ implementation: Use equivalent C++ syntax.
7.2a-060 — Find missing number in 1 to N array (variation 60)
- Level: Intermediate
- Loop type: for
- Approach: Use the appropriate loop construct for the iteration pattern.
- Time: O(n) unless specified otherwise.
- Edge cases: empty input, single element, large values.
- JS implementation: Write a function.
- C++ implementation: Use equivalent C++ syntax.
7.2a-061 — Count digits in a number using while (variation 61)
- Level: Intermediate
- Loop type: while
- Approach: Use the appropriate loop construct for the iteration pattern.
- Time: O(n) unless specified otherwise.
- Edge cases: empty input, single element, large values.
- JS implementation: Write a function.
- C++ implementation: Use equivalent C++ syntax.
7.2a-062 — Sum all elements in an array using for (variation 62)
- Level: Intermediate
- Loop type: do-while
- Approach: Use the appropriate loop construct for the iteration pattern.
- Time: O(n) unless specified otherwise.
- Edge cases: empty input, single element, large values.
- JS implementation: Write a function.
- C++ implementation: Use equivalent C++ syntax.
7.2a-063 — Find the maximum element in an array (variation 63)
- Level: Intermediate
- Loop type: for
- Approach: Use the appropriate loop construct for the iteration pattern.
- Time: O(n) unless specified otherwise.
- Edge cases: empty input, single element, large values.
- JS implementation: Write a function.
- C++ implementation: Use equivalent C++ syntax.
7.2a-064 — Count occurrences of a character in a string (variation 64)
- Level: Intermediate
- Loop type: while
- Approach: Use the appropriate loop construct for the iteration pattern.
- Time: O(n) unless specified otherwise.
- Edge cases: empty input, single element, large values.
- JS implementation: Write a function.
- C++ implementation: Use equivalent C++ syntax.
7.2a-065 — Print multiplication table for a number (variation 65)
- Level: Intermediate
- Loop type: do-while
- Approach: Use the appropriate loop construct for the iteration pattern.
- Time: O(n) unless specified otherwise.
- Edge cases: empty input, single element, large values.
- JS implementation: Write a function.
- C++ implementation: Use equivalent C++ syntax.
7.2a-066 — Generate first N Fibonacci numbers (variation 66)
- Level: Intermediate
- Loop type: for
- Approach: Use the appropriate loop construct for the iteration pattern.
- Time: O(n) unless specified otherwise.
- Edge cases: empty input, single element, large values.
- JS implementation: Write a function.
- C++ implementation: Use equivalent C++ syntax.
7.2a-067 — Check if a number is an Armstrong number (variation 67)
- Level: Intermediate
- Loop type: while
- Approach: Use the appropriate loop construct for the iteration pattern.
- Time: O(n) unless specified otherwise.
- Edge cases: empty input, single element, large values.
- JS implementation: Write a function.
- C++ implementation: Use equivalent C++ syntax.
7.2a-068 — Convert decimal to binary using while (variation 68)
- Level: Intermediate
- Loop type: do-while
- Approach: Use the appropriate loop construct for the iteration pattern.
- Time: O(n) unless specified otherwise.
- Edge cases: empty input, single element, large values.
- JS implementation: Write a function.
- C++ implementation: Use equivalent C++ syntax.
7.2a-069 — Find all factors of a number (variation 69)
- Level: Intermediate
- Loop type: for
- Approach: Use the appropriate loop construct for the iteration pattern.
- Time: O(n) unless specified otherwise.
- Edge cases: empty input, single element, large values.
- JS implementation: Write a function.
- C++ implementation: Use equivalent C++ syntax.
7.2a-070 — Compute power without Math.pow using loop (variation 70)
- Level: Intermediate
- Loop type: while
- Approach: Use the appropriate loop construct for the iteration pattern.
- Time: O(n) unless specified otherwise.
- Edge cases: empty input, single element, large values.
- JS implementation: Write a function.
- C++ implementation: Use equivalent C++ syntax.
7.2a-071 — Check if a string is a palindrome using loop (variation 71)
- Level: Intermediate
- Loop type: do-while
- Approach: Use the appropriate loop construct for the iteration pattern.
- Time: O(n) unless specified otherwise.
- Edge cases: empty input, single element, large values.
- JS implementation: Write a function.
- C++ implementation: Use equivalent C++ syntax.
7.2a-072 — Find the LCM of two numbers (variation 72)
- Level: Intermediate
- Loop type: for
- Approach: Use the appropriate loop construct for the iteration pattern.
- Time: O(n) unless specified otherwise.
- Edge cases: empty input, single element, large values.
- JS implementation: Write a function.
- C++ implementation: Use equivalent C++ syntax.
7.2a-073 — Count vowels and consonants in a string (variation 73)
- Level: Intermediate
- Loop type: while
- Approach: Use the appropriate loop construct for the iteration pattern.
- Time: O(n) unless specified otherwise.
- Edge cases: empty input, single element, large values.
- JS implementation: Write a function.
- C++ implementation: Use equivalent C++ syntax.
7.2a-074 — Reverse an array in-place using loop (variation 74)
- Level: Intermediate
- Loop type: do-while
- Approach: Use the appropriate loop construct for the iteration pattern.
- Time: O(n) unless specified otherwise.
- Edge cases: empty input, single element, large values.
- JS implementation: Write a function.
- C++ implementation: Use equivalent C++ syntax.
7.2a-075 — Find the second largest element in an array (variation 75)
- Level: Intermediate
- Loop type: for
- Approach: Use the appropriate loop construct for the iteration pattern.
- Time: O(n) unless specified otherwise.
- Edge cases: empty input, single element, large values.
- JS implementation: Write a function.
- C++ implementation: Use equivalent C++ syntax.
7.2a-076 — Sum of first N natural numbers using loop (variation 76)
- Level: Intermediate
- Loop type: while
- Approach: Use the appropriate loop construct for the iteration pattern.
- Time: O(n) unless specified otherwise.
- Edge cases: empty input, single element, large values.
- JS implementation: Write a function.
- C++ implementation: Use equivalent C++ syntax.
7.2a-077 — Check if a number is a perfect number (variation 77)
- Level: Intermediate
- Loop type: do-while
- Approach: Use the appropriate loop construct for the iteration pattern.
- Time: O(n) unless specified otherwise.
- Edge cases: empty input, single element, large values.
- JS implementation: Write a function.
- C++ implementation: Use equivalent C++ syntax.
7.2a-078 — Print all prime numbers up to N (Sieve approach) (variation 78)
- Level: Intermediate
- Loop type: for
- Approach: Use the appropriate loop construct for the iteration pattern.
- Time: O(n) unless specified otherwise.
- Edge cases: empty input, single element, large values.
- JS implementation: Write a function.
- C++ implementation: Use equivalent C++ syntax.
7.2a-079 — Binary to decimal conversion using loop (variation 79)
- Level: Intermediate
- Loop type: while
- Approach: Use the appropriate loop construct for the iteration pattern.
- Time: O(n) unless specified otherwise.
- Edge cases: empty input, single element, large values.
- JS implementation: Write a function.
- C++ implementation: Use equivalent C++ syntax.
7.2a-080 — Find missing number in 1 to N array (variation 80)
- Level: Intermediate
- Loop type: do-while
- Approach: Use the appropriate loop construct for the iteration pattern.
- Time: O(n) unless specified otherwise.
- Edge cases: empty input, single element, large values.
- JS implementation: Write a function.
- C++ implementation: Use equivalent C++ syntax.
7.2a-081 — Count digits in a number using while (variation 81)
- Level: Intermediate
- Loop type: for
- Approach: Use the appropriate loop construct for the iteration pattern.
- Time: O(n) unless specified otherwise.
- Edge cases: empty input, single element, large values.
- JS implementation: Write a function.
- C++ implementation: Use equivalent C++ syntax.
7.2a-082 — Sum all elements in an array using for (variation 82)
- Level: Intermediate
- Loop type: while
- Approach: Use the appropriate loop construct for the iteration pattern.
- Time: O(n) unless specified otherwise.
- Edge cases: empty input, single element, large values.
- JS implementation: Write a function.
- C++ implementation: Use equivalent C++ syntax.
7.2a-083 — Find the maximum element in an array (variation 83)
- Level: Intermediate
- Loop type: do-while
- Approach: Use the appropriate loop construct for the iteration pattern.
- Time: O(n) unless specified otherwise.
- Edge cases: empty input, single element, large values.
- JS implementation: Write a function.
- C++ implementation: Use equivalent C++ syntax.
7.2a-084 — Count occurrences of a character in a string (variation 84)
- Level: Intermediate
- Loop type: for
- Approach: Use the appropriate loop construct for the iteration pattern.
- Time: O(n) unless specified otherwise.
- Edge cases: empty input, single element, large values.
- JS implementation: Write a function.
- C++ implementation: Use equivalent C++ syntax.
7.2a-085 — Print multiplication table for a number (variation 85)
- Level: Intermediate
- Loop type: while
- Approach: Use the appropriate loop construct for the iteration pattern.
- Time: O(n) unless specified otherwise.
- Edge cases: empty input, single element, large values.
- JS implementation: Write a function.
- C++ implementation: Use equivalent C++ syntax.
7.2a-086 — Generate first N Fibonacci numbers (variation 86)
- Level: Intermediate
- Loop type: do-while
- Approach: Use the appropriate loop construct for the iteration pattern.
- Time: O(n) unless specified otherwise.
- Edge cases: empty input, single element, large values.
- JS implementation: Write a function.
- C++ implementation: Use equivalent C++ syntax.
7.2a-087 — Check if a number is an Armstrong number (variation 87)
- Level: Intermediate
- Loop type: for
- Approach: Use the appropriate loop construct for the iteration pattern.
- Time: O(n) unless specified otherwise.
- Edge cases: empty input, single element, large values.
- JS implementation: Write a function.
- C++ implementation: Use equivalent C++ syntax.
7.2a-088 — Convert decimal to binary using while (variation 88)
- Level: Intermediate
- Loop type: while
- Approach: Use the appropriate loop construct for the iteration pattern.
- Time: O(n) unless specified otherwise.
- Edge cases: empty input, single element, large values.
- JS implementation: Write a function.
- C++ implementation: Use equivalent C++ syntax.
7.2a-089 — Find all factors of a number (variation 89)
- Level: Intermediate
- Loop type: do-while
- Approach: Use the appropriate loop construct for the iteration pattern.
- Time: O(n) unless specified otherwise.
- Edge cases: empty input, single element, large values.
- JS implementation: Write a function.
- C++ implementation: Use equivalent C++ syntax.
7.2a-090 — Compute power without Math.pow using loop (variation 90)
- Level: Intermediate
- Loop type: for
- Approach: Use the appropriate loop construct for the iteration pattern.
- Time: O(n) unless specified otherwise.
- Edge cases: empty input, single element, large values.
- JS implementation: Write a function.
- C++ implementation: Use equivalent C++ syntax.
7.2a-091 — Check if a string is a palindrome using loop (variation 91)
- Level: Intermediate
- Loop type: while
- Approach: Use the appropriate loop construct for the iteration pattern.
- Time: O(n) unless specified otherwise.
- Edge cases: empty input, single element, large values.
- JS implementation: Write a function.
- C++ implementation: Use equivalent C++ syntax.
7.2a-092 — Find the LCM of two numbers (variation 92)
- Level: Intermediate
- Loop type: do-while
- Approach: Use the appropriate loop construct for the iteration pattern.
- Time: O(n) unless specified otherwise.
- Edge cases: empty input, single element, large values.
- JS implementation: Write a function.
- C++ implementation: Use equivalent C++ syntax.
7.2a-093 — Count vowels and consonants in a string (variation 93)
- Level: Intermediate
- Loop type: for
- Approach: Use the appropriate loop construct for the iteration pattern.
- Time: O(n) unless specified otherwise.
- Edge cases: empty input, single element, large values.
- JS implementation: Write a function.
- C++ implementation: Use equivalent C++ syntax.
7.2a-094 — Reverse an array in-place using loop (variation 94)
- Level: Intermediate
- Loop type: while
- Approach: Use the appropriate loop construct for the iteration pattern.
- Time: O(n) unless specified otherwise.
- Edge cases: empty input, single element, large values.
- JS implementation: Write a function.
- C++ implementation: Use equivalent C++ syntax.
7.2a-095 — Find the second largest element in an array (variation 95)
- Level: Intermediate
- Loop type: do-while
- Approach: Use the appropriate loop construct for the iteration pattern.
- Time: O(n) unless specified otherwise.
- Edge cases: empty input, single element, large values.
- JS implementation: Write a function.
- C++ implementation: Use equivalent C++ syntax.
7.2a-096 — Sum of first N natural numbers using loop (variation 96)
- Level: Intermediate
- Loop type: for
- Approach: Use the appropriate loop construct for the iteration pattern.
- Time: O(n) unless specified otherwise.
- Edge cases: empty input, single element, large values.
- JS implementation: Write a function.
- C++ implementation: Use equivalent C++ syntax.
7.2a-097 — Check if a number is a perfect number (variation 97)
- Level: Intermediate
- Loop type: while
- Approach: Use the appropriate loop construct for the iteration pattern.
- Time: O(n) unless specified otherwise.
- Edge cases: empty input, single element, large values.
- JS implementation: Write a function.
- C++ implementation: Use equivalent C++ syntax.
7.2a-098 — Print all prime numbers up to N (Sieve approach) (variation 98)
- Level: Intermediate
- Loop type: do-while
- Approach: Use the appropriate loop construct for the iteration pattern.
- Time: O(n) unless specified otherwise.
- Edge cases: empty input, single element, large values.
- JS implementation: Write a function.
- C++ implementation: Use equivalent C++ syntax.
7.2a-099 — Binary to decimal conversion using loop (variation 99)
- Level: Intermediate
- Loop type: for
- Approach: Use the appropriate loop construct for the iteration pattern.
- Time: O(n) unless specified otherwise.
- Edge cases: empty input, single element, large values.
- JS implementation: Write a function.
- C++ implementation: Use equivalent C++ syntax.
7.2a-100 — Find missing number in 1 to N array (variation 100)
- Level: Intermediate
- Loop type: while
- Approach: Use the appropriate loop construct for the iteration pattern.
- Time: O(n) unless specified otherwise.
- Edge cases: empty input, single element, large values.
- JS implementation: Write a function.
- C++ implementation: Use equivalent C++ syntax.
7.2a-101 — Count digits in a number using while (variation 101)
- Level: Intermediate
- Loop type: do-while
- Approach: Use the appropriate loop construct for the iteration pattern.
- Time: O(n) unless specified otherwise.
- Edge cases: empty input, single element, large values.
- JS implementation: Write a function.
- C++ implementation: Use equivalent C++ syntax.
7.2a-102 — Sum all elements in an array using for (variation 102)
- Level: Intermediate
- Loop type: for
- Approach: Use the appropriate loop construct for the iteration pattern.
- Time: O(n) unless specified otherwise.
- Edge cases: empty input, single element, large values.
- JS implementation: Write a function.
- C++ implementation: Use equivalent C++ syntax.
7.2a-103 — Find the maximum element in an array (variation 103)
- Level: Intermediate
- Loop type: while
- Approach: Use the appropriate loop construct for the iteration pattern.
- Time: O(n) unless specified otherwise.
- Edge cases: empty input, single element, large values.
- JS implementation: Write a function.
- C++ implementation: Use equivalent C++ syntax.
7.2a-104 — Count occurrences of a character in a string (variation 104)
- Level: Intermediate
- Loop type: do-while
- Approach: Use the appropriate loop construct for the iteration pattern.
- Time: O(n) unless specified otherwise.
- Edge cases: empty input, single element, large values.
- JS implementation: Write a function.
- C++ implementation: Use equivalent C++ syntax.
7.2a-105 — Print multiplication table for a number (variation 105)
- Level: Intermediate
- Loop type: for
- Approach: Use the appropriate loop construct for the iteration pattern.
- Time: O(n) unless specified otherwise.
- Edge cases: empty input, single element, large values.
- JS implementation: Write a function.
- C++ implementation: Use equivalent C++ syntax.
7.2a-106 — Generate first N Fibonacci numbers (variation 106)
- Level: Intermediate
- Loop type: while
- Approach: Use the appropriate loop construct for the iteration pattern.
- Time: O(n) unless specified otherwise.
- Edge cases: empty input, single element, large values.
- JS implementation: Write a function.
- C++ implementation: Use equivalent C++ syntax.
7.2a-107 — Check if a number is an Armstrong number (variation 107)
- Level: Intermediate
- Loop type: do-while
- Approach: Use the appropriate loop construct for the iteration pattern.
- Time: O(n) unless specified otherwise.
- Edge cases: empty input, single element, large values.
- JS implementation: Write a function.
- C++ implementation: Use equivalent C++ syntax.
7.2a-108 — Convert decimal to binary using while (variation 108)
- Level: Intermediate
- Loop type: for
- Approach: Use the appropriate loop construct for the iteration pattern.
- Time: O(n) unless specified otherwise.
- Edge cases: empty input, single element, large values.
- JS implementation: Write a function.
- C++ implementation: Use equivalent C++ syntax.
7.2a-109 — Find all factors of a number (variation 109)
- Level: Intermediate
- Loop type: while
- Approach: Use the appropriate loop construct for the iteration pattern.
- Time: O(n) unless specified otherwise.
- Edge cases: empty input, single element, large values.
- JS implementation: Write a function.
- C++ implementation: Use equivalent C++ syntax.
7.2a-110 — Compute power without Math.pow using loop (variation 110)
- Level: Intermediate
- Loop type: do-while
- Approach: Use the appropriate loop construct for the iteration pattern.
- Time: O(n) unless specified otherwise.
- Edge cases: empty input, single element, large values.
- JS implementation: Write a function.
- C++ implementation: Use equivalent C++ syntax.
7.2a-111 — Check if a string is a palindrome using loop (variation 111)
- Level: Intermediate
- Loop type: for
- Approach: Use the appropriate loop construct for the iteration pattern.
- Time: O(n) unless specified otherwise.
- Edge cases: empty input, single element, large values.
- JS implementation: Write a function.
- C++ implementation: Use equivalent C++ syntax.
7.2a-112 — Find the LCM of two numbers (variation 112)
- Level: Intermediate
- Loop type: while
- Approach: Use the appropriate loop construct for the iteration pattern.
- Time: O(n) unless specified otherwise.
- Edge cases: empty input, single element, large values.
- JS implementation: Write a function.
- C++ implementation: Use equivalent C++ syntax.
7.2a-113 — Count vowels and consonants in a string (variation 113)
- Level: Intermediate
- Loop type: do-while
- Approach: Use the appropriate loop construct for the iteration pattern.
- Time: O(n) unless specified otherwise.
- Edge cases: empty input, single element, large values.
- JS implementation: Write a function.
- C++ implementation: Use equivalent C++ syntax.
7.2a-114 — Reverse an array in-place using loop (variation 114)
- Level: Intermediate
- Loop type: for
- Approach: Use the appropriate loop construct for the iteration pattern.
- Time: O(n) unless specified otherwise.
- Edge cases: empty input, single element, large values.
- JS implementation: Write a function.
- C++ implementation: Use equivalent C++ syntax.
7.2a-115 — Find the second largest element in an array (variation 115)
- Level: Intermediate
- Loop type: while
- Approach: Use the appropriate loop construct for the iteration pattern.
- Time: O(n) unless specified otherwise.
- Edge cases: empty input, single element, large values.
- JS implementation: Write a function.
- C++ implementation: Use equivalent C++ syntax.
7.2a-116 — Sum of first N natural numbers using loop (variation 116)
- Level: Intermediate
- Loop type: do-while
- Approach: Use the appropriate loop construct for the iteration pattern.
- Time: O(n) unless specified otherwise.
- Edge cases: empty input, single element, large values.
- JS implementation: Write a function.
- C++ implementation: Use equivalent C++ syntax.
7.2a-117 — Check if a number is a perfect number (variation 117)
- Level: Intermediate
- Loop type: for
- Approach: Use the appropriate loop construct for the iteration pattern.
- Time: O(n) unless specified otherwise.
- Edge cases: empty input, single element, large values.
- JS implementation: Write a function.
- C++ implementation: Use equivalent C++ syntax.
7.2a-118 — Print all prime numbers up to N (Sieve approach) (variation 118)
- Level: Intermediate
- Loop type: while
- Approach: Use the appropriate loop construct for the iteration pattern.
- Time: O(n) unless specified otherwise.
- Edge cases: empty input, single element, large values.
- JS implementation: Write a function.
- C++ implementation: Use equivalent C++ syntax.
7.2a-119 — Binary to decimal conversion using loop (variation 119)
- Level: Intermediate
- Loop type: do-while
- Approach: Use the appropriate loop construct for the iteration pattern.
- Time: O(n) unless specified otherwise.
- Edge cases: empty input, single element, large values.
- JS implementation: Write a function.
- C++ implementation: Use equivalent C++ syntax.
7.2a-120 — Find missing number in 1 to N array (variation 120)
- Level: Intermediate
- Loop type: for
- Approach: Use the appropriate loop construct for the iteration pattern.
- Time: O(n) unless specified otherwise.
- Edge cases: empty input, single element, large values.
- JS implementation: Write a function.
- C++ implementation: Use equivalent C++ syntax.
7.2a-121 — Count digits in a number using while (variation 121)
- Level: Advanced
- Loop type: while
- Approach: Use the appropriate loop construct for the iteration pattern.
- Time: O(n) unless specified otherwise.
- Edge cases: empty input, single element, large values.
- JS implementation: Write a function.
- C++ implementation: Use equivalent C++ syntax.
7.2a-122 — Sum all elements in an array using for (variation 122)
- Level: Advanced
- Loop type: do-while
- Approach: Use the appropriate loop construct for the iteration pattern.
- Time: O(n) unless specified otherwise.
- Edge cases: empty input, single element, large values.
- JS implementation: Write a function.
- C++ implementation: Use equivalent C++ syntax.
7.2a-123 — Find the maximum element in an array (variation 123)
- Level: Advanced
- Loop type: for
- Approach: Use the appropriate loop construct for the iteration pattern.
- Time: O(n) unless specified otherwise.
- Edge cases: empty input, single element, large values.
- JS implementation: Write a function.
- C++ implementation: Use equivalent C++ syntax.
7.2a-124 — Count occurrences of a character in a string (variation 124)
- Level: Advanced
- Loop type: while
- Approach: Use the appropriate loop construct for the iteration pattern.
- Time: O(n) unless specified otherwise.
- Edge cases: empty input, single element, large values.
- JS implementation: Write a function.
- C++ implementation: Use equivalent C++ syntax.
7.2a-125 — Print multiplication table for a number (variation 125)
- Level: Advanced
- Loop type: do-while
- Approach: Use the appropriate loop construct for the iteration pattern.
- Time: O(n) unless specified otherwise.
- Edge cases: empty input, single element, large values.
- JS implementation: Write a function.
- C++ implementation: Use equivalent C++ syntax.
7.2a-126 — Generate first N Fibonacci numbers (variation 126)
- Level: Advanced
- Loop type: for
- Approach: Use the appropriate loop construct for the iteration pattern.
- Time: O(n) unless specified otherwise.
- Edge cases: empty input, single element, large values.
- JS implementation: Write a function.
- C++ implementation: Use equivalent C++ syntax.
7.2a-127 — Check if a number is an Armstrong number (variation 127)
- Level: Advanced
- Loop type: while
- Approach: Use the appropriate loop construct for the iteration pattern.
- Time: O(n) unless specified otherwise.
- Edge cases: empty input, single element, large values.
- JS implementation: Write a function.
- C++ implementation: Use equivalent C++ syntax.
7.2a-128 — Convert decimal to binary using while (variation 128)
- Level: Advanced
- Loop type: do-while
- Approach: Use the appropriate loop construct for the iteration pattern.
- Time: O(n) unless specified otherwise.
- Edge cases: empty input, single element, large values.
- JS implementation: Write a function.
- C++ implementation: Use equivalent C++ syntax.
7.2a-129 — Find all factors of a number (variation 129)
- Level: Advanced
- Loop type: for
- Approach: Use the appropriate loop construct for the iteration pattern.
- Time: O(n) unless specified otherwise.
- Edge cases: empty input, single element, large values.
- JS implementation: Write a function.
- C++ implementation: Use equivalent C++ syntax.
7.2a-130 — Compute power without Math.pow using loop (variation 130)
- Level: Advanced
- Loop type: while
- Approach: Use the appropriate loop construct for the iteration pattern.
- Time: O(n) unless specified otherwise.
- Edge cases: empty input, single element, large values.
- JS implementation: Write a function.
- C++ implementation: Use equivalent C++ syntax.
7.2a-131 — Check if a string is a palindrome using loop (variation 131)
- Level: Advanced
- Loop type: do-while
- Approach: Use the appropriate loop construct for the iteration pattern.
- Time: O(n) unless specified otherwise.
- Edge cases: empty input, single element, large values.
- JS implementation: Write a function.
- C++ implementation: Use equivalent C++ syntax.
7.2a-132 — Find the LCM of two numbers (variation 132)
- Level: Advanced
- Loop type: for
- Approach: Use the appropriate loop construct for the iteration pattern.
- Time: O(n) unless specified otherwise.
- Edge cases: empty input, single element, large values.
- JS implementation: Write a function.
- C++ implementation: Use equivalent C++ syntax.
7.2a-133 — Count vowels and consonants in a string (variation 133)
- Level: Advanced
- Loop type: while
- Approach: Use the appropriate loop construct for the iteration pattern.
- Time: O(n) unless specified otherwise.
- Edge cases: empty input, single element, large values.
- JS implementation: Write a function.
- C++ implementation: Use equivalent C++ syntax.
7.2a-134 — Reverse an array in-place using loop (variation 134)
- Level: Advanced
- Loop type: do-while
- Approach: Use the appropriate loop construct for the iteration pattern.
- Time: O(n) unless specified otherwise.
- Edge cases: empty input, single element, large values.
- JS implementation: Write a function.
- C++ implementation: Use equivalent C++ syntax.
7.2a-135 — Find the second largest element in an array (variation 135)
- Level: Advanced
- Loop type: for
- Approach: Use the appropriate loop construct for the iteration pattern.
- Time: O(n) unless specified otherwise.
- Edge cases: empty input, single element, large values.
- JS implementation: Write a function.
- C++ implementation: Use equivalent C++ syntax.
7.2a-136 — Sum of first N natural numbers using loop (variation 136)
- Level: Advanced
- Loop type: while
- Approach: Use the appropriate loop construct for the iteration pattern.
- Time: O(n) unless specified otherwise.
- Edge cases: empty input, single element, large values.
- JS implementation: Write a function.
- C++ implementation: Use equivalent C++ syntax.
7.2a-137 — Check if a number is a perfect number (variation 137)
- Level: Advanced
- Loop type: do-while
- Approach: Use the appropriate loop construct for the iteration pattern.
- Time: O(n) unless specified otherwise.
- Edge cases: empty input, single element, large values.
- JS implementation: Write a function.
- C++ implementation: Use equivalent C++ syntax.
7.2a-138 — Print all prime numbers up to N (Sieve approach) (variation 138)
- Level: Advanced
- Loop type: for
- Approach: Use the appropriate loop construct for the iteration pattern.
- Time: O(n) unless specified otherwise.
- Edge cases: empty input, single element, large values.
- JS implementation: Write a function.
- C++ implementation: Use equivalent C++ syntax.
7.2a-139 — Binary to decimal conversion using loop (variation 139)
- Level: Advanced
- Loop type: while
- Approach: Use the appropriate loop construct for the iteration pattern.
- Time: O(n) unless specified otherwise.
- Edge cases: empty input, single element, large values.
- JS implementation: Write a function.
- C++ implementation: Use equivalent C++ syntax.
7.2a-140 — Find missing number in 1 to N array (variation 140)
- Level: Advanced
- Loop type: do-while
- Approach: Use the appropriate loop construct for the iteration pattern.
- Time: O(n) unless specified otherwise.
- Edge cases: empty input, single element, large values.
- JS implementation: Write a function.
- C++ implementation: Use equivalent C++ syntax.
7.2a-141 — Count digits in a number using while (variation 141)
- Level: Advanced
- Loop type: for
- Approach: Use the appropriate loop construct for the iteration pattern.
- Time: O(n) unless specified otherwise.
- Edge cases: empty input, single element, large values.
- JS implementation: Write a function.
- C++ implementation: Use equivalent C++ syntax.
7.2a-142 — Sum all elements in an array using for (variation 142)
- Level: Advanced
- Loop type: while
- Approach: Use the appropriate loop construct for the iteration pattern.
- Time: O(n) unless specified otherwise.
- Edge cases: empty input, single element, large values.
- JS implementation: Write a function.
- C++ implementation: Use equivalent C++ syntax.
7.2a-143 — Find the maximum element in an array (variation 143)
- Level: Advanced
- Loop type: do-while
- Approach: Use the appropriate loop construct for the iteration pattern.
- Time: O(n) unless specified otherwise.
- Edge cases: empty input, single element, large values.
- JS implementation: Write a function.
- C++ implementation: Use equivalent C++ syntax.
7.2a-144 — Count occurrences of a character in a string (variation 144)
- Level: Advanced
- Loop type: for
- Approach: Use the appropriate loop construct for the iteration pattern.
- Time: O(n) unless specified otherwise.
- Edge cases: empty input, single element, large values.
- JS implementation: Write a function.
- C++ implementation: Use equivalent C++ syntax.
7.2a-145 — Print multiplication table for a number (variation 145)
- Level: Advanced
- Loop type: while
- Approach: Use the appropriate loop construct for the iteration pattern.
- Time: O(n) unless specified otherwise.
- Edge cases: empty input, single element, large values.
- JS implementation: Write a function.
- C++ implementation: Use equivalent C++ syntax.
7.2a-146 — Generate first N Fibonacci numbers (variation 146)
- Level: Advanced
- Loop type: do-while
- Approach: Use the appropriate loop construct for the iteration pattern.
- Time: O(n) unless specified otherwise.
- Edge cases: empty input, single element, large values.
- JS implementation: Write a function.
- C++ implementation: Use equivalent C++ syntax.
7.2a-147 — Check if a number is an Armstrong number (variation 147)
- Level: Advanced
- Loop type: for
- Approach: Use the appropriate loop construct for the iteration pattern.
- Time: O(n) unless specified otherwise.
- Edge cases: empty input, single element, large values.
- JS implementation: Write a function.
- C++ implementation: Use equivalent C++ syntax.
7.2a-148 — Convert decimal to binary using while (variation 148)
- Level: Advanced
- Loop type: while
- Approach: Use the appropriate loop construct for the iteration pattern.
- Time: O(n) unless specified otherwise.
- Edge cases: empty input, single element, large values.
- JS implementation: Write a function.
- C++ implementation: Use equivalent C++ syntax.
7.2a-149 — Find all factors of a number (variation 149)
- Level: Advanced
- Loop type: do-while
- Approach: Use the appropriate loop construct for the iteration pattern.
- Time: O(n) unless specified otherwise.
- Edge cases: empty input, single element, large values.
- JS implementation: Write a function.
- C++ implementation: Use equivalent C++ syntax.
7.2a-150 — Compute power without Math.pow using loop (variation 150)
- Level: Advanced
- Loop type: for
- Approach: Use the appropriate loop construct for the iteration pattern.
- Time: O(n) unless specified otherwise.
- Edge cases: empty input, single element, large values.
- JS implementation: Write a function.
- C++ implementation: Use equivalent C++ syntax.
7.2a-151 — Check if a string is a palindrome using loop (variation 151)
- Level: Advanced
- Loop type: while
- Approach: Use the appropriate loop construct for the iteration pattern.
- Time: O(n) unless specified otherwise.
- Edge cases: empty input, single element, large values.
- JS implementation: Write a function.
- C++ implementation: Use equivalent C++ syntax.
7.2a-152 — Find the LCM of two numbers (variation 152)
- Level: Advanced
- Loop type: do-while
- Approach: Use the appropriate loop construct for the iteration pattern.
- Time: O(n) unless specified otherwise.
- Edge cases: empty input, single element, large values.
- JS implementation: Write a function.
- C++ implementation: Use equivalent C++ syntax.
7.2a-153 — Count vowels and consonants in a string (variation 153)
- Level: Advanced
- Loop type: for
- Approach: Use the appropriate loop construct for the iteration pattern.
- Time: O(n) unless specified otherwise.
- Edge cases: empty input, single element, large values.
- JS implementation: Write a function.
- C++ implementation: Use equivalent C++ syntax.
7.2a-154 — Reverse an array in-place using loop (variation 154)
- Level: Advanced
- Loop type: while
- Approach: Use the appropriate loop construct for the iteration pattern.
- Time: O(n) unless specified otherwise.
- Edge cases: empty input, single element, large values.
- JS implementation: Write a function.
- C++ implementation: Use equivalent C++ syntax.
7.2a-155 — Find the second largest element in an array (variation 155)
- Level: Advanced
- Loop type: do-while
- Approach: Use the appropriate loop construct for the iteration pattern.
- Time: O(n) unless specified otherwise.
- Edge cases: empty input, single element, large values.
- JS implementation: Write a function.
- C++ implementation: Use equivalent C++ syntax.
7.2a-156 — Sum of first N natural numbers using loop (variation 156)
- Level: Advanced
- Loop type: for
- Approach: Use the appropriate loop construct for the iteration pattern.
- Time: O(n) unless specified otherwise.
- Edge cases: empty input, single element, large values.
- JS implementation: Write a function.
- C++ implementation: Use equivalent C++ syntax.
7.2a-157 — Check if a number is a perfect number (variation 157)
- Level: Advanced
- Loop type: while
- Approach: Use the appropriate loop construct for the iteration pattern.
- Time: O(n) unless specified otherwise.
- Edge cases: empty input, single element, large values.
- JS implementation: Write a function.
- C++ implementation: Use equivalent C++ syntax.
7.2a-158 — Print all prime numbers up to N (Sieve approach) (variation 158)
- Level: Advanced
- Loop type: do-while
- Approach: Use the appropriate loop construct for the iteration pattern.
- Time: O(n) unless specified otherwise.
- Edge cases: empty input, single element, large values.
- JS implementation: Write a function.
- C++ implementation: Use equivalent C++ syntax.
7.2a-159 — Binary to decimal conversion using loop (variation 159)
- Level: Advanced
- Loop type: for
- Approach: Use the appropriate loop construct for the iteration pattern.
- Time: O(n) unless specified otherwise.
- Edge cases: empty input, single element, large values.
- JS implementation: Write a function.
- C++ implementation: Use equivalent C++ syntax.
7.2a-160 — Find missing number in 1 to N array (variation 160)
- Level: Advanced
- Loop type: while
- Approach: Use the appropriate loop construct for the iteration pattern.
- Time: O(n) unless specified otherwise.
- Edge cases: empty input, single element, large values.
- JS implementation: Write a function.
- C++ implementation: Use equivalent C++ syntax.
7.2a-161 — Count digits in a number using while (variation 161)
- Level: Advanced
- Loop type: do-while
- Approach: Use the appropriate loop construct for the iteration pattern.
- Time: O(n) unless specified otherwise.
- Edge cases: empty input, single element, large values.
- JS implementation: Write a function.
- C++ implementation: Use equivalent C++ syntax.
7.2a-162 — Sum all elements in an array using for (variation 162)
- Level: Advanced
- Loop type: for
- Approach: Use the appropriate loop construct for the iteration pattern.
- Time: O(n) unless specified otherwise.
- Edge cases: empty input, single element, large values.
- JS implementation: Write a function.
- C++ implementation: Use equivalent C++ syntax.
7.2a-163 — Find the maximum element in an array (variation 163)
- Level: Advanced
- Loop type: while
- Approach: Use the appropriate loop construct for the iteration pattern.
- Time: O(n) unless specified otherwise.
- Edge cases: empty input, single element, large values.
- JS implementation: Write a function.
- C++ implementation: Use equivalent C++ syntax.
7.2a-164 — Count occurrences of a character in a string (variation 164)
- Level: Advanced
- Loop type: do-while
- Approach: Use the appropriate loop construct for the iteration pattern.
- Time: O(n) unless specified otherwise.
- Edge cases: empty input, single element, large values.
- JS implementation: Write a function.
- C++ implementation: Use equivalent C++ syntax.
7.2a-165 — Print multiplication table for a number (variation 165)
- Level: Advanced
- Loop type: for
- Approach: Use the appropriate loop construct for the iteration pattern.
- Time: O(n) unless specified otherwise.
- Edge cases: empty input, single element, large values.
- JS implementation: Write a function.
- C++ implementation: Use equivalent C++ syntax.
7.2a-166 — Generate first N Fibonacci numbers (variation 166)
- Level: Advanced
- Loop type: while
- Approach: Use the appropriate loop construct for the iteration pattern.
- Time: O(n) unless specified otherwise.
- Edge cases: empty input, single element, large values.
- JS implementation: Write a function.
- C++ implementation: Use equivalent C++ syntax.
7.2a-167 — Check if a number is an Armstrong number (variation 167)
- Level: Advanced
- Loop type: do-while
- Approach: Use the appropriate loop construct for the iteration pattern.
- Time: O(n) unless specified otherwise.
- Edge cases: empty input, single element, large values.
- JS implementation: Write a function.
- C++ implementation: Use equivalent C++ syntax.
7.2a-168 — Convert decimal to binary using while (variation 168)
- Level: Advanced
- Loop type: for
- Approach: Use the appropriate loop construct for the iteration pattern.
- Time: O(n) unless specified otherwise.
- Edge cases: empty input, single element, large values.
- JS implementation: Write a function.
- C++ implementation: Use equivalent C++ syntax.
7.2a-169 — Find all factors of a number (variation 169)
- Level: Advanced
- Loop type: while
- Approach: Use the appropriate loop construct for the iteration pattern.
- Time: O(n) unless specified otherwise.
- Edge cases: empty input, single element, large values.
- JS implementation: Write a function.
- C++ implementation: Use equivalent C++ syntax.
7.2a-170 — Compute power without Math.pow using loop (variation 170)
- Level: Advanced
- Loop type: do-while
- Approach: Use the appropriate loop construct for the iteration pattern.
- Time: O(n) unless specified otherwise.
- Edge cases: empty input, single element, large values.
- JS implementation: Write a function.
- C++ implementation: Use equivalent C++ syntax.