Python program to check if a number is an Armstrong number:
# Get user input
num = int(input("Enter a number: "))
# Initialize sum
sum = 0
# Find the sum of the cube of each digit
temp = num
while temp > 0:
digit = temp % 10 # Get last digit
sum += digit ** 3 # Cube the digit and add to sum
temp //= 10 # Remove last digit
# Display the result
if num == sum:
print(num, "is an Armstrong number")
else:
print(num, "is not an Armstrong number")
Explanation:
1. Take input from the user.
2. Extract each digit using % 10.
3. Cube the digit and add it to sum.
4. Remove the last digit using // 10.
5. If the sum of cubes equals the original number, it's an Armstrong number.
# Read an integer from the user
n = int(input("Enter number: "))
# Initialize reverse number
rev = 0
# Loop to extract and reverse digits
while n > 0:
dig = n % 10 # Get the last digit
rev = rev * 10 + dig # Append the digit to rev
n = n // 10 # Remove the last digit
# Print the reversed number
print("Reverse of the number:", rev)
17. **Printing First 10 Natural Numbers**
```python
for i in range(1, 11):
print(i)
```
18. **Printing First 10 Even Numbers**
```python
for i in range(2, 21, 2):
print(i)
```
19. **Printing Odd Numbers from 1 to n**
```python
n = int(input("Enter value of n: "))
for i in range(1, n+1, 2):
print(i)
7. **Calculating Simple Interest**
```python
principal = 2000
rate = 4.5
time = 10
simple_interest = (principal * rate * time) / 100
print("Simple Interest:", simple_interest)
```
### **Input-Based Programs**
8. **Calculating Area and Perimeter of a Rectangle**
```python
length = float(input("Enter length: "))
breadth = float(input("Enter breadth: "))
area = length * breadth
perimeter = 2 * (length + breadth)
print("Area:", area)
print("Perimeter:", perimeter)
```
9. **Calculating Area of a Triangle**
```python
base = float(input("Enter base: "))
height = float(input("Enter height: "))
area = 0.5 * base * height
print("Area of Triangle:", area)