Python Coding Questions on Numbers String Program | Array Program

https://quescol.com/interview-preparation/python-coding-question
1 Write a program to reverse an integer in Python
n = int(input("please give a number : ")) print("before reverse your numeber is : %d" %n) reverse = 0 while n!=0: reverse = reverse*10 + n%10 n = (n//10) print("After reverse : %d" %reverse)
2 Write a program in Python to check whether an integer is Armstrong number or not.
* It is a number which is equal to the sum of cube of its digits. * For eg: 153, 370 etc. 1^3 = (1*1*1) = 1 5^3 = (5*5*5) = 125 3^3= (3*3*3) = 27 Now add the cube 1+125+27 = 153 It means 153 is an Armstrong number. #=== Program n =int(input('Enter your number:')) given_number = n print('Given number is:',given_number) converted_number =0 while n != 0: rem = (n%10) n = (n//10) powerN = pow(rem,3) converted_number =(converted_number+powerN) if converted_number == given_number: print(given_number, ' is Armstrong number') else: print(given_number, ' is not Armstrong number')
3 Write a program in Python to check given number is prime or not.
* In Mathematical term, A prime number is a number which can be divided by only 1 and number itself n =int(input('Enter your number:')) for i in range(2,n): rem = (n%i) if rem ==0: print(n, ' is not a prime number') break else: print(n, ' is a prime number')
4 Write a program in Python to print the Fibonacci series using iterative method.
f=0 s=1 n =int(input('Enter your number:')) if n==0: print(s) else: print(f) print(s) for i in range(2,n): next = f+s print(next) f =s s =next
5 Write a program in Python to print the fibonacci series using recursive method.
n =int(input('Enter your number:')) def fibonacci(num): if num==0: return 0 elif num==1: return 1 else: f = fibonacci(num-1) + fibonacci(num-2) return f for i in range(0,n): f = fibonacci(i) print(f)
6 Write a program in Python to check whether a number is palindrome or not using iterative method.
n =int(input('Enter your number:')) original_number =n reverse=0 while n != 0: reverse = reverse*10 + n%10; print('reverse:',reverse) n = (n//10) if original_number == reverse: print('Given number is palindrome') else: print('Given number is not palindrome')
7 Write a program in Python to check whether a number is palindrom or not using recursive method.
8 Write a program in Python to find greatest among three integers.
n1 = int(input("please give first number n1: ")) n2 = int(input("please give second number n2: ")) n3 = int(input("please give third number n3: ")) if n1>=n2 and n1>=n3: print(" n1 is greatest"); if n2>=n1 and n2>=n3: print(" n2 is greatest"); if n3>=n1 and n3>=n2: print("n3 is greatest");
9 Write a program in Python to check if a number is binary?
* In the below program if someone give any input in 0 and 1 format then our program will run and give output as given number is in binary format. * And if someone give another number different from 0 and 1 like 2, 3 or any other then our program will give output as given number is not in a binary format. num = int(input("please give a number : ")) while(num>0): j=num%10 if j!=0 and j!=1: print("num is not binary") break num=num//10 if num==0: print("num is binary")
10 Write a program in Python to find sum of digits of a number using recursion?
11 Write a program in Python to swap two numbers without using third variable?
12 Write a program in Python to swap two numbers using third variable?
13 Write a program in Python to find prime factors of a given integer.
14 Write a program in Python to add two integer without using arithmetic operator?
15 Write a program in Python to find given number is perfect or not?
A perfect number is a positive integer that is equal to the sum of its positive divisors, excluding the number itself. n = int(input("please give first number a: ")) sum=0 for i in range(1,(num//2)+1): remainder = num % i if remainder == 0: sum = sum + i if sum == num: print("given no. is perfect number") else: print("given no. is not a perfect number")
16 Python Program to find the Average of numbers with explanations.
17 Python Program to calculate factorial using iterative method.
The factorial of a number is the multiplication of each and every number till the given number except 0. Case1: If the user input the 5. then the output should be 120 ( 1*2*3*4*5= 120). Case2: If the user input the 6. then the output should be 720 (1*2*3*4*5*6 =720). num=int(input("Enter the whole number to find the factorial: ")) factorial = 1 if num < 0: print("Factorial can't be calculated for negative number") elif num == 0: print("Factorial of 0 is 1") else: #calculating the factorial of the input number for i in range(1,num + 1): factorial = factorial*i print("Factorial of",num,"is",factorial)
18 Python Program to calculate factorial using recursion.
def fact(n): if n == 1: return n else: #recursion return n*fact(n-1) num = int(input("Enter a whole number to find Factorial: ")) if num < 0: print("Factorial can't be calculated for negative number") elif num == 0: print("Factorial of 0 is 1") else: print("Factorial of",num,"is",fact(num))
19 Python Program to check a given number is even or odd.
num = int(input("Enter a number to check even/odd ")) #if number is divisible by 2 if num%2 == 0: print(num,"is even number") else: print(num,"is odd number")
20 Python program to print first n Prime Number with explanation.
Prime Numbers are the numbers that have only 2 factors 1 and the number itself. It is only defined for the number which is greater than ‘1’. 5 is a prime number because only factors of 5 are ‘1 and 5’. 11 is a prime number because only factors of 11 are ‘1 and 11’. 10 is not prime because the factors of 10 are ‘ 1,2,5 and 10’. ======================================== n = int(input("Enter number: ")) for i in range(2,n): rem = n%i if rem ==0: print(n, " is not prime number") break else: print(n, " is prime number")
21 Python Program to print Prime Number in a given range
22 Python Program to find Smallest number among three.
23 Python program to calculate the power using the POW method.
Case1: If the user inputs the base of the number as 2 and exponent as 3 then the output should be ‘8’. Case2: If the user inputs the base of the number as 5 and exponent as 2. then the output should be ‘25’. ============================= base = int(input("Enter the value for base :")) exponent = int(input("Enter the value for exponent :")) print(base,"to power ",exponent,"=",end = ' ') print(pow(base,exponent))
24 Python Program to calculate the power without using POW function.(using for loop)
base = int(input("Enter the value for base :")) exponent = int(input("Enter the value for exponent :")) result=1; print(base,"to power ",exponent,"=",end = ' ') #using while loop with a condition that come out of while loop if exponent is 0 while exponent != 0: result = base * result exponent-=1 print(result)
25 Python Program to calculate the square of a given number.
num = int(input("Enter a number to calculate square : ")) print("square =",num*num)
26 Python Program to calculate the cube of a given number
num = int(input("Enter a number to calculate cube : ")) print("cube =",num*num*num)
27 Python Program to calculate the square root of a given number.
Case1: If the user inputs the number as 4. then the output should be ‘2’. Case2: If the user inputs the number as 25. then the output should be ‘5’. ================================ import math #take input from the user num = int(input("Enter a number to find square root : ")) #check if the input number is negative if num<0: print("Negative numbers can't have square roots") else: print("square roots = ",math.sqrt(num))
29 Python program to calculate LCM of given two numbers.
Case1: If the user inputs the numbers 4 and 6. then the output should be ‘12’. Case2: If the user inputs the numbers 5 and 7. then the output should be ‘35’. =============================== num1 = int(input("Enter first number: ")) num2 = int(input("Enter second number: ")) if num1 > num2: greater = num1 else: greater = num2 while(True): if((greater % num1 == 0) and (greater % num2 == 0)): lcm = greater break greater += 1 print("LCM of",num1,"and",num2,"=",greater)
30 Python Program to find GCD or HCF of two numbers.
Case1: If the user inputs the numbers 4 and 6. then the output should be ‘2’. Case2: If the user inputs the numbers 5 and 7. then the output should be ‘1’. ========================================= #taking two inputs from the user num1 = int(input("Enter first number: ")) num2 = int(input("Enter second number: ")) #checking for smaller number if num1 > num2: minimum = num2 else: minimum = num1 #finding highest factor of the numbers for i in range(1, minimum+1): if((num1 % i == 0) and (num2 % i == 0)): hcf = i print("hcf/gcd of",num1,"and",num2,"=",hcf)
31 Python Program to find GCD of two numbers using recursion.
32 Python Program to Convert Decimal Number into Binary.
33 Python Program to convert Decimal number to Octal number.
34 Python Program to check the given year is a leap year or not.
35 Python Program to convert Celsius to Fahrenheit.
36 Python Program to convert Fahrenheit to Celsius.
Case1: If the given user input in the Fahrenheit temperature 100. then the output should be ‘37.78’. Case2: If the user input the Fahrenheit temperature 50. then the output should be ’10’. =============================================== fahrenheit = float(input("Please give the Hahrenheit Temperature : ")) #converting Celsius into Fahrenheit celsius = ((fahrenheit-32)*5)/9 print("Celcius= ",celsius)
37 Python program to calculate Simple Interest with explanation.
X