Skip to main content

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

Popular posts from this blog

Lessons learned from a bicycle.

M y Bicycle tales 🚲 I vividly remember my first cycling experience on a Hero 'Cyclone' bike, a sky blue colored that belonged to my elder sister. As a child, riding that cycle felt like a privilege, but due to my height, it wasn't the perfect fit for me. My parents always reassured me that once I grew taller, my sister would lend it to me. I longed for the day when I could ride that cycle. Finally, in 7th grade, my patience paid off. My sister offered me the Cyclone bicycle. The feeling of joy I experienced that day is indescribable. For two months, I felt like a prince, seizing every opportunity to go outdoors with my bicycle. Whether it was fetching groceries, to buy milk, or helping my dad with bringing hardware accessories from nearby store, I was always ready for a ride, exploring the streets near my home. But one unfortunate day, tragedy struck. Returning home from my tuition classes, my  Cyclone bicycle was nowhere to be found. Panic set in, and I rushed to a nearby...

Basic knowledge of cars which everyone should have.

Let’s drive towards the automobiles industry. A.    Well-known car manufacturing companies with their country of origin. ·       Mercedes, AUDI, BMW, Volkswagen, Porsche - Germany ·       TATA, Mahindra, Maruti -India ·       Suzuki, Isuzu, Honda, Toyota, Nissan, Mitsubishi, Datsun – Japan ·       Ford, Chevrolet - America ·       Hyundai, Kia - South Korea ·       MG Motors - China ·         Landrover, Jaguar, Mini – England   B.    Types of cars ·       Hatchback ·       Sedan ·       SUV (Sport Utility Vehicle) ·       Coupe ·       Limousine ·       ATV (All Terrain Vehicle) ·     ...

How to know behavior patterns of the buyer like their basic needs, influences and motives?

 How to learn making customer strategy through case study. (PART -2) How to know the buyer behavior patterns like their basic needs, influences and motives? Maslow’s Hierarchy of Needs   1)    Social Needs:- Buyers are conscious towards numerous variables of chocolates like structure and size of the chocolate, its consistency in the mouth, contact, its softening and so forth. There are different elements like moral obligation of Hershey’s company and corporate social duty influences the buying choice of the chocolate customers. For instance corporate social duty of the organization incorporates lawful consistence, ecological issue and so on. In assembling the item organization ought to pursue the principles and controls made by the controllers of the business. Hershey dependably pursued the legitimate necessities of the chocolate manufacturing industry. Likewise the corporate social obligation of the organization impacts the buyers to consider the organization on...