Home | | Problem Solving and Python Programming | Basic python programs

Chapter: Problem Solving and Python Programming : Algorithmic Problem Solving

Study Material, Lecturing Notes, Assignment, Reference, Wiki description explanation, brief detail

Basic python programs

Problem Solving and Python Programming : Algorithmic Problem Solving

Basic python programs:

 

Addition of two numbers

a=eval(input(“enter first no”))

b=eval(input(“enter second no”))

c=a+b

print(“the sum is “,c)

 

Output

enter first no

5

enter second no

6

the sum is 11

 

Area of rectangle

l=eval(input(“enter the length of rectangle”))

b=eval(input(“enter the breath of rectangle”))

a=l*b

print(a)

 

Output

enter the length of rectangle 5

enter the breath of rectangle 6

30

 

Area & circumference of circle

r=eval(input(“enter the radius of circle”))

a=3.14*r*r

c=2*3.14*r

print(“the area of circle”,a)

print(“the circumference of circle”,c)

 

output

enter the radius of circle4

the area of circle 50.24

the circumference of circle

25.12

 

Calculate simple interest

p=eval(input(“enter principle amount”))

n=eval(input(“enter no of years”))

r=eval(input(“enter rate of interest”))

si=p*n*r/100

print(“simple interest is”,si)

 

Output

enter principle amount 5000

enter no of years 4

enter rate of interest6

simple interest is 1200.0

 

Calculate engineering cutoff

p=eval(input(“enter physics marks”))

c=eval(input(“enter chemistry marks”))

m=eval(input(“enter maths marks”))

cutoff=(p/4+c/4+m/2)

print(“cutoff =”,cutoff)

 

Output

enter physics marks 100

enter chemistry marks 99

enter maths marks 96

cutoff = 97.75

 

Check voting eligibility

age=eval(input(“enter ur age”))

If(age>=18):

print(“eligible for voting”)

else:

print(“not eligible for voting”)

 

output

Enter ur age

19

Eligible for voting

 

Find greatest of three numbers

a=eval(input(“enter the value of a”))

b=eval(input(“enter the value of b”))

c=eval(input(“enter the value of c”))

if(a>b):

if(a>c):

print(“the greatest no is”,a)

else:

print(“the greatest no is”,c)

else:

if(b>c):

print(“the greatest no is”,b)

else:

print(“the greatest no is”,c)

 

output

enter the value of a 9

enter the value of a 1

enter the value of a 8

the greatest no is

 9

 

Print n natural numbers

for i in range(1,5,1):

print(i)

 

Output

1 2 3 4

 

Print n odd numbers      

for i in range(1,10,2):

print(i)

 

Output

1 3 5 7 9

 

Print n even numbers

for i in range(2,10,2):

print(i)

 

Output

2 4 6 8

 

Print squares of numbers

for i in range(1,5,1):

print(i*i)

 

Output

1 4 9 16

 

Print squares of numbers

for i in range(1,5,1):

print(i*i*i)

 

Output

1 8  27 64

 

Print n natural numbers

i=1

while(i<=5):

print(i)

i=i+1

 

Output

1

2

3

4

5

 

Print n odd numbers

i=2

while(i<=10):

print(i)

i=i+2

 

Output

2

4

6

8

10

 

Print n even numbers

i=1

while(i<=10):

print(i)

i=i+2

 

Output

1

3

5

7

9

 

Print n squares of numbers

i=1

while(i<=5):

print(i*i)

i=i+1

 

Output

1

4

9

16

25

 

Print n cubes numbers

i=1

while(i<=3):

print(i*i*i)

i=i+1

 

Output

1

8

27

 

find sum of n numbers

i=1

sum=0

while(i<=10):

sum=sum+i

i=i+1

print(sum)

 

Output

55

 

factorial of n numbers/product of n numbers

i=1

product=1

while(i<=10):

product=product*i

i=i+1

print(product)

 

Output

3628800

 

sum of n numbers

def add():

a=eval(input(“enter a value”))

b=eval(input(“enter b value”))

c=a+b

print(“the sum is”,c)

add()

 

Output

enter a value

6

enter b value

4

the sum is 10

 

area of rectangle using function

def area():

l=eval(input(“enter the length of rectangle”))

b=eval(input(“enter the breath of rectangle”))

a=l*b

print(“the area of rectangle is”,a)

area()

 

Output

enter the length of

rectangle 20

enter the breath of

rectangle 5

the area of rectangle is

100

 

swap two values of variables

def swap():

a=eval(input("enter a value"))

b=eval(input("enter b value"))

c=a

a=b

b=c

print("a=",a,"b=",b)

swap()

 

Output

enter a value3

enter b value5

a= 5 b= 3

 

check the no divisible by 5 or not

def div():

n=eval(input("enter n value"))

if(n%5==0):

print("the number is divisible by 5")

else:

print("the number not divisible by 5")

div()

 

Output

enter n value10

the number is divisible by

5

 

find reminder and quotient of given no

def reminder():

a=eval(input("enter a"))

b=eval(input("enter b"))

R=a%b

print("the reminder is",R)

def quotient():

a=eval(input("enter a"))

b=eval(input("enter b"))

Q=a/b

print("the reminder is",Q)

reminder()

quotient()

 

 

Output

enter a 6

enter b 3

the reminder is 0

enter a 8

enter b 4

the reminder is 2.0

 

convert the temperature

def ctof():

c=eval(input("enter temperature in centigrade"))

f=(1.8*c)+32

print("the temperature in Fahrenheit is",f)

def ftoc():

f=eval(input("enter temp in Fahrenheit"))

c=(f-32)/1.8

print("the temperature in centigrade is",c)

ctof()

ftoc()

 

Output

enter temperature in

centigrade 37

the temperature in

Fahrenheit is 98.6

enter temp in Fahrenheit

100

the temperature in

centigrade is 37.77

 

program for basic calculator

def add():

a=eval(input("enter a value"))

b=eval(input("enter b value"))

c=a+b

print("the sum is",c)

def sub():

a=eval(input("enter a value"))

b=eval(input("enter b value"))

c=a-b

print("the diff is",c)

def mul():

a=eval(input("enter a value"))

b=eval(input("enter b value"))

c=a*b

print("the mul is",c)

def div():

a=eval(input("enter a value"))

b=eval(input("enter b value"))

c=a/b

print("the div is",c)

add()

sub()

mul()

div()

 

Output

enter a value 10

enter b value 10

the sum is 20

enter a value 10

enter b value 10

the diff is 0

enter a value 10

enter b value 10

the mul is 100

enter a value 10

enter b value 10

the div is 1

 

Study Material, Lecturing Notes, Assignment, Reference, Wiki description explanation, brief detail


Copyright © 2018-2020 BrainKart.com; All Rights Reserved. Developed by Therithal info, Chennai.