0% found this document useful (0 votes)
2 views15 pages

Loops Question Set

This is very good

Uploaded by

Anveshka Shukla
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views15 pages

Loops Question Set

This is very good

Uploaded by

Anveshka Shukla
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

For loop

EASY LEVEL (1–10)


Print Numbers from 1 to N
Problem:
Given an integer N, print numbers from 1 to N using a for loop.
Input:
An integer N.
Output:
Numbers from 1 to N (space separated).
Example:
Input: 5
Output:
1 2 3 4 5
Sum of First N Numbers
Problem:
Given N, compute the sum of numbers from 1 to N.
Input:
Integer N.
Output:
Single integer representing the sum.
Example:
Input: 5
Output:
15
Multiplication Table
Problem:
Given an integer N, print its multiplication table up to 10.
Input:
Integer N.
Output:
N x 1 = …

N x 10 = …
Example:
Input: 3
Output:
3 x 1 = 3
3 x 2 = 6
...
3 x 10 = 30
Count Even Numbers
Problem:
Given N, count how many even numbers exist between 1 and N.
Input:
Integer N.
Output:
Count of even numbers.
Example:
Input: 10
Output:
5
Reverse Counting
Problem:
Print numbers from N down to 1.
Input:
Integer N.
Output:
N to 1.
Example:
Input: 5
Output:
5 4 3 2 1
Square Series
Problem:
Print squares of numbers from 1 to N.
Input:
Integer N.
Output:
Sequence of squares.
Example:
Input: 4
Output:
1 4 9 16
Count Digits
Problem:
Given an integer N, count how many digits it contains.
Input:
Integer N.
Output:
Digit count.
Example:
Input: 12345
Output:
5
Sum of Digits
Problem:
Given N, find sum of its digits.
Input:
Integer N.
Output:
Sum of digits.
Example:
Input: 123
Output:
6
Count Vowels
Problem:
Given a string S, count number of vowels.
Input:
String S.
Output:
Integer count.
Example:
Input: hello
Output:
2
Factorial
Problem:
Compute factorial of N using loop.
Input:
Integer N.
Output:
Factorial value.
Example:
Input: 5
Output:
120
MEDIUM LEVEL (11–20)
Reverse a Number
Problem:
Reverse digits of integer N.
Input:
Integer N.
Output:
Reversed number.
Example:
Input: 123
Output:
321
Fibonacci Series
Problem:
Print first N Fibonacci numbers.
Input:
Integer N.
Output:
Sequence.
Example:
Input: 5
Output:
0 1 1 2 3
Prime Check
Problem:
Determine if N is prime.
Input:
Integer N.
Output:
True or False.
Example:
Input: 7
Output:
True
Print All Factors
Problem:
Print all factors of N.
Input:
Integer N.
Output:
Factors in ascending order.
Example:
Input: 6
Output:
1 2 3 6
Largest Digit
Problem:
Find largest digit in N.
Input:
Integer N.
Output:
Largest digit.
Example:
Input: 5482
Output:
8
Armstrong Number
Problem:
Check whether N is an Armstrong number (3-digit).
Input:
Integer N.
Output:
True or False.
Example:
Input: 153
Output:
True
Count Uppercase and Lowercase
Problem:
Given string S, count uppercase and lowercase characters.
Input:
String S.
Output:
Two integers.
Example:
Input: PyThOn
Output:
Uppercase: 3
Lowercase: 3
Sum of Even and Odd Separately
Problem:
Given N, compute sum of even and odd numbers from 1 to N separately.
Input:
Integer N.
Output:
Two integers.
Example:
Input: 5
Output:
Even: 6
Odd: 9
Pattern Printing – Increasing Stars
Problem:
Print pattern:
Input: 4
Output:
*
**
Pattern – Number Triangle
Input: 4
Output:
1
12
123
1234
HARD LEVEL (21–30)
Print All Primes up to N
Problem:
Print all prime numbers between 1 and N.
Input:
Integer N.
Output:
All primes in ascending order.
Example:
Input: 10
Output:
2 3 5 7
Palindrome Number
Problem:
Check whether N is palindrome.
Input:
Integer N.
Output:
True or False.
Example:
Input: 121
Output:
True
Perfect Number
Problem:
Check whether N is perfect (sum of proper divisors equals number).
Input:
Integer N.
Output:
True or False.
Example:
Input: 6
Output:
True
Strong Number
Problem:
Check if N is Strong number (sum of factorial of digits equals number).
Input:
Integer N.
Output:
True or False.
Example:
Input: 145
Output:
True
Pyramid Pattern
Problem:
Given an integer N, print a centered pyramid of stars.
Input:
4
Output:
*
***
*****
*******

Reverse Pyramid
Problem:
Given an integer N, print an inverted centered pyramid.
Input:
4
Output:
*******
*****
***
*

Number Palindrome Pattern
Problem:
Given an integer N, print a number palindrome pyramid.
Input:
4
Output:
1
121
12321
1234321

Hollow Pyramid Pattern
Problem:
Print a hollow pyramid of height N.
Input:
4
Output:
*
* *
* *
*******

Right-Aligned Increasing Pattern
Problem:
Print a right-aligned increasing star pattern.
Input:
4
Output:
*
**
***
****

Diamond Pattern
Problem:
Print a diamond pattern of height N.
Input:
4
Output:
*
***
*****
*******
*****
***
*

# While loop

## EASY LEVEL (1–10)

1. Print numbers from 1 to N

Problem:

Given an integer N, print numbers from 1 to N using a while loop.

Example:

```
Input:5
Output:
12345
```

---

1. Print numbers from N to 1

```
Input:5
Output:
54321
```

---

1. Print all even numbers up to N

```
Input:10
Output:
246810
```

---

1. Print all odd numbers up to N

```
Input:10
Output:
13579
```

---

1. Find sum from 1 to N

```
Input:5
Output:
15
```

---

1. Print multiplication table of N

```
Input:3
Output:
3x1=3
3x2=6
...
3x10=30
```

---

1. Count digits in a number

```
Input:12345
Output:
5
```

---

1. Reverse a number

```
Input:123
Output:
321
```

---

1. Sum of digits

```
Input:123
Output:
6
```

---

1. Print factorial of N

```
Input:5
Output:
120
```
---

## MEDIUM LEVEL (11–20)

1. Check if number is palindrome

```
Input:121
Output:
True
```

---

1. Check if number is Armstrong (3-digit)

```
Input:153
Output:
True
```

---

1. Print Fibonacci series up to N terms

```
Input:5
Output:
01123
```

---

1. Check if number is prime

```
Input:7
Output:
True
```

---

1. Print all factors of N

```
Input:6
Output:
1236
```

---

1. Find largest digit in number

```
Input:5482
Output:
8
```

---
1. Count frequency of a digit in number

```
Input:112233
Digit:2
Output:
2
```

---

1. Calculate power without using ** operator

```
Input:
Base:2
Power:3

Output:
8
```

---

1. Convert decimal to binary

```
Input:5
Output:
101
```

---

1. Print this pattern (using while)

```
Input:4
Output:
*
**
***
****
```

---

## HARD LEVEL (21–30)

1. Print reverse pattern

```
Input:4
Output:
****
***
**
*
```

---

1. Print centered pyramid (while loop only)


```
Input:4
Output:
*
***
*****
*******
```

---

1. Print number triangle

```
Input:4
Output:
1
12
123
1234
```

---

1. Print number palindrome pattern

```
Input:4
Output:
1
121
12321
1234321
```

---

1. Find GCD of two numbers using while loop

```
Input:
1218
Output:
6
```

---

1. Find LCM using while

```
Input:
46
Output:
12
```

---

1. Keep asking user input until they enter 0

Example:
```
Input:
5
3
8
0

Output:
Programstopped
```

---

1. Password validation loop

Keep asking user for password until correct password is entered.

---

1. Banking simulation

Keep deducting withdrawal until balance becomes insufficient.

---

1. Menu-driven calculator

Keep showing menu until user selects Exit.

Example menu:

```
1. Add
2. Subtract
3. Multiply
4. Exit
```

# Nested loop

## EASY LEVEL (1–10)


Nested Loops + break / continue

1. Print a 5x5 grid of numbers

Print numbers from 1 to 5 in 5 rows using nested loops.

Example:

```
Output:
12345
12345
12345
12345
12345
```

---

1. Print multiplication table from 1 to 3

```
Output:
123
246
369
```

---

1. Print triangle but skip number 2 using continue

```
Output:
1
13
134
```

---

1. Print numbers 1 to 5 but stop completely if number 4 appears (use break).

---

1. Nested loop: Stop inner loop when j == 3

```
Output:
12
12
12
```

---

1. Skip printing even numbers inside nested loop (use continue).

---

1. Print pattern but skip middle row using continue.

---

1. Search for number in 2D grid; break when found.

---

1. Print numbers but use pass when number is 5.

---

1. Print 3x3 matrix but break outer loop if i == 3.

---

## MEDIUM LEVEL (11–20)


More Logic with break, continue

1. Print prime numbers from 1 to 20 using nested loops.

Break inner loop once divisor found.

---
1. Multiplication table but skip multiples of 5 using continue.

---

1. Print square pattern but stop entire process if user enters 0.

---

1. Nested loop: Print coordinates (i, j).

Break if i == j.

Example:

```
(1,1)→stoprow
(2,1)
(2,2)→stoprow
```

---

1. Print pyramid but skip printing star when column equals center.

---

1. Search for a character in a string grid.

Break both loops when found.

---

1. Print 5x5 pattern but skip border elements using continue.

---

1. Password retry system:

Outer loop limits 3 attempts.

Inner loop checks conditions.

Break when correct.

---

1. Print triangle but use pass inside condition when number is divisible by
4.

---

1. Print matrix but break outer loop if sum of row exceeds 10.

---

## HARD LEVEL (21–30)


Complex Nested Logic

1. Prime Pattern

Print numbers 1–50, and for each number check if prime using nested loop.
Use break once factor is found.

---

1. FizzBuzz Grid

Print numbers 1–25 in 5x5 grid.

If divisible by 3 → Fizz

If divisible by 5 → Buzz

Use continue to skip normal print when replaced.

---

1. Triangle pattern but stop entire program when row == 4.

---

1. Nested loop login system

Outer loop: 3 attempts

Inner loop: validate password length

Break when correct

Continue when incorrect

---

1. Print multiplication matrix but skip diagonal elements.

---

1. Find first pair (i, j) such that i*j == target.

Break both loops when found.

---

1. Print diamond pattern but skip center line using continue.

---

1. Print 1–100 grid 10x10.

Stop entire program if number 57 appears.

---

1. Generate all pairs from two lists.

Skip pairs where sum is even (continue).

---
1. Simulated ATM system

Outer loop: menu

Inner loop: validate choice

Break when exit selected

Continue on invalid input

Use pass for unimplemented option.

You might also like