Module 1 - Programs Python
Module 1 - Programs Python
program
num = float(input("Input a number: "))
# Check if the number is greater than zero.
if num > 0:
# If true, print that it is a positive number.
print("It is a positive number")
# Check if the number is equal to zero.
elif num == 0:
# If true, print that it is zero.
print("It is zero")
else:
# If the above conditions are not met, print that it is a negative number.
print("It is a negative number")
Output
Input a number: 5
It is a positive number
2. Write a Python program to check whether the number is even or odd.
output
Enter a number: 6
This is an even number.
output
Guess a number between 1 and 10 until you get it right : 9
Well guessed!
Step 1: Start
Step 2: Initialize the number num
Step 3: Convert num to a string and store it in num_str
Step 4: Calculate the length of num_str and store it in num_len
Step 5:
Initialize a variable armstrong_sum to 0
For each digit d in num_str:
a. Convert d to an integer
b. Raise it to the power of num_len
c. Add the result to armstrong_sum
Step 6:
If armstrong_sum equals num:
Print "num is an Armstrong number"
Else:
Print "num is not an Armstrong number"
Step 7: End
num = 153
num_str = str(num)
num_len = len(num_str)
armstrong_sum = sum([int(digit) ** num_len for digit in num_str])
if armstrong_sum == num:
print(f"{num} is an Armstrong number.")
else:
print(f"{num} is not an Armstrong number.")
output
153 is an Armstrong number.