0% found this document useful (0 votes)
20 views3 pages

Assignment 3 1

Assignment
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views3 pages

Assignment 3 1

Assignment
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Assignment-III

(Loops)

1. Write a Python program to input a string message and display it 10 times in the following
manner. Use a while loop. Let the string message be “Hello”.
Enter a
message Hello

1st Hello
2nd Hello
3rd Hello
4th Hello
5th Hello
6th Hello
7th Hello
8th Hello
9th Hello
10thHello

2. Write a Python program that gets an integer from the user. Count from 0 to that number.
Use a for loop to do it.

Count to: 20
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20

3. Write a Python program that gets three integers from the user. Count from the first number
to the second number in increments of the third number. Use a for loop to do it.

Count from: 4
Count to: 13
Count by: 3
4 7 10 13

4. Write a Python program that uses a for loop. With the loop, make the variable x go from -2
to 2, counting by 0.5. (This means that x can't be an int.)
-2.0
-1.5
-1.0
-0.5
0.0
0.5
1.0
1.5
2.0

5. Write a Python program that, using one for loop and one if statement, prints the integers
from 1,000 to 2,000 with five integers per line. Hint: Use the % operation.
6. Write a Python program to print the following output using loop.

1
121
1213121
121312141213121
1213121412131215121312141213121

7. If we list all the natural numbers below 10 that are multiples of 3 or 5, we get 3, 5, 6 and 9.
The sum of these multiples is 23. Write a Python program to find the sum of all the
multiples of 3 or 5 below 1000.

8. Write a Python program to print the multiplication table of a number entered by the user.

Enter a no. for which you want to find the multiplication table:
8 8x1=8
8x2=16
8x3=24
8x4=32
8x5=40
8x6=48
8x7=56
8x8=64
8x9=72
8x10=80

9. Write a Python program to find the difference between the sum of the squares of the first
one hundred natural numbers and the square of the sum.

The sum of the squares of the first ten natural numbers


is, 12 + 22 + ... + 102 = 385

The square of the sum of the first ten natural numbers


is, (1 + 2 + ... + 10)2 = 552 = 3025

Hence the difference between the sum of the squares of the first
ten natural numbers and the square of the sum is 3025 − 385 =
2640.

10. Write a Python program called FunctionGrowth that prints a table of the values log N, N,
N log N, N2, N3, and 2N for N = 16, 32, 64, ..., 2048. Use tabs (\t characters) to line up
columns.

11. An integer n is divisible by 9 if the sum of its digits is divisible by 9. Write a Python
program to display each digit, starting with the rightmost digit.

Your program should also determine whether or not the number is divisible by 9. Test it
on the following numbers:

n = 154368
n = 621594
n = 123456

Hint: Use the % operator to get each digit; then use // to


remove that digit. So 154368 % 10 gives 8 and 154368 // 10 gives
15436.
The next digit extracted should be 6, then 3 and so on.

12. Write a Python program to print largest power of two less than or equal to N.

13. Write a Python program to print the below given pattern using while loop as well as for
loop in two different programs.

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

14. Write the Python programs to print the following four patterns using for loop using four
different programs.

(a) (b)

* 1
** 22
*** 333
**** 4444
***** 55555

15. Write a Python program to print the following pattern using nested loops.

* * * * * * * * * * 1
* * * * * * 2
* * * * 3
* * * * 4
* * * 5
* * * * 6
* * 7
* * * * 8
* * * 9
* * * * 10

You might also like