0% found this document useful (0 votes)
2 views2 pages

Basic Programs

Uploaded by

shrads787
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views2 pages

Basic Programs

Uploaded by

shrads787
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

1.

Print Hello World

print("Hello, World!")

2. Add Two Numbers


a = 10
b = 20
sum = a + b
print("Sum =", sum)

3. Find Even or Odd


num = int(input("Enter a number: "))
if num % 2 == 0:
print("Even")
else:
print("Odd")

4. Find Largest of Two Numbers


a = int(input("Enter first number: "))
b = int(input("Enter second number: "))

if a > b:
print("Largest:", a)
else:
print("Largest:", b)

5. Calculate Factorial
num = int(input("Enter a number: "))
fact = 1

for i in range(1, num + 1):


fact *= i

print("Factorial =", fact)

6. Check Prime Number


num = int(input("Enter a number: "))

if num > 1:
for i in range(2, num):
if num % i == 0:
print("Not Prime")
break
else:
print("Prime")
else:
print("Not Prime")

7. Reverse a String
text = input("Enter a string: ")

print("Reversed String:", text[::-1])

8. Find Sum of Digits


num = int(input("Enter a number: "))
total = 0

while num > 0:


digit = num % 10
total += digit
num //= 10

print("Sum of digits =", total)

9. Fibonacci Series
n = int(input("Enter number of terms: "))

a, b = 0, 1

for i in range(n):
print(a, end=" ")
a, b = b, a + b

10.

You might also like