Looping Structures

[Set – 2]

1. Write a Python program that prompts the user to enter a positive integer. Your program should display all the factors of the number. Additionally, calculate and display the sum of its factors.

Sample output:
Enter a positive integer: 45
Factors: 1 3 5 9 15 45
Sum of factors: 78
Solution

2. Write a program that uses a loop to repeatedly ask the user to enter positive numbers. The loop will come to an end when a negative number is entered. After collecting all the positive numbers, the program will compute their sum and display the result to the user.
Solution

3. Write a program that uses a loop to repeatedly ask the user to enter integers. The loop will come to an end when zero is entered. After collecting all the integers, the program will compute and display the average of all the entered numbers.
Solution

4. Write a program to enter the numbers till the user wants and at the end it should display the count of positive, negative and zeros entered. Solution

5. Write a program to enter the numbers till the user wants and at the end the program should display the largest and smallest numbers entered. Solution

6. Write a program that asks the user to input a positive integer. Your program should find and display the sum of digits of number. For example, sum of digits of number 32518 is 3+2+5+1+8 = 19.
Solution

7. An Armstrong number of three digits is an integer in which the sum of the cubes of its digits is equal to the number itself. For example, 371 is an Armstrong number since 33 + 73 + 13 = 371.

Write a program to check whether a number is an Armstrong number or not. Solution

8. Write a program that prompts the user to input a number and reverse its digits. For example, the reverse of 12345 is 54321; reverse of 5600 is 65. Solution

9. A palindromic number is a number that remains the same when its digits are reversed. For example, 16461. Write a program that prompts the user to input a number and determine whether the number is palindrome or not. Solution

10. Write a program that prompts the user to input a decimal integer and display its binary equivalent. Solution

11. Write a program that prompts the user to input a binary number and display its decimal equivalent. Solution

12. Write a program that prompts the user to input a positive integer. It should then output a message indicating whether the number is a prime number. A prime number is a number that is evenly divisible only by itself and 1. For example, the number 5 is prime because it can be evenly divided only by 1 and 5. The number 6, however, is not prime because it can be divided evenly by I, 2, 3, and 6. Solution

13. Write a program to obtain the first 25 numbers of a Fibonacci sequence. In a Fibonacci sequence the sum of two successive terms gives the third term. Following are the first few terms of the Fibonacci sequence:
0 1 1 2 3 5 8 13 21 34 55 89... Solution

14. Write a program that prompts the user to input two numbers and display its HCF. The Highest Common Factor (HCF) also called the Greatest Common Divisor (GCD) of two whole numbers, is the largest whole number that's a factor of both of them. Solution

15. Write a program to add first seven terms of the following series using a for loop:
Solution

16. Compute the sum up to n terms in the series
1 - 1/2 + 1/3 - 1/4 + 1/5 -... 1/n
where n is a positive integer and input by user. Solution

17. Write programs to print following patterns :

1.
**********
**********
**********
**********
        
2.
*
**
***
****
*****
3.
    *
   **
  ***
 ****
*****
4.
    *
   ***
  *****
 *******
*********
5.
    1
   222
  33333
 4444444
555555555
6.
    1
   212
  32123
 4321234
543212345
Solution

18. Floyd's triangle is a right-angled triangular array of natural numbers as shown below:

Write a program to print the Floy'd triangle. Solution

19. Write a program to compute sin x for given x. The user should supply x and a positive integer n. We compute the sine of x using the series and the computation should use all terms in the series up through the term involving xn
sin x = x - x3/3! + x5/5! - x7/7! + x9/9! ........ Solution

20. Write a program to compute cosine of x. The user should supply x and a positive integer n. We compute the cosine of x using the series and the computation should use all terms in the series up through the term involving xn
cos x = 1 - x2/2! + x4/4! - x6/6! .....Solution

21. Write a program that generates a random number and asks the user to guess what the number is. If the user's guess is higher than the random number, the program should display "Too high, try again." If the user's guess is lower than the random number, the program should display "Too low, try again." The program should use a loop that repeats until the user correctly guesses the random number. Program should count and display number of tries to win the game. Solution