The document provides Python programs for various tasks including checking if a number is an Armstrong number, reversing a number, printing natural and even numbers, calculating simple interest, and finding the area and perimeter of a rectangle and triangle. Each program includes user input, calculations, and output statements. The document serves as a practical guide for beginners to understand basic programming concepts in Python.
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 ratings0% found this document useful (0 votes)
6 views
Imp python pgm
The document provides Python programs for various tasks including checking if a number is an Armstrong number, reversing a number, printing natural and even numbers, calculating simple interest, and finding the area and perimeter of a rectangle and triangle. Each program includes user input, calculations, and output statements. The document serves as a practical guide for beginners to understand basic programming concepts in Python.
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
You are on page 1/ 3
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)