Most Important Programs For Freshers In Interview

 Important Programs

1)Fibonacci Series

A Fibonacci sequence is the integer sequence of 0, 1, 1, 2, 3, 5, 8....

The first two terms are 0 and 1. All other terms are obtained by adding the preceding two terms. This means to say the nth term is the sum of (n-1)th and (n-2)th term.

Code:-

 nterms = int(input("How many terms? "))

# first two terms
n1, n2 = 0, 1
count = 0

# check if the number of terms is valid
if nterms <= 0:
   print("Please enter a positive integer")
elif nterms == 1:
   print("Fibonacci sequence upto",nterms,":")
   print(n1)
else:
   print("Fibonacci sequence:")
   while count < nterms:
       print(n1)
       nth = n1 + n2
       # update values
       n1 = n2
       n2 = nth
       count += 1

Output:-

How many terms? 7
Fibonacci sequence:0 1 1 2 3 5 8



2)Prime or not

Code:-

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

Output:-

Enter an input number:17
17 is a prime number

 3) Palindrome

What is a Palindrome?
A palindrome is nothing but any number or a string which remains unaltered when reversed.

Palindrome Program using While Loop
This is one of the easiest programs to find Palindrome program using while loop. Let’ dive into an example to check whether a given input is a palindrome or not.

Code:-
num=int(input("Enter a number:"))
temp=num
rev=0
while(num>0):
    dig=num%10
    rev=rev*10+dig
    num=num//10
if(temp==rev):
    print("The number is palindrome!")
else:
    print("Not a palindrome!")


Output:

Enter a number:121
The number is palindrome!


Palindrome Program( String) using inbuilt Method

string=input(("Enter a string:"))
if(string==string[::-1]):
      print("The string is a palindrome")
else:
      print("Not a palindrome")

Output:
 Enter a string: RACECAR
The string is a palindrome

4)Armstrong number


Given a number x, determine whether the given number is Armstrong number or not. A positive integer of n digits is called an Armstrong number of order n (order is number of digits) 

if. abcd... = pow(a,n) + pow(b,n) + pow(c,n) + pow(d,n) + ....

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


5)Sum of Digits:-

Code:-

n=int(input("Enter a number:"))
tot=0
while(n>0):
    dig=n%10
    tot=tot+dig
    n=n//10
print("The total sum of digits is:",tot)



 
Case 1:
Enter a number:1892
The total sum of digits is: 20
 
Case 2:
Enter a number:157
The total sum of digits is: 13


6)Factorial:-

Code:-

# Python program to find the factorial of a number provided by the user.

# change the value for a different result
num = 7

# To take input from the user
#num = int(input("Enter a number: "))

factorial = 1

# check if the number is negative, positive or zero
if num < 0:
   print("Sorry, factorial does not exist for negative numbers")
elif num == 0:
   print("The factorial of 0 is 1")
else:
   for i in range(1,num + 1):
       factorial = factorial*i
   print("The factorial of",num,"is",factorial)

Output:-The factorial of 7 is 5040



7)Swaping of Two numbers:-

Code:-

# Python program to demonstrate
# swapping of two variables

x = 10
y = 50

# Swapping of two variables
# Using third variable
temp = x
x = y
y = temp

print("Value of x:", x)
print("Value of y:", y)



output:- Value of x: 50 
               Value of y: 10


2) Method:-

# Python program to demonstrate
# swapping of two variables


x = 10
y = 50

# Swapping of two variables
# without using third variable
x, y = y, x

print("Value of x:", x)
print("Value of y:", y)

3)Method:-
# Python program to demonstrate
# Swapping of two variables

x = 10
y = 50

# Swapping using xor
x = x ^ y
y = x ^ y
x = x ^ y

print("Value of x:", x)
print("Value of y:", y)

4) Method:-


# Python program to demonstrate
# swapping of two variables

x = 10
y = 50

# Swapping of two variables
# using arithmetic operations
x = x + y
y = x - y
x = x - y

print("Value of x:", x)
print("Value of y:", y)


5) Method:-

# Python program to demonstrate
# swapping of two variables

x = 10
y = 50

# Swapping of two numbers
# Using multiplication operator

x = x * y
y = x / y
x = x / y

print("Value of x : ", x)
print("Value of y : ", y)


Matrix Multiplication:-

# Python3 program to multiply two matrices.
MAX = 100

# Function to print Matrix
def printMatrix(M, rowSize, colSize) :
for i in range(rowSize) :
for j in range(colSize) :
print(M[i][j], end = " ")

print()

# Function to multiply two matrices
# A[][] and B[][]
def multiplyMatrix(row1, col1, A,
row2, col2, B) :
# Matrix to store the result
C = [[0 for i in range(MAX)]
for j in range(MAX)]

# Check if multiplication is Possible
if (row2 != col1) :
print("Not Possible")
return
# Multiply the two
for i in range(row1) :
for j in range(col2) :
C[i][j] = 0
for k in range(row2) :
C[i][j] += A[i][k] * B[k][j];

# Print the result
print("Resultant Matrix: ")
printMatrix(C, row1, col2)

# Driver Code
if __name__ == "__main__" :

A = [[0 for i in range(MAX)]
for j in range(MAX)]
B = [[0 for i in range(MAX)]
for j in range(MAX)]

# Read size of Matrix A from user
row1 = int(input("Enter the number of rows of First Matrix: "))
col1 = int(input("Enter the number of columns of First Matrix: "))

# Read the elements of Matrix A from user
print("Enter the elements of First Matrix: ");
for i in range(row1) :
for j in range(col1) :
A[i][j] = int(input("A[" + str(i) +
"][" + str(j) + "]: "))

# Read size of Matrix B from user
row2 = int(input("Enter the number of rows of Second Matrix: "))
col2 = int(input("Enter the number of columns of Second Matrix: "))

# Read the elements of Matrix B from user
print("Enter the elements of Second Matrix: ");
for i in range(row2) :
for j in range(col2) :
B[i][j] = int(input("B[" + str(i) +
"][" + str(j) + "]: "))

# Print the Matrix A
print("First Matrix: ")
printMatrix(A, row1, col1)

# Print the Matrix B
print("Second Matrix: ")
printMatrix(B, row2, col2)

# Find the product of the 2 matrices
multiplyMatrix(row1, col1, A, row2, col2, B)

# This code is contributed by Ryuga





Output:-

Enter the number of rows of First Matrix: 2
Enter the number of columns of First Matrix: 3
Enter the elements of First Matrix: 
A[0][0]: 1
A[0][1]: 2
A[0][2]: 3
A[1][0]: 4
A[1][1]: 5
A[1][2]: 6
Enter the number of rows of Second Matrix: 3
Enter the number of columns of Second Matrix: 2
Enter the elements of First Matrix: 
B[0][0]: 1
B[0][1]: 2
B[1][0]: 3
B[1][1]: 4
B[2][0]: 5
B[2][1]: 6

First Matrix: 
1 2 3 
4 5 6 

Second Matrix: 
1 2 
3 4 
5 6 

Resultant Matrix: 
22 28 
49 64 
 

Time Complexity: O(row1 * col2 * row2)
Auxiliary Space: O(row1 * col2)

9)Alphabet Triangle:-

Code:-

# pyramid alphabet pattern
n = 5
for i in range(n):
    for j in range(n - i - 1):
        print(' ', end='')
    for k in range(2 * i + 1):
        print(chr(65 + k), end='')
    print()


Output:-

      A
   ABC
  ABCDE
 ABCDEFG
ABCDEFGHI

10) Bubble Sort:-

Code:-

# Python program for implementation of Bubble Sort

def bubbleSort(arr):
n = len(arr)

# Traverse through all array elements
for i in range(n-1):
# range(n) also work but outer loop will
# repeat one time more than needed.

# Last i elements are already in place
for j in range(0, n-i-1):

# traverse the array from 0 to n-i-1
# Swap if the element found is greater
# than the next element
if arr[j] > arr[j + 1] :
arr[j], arr[j + 1] = arr[j + 1], arr[j]

# Driver code to test above
arr = [64, 34, 25, 12, 22, 11, 90]

bubbleSort(arr)

print ("Sorted array is:")
for i in range(len(arr)):
print ("% d" % arr[i],end=" ")













Post a Comment

Previous Post Next Post