Download as pdf or txt
Download as pdf or txt
You are on page 1of 6

1.

Write a Python program to check whether the given number is positive


or negative
a=int(input("Enter the nummber to check:"))
if a>0:
print("It is postive.")
else:
print("It is negative.")

Output:-

2. Write a Python to Reverse an input number using recursion


Number = int(input("Please Enter any Number: "))
Reverse = 0
while(Number > 0):
Reminder = Number %10
Reverse = (Reverse *10) + Reminder
Number = Number //10

print("\n Reverse of entered number is = %d" %Reverse)

Output:-

3. Write a Python Program to find greatest 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("The largest number is",largest)


Output:-

4. Write a Python Program to print Fibonacci series in a given range


nterms = int(input("How many terms? "))

n1, n2 = 0, 1
count = 0

if nterms <= 0:
print("Please enter a positive integer")
elif nterms == 1:
print("Fibonacci sequence upto",nterms,":")
print(n1)
else:
print("Fibonacci sequence:")
while count < nterms:
print(n1)
nth = n1 + n2
n1 = n2
n2 = nth
count += 1

Output:-

5. Write a Python Program to find factorial of a given number


num=int(input("Enter the postive integer:"))
factorial=1
for i in range(1,num + 1):
factorial = factorial*i
print("The factorial of",num,"is",factorial)

Output:-
6. Write a Python Find Prime numbers in a given range
lower = int(input("Enter lower range: "))
upper = int(input("Enter upper range: "))

for num in range(lower,upper + 1):


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

Output:-

7. Write a Python Program to check if given number is Armstrong or not


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

while temp > 0:


digit = temp % 10
sum += digit ** 3
temp //= 10

if num == sum:
print(num,"is an Armstrong number")
else:
print(num,"is not an Armstrong number")

Output
8. Write a Python Program to check if given number is palindrome or not
num = int(input("enter a number: "))

temp = num
rev = 0

while temp != 0:
rev = (rev * 10) + (temp % 10)
temp = temp // 10

if num == rev:
print("number is palindrome")
else:
print("number is not palindrome")

Output:-

9. Write a Python Program to check if number is odd or even


a=int(input("Enter the number:"))
if a%2==0:
print(a,"is even number")
else:
print(a,"is odd number")

Output

10. Write a Python Program to find out the ASCII value of a character
ch = input("Enter any character: ")

print("The ASCII value of char " + ch + " is: ",ord(ch))

Output
11. Write a Python Program to find the size of int, float, double and char

12. Write a Python Program to check whether an alphabet is vowel or


consonant
l = input("Input a letter of the alphabet: ")

if l in ('a', 'e', 'i', 'o', 'u'):


print("%s is a vowel." % l)
else:
print("%s is a consonant."%l)

Output

13. Write a Python Program to check leap year


year = int(input("Enter a year: "))
if (year % 4) == 0:
if (year % 100) == 0:
if (year % 400) == 0:
print("{0} is a leap year".format(year))
else:
print("{0} is not a leap year".format(year))
else:
print("{0} is a leap year".format(year))
else:
print("{0} is not a leap year".format(year))

Output

14. Write a Python Program to find sum of first n natural numbers


num = int(input("Enter the value of n: "))
hold = num
sum = 0

if num <= 0:
print("Enter a whole positive number!")
else:
while num > 0:
sum = sum + num
num = num - 1
print("Sum of first", hold, "natural numbers is: ", sum)

Output

16. Program to to take a input as string and check the string is on the
upper case or in lower case if upper case then display is in exact case and
then convert string from upper case to lower case and vice versa.
str1=input("Enter the string:");
newStr = "";

for i in range(0, len(str1)):


if str1[i].islower():
newStr += str1[i].upper();
elif str1[i].isupper():
newStr += str1[i].lower();

else:
newStr += str1[i];
print("String after case conversion : " + newStr);

Output

You might also like