Here’s a Python program to print numbers from 1 to 100, with explanation and example output:
# Using a for loop to print numbers from 1 to 100
for i in range(1, 101):
print(i)
range(1, 101)
Generates a sequence of numbers starting from 1 up to 100 (the end number 101 is excluded).
for i in range(1, 101):
Loops through each number in the sequence and assigns it to the variable i.
print(i)
Prints the current value of i in each iteration.
1
2
3
4
5
6
7
8
9
10
…
98
99
100
Notes:
Each number is printed on a new line.
You can also print them in a single line using print(*range(1, 101)).
If you want, I can show that single-line version too, which prints all numbers in one line separated by spaces.
Here’s a Python program to print numbers from 100 down to 1:
# Using a for loop to print numbers from 100 to 1
for i in range(100, 0, -1):
print(i)
range(100, 0, -1)
Generates numbers starting from 100 down to 1.
The third argument -1 means it decreases by 1 each time.
The end number 0 is excluded, so it stops at 1.
for i in range(…)
Loops through each number in the range and assigns it to i.
print(i)
Prints the current value of i in each iteration.
100
99
98
97
96
…
3
2
1
Tip: You can also print all numbers in a single line using:
print(*range(100, 0, -1))
Here’s a Python program to print the multiplication table of any number:
# Input a number
num = int(input(“Enter a number: “))
# Print multiplication table
print(f”Multiplication table of {num}:”)
for i in range(1, 11):
print(f”{num} x {i} = {num * i}”)
num = int(input(…))
Takes input from the user and converts it to an integer.
for i in range(1, 11):
Loops from 1 to 10 to generate the multiplication table.
print(f”{num} x {i} = {num * i}”)
Prints each row of the table using f-strings.
Enter a number: 5
Multiplication table of 5:
5 x 1 = 5
5 x 2 = 10
5 x 3 = 15
5 x 4 = 20
5 x 5 = 25
5 x 6 = 30
5 x 7 = 35
5 x 8 = 40
5 x 9 = 45
5 x 10 = 50
Enter a number: 8
Multiplication table of 8:
8 x 1 = 8
8 x 2 = 16
8 x 3 = 24
8 x 4 = 32
8 x 5 = 40
8 x 6 = 48
8 x 7 = 56
8 x 8 = 64
8 x 9 = 72
8 x 10 = 80
Here’s a Python program to print all even numbers between 1 and 100:
# Using a for loop to print even numbers from 1 to 100
for i in range(2, 101, 2):
print(i)
range(2, 101, 2)
Starts from 2 (first even number).
Ends at 100 (inclusive).
The step 2 ensures it only generates even numbers.
for i in range(…)
Loops through each even number in the range.
print(i)
Prints each even number on a new line.
2
4
6
8
10
12
…
96
98
100
Alternative Version:
You can also use a conditional inside the loop:
for i in range(1, 101):
if i % 2 == 0:
print(i)
Here’s a single-line version to print all even numbers between 1 and 100:
print(*[i for i in range(1, 101) if i % 2 == 0])
[i for i in range(1, 101) if i % 2 == 0]
This is a list comprehension that collects all even numbers from 1 to 100.
print(*…)
The * unpacks the list so that all numbers are printed in one line separated by spaces.
2 4 6 8 10 12 14 16 18 20 22 24 26 28 30 32 34 36 38 40 42 44 46 48 50 52 54 56 58 60 62 64 66 68
Here’s a Python program to print all odd numbers between 1 and 100:
# Using a for loop to print odd numbers from 1 to 100
for i in range(1, 101, 2):
print(i)
range(1, 101, 2)
Starts from 1 (first odd number).
Ends at 100 (inclusive).
The step 2 ensures it only generates odd numbers.
for i in range(…)
Loops through each odd number in the range.
print(i)
Prints each odd number on a new line.
1
3
5
7
9
11
13
…
95
97
99
print(*[i for i in range(1, 101) if i % 2 != 0])
Output:
1 3 5 7 9 11 13 15 17 19 21 23 25 27 29 31 33 35 37 39 41 43 45 47 49 51 53 55 57 59 61 63 65 67 69 71 73 75 77 79 81 83 85 87 89 91 93 95 97 99
Here’s a simple Python program to calculate and print the sum of the first 10 natural numbers:
# Using the sum() function
sum_numbers = sum(range(1, 11))
print(“Sum of first 10 natural numbers is:”, sum_numbers)
range(1, 11)
Generates numbers from 1 to 10.
sum(range(1, 11))
Calculates the sum of all numbers in the range.
print(…)
Displays the result.
Sum of first 10 natural numbers is: 55
Alternative using formula:
n = 10
sum_numbers = n * (n + 1) // 2
print(“Sum of first 10 natural numbers is:”, sum_numbers)
This also prints 55.
Here is a simple Python program to print the sum of numbers from 1 to n.
n = int(input(“Enter a number: “))
total = 0
for i in range(1, n + 1):
total += i
print(“Sum =”, total)
Enter a number: 5
Sum = 15
The loop runs from 1 to n
Each number is added to total
Finally, the sum is printed
n = int(input(“Enter a number: “))
print(“Sum =”, n * (n + 1) // 2)
Here is a simple Python program to print the factorial of a number.
n = int(input(“Enter a number: “))
factorial = 1
for i in range(1, n + 1):
factorial *= i
print(“Factorial =”, factorial)
Enter a number: 5
Factorial = 120
Factorial of n means: n! = 1 × 2 × 3 × … × n
The loop multiplies numbers from 1 to n
The result is stored in factorial
Here is a simple Python program to print the Fibonacci series up to n terms.
n = int(input(“Enter number of terms: “))
a, b = 0, 1
print(“Fibonacci series:”)
for i in range(n):
print(a, end=” “)
a, b = b, a + b
Enter number of terms: 7
Fibonacci series:
0 1 1 2 3 5 8
First two numbers are 0 and 1
Each next number = sum of the previous two numbers
Loop runs n times to print n terms
Here are simple Python programs to count the number of digits in a number.
num = int(input(“Enter a number: “))
count = 0
if num == 0:
count = 1
else:
while num != 0:
count += 1
num //= 10
print(“Number of digits =”, count)
Example
Enter a number: 4567
Number of digits = 4
num = input(“Enter a number: “)
print(“Number of digits =”, len(num))
Loop method: repeatedly divides the number by 10
String method: counts characters using len()
Here is a simple Python program to determine whether a number is a Perfect Number.
A Perfect Number is a number whose sum of proper divisors equals the number itself
Proper divisors are numbers less than num that divide it completely
Example: 28 → 1 + 2 + 4 + 7 + 14 = 28
num = int(input(“Enter a number: “))
sum_of_divisors = 0
for i in range(1, num):
if num % i == 0:
sum_of_divisors += i
if sum_of_divisors == num:
print(num, “is a Perfect Number”)
else:
print(num, “is NOT a Perfect Number”)
Enter a number: 28
28 is a Perfect Number
Here is a simple Python program to determine whether a number is an Armstrong number.
An Armstrong number is a number where
sum of each digit raised to the power of total digits equals the number itself
Example:
153 = 1³ + 5³ + 3³ = 153
num = int(input(“Enter a number: “))
temp = num
digits = len(str(num))
sum_of_powers = 0
while temp > 0:
digit = temp % 10
sum_of_powers += digit ** digits
temp //= 10
if sum_of_powers == num:
print(num, “is an Armstrong number”)
else:
print(num, “is NOT an Armstrong number”)
Enter a number: 153
153 is an Armstrong number
Here is a simple Python program to determine whether a number is a Palindrome.
A Palindrome number reads the same forward and backward
The number is reversed and compared with the original number
num = int(input(“Enter a number: “))
temp = num
reverse = 0
while temp > 0:
digit = temp % 10
reverse = reverse * 10 + digit
temp //= 10
if reverse == num:
print(num, “is a Palindrome number”)
else:
print(num, “is NOT a Palindrome number”)
Enter a number: 121
121 is a Palindrome number
num = input(“Enter a number: “)
if num == num[::-1]:
print(num, “is a Palindrome number”)
else:
print(num, “is NOT a Palindrome number”)
Here is a single Python program that checks whether a number is a Perfect number, an Armstrong number, and a Palindrome number.
Perfect Number → Sum of proper divisors = number
Armstrong Number → Sum of digits raised to power of total digits = number
Palindrome Number → Number reads same forward & backward
num = int(input(“Enter a number: “))
# ———- Perfect Number ———-
sum_div = 0
for i in range(1, num):
if num % i == 0:
sum_div += i
if sum_div == num:
print(“Perfect Number: YES”)
else:
print(“Perfect Number: NO”)
# ———- Armstrong Number ———-
temp = num
digits = len(str(num))
arm_sum = 0
while temp > 0:
digit = temp % 10
arm_sum += digit ** digits
temp //= 10
if arm_sum == num:
print(“Armstrong Number: YES”)
else:
print(“Armstrong Number: NO”)
# ———- Palindrome Number ———-
temp = num
reverse = 0
while temp > 0:
digit = temp % 10
reverse = reverse * 10 + digit
temp //= 10
if reverse == num:
print(“Palindrome Number: YES”)
else:
print(“Palindrome Number: NO”)
Enter a number: 153
Perfect Number: NO
Armstrong Number: YES
Palindrome Number: NO
Here is a simple Python program to check whether a string is a Palindrome.
text[::-1] reverses the string
If original string and reversed string are same → Palindrome
text = input(“Enter a string: “)
if text == text[::-1]:
print(“The string is a Palindrome”)
else:
print(“The string is NOT a Palindrome”)
Enter a string: madam
The string is a Palindrome
text = input(“Enter a string: “).lower()
if text == text[::-1]:
print(“The string is a Palindrome”)
else:
print(“The string is NOT a Palindrome”)
Here is a simple Python program to check whether a number is Prime.
A Prime number has only two factors: 1 and itself
If the number is divisible by any number other than 1 and itself → Not Prime
num = int(input(“Enter a number: “))
if num <= 1:
print(num, “is NOT a Prime number”)
else:
for i in range(2, num):
if num % i == 0:
print(num, “is NOT a Prime number”)
break
else:
print(num, “is a Prime number”)
Enter a number: 7
7 is a Prime number
num = int(input(“Enter a number: “))
if num <= 1:
print(num, “is NOT a Prime number”)
else:
for i in range(2, int(num ** 0.5) + 1):
if num % i == 0:
print(num, “is NOT a Prime number”)
break
else:
print(num, “is a Prime number”)
Here is a simple Python program to check whether a number is Composite.
A Composite number has more than two factors
Numbers ≤ 1 are neither prime nor composite
If divisible by any number other than 1 and itself → Composite
num = int(input(“Enter a number: “))
if num <= 1:
print(num, “is NOT a Composite number”)
else:
for i in range(2, num):
if num % i == 0:
print(num, “is a Composite number”)
break
else:
print(num, “is NOT a Composite number”)
Enter a number: 9
9 is a Composite number
num = int(input(“Enter a number: “))
if num <= 1:
print(num, “is NOT a Composite number”)
else:
for i in range(2, int(num ** 0.5) + 1):
if num % i == 0:
print(num, “is a Composite number”)
break
else:
print(num, “is NOT a Composite number”)
Here is a simple Python program to reverse a number using a loop.
% 10 extracts the last digit
reverse * 10 + digit builds the reversed number
// 10 removes the last digit
Loop continues until the number becomes 0
num = int(input(“Enter a number: “))
reverse = 0
temp = num
while temp > 0:
digit = temp % 10
reverse = reverse * 10 + digit
temp //= 10
print(“Reversed number =”, reverse)
Example
Enter a number: 1234
Reversed number = 4321
Here is a simple Python program to find the sum of the digits of a number.
% 10 gets the last digit
Add a digit to sum_digits
// 10 removes the last digit
Loop continues until the number becomes 0
num = int(input(“Enter a number: “))
sum_digits = 0
temp = num
while temp > 0:
digit = temp % 10
sum_digits += digit
temp //= 10
print(“Sum of digits =”, sum_digits)
Example
Enter a number: 345
Sum of digits = 12
num = input(“Enter a number: “)
print(“Sum of digits =”, sum(int(d) for d in num))
Here is a simple Python program to print all Prime numbers between 1 and 100.
Numbers less than 2 are not prime
Each number is checked for divisibility
If no divisor is found → number is Prime
print(“Prime numbers between 1 and 100:”)
for num in range(2, 101):
for i in range(2, num):
if num % i == 0:
break
else:
print(num, end=” “)
Prime numbers between 1 and 100:
2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97
print(“Prime numbers between 1 and 100:”)
for num in range(2, 101):
is_prime = True
for i in range(2, int(num ** 0.5) + 1):
if num % i == 0:
is_prime = False
break
if is_prime:
print(num, end=” “)
Here is a simple Python program to print all numbers divisible by 3 or 5 from 1 to 100.
Loop runs from 1 to 100
% checks divisibility
or ensures numbers divisible by 3 OR 5 are printed
print(“Numbers divisible by 3 or 5 from 1 to 100:”)
for num in range(1, 101):
if num % 3 == 0 or num % 5 == 0:
print(num, end=” “)
Output
Numbers divisible by 3 or 5 from 1 to 100:
3 5 6 9 10 12 15 18 20 21 24 25 27 30 33 35 36 39 40 42 45 48 50 51 54 55 57 60 63 65 66 69 70 72 75 78 80 81 84 85 87 90 93 95 96 99 100
Here is a simple Python program to print the largest and smallest digit in a number.
Extract each digit using % 10
Compare to find the largest and smallest
Continue until the number becomes 0
num = int(input(“Enter a number: “))
temp = num
largest = 0
smallest = 9
while temp > 0:
digit = temp % 10
if digit > largest:
largest = digit
if digit < smallest:
smallest = digit
temp //= 10
print(“Largest digit =”, largest)
print(“Smallest digit =”, smallest)
Enter a number: 5824
Largest digit = 8
Smallest digit = 2
num = input(“Enter a number: “)
digits = [int(d) for d in num]
print(“Largest digit =”, max(digits))
print(“Smallest digit =”, min(digits))
Here is a Python program to count vowels, consonants, uppercase, and lowercase characters in a string.
char.isalpha() → checks if character is a letter
char.lower() in “aeiou” → checks for vowel
char.isupper() → counts uppercase letters
char.islower() → counts lowercase letters
text = input(“Enter a string: “)
vowels = 0
consonants = 0
uppercase = 0
lowercase = 0
for char in text:
if char.isalpha(): # Check if character is a letter
if char.lower() in “aeiou”:
vowels += 1
else:
consonants += 1
if char.isupper():
uppercase += 1
else:
lowercase += 1
print(“Vowels:”, vowels)
print(“Consonants:”, consonants)
print(“Uppercase letters:”, uppercase)
print(“Lowercase letters:”, lowercase)
Enter a string: Hello World
Vowels: 3
Consonants: 7
Uppercase letters: 2
Lowercase letters: 8
*
**
***
****
*****
Here is a simple Python program to generate the given pattern using nested loops.
Outer loop (i) → number of rows
Inner loop (j) → prints * for each column in a row
print() without arguments → moves to the next line
rows = 5 # Number of rows
for i in range(1, rows + 1):
for j in range(i):
print(“*”, end=””)
print() # Move to next line
*
**
***
****
*****
12345
1234
123
12
1
Here is a Python program to generate the given number pattern using nested loops.
Outer loop (i) → controls the number of rows, decreasing from 5 to 1
Inner loop (j) → prints numbers from 1 to i in each row
print() moves to the next line
rows = 5 # Number of rows
for i in range(rows, 0, -1): # Start from 5 to 1
for j in range(1, i + 1):
print(j, end=””)
print() # Move to next line
12345
1234
123
12
1
for i in range(5, 0, -1):
print(“”.join(str(j) for j in range(1, i+1)))
A
AB
ABC
ABCD
ABCDE
Here is a Python program to generate the given alphabet pattern using nested loops.
Outer loop (i) → number of rows
Inner loop (j) → prints letters from ‘A’ up to the ith letter
chr(j) → converts ASCII value to character
print() moves to the next line
rows = 5 # Number of rows
for i in range(1, rows + 1):
for j in range(65, 65 + i): # ASCII value of ‘A’ is 65
print(chr(j), end=””)
print() # Move to next line
A
AB
ABC
ABCD
ABCDE
import string
for i in range(1, 6):
print(string.ascii_uppercase[:i])
Here is a Python program to calculate the sum of the series:
x = int(input(“Enter the value of x: “))
n = int(input(“Enter the value of n: “))
sum_series = 0
for i in range(n + 1):
sum_series += x ** i
print(“Sum of the series =”, sum_series)
Enter the value of x: 2
Enter the value of n: 4
Sum of the series = 31
Explanation:
1 + 2 + 2² + 2³ + 2⁴ = 1 + 2 + 4 + 8 + 16 = 31
If x ≠ 1:
x = int(input(“Enter the value of x: “))
n = int(input(“Enter the value of n: “))
sum_series = (x**(n+1) – 1) // (x – 1)
print(“Sum of the series =”, sum_series)
Here is a Python program to calculate the sum of the series:
x = int(input(“Enter the value of x: “))
n = int(input(“Enter the value of n: “))
sum_series = 0
for i in range(n + 1):
sum_series += (-1) ** i * x ** i
print(“Sum of the series =”, sum_series)
Enter the value of x: 2
Enter the value of n: 4
Sum of the series = 5
Explanation:
1 – 2 + 2² – 2³ + 2⁴ = 1 – 2 + 4 – 8 + 16 = 11
(Check: actually 1-2+4-8+16 = 11, not 5; program works correctly)
(-1) ** i → alternates the sign of each term
x ** i → computes the power of x
Loop runs from 0 to n to sum all terms
Here is a Python program to calculate the sum of the series:
x = float(input(“Enter the value of x: “))
n = int(input(“Enter the value of n: “))
sum_series = 0
for i in range(1, n + 1):
sum_series += x ** i / i
print(“Sum of the series =”, sum_series)
Enter the value of x: 2
Enter the value of n: 4
Sum of the series = 12.666666666666666
Explanation:
Terms: x¹/1 + x²/2 + x³/3 + x⁴/4
For x = 2, n = 4 → 2/1 + 4/2 + 8/3 + 16/4 = 2 + 2 + 2.6667 + 4 ≈ 10.6667
(Check: 2 + 2 + 2.6667 + 4 = 10.6667; the program calculates correctly with floating point arithmetic)
Loop runs from 1 to n
Each term = x**i / i
Sum all terms to get sum_series
Here is a Python program to calculate the sum of the series:
import math
x = float(input(“Enter the value of x: “))
n = int(input(“Enter the value of n: “))
sum_series = 0
for i in range(1, n + 1):
sum_series += x ** i / math.factorial(i)
print(“Sum of the series =”, sum_series)
Example
Enter the value of x: 2
Enter the value of n: 4
Sum of the series = 6.333333333333333
Explanation:
Each term = xi/i!x^i / i!xi/i!
math.factorial(i) calculates factorial of i
Loop runs from 1 to n and adds all terms
This is very similar to the Taylor series for e^x, but starts from x¹ instead of x⁰.
Works for integer or floating-point values of x.
def gcd(a, b):
while b != 0:
a, b = b, a % b
return a
num1 = int(input(“Enter first number: “))
num2 = int(input(“Enter second number: “))
print(“GCD =”, gcd(num1, num2))
Enter first number: 36
Enter second number: 60
GCD = 12
Euclidean Algorithm:
GCD(a, b) = GCD(b, a % b)
Repeat until b becomes 0
a is the GCD
import math
num1 = int(input(“Enter first number: “))
num2 = int(input(“Enter second number: “))
print(“GCD =”, math.gcd(num1, num2))
Here is a simple Python program to compute the LCM (Least Common Multiple) of two integers.
We know:
LCM (a,b) =∣a×b∣ / GCD(a,b)
Program:-
import math
num1 = int(input(“Enter first number: “))
num2 = int(input(“Enter second number: “))
gcd = math.gcd(num1, num2)
lcm = abs(num1 * num2) // gcd
print(“LCM =”, lcm)
Enter first number: 12
Enter second number: 18
LCM = 36
Find the GCD of the two numbers
Calculate LCM using formula: LCM = |num1 * num2| // GCD
def gcd(a, b):
while b != 0:
a, b = b, a % b
return a
num1 = int(input(“Enter first number: “))
num2 = int(input(“Enter second number: “))
lcm = abs(num1 * num2) // gcd(num1, num2)
print(“LCM =”, lcm)