You are on page 1of 6

5.

Python programming to solve scientific problems using Iterative loops


While loop

1. Write a program that sums a series of (positive) integers entered by the user, excluding all
numbers that are greater than 100.
2. Write a program, in which the user can enter any number of positive and negative integer
values that displays the number of positive values entered, as well as the number of
negative values.
3. Write a program to check whether given number is narcissistic number or palindrome
number.
4. Write a program to covert a binary number to decimal number and vice versa using loops.
5. Write a program to print Fibonacci series.
6. Write a program to find whether the given number is perfect number or not. Note: A
number is a perfect number if the sum of the factors of the number other than itself is
equal to that number. Example: 28=1+2+4+7+14 is a perfect number.
7. The log2 of a given number Nis given by M in the equation N= 2M. The value of M is
approximately equal to the number of times N can be divided by 2 until it becomes 0.
Write a loop that computes this approximation of the log2 of a given number N.
8. Display the integer values 1–100, ten numbers per row, with the columns aligned as
shown below, using only one while loop.

1 2 3 4 5 6 7 8 9 10

11 12 13 14 15 16 17 18 19 20

21 22 23 24 25 26 27 28 29 30

91 92 93 94 95 96 97 98 99 100

9. The factorial of an integer Nis the product of all of the integers between 1 and N,
inclusive. Write a while loop that computes the factorial of a given integer N.
10. Write a program that receives a series of numbers from the user and allows the user to
press the ‘#’ symbol to indicate that he or she is finished providing inputs. After the user
presses the ‘#’ symbol, the program should print the sum of the numbers and their
average.
11. Develop and test a Python program that calculates the monthly mortgage payments for a
given loan amount, term (number of years) and range of interest rates from 3% to 18%.
The fundamental formula for determining this is A/D, where A is the original loan
amount, and D is the discount factor. The discount factor is calculated as, ,
where n is the number of total payments (12 times the number of years of the loan) and r
is the interest rate, expressed in decimal form (e.g., .05), divided by 12. A monthly
payment table should be generated as shown below,

Loan Amount: $350,000 Term: 30 years

Interest Rate Monthly Payment

3% 1475.61
4% 1670.95
5% 1878.88
6% 2098.43
. .
. .
18% 5274.80
Check your results with an online mortgage calculator.
12. The function random.randint from the random module can be used to produce an integer
from a range of values. For example, random.randint(1,6) produces the values 1 to 6 with
equal probability. Such a value can be used, for example, to simulate the roll of a die.
Using this technique, write a program that will play several rounds on the popular dice
game known as “craps”. The rules of craps are as follows: A player rolls a pair of dice. If
the sum of dice is either 7 or 11 on the first throw, the player wins. If the sum is 2, 3 or 12
on the first throw, the player loses. Any other sum becomes the players “point”. To win,
the player must continue rolling the dice until a roll matches the point. This is termed
“making the point”. If the player rolls a seven before making the point the player loses.
13. Use a random number generator to create a program to play “guess my number”. The
computer selects a random number, and prompts the user for a guess. At each step the
computer responds either “you are right”, “too low”, or “too high”. The loop continues
until the user guesses the correct number
14. The greatest common divisor of two positive integers, A and B, is the largest number that
can be evenly divided into both of them. Euclid’s algorithm can be used to find the
greatest common divisor (GCD) of two positive integers. Write a program that lets the
user enter two integers and then print their GCD.
15. Translate the following for loops to equivalent while loops:
a. for count in range(100):
print(count)
b. for count in range(1,101):
print(count)
c. for count in range(100,0,-1):
print(count)
For loop

1. Using for loop, print of table of Celsius/Fahrenheit equivalences. Let c be the Celsius
temperatures, ranging from 0 to 100. For each value of c, print the corresponding
Fahrenheit temperature.
2. Using for loop, print a table of powers of x, where x ranges from 1 to 10. For each value
x, print the quantity x, x2, and x3. Using tab characters in your print statement to make the
values line up nicely.
3. Write a program to print odd numbers from n1 to n2.
4. Write python programs to print the following sequences,
a. 1,3,9,27,81,243…
b. -4,-2,0,2,4
c. 1,8,27,64…
d. -11 -12 -14 -18 -26 -42…
5. Write python programs to print the sum of the following nth series,
a. –x+x2-x3+x4…
b.

c.
6. Write a program to produce a simple table of sine(x), cos(x) and tan(x). Make the
variable x range from 0 to 360 in steps of 30.
7. Write a program that reads a word, and prints the number of letters in the word, the
number of vowels in the word, and the percentage of vowels.
Enter a word: college
Letters: 7
Vowels: 3
Percentage of vowels:42.86
8. Write a program that asks the user to input 10 integers, and then prints the largest odd
number that was entered. If no odd number was entered, it should print a message to that
effect.
9. Random numbers can also be used to create a simple tutor program. For example,
suppose you are teaching multiplication. A program might randomly select two numbers
between 1 and 9, and ask a question such as the following: How much is 6 times 8? The
student types the answer, and the computer checks that it is either correct or not, issuing
an appropriate message. Write a program that will loop 10 times producing questions of
this form and checking the answers.
10. The German mathematician Gottfried Leibniz developed the following method to
approximate the value of π:
π/4 = 1 – 1/3 + 1/5 – 1/7 + . . .
Write a program that allows the user to specify the number of iterations used in this
approximation and that displays the resulting value.

Nesting of Loops

1. Write a program containing a pair of nested while loops that displays the integer values
1–100, ten numbers per row, with the columns aligned as shown below,

1 2 3 4 5 6 7 8 9 10

11 12 13 14 15 16 17 18 19 20

21 22 23 24 25 26 27 28 29 30

91 92 93 94 95 96 97 98 99 100

2. Using a series of nested for loops, find all Pythagorean triples consisting of positive
integers less than or equal to 20
3. Write a program to print prime numbers from 1 to n.
4. The cost of a Scooter is Rs25000/-. Three optional accessories are supplied at different
costs as follows.
Mirror:Rs85 Crash Guard:Rs225 Side box:Rs 300 If A,B, and C represents
the three accessories, the total cost of the scooter is 25000+A*85+B*225+C*300
where A,B and C are either 0 or 1 depending upon whether the option is required or not.
Write a C program to print the total cost as per the illustration given below.
Base Price Mirror Crash Guard Side box Total
25000 0 0 0 25000
25000 0 0 1 25300
25000 0 1 0 25225
. . . . .
. . . . .
. . . . .
25000 1 1 1 25610
5. Write a program to calculate and print the sum of first n terms of following:
1+ (1+2) + (1+2+3) + (1+2+3+4) +…
6. Write a program to print following patterns
a)
*
**
***

b)
1
23
456
7 8 9 10

c)
1
12
123
1234

d)

*****
* *
* *
* *
*****

e)
$***$
*$ $*
* $ *
*$ $*
$***$
f)
*
**
***
****
*****
****
***
**
*

g)
1
212
32123

You might also like