Yash PPS Assignment

Download as pdf or txt
Download as pdf or txt
You are on page 1of 9

PPS ASSignment

Name: Yash Balanse


Roll: 1249

1. Write a program in Python to accept N numbers from user. Compute


and display maximum in list, minimum in list, sum and average of
numbers.

Explanation of the code:

1. First, the program accepts the length(no. of elements) from the user and
the entered value is assigned to the variable ‘n’.
2. A for loop is created which iterates n times and each time the user is
asked to enter a number.
3. During each iteration the entered numbers are appended to the list L
(which was initially a blank list) using append() function.
4. The list formed is then displayed using print().
5. Further maximum, minimum and sum of the numbers are calculated
using max(), min(), sum() functions respectively and then dispayed.
6. Alongside, the average of numbers is calculated and printed by dividing
sum of the numbers by length of the list L.
2. Write a program in Python to accept from user the number of
Fibonacci numbers to be generated and print the Fibonacci series.

Explanation of the code:

1. The user is first asked to enter the no. of terms for the Fibonacci series
and this value is assigned to the variable n
2. The first two terms of Fibonacci series 0 & 1 are assigned to the variables
t1 & t2 respectively.
3. A variable ‘sum’ is created with initial value 0. Another variable i is
created having value 0, this variable i will serve as the loop variable for
while loop.
4. The while loop will continue iteration until the condition i<n is made
false.
5. During each iteration:
i. First the value of sum is printed, and the value of t2 is now
assigned to t1.
For example: After the first iteration the value of t1 will become 1
which was previously the value of t2.
ii. The value of sum is assigned to t2. And lastly the value of sum is
made t1+t2 which will be printed in the next iteration of the loop.
For example: in the 2nd iteration print(sum) will print 1 (i.e 0+1).
iii. ‘i+=1’ will ensure that the value of i increments by 1 after each
iteration (to avoid infinite looping)
iv. The loop will run n times and terminate after the value of i equals
n.
3. To accept an object mass in kilograms and velocity in meters per
second and display its momentum. Momentum is calculated as e=mc2
where m is the mass of the object and c is its velocity.

Explanation of the code:

1. In line 1, the value of mass of the object is taken from the user as input and
stored in the variable ‘m’.
Similarly, user is also asked to input the value of velocity and the value is
assigned to variable ‘c’.
2. A variable ‘e’ which denotes momentum is declared with value m x c^2.
3. Finally ‘e’ (momentum) is printed.
4. Write a program in Python to accept a number from user and print digits
of number in a reverse order.

Explanation of the code:

1. At first, the user is asked to enter a number which is to be reversed


which is to a variable ‘n’.
2. If the entered number is greater than 0, the while loop will iterate.
Here’s how it iterates if the input is 256:
1st Iteration:
Remainder = n%10
i.e in this case,
remainder = 256%10=6

n_reverse = n_reverse*10 + remainder


n_reverse = 0*10 + 6 = 6

n = n//10
n = 256//10 = 25

2nd Iteration:
Now n=25 & n_reverse=6
Remainder = n%10
remainder = 25%10=5

n_reverse = n_reverse*10 + remainder


n_reverse = 6*10 + 5 = 65

n = n//10
n = 25//10 = 2

3rd Iteration:
Now n=2 & n_reverse=65
Remainder = n%10
remainder = 2%10=2

n_reverse = n_reverse*10 + remainder


n_reverse = 65*10 + 2 = 652

n = n//10
n = 2//10 = 0

The loop won’t iterate further as the condition becomes false when n=0
5. Write a program in Python to accept two
numbers from user and compute smallest
multiple and Greatest Common Divisor of
these two numbers.

Explanation of the code:

For LCM:
This program takes two integers a & b from input. Then we find the
greatest one out of the two and assign it to a variable ‘greater’ as LCM
can only be greater than or equal to the largest number.
We use an infinite while loop to go from the greater number and
beyond. During each iteration we check if both a & b perfectly divide the
number. If so, then we store the number as LCM and use break keyword
to stop further iteration. Otherwise, the number is incremented by 1 and
the loop continues.
For GCD (HCF):

In the case of greatest common divisor we find smaller number out of a


& b and assign to the variable ‘smaller’ as the GCD(hcf) can only be less
than or equal to the smallest number.
We then use a for loop with loop variable as i and range from 1 to
smaller. During each iteration we check if i perfectly divides both the
input numbers a & b. If so, we store the number in variable GCD.
At the completion of the loop, we end up with the largest number that
perfectly divides both the numbers.
6. To input binary number from user and convert
it into decimal number

Explanation of the code:

1. First the user is asked to input a binary number which is converted to list
using list() function which is then assigned to a variable ‘bin’.
2. A variable ‘dec’ is declared with initial value 0.
3. length of the list is calculated using len() function and this value is
assigned to a variable ‘L’.
4. A for loop with loop variable i is used which will iterate L times.
During each iteration we use pop() function which deletes the last
element of the list and returns the deleted value. This returned value is
assigned to variable ‘digit’.
5. We use an if condition ‘digit==1’ inside the loop to multilply 1 with 2^i.
Consider the following example:
If the input is 1101:
1101 in Decimal is: 1 x 2^i = 1 x 2^0 = 1 ( i=0 for 1st iteration)
0 x 2^i = 1 x 2^1 = 0 ( i=1 for 2nd iteration)
1 x 2^i = 1 x 2^2 = 4 ( i=2 for 3rd iteration)
1 x 2^i = 1 x 2^3 = 8 ( i=3 for 4th iteration)
= 1 + 0 + 4 + 8 =13

You might also like