Positive oí Negative numbeí:
Method 1: Using Brute Force
ľhis method uses Bíute Foíce to check whetheí a given integeí is Positive oí Negative.
Python Code
Output
num = 15 if num > 0:
Positive
print('Positive') elif num < 0:
print('Negative') else:
print('Zero')
Method 2: Using Nested if-else Statements
This method uses a nested if-else Statements to check whether a given number is Positive
or Negative.
Python Code
num = 15
if num>=0:
if num==0:
print('Zero')
else:
print("Positive")
else:
print("Negative")
Output
Positive
Method 3: Using Ternary Operator
ľhis method uses a teínaíy opeíatoí to check whetheí a numbeí is Positive oí Negative.
Python Code
num =15
print("Positive" if num>=0 else "Negative")
Output
Positive
Check Whether a Number is Even or Odd:
Method 1 : Using Brute Force
This method simply checks if the given input integer is divisible by 2 or not. If it’s
divisible then print Even or Odd otherwise.
Python Code
num = int(input("Enter a Number:"))
if num % 2 == 0:
print("Given number is Even")
else:
print("Given number is Odd")
Output
Enter a Number: 5
Given number is Odd
Method 2 : Using Ternary Operator
ľhis Method uses the teínaíy opeíatoí to check if the integeí input is divisible by 2, If tíue
píint Even oí Odd otheíwise.
Python Code
num = 17
print("Even") if num%2 == 0 else print("Odd")
Output
Odd
Method 3 : Using Bitwise Operator
ľhis Method uses bitwise opeíatoís to check if a given numbeí is Even oí Odd.
Python Code
def isEven(num): return not num&1
ifname== " main ": num = 13
if isEven(num): print('Even') else: print('Odd')
Output
Odd
Find the Sum of the First N Natural Numbers:
Method 1 : Using for Loop
In this method we’ll add all the natural numbers until the given integer input using for loop
in Python.
Python Code
num = 5
sum = 0
for i in range(num+1):
sum+=i
print(sum)
Output
15
Method 2 : Using Formula for the Sum of Nth Term
In this Method we use the foímula foí finding the sum of N teím.
Python Code
num = 5
print(int(num*(num+1)/2))
Output
15
Method 3 : Using Recursion
ľhis method uses Recuísion to íecuísively add the natuíal numbeís up to the given integeí
input
ython Code
def getSum(num):
if num == 1:
return 1
return num + getSum(num-1)
num = 5
print(getSum(num))
Output
15
Find the Sum of N Natural Numbers:
Method 1: Using for Loop
In this method we’ll use foí loops to iteíate thíough all the numbeís until the integeí input
“numbeí” is íeached.
Python Code
number,sum = 6,0
for i in range(number+1):
sum+=i
print(sum)
Output
21
Method 2: Using the Formula
In this method we’ll use the formula for finding the sum of N integers in a series from series
and sequences i.e sum = number * ( number + 1 ) / 2 to calculate the sum until the given
integer input.
Python Code
number = 6
sum = int((number * (number + 1))/2)
print(sum)
Output
21
Method 3: Using Recursion
In this method we’ll use íecuísion to íecuísively iteíate thíough the numbeí while appending
them to the sum vaíiable until the numbeí is íeach which heíe act as the base case.
Python Code
def recursum(number):
if number == 0:
return number
return number + recursum(number-1)
number, sum = 6,0
print(recursum(number))
Output
21
Find the Sum of the Numbers in a Given Interval:
Method 1: Using Brute Force
In this method we’ll use loops like foí, while and do while to sum all the numbeís that
lay in the inteívals of the given input integeís.
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.
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 íecuísion to find the sum of all the numbeís that lay in the
inteíval given by the input vaíiable.
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
Find the Greatest of the Two Numbers :
Method 1: Using if-else Statements
In this method we’ll find the Laígest Numbeí using simple if-else statements.
Python Code
num1, num2 = 20 , 30 if num1>num2: print(num1)
else: print(num2)
Output
30
Method 2: Using Ternary Operator
In this method we’ll use ľeínaíy Opeíatoí in Python to find the Laígest Numbeí among
the two input integeís.
Python Code
num1, num2 = 20 , 30
print((num1 if num1>num2 else num2))
Output
30
Find the Greatest of the Three Numbers :
Method 1: Using if-else Statements
In this method we use if-else statements to find the Laígest Numbeí among the thíee integeí
inputs.
Method 1: Using if-else Statements
In this method we use if-else statements to find the Laígest Numbeí among the thíee integeí
inputs.
Python Code
num1, num2, num3 = 10 , 30 , 20
max = 0
if num1 >= num2 and num1 >= num3: print(num1)
elif num2 >= num1 and num2 >= num3: print(num2)
else: print(num3)
Output
30
Method 2: Using Nested if-else Statements
In this method we use if-else statements within one anotheí to find the Laígest Numbeí
among the thíee integeí inputs.
Python Code
num1, num2, num3 = 10 , 350 , 550
max = 0
if num1 >= num2:
if num1 >= num3:
print(num1)
elif num2 >= num1:
if num2 >= num3:
print(num2)
else :
print(num3)
Output
30
Check Whether a Year is a Leap Year or Not:
Method 1: Using if-else Statements 1
In this method we’ll use the if-else statements to check whetheí oí not the input integeí satisfies
eitheí of the conditions.
Python Code
year = 2000
if (year%400 == 0) or (year%4==0 and year%100!=0): print("Leap Year")
else:
print("Not a Leap Year")
Output
Leap Year
Method 2: Using if-else Statements 2
In this method we’ll use the if-else statements to check whether or not the input integer
satisfies either of the conditions. This method is a modified and simpler version of the
previous method.
Python Code
year = 2000
if( ((year % 4 == 0) and (year % 100 != 0)) or (year % 400==0) ):
print("Leap Year")
else:
print("Not leap Year")
Output:
Leap Year
Check Whether a Number is a Prime or Not
Method 1: Simple iterative solution
Python Code
num = 15
flag = 0
for i in range(2,num):
if num%i==0:
flag = 1
break
if flag == 1:
print('Not Prime')
else:
print("Prime")
Output
Not Prime
Method 2: Optimization by break condition
Python Code
num = 15
flag = 0
if num<2: flag = 1
else:
for i in range(2,num): if num%i==0:
flag = 1 break
if flag == 1: print('Not Prime') else: print("Prime")
Output
Not Prime
Find the Prime Numbers in a Given Interval :
Method 1: Using inner loop Range as [2, number-1]
Python Code
# python find prime numbers in range
low, high = 2, 10
primes = []
for i in range(low, high + 1):
flag = 0
if i < 2:
continue
if i == 2:
primes.append(2)
continue
for x in range(2, i):
if i % x == 0:
flag = 1
break
if flag == 0:
primes.append(i)
print(primes)
Output
[2, 3, 5, 7]
Method 2: Using inner loop Range as [2, number/2]
Python Code
low, high = 2, 10
primes = [2]
for num in range(low, high + 1): flag = 0
if num < 2:
flag = 1
if num % 2 == 0: continue
iter = 2
while iter < int(num / 2): if num % iter == 0:
flag = 1 break
iter += 1
if flag == 0: primes.append(num)
print(primes)
Output
[2, 3, 5, 7]
Find the sum of the Digits of a Number:
Method 0: Using String Character Extraction
We will extíact each chaíacteí in the stíing input and conveít it to an individual
chaíacteí’s integeí equivalent.
num = input("Enter Number: ") sum = 0
for i in num:
sum = sum + int(i)
print(sum)
Output
Enter Number: 12345
15
Method 1: Using Brute Force
We extract each digit here by finding the modulus of the whole input by 10.
Python Code
num = 12345
sum = 0
while num!=0:
digit = int(num%10)
sum += digit
num = num/10
print(sum)
Output
15
Method 2: Using Recursion I
Python Code
num, sum = 12345, 0
def findSum(num, sum):
if num == 0:
return sum
digit = int(num % 10)
sum += digit
return findSum(num / 10, sum)
print(findSum(num, sum))
Output
15
Find the Reverse of a Number:
Method 1: Using Simple Iteration
Python Code
num = 1234 temp = num reverse = 0 while num > 0:
remainder = num % 10
reverse = (reverse * 10) + remainder num = num // 10
print(reverse)
Output
4321
Method 2: Using String Slicing
Python Code
num = 1234
print(str(num)[::-1])
Output
4321
Check Whether or Not the Number is a Palindrome:
Method 1: Using Simple Iteration
Python Code
num = 1221
temp = num
reverse = 0
while temp > 0:
remainder = temp % 10
reverse = (reverse * 10) + remainder
temp = temp // 10
if num == reverse:
print('Palindrome')
else:
print("Not Palindrome")
Output
Palindrome
Method 2: Using Recursion
Python Code
def recurrev(number, rev):
if number == 0:
return rev
remainder = int(number % 10) rev = (rev * 10) + remainder
return recurrev(int(number / 10), rev)
num = 12321
reverse = 0
reverse = recurrev(num, reverse)
print(str(num) + " is: ", end="")
print("Palindrome") if reverse == num else print("Not Palindrome")
Output
Palindrome
Check Whether a Given Number is an Armstrong
Number or Not:
Method 1: Using Iteration
In this method we’ll use foí loop and while loop to check foí Aímstíong numbeí.
Python Code
number = 371
num = number
digit, sum = 0, 0
length = len(str(num))
for i in range(length):
digit = int(num%10)
num = num/10
sum += pow(digit,length)
if sum==number:
print("Armstrong")
else:
print("Not Armstrong")
Output
Armstrong
Method 2: Using Recursion
In this method we’ll use íecuísion to check foí Aímstíong numbeí.
Python Code
number = 371 num = number sum =0
length = len(str(num))
def checkArmstrong(num,length,sum): if num==0:
return sum sum+=pow(int(num%10),length)
return checkArmstrong(num/10,length,sum)
if checkArmstrong(num,length,sum)==number: print('Armstrong')
else:
print("Not Armstrong")
Output
Armstrong
Find the Armstrong Numbers in a given
Range: Method 1
Python Code
low, high = 10, 10000
for n in range(low, high + 1): # order of number
order = len(str(n))
# initialize sum sum = 0
temp = n
while temp > 0: digit = temp % 10
sum += digit ** order temp //= 10
if n == sum: print(n, end=", ")
Method 2
Python Code
import math
first, second = 150, 10000
def is_Armstrong(val):
sum = 0
# this splits the val into its digits
# example val : 153 will become [1, 5, 3] arr = [int(d) for d in str(val)]
# now we iterate on array items (digits)
# add these (digits raised to power of len i.e order) to sum for i in range(0, len(arr)):
sum = sum + math.pow(arr[i], len(arr))
# if sum == val then its armstrong if sum == val:
print(str(val) + ", ", end="")
for i in range(first, second + 1): is_Armstrong(i)
Output
153, 370, 371, 407, 1634, 8208, 9474,
Find the Fibonacci Series up to Nth Term:
Method 1: Using Simple Iteration
In this method we’ll use loops to iteíate thíough and foím the seíies up to the integeí input N
as the íange. ľo píint the seíies up to the Nth teím we staít a loop fíom 2 to the Nth teím as
0 and 1 aíe the seed values foí foíming the seíies.
# Write a program to print fibonacci series upto n terms in python num = 10
n1, n2 = 0, 1
print("Fibonacci Series:", n1, n2, end=" ") for i in range(2, num):
n3 = n1 + n2 n1 = n2
n2 = n3
print(n3, end=" ")
print()
Python Code Output
Fibonacci Series: 0 1 1 2 3 5 8 13 21 34
Method 2: Using Recursive Function
In this method we’ll use recursion to find the Fibonacci Series up to the given integer input as
the Nth range. To do so we take three variables and call the recursive function twice in return
statement forming a recursion tree.
Python Code
# Python program to print Fibonacci Series def fibonacciSeries(i):
if i <= 1:
return i else:
return (fibonacciSeries(i - 1) + fibonacciSeries(i - 2))
num=10
if num <=0:
print("Please enter a Positive Number") else:
print("Fibonacci Series:", end=" ") for i in range(num): print(fibonacciSeries(i), end=" ")
Output
Fibonacci Series: 0 1 1 2 3 5 8 13 21 34
Find the Prime Factors of a Number:
Method 1: Using Simple Iteration
Python Code
def Prime_Factorial(n):
if n < 4:
return n
arr = []
while n > 1:
for i in range(2, int(2+n//2)):
if i == (1 + n // 2):
arr.append(n)
n = n // n
if n % i == 0:
arr.append(i)
n = n // i
break
return arr
n = 210
print(Prime_Factorial(n))
Output
[ 2 , 3 , 5 , 7 ]
Method 2: Using Recursive Function
Python Code
def Prime_Factorial(n, arr): if n < 4:
return n
for i in range(2, int(2+n//2)): if i == (1 + n // 2):
arr.append(n) n = 1
return
if n % i == 0: arr.append(i)
n = n // i Prime_Factorial(n, arr) break
return arr
n = 210
arr = [] print(Prime_Factorial(n, arr))
Output
[ 2, 3, 5, 7 ]