Basic python programs which every fresher software engineer must learn.

 


1.         Write a program to swap two numbers without using three variables?

a=int(input("Type value of a "))
b=int(input("Type value of b "))
 
# #Method-1
# c=a
# a=b
# b=c
 
#Method-2
a=a+b
b=a-b
a=a-b
 
print("value of a is now %d"%(a))
print("value of b is now %d"%(b))
 

 

2.         Write a program to write a factorial number?

#Factorial of a number means to total product of all numbers before it i.e. 3!=3*2*1
number=int(input("Type your number "))
copy=number
fac=1
while (number>0):
    fac=fac*number
    number=number-1
print("%d!=%d"%(copy,fac))


3.         Write a program to check whether the number is Prime number?

#Prime no. are no. that are only divisible by 1 or itself
number=int(input("Enter the number "))
if number<2:
    print("%d is not a prime number"%(number))
else:
    for i in range(2,number):
        if number%i==0:
            print("%d is not a prime number"%(number))
            break
    else:
        print("%d is a prime number"%(number))

 

4.         Write a program to check whether the number is Palindrome number?

# Palindrome no. i.e. 1334331 =same on reversing order of digits
number=int(input("Type your number "))
temp=number
reverse=0
 
while(number>0):
    dig=number%10
    reverse=reverse*10+dig
    number=number//10
 
if(temp==reverse):
    print("%d is a palindrome"%(reverse))
else:
    print(" %d is not a palindrome" %(reverse))
 

5.         Write a program to check whether the number is Armstrong?

#Armstrong no. i.e. 153 =1^3 + 5^3 + 3^3, where 3 is total numbers of digits here.
number=int(input("Type your number "))
copy=number
sum=0
order=len(str(number))
 
while(number>0):
    digit=number%10
    sum=sum+digit**order
    number=number//10
 
if(sum==copy):
    print("%s is an armstrong"%(sum))
else:
    print("%s is not an armstrong"%(sum))

 

6.         Write a program to check whether the number is Perfect no.?

# Perfect no. i.e. 28 = 1+2+4+7+14(sum of all factor excluding no. itself)
number=int(input("Type your number "))
copy=number
sum=0
i=1
while(i<number):
    if number%i==0:
        sum=sum+i
    i=i+1
 
if(sum==copy):
    print("%s is a perfect no."%(sum))
else:
    print("%s is not a perfect no."%(sum))

 

7.         Write a program to check whether the number is Fibonacci numbers?

#Fibonacci series is an addition of numbers i.e. 1,1,2,3...
number=int(input("Type your total number of fibonacci series"))
a=0
b=1
for i in range(2,number):
    c=a+b
    a=b
    b=c
    print(c,end=" ")

 

8.         Write a program to Print no. 1 to 10 (using while and do while loop), print multiplication table?

number=int(input("Table of "))
n=int(input("upto "))
for i in range(1,n+1):
    print("%d X %d = %d"%(number,i,(number*i)))



Read more...

--- !!! Happy Reading !!!--- 

Please do leave your comment below in the comment section 

Your feedback is always welcome.

© punchTank



Comments