Using Python 1----Write a short program that will: Prompt the user for a number
ID: 669233 • Letter: U
Question
Using Python
1----Write a short program that will:
Prompt the user for a number
Print out whether the number is a perfect number
Prompt the user for another number if the number is not perfect
2----Write a program which gets a natural number n, and classifies the range of numbers (natural numbers less that n), with respect to perfect, abundant or deficient.
Use two while loop (the example discussed in the lecture).
Rewrite the program replacing the two while loops with two for loops and range.
3----Sum of consecutive integers
Write a program that prompts for an integer—let’s call it X—and then nds the sum of X consecutive integers starting at 1. That is, if X = 5, you will nd the sum of 1 + 2 + 3 + 4 + 5 = 15
Modify your program by enclosing your loop in another loop so that you can nd consecutive sums. For example, if 5 is entered, you will nd ve sums of consecutive numbers:
1 = 1
1+2 = 3
1+2+3 = 6
1+2+3+4 = 10
1+2+3+4+5 = 15
Print only each sum, not the arithmetic expression.
Modify your program again to only print sums if the sum is divisible by the number of operands. For example, with the sum 1 + 2 + 3 + 4 + 5 = 15, there are ve operands and the sum, 15, is divisible by 5, so that sum will be printed. (Do you notice a pattern?)
4---Write a program that prompts for an integer and prints the integer, but if something other than an integer is input, the program keeps asking for an integer. Here is a sample session:
Input an integer: abc
Error: try again.
Input an integer: 4a
Error: try again.
Input an integer: 2.5
Error: try again.
Input an integer: 123
The integer is: 123
Hint: the string isdigit method will be useful.
Explanation / Answer
1)Source Code:
Output 1:
Enter a number: 407
407 is not a prime number
11 times 37 is 407
Output 2:
Enter a number: 853
853 is a prime number
In this program, user is asked to enter a number and this program check whether that number is prime or not. Numbers less than or equal to 1 are not prime numbers. Hence, we only proceed if thenum is greater than 1. We check if num is exactly divisible by any number from 2 to num - 1. If we find a factor in that range, the number is not prime. Else the number is prime.
We can decrease the range of numbers where we look for factors. In the above program, our search range is from 2 to num - 1. We could have used the range, [2, num / 2] or [2, num ** 0.5]. The later range is based on the fact that a composite number must have a factor less than square root of that number. Otherwise the number is prime.
sorry for this i know only 1 bit of this question
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.