0% found this document useful (0 votes)
379 views10 pages

Python Program Class 11

The document contains 10 Python programs to solve various mathematical problems: 1) Check if a number is Armstrong number 2) Check if a number is prime 3) Print prime numbers from 1 to N 4) Print perfect numbers till 1000 5) Find sum of squares of first n natural numbers 6) Check if a number is Fibonacci number 7) Find sum of cubes of first n natural numbers 8) Check if a number is palindrome 9) Find greatest common divisor (GCD) of two numbers 10) Find lowest common multiple (LCM) of two numbers

Uploaded by

Arnab Roy
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
379 views10 pages

Python Program Class 11

The document contains 10 Python programs to solve various mathematical problems: 1) Check if a number is Armstrong number 2) Check if a number is prime 3) Print prime numbers from 1 to N 4) Print perfect numbers till 1000 5) Find sum of squares of first n natural numbers 6) Check if a number is Fibonacci number 7) Find sum of cubes of first n natural numbers 8) Check if a number is palindrome 9) Find greatest common divisor (GCD) of two numbers 10) Find lowest common multiple (LCM) of two numbers

Uploaded by

Arnab Roy
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 10

PYTHON PROGRAM

SUBJECT-COMPUTER SCIENCE
CLASS-XI

1)Determine whether a number is an Armstrong number


# Python program to determine whether
# the number is Armstrong number or not

# Function to calculate x raised to


# the power y
def power(x, y):

if y == 0:
return 1
if y % 2 == 0:
return power(x, y // 2) * power(x, y // 2)

return x * power(x, y // 2) * power(x, y // 2)

# Function to calculate order of the number


def order(x):

# Variable to store of the number


n=0
while (x != 0):
n=n+1
x = x // 10
return n

# Function to check whether the given


# number is Armstrong number or not
def isArmstrong(x):

n = order(x)
temp = x
sum1 = 0

while (temp != 0):


r = temp % 10
sum1 = sum1 + power(r, n)
temp = temp // 10

# If condition satisfies
return (sum1 == x)

# Driver code
x = 153
print(isArmstrong(x))

x = 1253
print(isArmstrong(x))
2)Determine whether a given number is prime or not.
# Python program to check if
# given number is prime or not
num = 11

# If given number is greater than 1


if num > 1:

# Iterate from 2 to n / 2
for i in range(2, int(num/2)+1):

# If num is divisible by any number between


# 2 and n / 2, it is not prime
if (num % i) == 0:
print(num, "is not a prime number")
break
else:
print(num, "is a prime number")

else:
print(num, "is not a prime number")
3)Write a program to print prime numbers from 1 to N
# Python3 program to display first N Prime numbers

#function to check if a given number is prime


def isPrime(n):
#since 0 and 1 is not prime return false.
if(n==1 or n==0):
return False
#Run a loop from 2 to n-1
for i in range(2,n):
#if the number is divisible by i, then n is not a prime number.
if(n%i==0):
return False

#otherwise, n is prime number.


return True

# Driver code
N = 100;
#check for every number from 1 to N
for i in range(1,N+1):
#check if current number is prime
if(isPrime(i)):
print(i,end=" ")
4)Write a program to print perfect number till 1000
# Python3 code to check if a given
# number is perfect or not

# Returns true if n is perfect


def isPerfect( n ):

# To store sum of divisors


sum = 1
# Find all divisors and add them
i=2
while i * i <= n:
if n % i == 0:
sum = sum + i + n/i
i += 1

# If sum of divisors is equal to


# n, then n is a perfect number

return (True if sum == n and n!=1 else False)

# Driver program
print("Below are all perfect numbers till 10000")
n=2
for n in range (10000):
if isPerfect (n):
print(n , " is a perfect number")

5) Python Program for Sum of squares of first n natural numbers

# Python3 Program to

# find sum of square

# of first n natural

# numbers

# Return the sum of


# square of first n

# natural numbers

def squaresum(n) :

# Iterate i from 1

# and n finding

# square of i and

# add to sum.

sm = 0

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

sm = sm + (i * i)

return sm

# Driven Program

n=4

print(squaresum(n))

6)Python program to check if a given number is Fibonacci number or not


# python program to check if x is a perfect square
import math

# A utility function that returns true if x is perfect square


def isPerfectSquare(x):
s = int(math.sqrt(x))
return s*s == x

# Returns true if n is a Fibonacci Number, else false


def isFibonacci(n):

# n is Fibonacci if one of 5*n*n + 4 or 5*n*n - 4 or both


# is a perferct square
return isPerfectSquare(5*n*n + 4) or isPerfectSquare(5*n*n - 4)

# A utility function to test above functions


for i in range(1,11):
if (isFibonacci(i) == True):
print (i,"is a Fibonacci Number")
else:
print (i,"is a not Fibonacci Number ")
7)Python program for cube sum of first n natural numbers.
# Simple Python program to find sum of series
# with cubes of first n natural numbers

# Returns the sum of series


def sumOfSeries(n):
sum = 0
for i in range(1, n+1):
sum +=i*i*i

return sum
# Driver Function
n=5
print(sumOfSeries(n))

8)Write a program to check palindrome number


n=int(input("Enter number:"))
temp=n
rev=0
while(n>0):
dig=n%10
rev=rev*10+dig
n=n//10
if(temp==rev):
print("The number is a palindrome!")
else:
print("The number isn't a palindrome!")
9) Write a program to find GCD of two numbers
# 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:
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))


10)Write a python program to find the LCM of two numbers.
# Python Program to find the L.C.M. of two input number

def compute_lcm(x, y):

# choose the greater number


if x > y:
greater = x
else:
greater = y

while(True):
if((greater % x == 0) and (greater % y == 0)):
lcm = greater
break
greater += 1

return lcm
num1 = 54
num2 = 24

print("The L.C.M. is", compute_lcm(num1, num2))

You might also like