Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
13 views

Python Programs

Uploaded by

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

Python Programs

Uploaded by

Keep Learning
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Python Programming Solutions part 1

Write a Program to check a given number is prime or not.


Sol:
Prime Numbers. => 2, 3, 5, 7, 11, 13.....
num = int(input("Enter a number: "))

# define a flag variable


flag = False

if num == 1:
print(num, "is not a prime number")
elif num > 1:
# check for factors
for i in range(2, num):
if (num % i) == 0:
# if factor is found, set flag to True
flag = True
# break out of loop
break

# check if flag is True


if flag==True:
print(num, "is not a prime number")
else:
print(num, "is a prime number")

Output:
Enter a number greater than 1 : 13
13 is prime Number

Write a Program to find factorial of a number.


Sol:
Factorial=> 5!
=5x4x3x2x1

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


fact=1

# check if the number is negative, positive or zero


if num < 0:
print("Sorry, 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):
fact = fact*i
print("The factorial of",num,"is",fact)
Python Programming Solutions part 1
Output:
Enter a number: 5
The factorial of 5 is 120

Write a Program to find Fibonacci Series upto n terms.


Sol:
Fibonacci Series: 0, 1, 1, 2, 3, 5, 8, 13........
Logic: 0, 1
0+1=1
1+1=2
1+2=3
2+3=5.......... So on.

# Program to display the Fibonacci sequence up to n-th term

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

# first two terms


n1, n2 = 0, 1
count = 0

# check if the number of terms is valid


if (nterms <= 0):
print("Please enter a positive integer")
# if there is only one term, return n1
elif (nterms == 1):
print("Fibonacci sequence upto",nterms,":")
print(n1)
# generate fibonacci sequence
else:
print("Fibonacci sequence:")
while count < nterms:
print(n1)
nth = n1 + n2
# update values
n1 = n2
n2 = nth
count += 1

Output:
How many terms?
6
Fibonacci sequence:
Python Programming Solutions part 1
0
1
1
2
3
5

** Process exited - Return Code: 0 **


Press Enter to exit terminal

Write a Program to find GCD of two numbers.


Sol:
USING RECURSION
def gcd(a, b):
if b == 0:
return a
else:
return gcd(b, a % b)

a = 12
b = 18

print(f"The GCD of {a} and {b} is {gcd(a, b)}")

Output:
The GCD of 12 and 18 is 6

** Process exited - Return Code: 0 **


Press Enter to exit terminal

Python Program to Find HCF or GCD


Sol:
# Python program to find H.C.F of two numbers

# define a function
def compute_hcf(x, y):

# choose the smaller number


if x > y:
Python Programming Solutions part 1
smaller = y
else:
smaller = x
for i in range(1, smaller+1):
if((x % i == 0) and (y % i == 0)):
hcf = i
return hcf

num1 = 54
num2 = 24

print("The H.C.F. is", compute_hcf(num1, num2))

Output:
The H.C.F. is 6

Python Program for converting a decimal number to binary


equivalent.
Sol:
num=6

l=list()
while num!=0:
r=num%2
l.append(r)
num=num//2
l.reverse()
print(*l)

Output:
110

** Process exited - Return Code: 0 **


Press Enter to exit terminal

Python Program to check armstrong number


Sol:
In case of an Armstrong number of 3 digits, the sum of cubes of each digit is equal to the
number itself.
For example:
Python Programming Solutions part 1

153

= 1*1*1 + 5*5*5 + 3*3*3

= 1 + 125 + 27

= 153

So, It is an armstrong number

Code:
# take input from the user
num = int(input("Enter a number: "))

# initialize sum
sum = 0

# find the sum of the cube of each digit


temp = num
while temp > 0:
digit = temp % 10
sum += digit ** 3
temp //= 10

# display the result


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

Output:
Enter a number:
153
153 is an Armstrong number

** Process exited - Return Code: 0 **


Press Enter to exit terminal

Python
Program to
check
Palindrome
number.
Sol:
Python Programming Solutions part 1

Code:
num = int(input("Enter a number: "))
temp = num
rev = 0
while temp != 0:
rev = (rev * 10) + (temp % 10)
temp //= 10
if num == rev:
print(num,"Number is palindrome")
else:
print(num,"Number is not palindrome")

Output:
Enter a number:
242
242 Number is palindrome

** Process exited - Return Code: 0 **


Press Enter to exit terminal

You might also like