1.
Write a Python program to check whether the given number is positive,
negative or zero.
Algorithm: Check if a Number is Positive, Negative, or Zero
Step 1: Start
Step 2: Input a number from the user and store it in variable num
Step 3:
If num > 0:
Print "It is a positive number"
Else if num == 0:
Print "It is zero"
Else:
Print "It is a negative number"
Step 4: End
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.
Algorithm: Even or Odd Number Checker
Step 1: Start
Step 2: Input a number from the user and store it in variable num
Step 3: Calculate the remainder of num divided by 2 and store it in variable mod
Step 4:
If mod > 0, then:
Print "This is an odd number."
Else:
Print "This is an even number."
Step 5: End
Program
num = int(input("Enter a number: "))
# Calculate the remainder when the number is divided by 2
mod = num % 2
# Check if the remainder is greater than 0, indicating an odd number
if mod > 0:
# Print a message indicating that the number is odd
print("This is an odd number.")
else:
# Print a message indicating that the number is even
print("This is an even number.")
output
Enter a number: 6
This is an even number.
3. Write a python program to guess the secret number between 1 to 9. If
the number is the same then it is a right guess else a wrong guess
Algorithm: Random Number Guessing Game
Step 1: Start
Step 2: Import the random module
Step 3: Generate a random number between 1 and 10 and store it in target_num
Step 4: Initialize guess_num as 0
Step 5:
Repeat the following steps while guess_num is not equal to target_num:
a. Prompt the user to guess a number between 1 and 10
b. Convert the input to an integer and store it in guess_num
Step 6: Once the guess is correct, print "Well guessed!"
Step 7: End
import random
# Generate a random number between 1 and 10 (inclusive) as the target number
target_num, guess_num = random.randint(1, 10), 0
# Start a loop that continues until the guessed number matches the target number
while target_num != guess_num:
# Prompt the user to input a number between 1 and 10 and convert it to an integer
guess_num = int(input('Guess a number between 1 and 10 until you get it right : '))
# Print a message indicating successful guessing once the correct number is guessed
print('Well guessed!')
output
Guess a number between 1 and 10 until you get it right : 9
Well guessed!
4. Write a python program to check whether a given number is Armstrong or
not. [Hint - An Armstrong number is any number of n digits which is equal to
the sum 1 of nth power of digits in the number. For example, 371 is an
Armstrong number since 3 ** 3 + 7 ** 3 + 1 ** 3 = 371]
Algorithm: Armstrong Number Checker
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.