Professional Documents
Culture Documents
Special Programs
Special Programs
Special Programs
The most common examples for Armstrong numbers are usually three-digit numbers, and hence,
first, we will write a program specifically for 3-digit integers:
#Enter input
sum = 0
temp = num
#Define a function
digit = temp % 10
temp = temp//10
if sum==num:
else:
Output 1:
Output 2:
# Python program to check if the number is an Armstrong number or not
# initialize sum
sum = 0
Output 1
Output 2
Progrma 2
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):
else:
print(fibonacci(9))
# Check if n is 0
# then it will return 0
elif n == 0:
return 0
# Check if n is 1,2
# it will return 1
elif n == 1 or n == 2:
return 1
else:
return Fibonacci(n-1) + Fibonacci(n-2)
# Driver Program
print(Fibonacci(9))
Prime Number
Program 1
# Program to check if a number is prime or not
if num > 1:
for i in range(2,num):
if (num % i) == 0:
break
else:
else:
OUTPUT:
Enter a number: 9
Enter a number: 23
23 is a prime number
Program 2
# A default function for Prime checking conditions
def PrimeChecker(a):
# Checking that given number is more than 1
if a > 1:
# Iterating over the given number with for loop
for j in range(2, int(a/2) + 1):
# If the given number is divisible or not
if (a % j) == 0:
print(a, "is not a prime number")
break
# Else it is a prime number
else:
print(a, "is a prime number")
# If the given number is 1
else:
print(a, "is not a prime number")
# Taking an input number from the user
a = int(input("Enter an input number:"))
# Printing result
PrimeChecker(a)
Find the Sum of the Numbers in a Given Interval in
Python Language
Given two integer inputs as the range [low , high], the objective is to find the sum of all the
numbers that lay in the given integer inputs as interval. In order to do so we usually iterate
through the the numbers in the given range and keep appending them to the sum variable.
Here are few methods to solve the above mentioned problem in Python Language.
In this method we’ll use loops like for, while and do while to sum all the numbers that lay in
the intervals of the given input integers.
Working
Python Code
num1, num2 = 3, 6
sum = 0
for i in range(num1,num2+1):
sum+=i
print(sum)
Output
18
Therefore in order to find the sum in a given interval we'll minus the sum of the numbers until the
lower range from the whole sum and add an offset as the lowest bound is itself included in the
summation. Hence the final formula is :
Sum = b * ( b + 1 ) / 2 – a * ( a + 1 ) / 2 + a .
Working
Python Code
num1, num2 = 3, 6
print(sum)
Output
18
In this method we’ll use recursion to find the sum of all the numbers that lay in the interval
given by the input variable.
Working
For the two integer inputs num1 and num2 as the interval
Python Code
def recursum(sum,num1,num2):
if num1 > num2:
return sum
return num1 + recursum(sum,num1+1,num2)
num1, num2 = 3, 6
sum = 0
print(recursum(sum,num1,num2))
Output
18