Python SOLUTION (Chapter 1 ) : Starting Programming


For any type of consultation, query or doubt. You may contact to the following: (+91) 9804 436 193 debabrataguha20@gmail.com 
You will get all the exercise in the Facebook group  https://www.facebook.com/groups/331231161093613/ 


1.     Enter 2 numbers and find the result after addition, subtraction, multiplication and division.

Soln.
# enter 2 no and find the result after addition, subtraction, multiplication and division.
 
n1=int(input("enter first no: "))
n2=int(input("enter second no: "))
print("Addition: ",n1+n2)
print("Subtraction: ",n1-n2)
print("Multiplication: ",n1*n2)
print("Division: ",n1/n2)


2.     Write a program to input your age and mobile number, then print the same.

Soln.
a=int(input("Enter the age: "))
m=long(input("Enter the mobile no: "))
print('Age: ',a)
print('Mobile no: ',m)


3.     Write a program to print a block F using hash (#), where the F has a height of seven characters and width of five and four characters. 
(
Expected Output

                       ######
                       #
                       #
                       #####
                       #
                       #
                       #
)

Soln.
print('##### ')
print('#     ')  
print('#     '
print('####  ')
print('#     ')
print('#     ')
print('#     ')




4.     Write a program to compute the perimeter and area of a rectangle with a height of 7 inches. and width of 5 inches.

Soln:
l=5
b=7
area = l * b
peri = 2 * ( l + b )
print('Area: ',area,' sq inches')
print('perimeter',peri,' inches')


5.      Write a program to compute the perimeter and area of a circle with a radius of 6 inches.

Soln.
r=6
peri = 2*3.14*r
area = 3.14 * r * r
print('Perimeter of a circle = ',peri ," inches")
print('Area of a circle = ',area," sq inches")


6.     Enter 2 sides of a rectangle and find the area and perimeter.

Soln.
l=int(input('Enter Length '))
b=int(input('Enter Bredth '))
area = l * b
peri = 2 * (l + b)
print('Area: ',area,' sq inches')
print('perimeter',peri,' inches')            


7.     Write a program that takes hours and minutes as input, and calculates the total number of minutes.

Soln.
h=int(input('Enter hours= '))
m=int(input('Enter minutes'))
t= h * 60 + m
print('time in minutes= ',t,' minutes')


8.     Enter the length in meter and find the equivalent in centimeter and kilometer.

Soln.
m=int(input('Enter lenght in m '))
c = m * 100
k = m / 1000
print('Length in cm ', c)
print('Length in km ', k)


9.      Write a program to find the third angle of a triangle if two angles are given

Soln.
a1=int(input('Enter first angle '))
a2=int(input('Enter second angle '))
a3=180-(a1+a2)
print('The third angle is ',a3)



10.  Enter the temperature in centigrade and find the equivalent in Fahrenheit.

Soln.
c=int(input('temp in centrigrade '))
f=(9*c/5)+32
print('Temp in Farenheit ',f)



11.  Enter the radius of a circle and find the area, diameter and perimeter.

Soln.
r=int(input('enter radius '))
peri = 3.14*r*2
dia  = 2 * r
area = 3.14 * r * r
print('perimeter of circle ',peri)
print('diameter of circle ',dia)
print('Area of circle ', area)



12.  Write a program that calculates the volume of a sphere. Volume of a sphere is (4/3)*Ï€ *r3

Soln.
r=int(input('enter radius '))
print('Volume of sphere ',(4/3)*3.14*r*r*r)



13.  Write a program that converts kilometers per hour to miles per hour. (1km = 0.621371 miles)

Soln.
r=int(input('enter speed in kmph '))
print('speed in mph ',r*0.621)
 


14.  Write a program to read an amount (integer value) and break the amount into smallest possible number of bank notes.

Soln.
n=int(input('enter amount '))
d=n//2000
print('no of 2000 rs. notes ',d)
n=n%2000
d=n//500
print('no of 500 rs. notes ',d)
n=n%500
d=n//200
print('no of 200 rs. notes ',d)
n=n%200
d=n//100
print('no of 100 rs. notes ',d)
n=n%100
d=n//50
print('no of 50 rs. notes ',d)
n=n%50
d=n//20
print('no of 20 rs. notes ',d)
n=n%20
d=n//10
print('no of 10 rs. notes ',d)
n=n%10
d=n//5
print('no of 5 rs. notes ',d)
n=n%5
d=n//2
print('no of 2 rs. notes ',d)
n=n%2
d=n//1
print('no of 1 rs. notes ',d)


15.  Enter a number and find the cube and square of the number.

Soln.
n=int(input('enter a no '))
print('Square of the no is ',n*n)
print('Cube of the no is ',n*n*n)

 
Alternative Soln#1:

n=int(input('enter a no '))
print('Square of the no is ',n**2)
print('Cube of the no is ',n**3)

Alternative Soln#2:

n=int(input('enter a no '))
print('Square of the no is ', pow (n,2)
print('Cube of the no is ', pow (n,3)



16.  Enter a number find the double and triple of that number.


Soln.
n=int(input('Enter a no '))
print('double ',n*2)
print('triple ',n*3)



17.  Enter time in seconds , then show it in the Hour: Minute : Second format.

Soln.
s=int(input('Enter time(in seconds): '))
h=s//3600
s=s%3600)
m=s//60
s=s%60
print('Time ',h,':',m,':',s)



18.  Write a program to convert a given integer (in days) to years, months and days, assumes that all months have 30 days and all years have 365 days.


           (
Input no. of days: 2535
Expected Output:
6 Year(s)
11 Month(s)
15 Day(s).
)

Soln.
d=int(input('Enter no of days  '))
y= d //365
d= d % 365
m= d // 30
d= d % 30
print(y,' years')
print(m,' months')
print(d,' days')


19.  Enter 2 numbers and store it into 2 variables a and b respectively. Swap the values of the variable.

Soln.
a=int(input('Enter 1st no '))
b=int(input('Enter 2nd no '))
c=a
a=b
b=c
print(' After the swapping values are ')
print(a)
print(b)


20.  Enter 2 numbers and store it into 2 variables a and b respectively. Swap the values of the variable without using a third variable.

Soln.
a=int(input('Enter a no '))
b=int(input('Enter a no '))
a=a+b
b=a-b
a=a-b
print(a)
print(b)


21.  Enter u,t and f and then find the distance (s) using the formula s=ut+(1/2)*ft2

Soln.
u=int(input('Enter speed '))
t=int(input('Enter time '))
f=int(input('Enter acceleration'))
print('Distance= ',(u*t)+(0.5*f*t*t))



22.  Enter 2 numbers and find the modulus without using the modulus operator (%).

Soln.
n=int(input('Enter a no '))
d=int(input('Enter a divisor '))
q=n//d
r=n-(d*q)
print(" Modulus : ",r)


23.  Write a program to enter P, T, R and calculate Simple Interest.
(

Formula is A=P ( 1 + RT )
Where A is the Accumulated amount of Principal Amount P with R rate of Interest for T no. of years 
)

Soln.
p=int(input('Enter Principal amount: Rs '))
t=int(input('Enter time period(years): '))
r=int(input('Enter rate of interest: '))

i=(p*t*r)/100
print('Simple Interest: RS ,',i)


24.  Write a program to enter P, T, R,N and calculate Compound Interest.
(

Formula is A=P ( 1 + R/N)NT

Where A is the Accumulated amount of Principal Amount P with R rate of Interest for T no. of years and N is the no. of times interest is compounded per year
)


Soln.
p=int(input('Enter Principal amount: Rs '))
t=int(input('Enter time period(years): '))
r=int(input('Enter rate of interest: ')) 
n=int(input('Enter no of times the interest is compounded: '))

i=(p*r/n) ** (n*t)

print('Compound Interest: RS ,',i)


Alternative Soln.

p=int(input('Enter Principal amount: Rs '))
t=int(input('Enter time period(years): '))
r=int(input('Enter rate of interest: ')) 
n=int(input('Enter no of times the interest is compounded: '))

i= pow ( (p*r/n) , (n*t) )

print('Compound Interest: RS ,',i)



25.  Write a program to enter base and height of a right angled triangle and find its area.
(
Area of a right angled triangle is (1/2) * base * height
)

Soln.
base   = int(input('Enter the base '))
height = int(input('Enter the height '))
area   = ( base * height ) / 2

print(' area is ', area )



26.  Write a program to enter the coordinates (x,y) of 2 points and find the distance between those 2 points.
(
Distance between point1 (x1,y1) and point2 (x2,y2) is square root of (x1-x2)2+ (y1-y2)2
)

Soln.
x1   = int(input('Enter the x coordinate of 1st point '))
y1   = int(input('Enter the y coordinate of 1st point '))

x2   = int(input('Enter the x coordinate of 2nd point '))
y2   = int(input('Enter the y coordinate of 2nd point '))

d    = (  (x1 – x2) ** 2  +  (y1 – y2) ** 2  )  ** 0.5

print(' distance between 2 point is ', d )


Alternative Soln.

x1   = int(input('Enter the x coordinate of 1st point '))
y1   = int(input('Enter the y coordinate of 1st point '))

x2   = int(input('Enter the x coordinate of 2nd point '))
y2   = int(input('Enter the y coordinate of 2nd point '))

d    =  pow( (pow( x1-x2, 2) + pow ( y1-y2, 2 ) ) , 0.5 )

print(' distance between 2 point is ', d )


27.  Write a program to enter two timings with hour, minute and second. Then add them and print the answer in the same format.
(
Suppose the 1st timing is: 5 hour 50 mins 40 seconds
And the 2nd timing is : 2 hour 20 mins 50 seconds

The sum of 2 timings will be: 8 hour 11 mins 30 seconds
)

Soln.
print('Enter the 1st timing ')
h1   = int(input('Enter the hour '))
m1   = int(input('Enter the minute '))
s1   = int(input('Enter the second '))

print('Enter the 2nd timing ')
h2   = int(input('Enter the hour '))
m2   = int(input('Enter the minute '))
s2   = int(input('Enter the second '))

ts   = (s1 + s2)
s    =  ts % 60
tm   =  m1 + m2 + (ts // 60)
m    =  tm % 60
h    =  h1 + h2 + (tm // 60)

print(' total time is ',h, ' hour',m, ' minute ', s , 'second')






Comments

Popular posts from this blog

Java concepts Theory

About Myself ..