num1 = float(input("Enter the first number: ")) num2 = float(input("Enter the second number: ")) sum = num1 + num2 print("Sum:", sum) difference = num1 - num2 print("Difference:", difference) product = num1 * num2 print("Product:", product) quotient = num1 / num2 print("Quotient:", quotient)
This program prompts the user to enter two numbers and performs basic arithmetic operations like addition, subtraction, multiplication, and division.
import random number = random.randint(1, 10) guess = int(input("Guess a number between 1 and 10: ")) if guess == number: print("Congratulations! You guessed it right.") else: print("Sorry, wrong guess. The number was", number)
In this program, the computer generates a random number between 1 and 10, and the user tries to guess the number. It provides feedback on whether the guess was correct or not.
num = int(input("Enter a number: ")) if num > 1: 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.") else: print(num, "is not a prime number.")
This program checks if a given number is prime or not. It uses a simple loop to iterate through numbers and checks for divisibility.
n = int(input("Enter the number of terms: ")) a, b = 0, 1 print("Fibonacci Series:") print(a, end=" ") for _ in range(n - 1): print(b, end=" ") a, b = b, a + b
This program generates the Fibonacci series up to a specified number of terms. It uses a loop and two variables to calculate and display the series.