Loops Practice
for loops, while loops, break, continue, and else clauses in Python.
1
Print 1 to N
Easy
Given an integer N, print all numbers from 1 to N on separate lines.
Input Format
A single integer N.
Output Format
Print numbers from 1 to N, each on a new line.
Sample Test Cases
|
Sample Input 1
5 |
Sample Output 1
1 2 3 4 5 |
|
Sample Input 2
1 |
Sample Output 2
1 |
|
Sample Input 3
10 |
Sample Output 3
1 2 3 4 5 6 7 8 9 10 |
Constraints
1 ≤ N ≤ 1000
Explanation
Use a for loop with range(1, N+1).
2
Sum of N Natural Numbers
Easy
Given an integer N, find the sum of the first N natural numbers.
Input Format
A single integer N.
Output Format
Print the sum as an integer.
Sample Test Cases
|
Sample Input 1
10 |
Sample Output 1
55 |
|
Sample Input 2
100 |
Sample Output 2
5050 |
|
Sample Input 3
1 |
Sample Output 3
1 |
Constraints
1 ≤ N ≤ 10^6
Explanation
Use a for loop to accumulate, or the formula N*(N+1)//2
3
Multiplication Table
Easy
Given an integer N, print its multiplication table from 1 to 10.
Input Format
A single integer N.
Output Format
Print 10 lines in format: "N x i = result" for i from 1 to 10.
Sample Test Cases
|
Sample Input 1
7 |
Sample Output 1
7 x 1 = 7 7 x 2 = 14 7 x 3 = 21 7 x 4 = 28 7 x 5 = 35 7 x 6 = 42 7 x 7 = 49 7 x 8 = 56 7 x 9 = 63 7 x 10 = 70 |
|
Sample Input 2
3 |
Sample Output 2
3 x 1 = 3 3 x 2 = 6 3 x 3 = 9 3 x 4 = 12 3 x 5 = 15 3 x 6 = 18 3 x 7 = 21 3 x 8 = 24 3 x 9 = 27 3 x 10 = 30 |
|
Sample Input 3
12 |
Sample Output 3
12 x 1 = 12 12 x 2 = 24 12 x 3 = 36 12 x 4 = 48 12 x 5 = 60 12 x 6 = 72 12 x 7 = 84 12 x 8 = 96 12 x 9 = 108 12 x 10 = 120 |
Constraints
1 ≤ N ≤ 100
Explanation
Loop i from 1 to 10 and print f"{N} x {i} = {N*i}"
4
Even Numbers up to N
Easy
Print all even numbers 2 to N.
Input Format
Single int N
Output Format
Space-separated evens.
Sample Test Cases
|
Sample Input 1
10 |
Sample Output 1
2 4 6 8 10 |
|
Sample Input 2
1 |
Sample Output 2
|
|
Sample Input 3
20 |
Sample Output 3
2 4 6 8 10 12 14 16 18 20 |
Constraints
1≤N≤1000
Explanation
range(2,N+1,2)
5
Sum of Even Numbers
Easy
Sum of even numbers 2 to N.
Input Format
Single int N
Output Format
Sum int.
Sample Test Cases
|
Sample Input 1
10 |
Sample Output 1
30 |
|
Sample Input 2
1 |
Sample Output 2
0 |
|
Sample Input 3
1000000 |
Sample Output 3
250000500000 |
Constraints
1≤N≤10⁶
Explanation
Step 2 loop.
6
Print in Reverse
Easy
Numbers N down to 1.
Input Format
Single int N
Output Format
Space-separated.
Sample Test Cases
|
Sample Input 1
5 |
Sample Output 1
5 4 3 2 1 |
|
Sample Input 2
1 |
Sample Output 2
1 |
|
Sample Input 3
10 |
Sample Output 3
10 9 8 7 6 5 4 3 2 1 |
Constraints
1≤N≤1000
Explanation
range(N,0,-1)
7
Count Digits
Easy
Count digits using while loop (N//=10).
Input Format
Single int N
Output Format
Digit count.
Sample Test Cases
|
Sample Input 1
98765 |
Sample Output 1
5 |
|
Sample Input 2
1000000000 |
Sample Output 2
10 |
|
Sample Input 3
5 |
Sample Output 3
1 |
Constraints
1≤N≤10⁹
Explanation
while N>0: count++, N//=10
8
Sum of Odd Numbers
Easy
Sum of odd numbers 1 to N.
Input Format
Single int N
Output Format
Sum int.
Sample Test Cases
|
Sample Input 1
9 |
Sample Output 1
25 |
|
Sample Input 2
2 |
Sample Output 2
1 |
|
Sample Input 3
1000000 |
Sample Output 3
250000000000 |
Constraints
1≤N≤10⁶
Explanation
Step 2 from 1.
9
Print Characters
Easy
Each character of string on new line.
Input Format
Single line string
Output Format
Each char per line.
Sample Test Cases
|
Sample Input 1
Python |
Sample Output 1
P y t h o n |
|
Sample Input 2
Hi |
Sample Output 2
H i |
|
Sample Input 3
ABC |
Sample Output 3
A B C |
Constraints
1≤|S|≤100
Explanation
for ch in S: print(ch)
10
Reverse Table
Easy
Multiplication table 10 down to 1.
Input Format
Single int N
Output Format
10 lines N×i=result.
Sample Test Cases
|
Sample Input 1
6 |
Sample Output 1
6×10=60 6×9=54 6×8=48 6×7=42 6×6=36 6×5=30 6×4=24 6×3=18 6×2=12 6×1=6 |
|
Sample Input 2
3 |
Sample Output 2
3×10=30 3×9=27 3×8=24 3×7=21 3×6=18 3×5=15 3×4=12 3×3=9 3×2=6 3×1=3 |
|
Sample Input 3
10 |
Sample Output 3
10×10=100 10×9=90 10×8=80 10×7=70 10×6=60 10×5=50 10×4=40 10×3=30 10×2=20 10×1=10 |
Constraints
1≤N≤100
Explanation
range(10,0,-1)
1
Factorial of a Number
Medium
Given a non-negative integer N, compute its factorial N! = N × (N-1) × (N-2) × ... × 1. Factorial of 0 is 1.
Input Format
A single integer N.
Output Format
Print the factorial as an integer.
Sample Test Cases
|
Sample Input 1
6 |
Sample Output 1
720 |
|
Sample Input 2
0 |
Sample Output 2
1 |
|
Sample Input 3
10 |
Sample Output 3
3628800 |
Constraints
0 ≤ N ≤ 20
Explanation
Initialize result = 1, loop from 1 to N multiplying step by step.
2
Fibonacci Series
Medium
Generate the first N terms of the Fibonacci series: 0, 1, 1, 2, 3, 5, 8, 13, ... where each term is the sum of the previous two.
Input Format
A single integer N (number of terms).
Output Format
Print N space-separated Fibonacci numbers.
Sample Test Cases
|
Sample Input 1
8 |
Sample Output 1
0 1 1 2 3 5 8 13 |
|
Sample Input 2
1 |
Sample Output 2
0 |
|
Sample Input 3
10 |
Sample Output 3
0 1 1 2 3 5 8 13 21 34 |
Constraints
1 ≤ N ≤ 50
Explanation
Start with a=0, b=1. In a loop, print a, then update a,b = b, a+b.
3
Prime Number Checker
Medium
Given an integer N, determine if it is a prime number (divisible only by 1 and itself).
Input Format
A single integer N.
Output Format
Print "Prime" if N is prime, otherwise print "Not Prime".
Sample Test Cases
|
Sample Input 1
29 |
Sample Output 1
Prime |
|
Sample Input 2
2 |
Sample Output 2
Prime |
|
Sample Input 3
49 |
Sample Output 3
Not Prime |
Constraints
1 ≤ N ≤ 10^6
Explanation
Loop from 2 to sqrt(N). If N % i == 0 for any i, it is not prime.
4
Perfect Number Loop
Medium
Sum of proper divisors equals N?
Input Format
Single int N
Output Format
"Perfect" or "Not Perfect"
Sample Test Cases
|
Sample Input 1
496 |
Sample Output 1
Perfect |
|
Sample Input 2
28 |
Sample Output 2
Perfect |
|
Sample Input 3
12 |
Sample Output 3
Not Perfect |
Constraints
1≤N≤10000
Explanation
Loop 1..N//2, sum.
5
Sum of Squares
Medium
1²+2²+...+N²
Input Format
Single int N
Output Format
Sum int.
Sample Test Cases
|
Sample Input 1
5 |
Sample Output 1
55 |
|
Sample Input 2
1 |
Sample Output 2
1 |
|
Sample Input 3
1000 |
Sample Output 3
333833500 |
Constraints
1≤N≤1000
Explanation
sum += i*i
6
Palindromic Number Count
Medium
Count palindromes between A and B inclusive.
Input Format
Two ints A B
Output Format
Count.
Sample Test Cases
|
Sample Input 1
10 50 |
Sample Output 1
4 |
|
Sample Input 2
1 9 |
Sample Output 2
9 |
|
Sample Input 3
100 120 |
Sample Output 3
2 |
Constraints
1≤A≤B≤10⁵
Explanation
str(n)==str(n)[::-1]
7
Harshad Number
Medium
N divisible by sum of its digits.
Input Format
Single int N
Output Format
"Harshad" or "Not"
Sample Test Cases
|
Sample Input 1
18 |
Sample Output 1
Harshad Number |
|
Sample Input 2
21 |
Sample Output 2
Harshad Number |
|
Sample Input 3
15 |
Sample Output 3
Not |
Constraints
1≤N≤10⁶
Explanation
Sum digits, check N%sum==0.
8
Find Factors
Medium
All factors of N ascending.
Input Format
Single int N
Output Format
Space-separated factors.
Sample Test Cases
|
Sample Input 1
36 |
Sample Output 1
1 2 3 4 6 9 12 18 36 |
|
Sample Input 2
1 |
Sample Output 2
1 |
|
Sample Input 3
100 |
Sample Output 3
1 2 4 5 10 20 25 50 100 |
Constraints
1≤N≤10⁶
Explanation
1..N, N%i==0
9
Decimal to Binary
Medium
Convert N to binary using repeated division by 2.
Input Format
Single int N
Output Format
Binary (no leading zeros).
Sample Test Cases
|
Sample Input 1
25 |
Sample Output 1
11001 |
|
Sample Input 2
1000000 |
Sample Output 2
11110100001001000000 |
|
Sample Input 3
7 |
Sample Output 3
111 |
Constraints
1≤N≤10⁶
Explanation
while N>0: prepend N%2
10
LCM via Loop
Medium
Smallest number divisible by both A and B.
Input Format
Two ints A B
Output Format
LCM int.
Sample Test Cases
|
Sample Input 1
15 20 |
Sample Output 1
60 |
|
Sample Input 2
7 11 |
Sample Output 2
77 |
|
Sample Input 3
4 6 |
Sample Output 3
12 |
Constraints
1≤A,B≤10⁴
Explanation
Start from max(A,B), increment.
1
Armstrong Number Check
Hard
An Armstrong number (also called narcissistic number) is a number that equals the sum of its own digits each raised to the power of the number of digits. For example, 153 = 1³ + 5³ + 3³. Check if a given number is an Armstrong number.
Input Format
A single integer N.
Output Format
Print "Armstrong Number" if N is an Armstrong number, otherwise print "Not an Armstrong Number".
Sample Test Cases
|
Sample Input 1
153 |
Sample Output 1
Armstrong Number |
|
Sample Input 2
9474 |
Sample Output 2
Armstrong Number |
|
Sample Input 3
123 |
Sample Output 3
Not an Armstrong Number |
Constraints
1 ≤ N ≤ 10^7
Explanation
Count digits first, then extract each digit, raise to power of digit count, and sum.
2
Count Digits, Letters, and Special Characters
Hard
Given a string containing a mix of characters, count the number of digits, alphabetic letters, and special characters in it.
Input Format
A single line containing a string S.
Output Format
Print three lines: "Digits: [count]", "Letters: [count]", "Specials: [count]"
Sample Test Cases
|
Sample Input 1
Hello123!@# |
Sample Output 1
Digits: 3 Letters: 5 Specials: 3 |
|
Sample Input 2
Abc123$%^ |
Sample Output 2
Digits: 3 Letters: 3 Specials: 3 |
|
Sample Input 3
No digits here |
Sample Output 3
Digits: 0 Letters: 12 Specials: 2 |
Constraints
1 ≤ |S| ≤ 1000
Explanation
Loop through each character. Use .isdigit(), .isalpha() methods.
3
Circular Prime
Hard
N and all its digit rotations are prime.
Input Format
Single int N
Output Format
"Circular Prime" or "Not"
Sample Test Cases
|
Sample Input 1
197 |
Sample Output 1
Circular Prime |
|
Sample Input 2
13 |
Sample Output 2
Circular Prime |
|
Sample Input 3
23 |
Sample Output 3
Not |
Constraints
10≤N≤10⁶
Explanation
Rotate digits, check prime each.
4
Ugly Number
Hard
Only prime factors 2,3,5.
Input Format
Single int N
Output Format
"Ugly Number" or "Not"
Sample Test Cases
|
Sample Input 1
14 |
Sample Output 1
Not an Ugly Number |
|
Sample Input 2
1 |
Sample Output 2
Ugly Number |
|
Sample Input 3
30 |
Sample Output 3
Ugly Number |
Constraints
1≤N≤10⁶
Explanation
Divide by 2,3,5 until can't.
5
Happy Number
Hard
Sum of squares of digits → 1 or cycle. Detect cycle with set.
Input Format
Single int N
Output Format
"Happy" or "Unhappy"
Sample Test Cases
|
Sample Input 1
19 |
Sample Output 1
Happy Number |
|
Sample Input 2
2 |
Sample Output 2
Unhappy |
|
Sample Input 3
4 |
Sample Output 3
Unhappy |
Constraints
1≤N≤10⁶
Explanation
Set to detect cycles.
6
Disarium Numbers
Hard
Sum of digits^position equals number. Find between A and B.
Input Format
Two ints A B
Output Format
Space-separated or "None".
Sample Test Cases
|
Sample Input 1
1 100 |
Sample Output 1
1 2 3 4 5 6 7 8 9 89 |
|
Sample Input 2
1 10 |
Sample Output 2
1 2 3 4 5 6 7 8 9 |
|
Sample Input 3
90 100 |
Sample Output 3
None |
Constraints
1≤A≤B≤10000
Explanation
Extract digits, sum powers.
7
Smith Number
Hard
Composite with sum-of-digits = sum-of-digits of prime factors.
Input Format
Single int N
Output Format
"Smith Number" or "Not"
Sample Test Cases
|
Sample Input 1
666 |
Sample Output 1
Smith Number |
|
Sample Input 2
85 |
Sample Output 2
Smith Number |
|
Sample Input 3
22 |
Sample Output 3
Smith Number |
Constraints
4≤N≤10⁵
Explanation
Find prime factors, sum digits.
8
Kaprekar Constant
Hard
4-digit N reaches 6174. Count iterations (desc-asc).
Input Format
Single 4-digit N (not all same)
Output Format
Iteration count.
Sample Test Cases
|
Sample Input 1
3524 |
Sample Output 1
3 |
|
Sample Input 2
1000 |
Sample Output 2
5 |
|
Sample Input 3
6174 |
Sample Output 3
0 |
Constraints
1000-9999, not identical
Explanation
Subtract asc from desc, repeat.
9
Floyd's Triangle
Hard
First N rows: sequential numbers in right triangle.
Input Format
Single int N
Output Format
N rows, row i has i numbers.
Sample Test Cases
|
Sample Input 1
5 |
Sample Output 1
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
|
Sample Input 2
1 |
Sample Output 2
1 |
|
Sample Input 3
3 |
Sample Output 3
1 2 3 4 5 6 |
Constraints
1≤N≤20
Explanation
Counter + nested loops.
10
Neon Number
Hard
Sum of digits of N² equals N.
Input Format
Single int N
Output Format
"Neon Number" or "Not"
Sample Test Cases
|
Sample Input 1
9 |
Sample Output 1
Neon Number |
|
Sample Input 2
1 |
Sample Output 2
Neon Number |
|
Sample Input 3
45 |
Sample Output 3
Not |
Constraints
1≤N≤10⁴
Explanation
N*N, sum digits, compare.