0% found this document useful (0 votes)
18 views

Practice Programs Solution

Uploaded by

cs.harimohan
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views

Practice Programs Solution

Uploaded by

cs.harimohan
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 16

UNIT-2 (Programs)

1. Python Program to take three numbers from the user and print the
greatest number.

x = int(input("Enter First number"))


y = int(input("Enter Second number"))
z = int(input("Enter Third number"))
a = x if x>y else y
b = a if a>z else z
print("Largest Number is :", b)
2. Write a program to print whether given number is even or odd.

x = int(input("Enter First number"))


if x%2==0:
print("Even Number")
else:
print("Odd Number")

3. Write a program to print any person is eligible for vote or not (age should
not less than 18 for voting).

age = int(input("Enter Your Age : "))


if age>=18:
print("You are eligible for vote")
else:
print("You are not eligible for vote")
4. Write a Program to check Armstrong Number.

A positive integer of n digits is called an Armstrong number of order


n (order is number of digits) if.
abcd... = pow(a,n) + pow(b,n) + pow(c,n) + pow(d,n) + ....
\ Input: 153
Output: Yes
153 is an Armstrong number.
1*1*1 + 5*5*5 + 3*3*3 = 153

x = input("Enter a Number : ")


l = len(x)
s=0
for i in x:
s = s + int(i)**l

if s == int(x):
print("Armstrong number")
else:
print("Not an Armstrong number")
5. Write a program to check whether a number entered by user is Prime or
not.

x = int(input("Enter a Number : "))


f=0
if x<2:
print("Not a Prime Number")
else:
for i in range(2,x):
if x%i==0:
f=1
break
if f==0:
print("Prime Number")
else:
print("Not a Prime Number")
OR

import math
x = int(input("Enter a Number : "))
f=0
if x<2:
print("Not a Prime Number")
else:
for i in range(2,int(math.sqrt(x)+1)):
if x%i==0:
f=1
break
if f==0:
print("Prime Number")
else:
print("Not a Prime Number")
6. Write a program to calculate factorial of a number entered by user.

x = int(input("Enter a Number : "))


f=1
if x<0:
print("Factorial not exist for negative numbers")
elif x==0:
print("Factorial of 0 is : 1")
else:
for i in range(2,x+1):
f=f*i
print("Factorial is : ",f)
7. Python Program to check whether a given number is a palindrome.

x = int(input("Enter a Number : "))


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

if num==rev:
print("Palindrome Number")
else:
print("Not a Palindrome Number")
OR

x = input("Enter a Number : ")


rev = ''
for i in range(len(x)-1, -1, -1):
rev = rev + x[i]

if x==rev:
print("Palindrome Number")
else:
print("Not a Palindrome Number")
8. Write a program to print all Prime numbers between given range.

x = int(input("From: "))
y = int(input("To: "))
print("Prime Numbers between",x,"and",y)
for i in range(x,y+1):
f=0
for j in range(2,i):
if i%j==0:
f=1
break
if f==0:
print(i,end=' ')
9. Write a program to print n-th Fibonacci number.

x = int(input("Which term of fibonacci series you want to print: "))


a,b = 0,1
if x==1:
print("0")
elif x==2 or x==3:
print("1")
else:
for i in range(x-2):
c=a+b
a=b
b=c
print(c)
10.Write a program to check if a given number is Fibonacci number?
x = int(input("Enter a number: "))
a,b = 1,2
f=0
if x>=0 and x<=2:
print("Fibonacci Number")
else:
n=3
while n<=x:
c=a+b
if x==c:
print("Fibonacci Number")
f=1
break
a=b
b=c
n+=1
if f==0:
print("Not a Fibonacci Number")
OR

A number (n) is Fibonacci if and only if one or both of (5*n2 + 4) or


(5*n2 – 4) is a perfect square.

import math
import sys
x = int(input("Enter a number: "))

if x==0:
print("Fibonacci Number")
sys.exit()

a = math.sqrt(5*x*x-4)
b = math.sqrt(5*x*x+4)

if a.is_integer() or b.is_integer():
print("Fibonacci Number")
else:
print("Not a Fibonacci Number")
11.Write a program for Sum of squares of first n natural numbers.
Ex: if Input: N = 4
Then Output: 30
12 + 22 + 32 + 42 = 1 + 4 + 9 + 16 = 30

n = int(input("Enter Number of Terms: "))


s=0
for i in range(1, n+1):
s = s + i**2
print("Sum of squares of first", n, "natural numbers :", s)

12.Write a program for cube sum of first n natural numbers.


n = int(input("Enter Number of Terms: "))
s=0
for i in range(1, n+1):
s = s + i**3
print("Sum of cube of first", n, "natural numbers :", s)
13.WAP to print given pattern

n = int(input("Enter Number of Lines: "))


x=1
for i in range(1,n+1):
print(" "*(n-i), "*"*x, sep='')
x+=2
14.Write a program to take an integer N as an input and display the pattern.
Input
5
Output
*
**
***
****
*****
****
***
**
*
n = int(input("Enter a number : "))
for i in range(1,n+1):
print("* "*i)
for i in range(n-1,0,-1):
print("* "*i)
15.WAP to print given pattern

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


for i in range(1,n+1):
print("*"*i + " "*(2*(n-i)) + "*"*i)
for i in range(n-1,0,-1):
print("*"*i + " "*(2*(n-i)) + "*"*i)

You might also like