Special Programs

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 9

For 3-digit numbers (using while loop)

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

num = int(input("Enter 3-digit number : "))

sum = 0

temp = num

#Define a function

while temp > 0:

digit = temp % 10

sum += digit * digit * digit

temp = temp//10

if sum==num:

print('It is an Armstrong number')

else:

print('It is not an Armstrong number')

Output 1:

Output 2:
# Python program to check if the number is an Armstrong number or not

# 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")
Run Code

Output 1

Enter a number: 663


663 is not an Armstrong number

Output 2

Enter a number: 407


407 is an Armstrong number
Palindrome number
Program 1
Num = int(input("Enter a value:"))
Temp = num
Rev = 0
while(num > 0):
dig = num % 10
revrev = rev * 10 + dig
numnum = num // 10
if(temp == rev):
print("This value is a palindrome number!")
else:
print("This value is not a palindrome number!")

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):

print("The number is a palindrome!")

else:

print("The number isn't a palindrome!")


Fibonacci Series
# Fibonacci Series using Dynamic Programming
def fibonacci(n):

# Taking 1st two fibonacci numbers as 0 and 1


f = [0, 1]

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


f.append(f[i-1] + f[i-2])
return f[n]

print(fibonacci(9))

# Function for nth Fibonacci number


def Fibonacci(n):

# Check if input is 0 then it will


# print incorrect input
if n < 0:
print("Incorrect input")

# 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

# Input from the user

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

# If number is greater than 1

if num > 1:

# Check if factor exist

for i in range(2,num):

if (num % i) == 0:

print(num,"is not a prime number")

break

else:

print(num,"is a prime number")

# Else if the input number is less than or equal to 1

else:

print(num,"is not a prime number")

OUTPUT:

Enter a number: 9

9 is not a prime number

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.

 Method 1: Using Brute Force


 Method 2: Using the Formula
 Method 3: Using Recursion

Method 1: Using Brute Force

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

For in integer inputs num1 and num2 as the intervals

 Initialize the required variables.


 Run a for loop from num1 to num2+1 i.e [num1,num2].
 Append i to sum variable with each iteration.
 Print sum variable.

Let’s implement the above logic in Python Language.

Python Code
num1, num2 = 3, 6
sum = 0
for i in range(num1,num2+1):
sum+=i
print(sum)
Output
18

Method 2: Using the Formula


In this method we’ll use formula mentioned below to find the sum of all the numbers that lay
in the interval given by the input variable.

Formula to Find the Sum of Numbers in an Interval


The formula to find the sum of n natural numbers is:
Sum = n * ( n + 1 ) / 2

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

For the two integer inputs num1 and num2

 Initialize the required variables.


 perform sum = int((num2*(num2+1)/2) – (num1*(num1+1)/2) + num1).
 Print Sum variable.

Let’s implement the above logic in Python Language.

Python Code

num1, num2 = 3, 6

sum = int((num2*(num2+1)/2) - (num1*(num1+1)/2) + num1)

print(sum)

Output

18

Method 3: Using Recursion

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

 Initialize the required variables.


 Define a recursive function with base case as num1>num2.
 Set the recursive step call as num1 + recursum(sum, num1+1, num2).
 call the recursum() function and print the returned value.

Let’s implement the above logic in Python Language.

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

You might also like