1.) Python program to print message "Hello, World!".
print("Hello, World!")
2.) Python program to check if a Number is Even or Odd.
num = int(input("Enter a number: "))
if num % 2 == 0:
print(f"{num} is an even number.")
else:
print(f"{num} is an odd number.")
3.) Python program to find the Sum of Two Numbers.
num1 = float(input("Enter the first number: "))
num2 = float(input("Enter the second number: "))
sum = num1 + num2
print(f"The sum of {num1} and {num2} is {sum}.")
4.) Python program to calculate Factorial of a Number.
num = int(input("Enter a Positive Integer Number: "))
factorial = 1
if num < 0:
print("Factorial does not exist for negative numbers.")
elif num == 0:
print("The factorial of 0 is 1.")
else:
for i in range(1, num + 1):
factorial *= i
print(f"The factorial of {num} is {factorial}.")
5.) Python program for Simple Calculator.
def calculator(num1, num2, operator):
if operator == '+':
return num1 + num2
elif operator == '-':
return num1 - num2
elif operator == '*':
return num1 * num2
elif operator == '/':
return num1 / num2
else:
return "Invalid operator"
num1 = float(input("Enter first number: "))
operator = input("Enter operator (+, -, *, /): ")
num2 = float(input("Enter second number: "))
result = calculator(num1, num2, operator)
print(f"Result: {result}")
6.) Python program to check whether given number is Prime Number or not.
num = int(input("Enter a number: "))
if num > 1:
for i in range(2, num):
if num % i == 0:
print(f"{num} is not a prime number.")
break
else:
print(f"{num} is a prime number.")
else:
print(f"{num} is not a prime number.")
7.) Python program to print Fibonacci Series.
n = int(input("How many terms? "))
a, b = 0, 1
count = 0
if n <= 0:
print("Please enter a positive integer.")
elif n == 1:
print("Fibonacci sequence up to 1 term: ")
print(a)
else:
print("Fibonacci sequence: ")
while count < n:
print(a)
nth = a + b
a = b
b = nth
count += 1
8.) Python program to find the Largest of Three Numbers.
num1 = float(input("Enter first number: "))
num2 = float(input("Enter second number: "))
num3 = float(input("Enter third number: "))
if num1 >= num2 and num1 >= num3:
largest = num1
elif num2 >= num1 and num2 >= num3:
largest = num2
else:
largest = num3
print(f"The largest number is {largest}.")
9.) Python program to find that given number or word is palindrome or not.
word = input("Enter a word or number: ")
if word == word[::-1]:
print(f"{word} is a palindrome.")
else:
print(f"{word} is not a palindrome.")
10.) Python program to count the Number of Vowels in a String.
string = input("Enter a string: ")
vowels = "aeiouAEIOU"
count = 0
for char in string:
if char in vowels:
count += 1
print(f"The number of vowels in the string is {count}.")
11.) Python program to reverse a String.
string = input("Enter a string: ")
reversed_string = string[::-1]
print(f"Reversed string: {reversed_string}")
12.) Python program to count occurrences of a Character in a String.
string = input("Enter a string: ")
char = input("Enter the character to count: ")
count = string.count(char)
print(f"The character '{char}' appears {count} times in the string.")
13.) Python program to check for an Armstrong Number.
num = int(input("Enter a number: "))
order = len(str(num))
sum = 0
temp = num
while temp > 0:
digit = temp % 10
sum += digit ** order
temp //= 10
if num == sum:
print(f"{num} is an Armstrong number.")
else:
print(f"{num} is not an Armstrong number.")
14.) Python program to check Leap Year.
year = int(input("Enter a year: "))
if (year % 400 == 0) or (year % 100 != 0 and year % 4 == 0):
print(f"{year} is a leap year.")
else:
print(f"{year} is not a leap year.")
15.) Python program to calculate Sum of Natural Numbers.
n = int(input("Enter a positive integer: "))
if n < 0:
print("Enter a positive integer.")
else:
sum = (n * (n + 1)) // 2
print(f"The sum of the first {n} natural numbers is {sum}.")
16.) Python program to find the Smallest Number in a List.
numbers = [int(x) for x in input("Enter numbers separated by spaces: ").split()]
smallest = min(numbers)
print(f"The smallest number is {smallest}.")
17.) Python program to find the Largest Word in a Sentence.
sentence = input("Enter a sentence: ")
words = sentence.split()
largest_word = max(words, key=len)
print(f"The largest word is '{largest_word}'.")
18.) Python program to convert Celsius to Fahrenheit.
celsius = float(input("Enter temperature in Celsius: "))
fahrenheit = (celsius * 9/5) + 32
print(f"{celsius}°C is equal to {fahrenheit}°F.")
19.) Python program to generate a Multiplication Table.
num = int(input("Enter a number: "))
for i in range(1, 11):
print(f"{num} x {i} = {num * i}")
20.) Python program to calculate Simple Interest.
principal = float(input("Enter the principal amount: "))
rate = float(input("Enter the annual interest rate (in %): "))
time = float(input("Enter the time (in years): "))
simple_interest = (principal * rate * time) / 100
print(f"The simple interest is {simple_interest}.")
21.) Python program to check that given List is Sorted or Not.
numbers = [int(x) for x in input("Enter numbers separated by spaces: ").split()]
if numbers == sorted(numbers):
print("The list is sorted.")
else:
print("The list is not sorted.")
22.) Python program to remove Duplicate elements from a List.
numbers = [int(x) for x in input("Enter numbers separated by spaces: ").split()]
unique_numbers = list(set(numbers))
print(f"List after removing duplicates: {unique_numbers}")
23.) Python program to find the GCD (Greatest Common Divisor) of Two Numbers.
def gcd(a, b):
while b:
a, b = b, a % b
return a
num1 = int(input("Enter the first number: "))
num2 = int(input("Enter the second number: "))
result = gcd(num1, num2)
print(f"The GCD of {num1} and {num2} is {result}.")
24.) Python program to count the Frequency of Words in a String.
sentence = input("Enter a sentence: ")
words = sentence.split()
word_freq = {}
for word in words:
word_freq[word] = word_freq.get(word, 0) + 1
for word, freq in word_freq.items():
print(f"{word}: {freq}")
25.) Python program to swap the values of Two Variables.
a = input("Enter value for a: ")
b = input("Enter value for b: ")
print(f"Before swapping, a = {a} and b = {b}.")
# Swapping
a, b = b, a
print(f"After swapping, a = {a} and b = {b}.")
No comments:
Post a Comment