Question

a) Write a program that uses a while loop to print all divisors of a number...

a) Write a program that uses a while loop to print all divisors of a number supplied by the user. The program should also print the sum of all the divisors and whether the number the user entered is a prime number or not. Note: The definition of a divisor is a number that divides another evenly (i.e., without a remainder) and the definition of a prime number is a number whose only divisors are 1 and itself.

b) Implement the same program as above using a for loop instead of a while loop. Which implementation do you think is the better choice for this problem? Discuss with a friend and/or a TA if you are unsure.

Sample Outputs (user input highlighted)

Enter an integer: 20

The divisors are:

1

2

4

5

10

20

The sum of the divisors is 42

The number is not prime.

Enter an integer: 19

The divisors are:

1

19

The sum of the divisors is 20

The number is prime!

use Python

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Here in this Python Program

- We are accepting int input from the user and calling the function Divisors

- The first step, we are calculating the divisors for the number

- Then printing the divisors of that number

- Again calculating the sum of the divisors and printing on to the screen

- Checking if the number is prime or not and

- Finally printing whether it is prime or not.

Program using While Loop:

def Divisors(num):
divisors_list=[]
#calculating Divisors for a Number
i=1
while i<=num:
if(num%i==0):
divisors_list.append(i)
i=i+1
print("The Divisors are:")
for i in divisors_list:
print(i)
sum=0
#Calulcating the Sum of Divisors
for i in range(0,len(divisors_list)):
sum=sum+divisors_list[i]
print("The sum of the Divisor is",sum)
  
#checking whether the number is prime or not
if num>1:
for i in range(2,num//2):
if num%i==0:
print("The Number is not prime")
break;
else:
print("The Number is prime!!!")
else:
print("The Number is not prime")
  

number=int(input("Enter an integer: "))
Divisors(number)

Output:

Program using For Loop:

def Divisors(num):
divisors_list=[]

#calculating Divisors for a Number
for i in range(1, num + 1):
if num % i == 0:
divisors_list.append(i)
#printing divisors
print("The Divisors are:")
for i in divisors_list:
print(i)
sum=0
#Calulcating the Sum of Divisors
for i in range(0,len(divisors_list)):
sum=sum+divisors_list[i]
print("The sum of the Divisor is",sum)
  
#checking whether the number is prime or not
if num>1:
for i in range(2,num//2):
if num%i==0:
print("The Number is not prime")
break;
else:
print("The Number is prime!!!")
else:
print("The Number is not prime")
  

number=int(input("Enter an integer: "))
Divisors(number)

Output:

Hope this Helps!!!

If not please comment, I will Help you with that....

Add a comment
Know the answer?
Add Answer to:
a) Write a program that uses a while loop to print all divisors of a number...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • A perfect number is a positive integer that is equal to the sum of its (proper)...

    A perfect number is a positive integer that is equal to the sum of its (proper) positive divisors, including 1 but excluding itself. A divisor of a number is one which divides the number evenly (i.e., without a remainder). For example, consider number 6. Its divisors are 1, 2, 3, and 6. Since we do not include number itself, we only have 1, 2, and 3. Because the sum of these divisors of 6 is 6, i.e., 1 + 2...

  • A positive integer is a prime number if its only positive integer divisors are itself and...

    A positive integer is a prime number if its only positive integer divisors are itself and 1. Write a program to determine whether or not a given integer is prime. The program should contain two functions: main: to ask the user for a positive integer and to print the result isPrime: to determine whether the user's input is prime by testing all possible divisors. This function should return two values: variable_1: a Boolean value indicating whether the number is prime...

  • Need help programing this in C. rinteivsors Print the proper divisors of an integer value The...

    Need help programing this in C. rinteivsors Print the proper divisors of an integer value The program should read a single integer input value, which you can assume will be positive. It should then print a single line of output with all of the proper divisors of the input value in order from least to greatest. A proper divisor d of an integer n is an integer that evenly divides n: i.e., nld is an integer For example, if the...

  • Write a Python program to print all Perfect numbers between 1 to n. (Use any loop...

    Write a Python program to print all Perfect numbers between 1 to n. (Use any loop you want) Perfect number is a positive integer which is equal to the sum of its proper positive divisors. Proper divisors of 6 are 1, 2, 3. Sum of its proper divisors = 1 + 2 + 3 = 6. Hence 6 is a perfect number. *** proper divisor means the remainder will be 0 when divided by that number. Sample Input: n =...

  • Write a java program to print all the prime numbers below a certain given number. A...

    Write a java program to print all the prime numbers below a certain given number. A prime number is defined as a number that can only be divided by 1 and itself. Requirements: 1. Accept the upper limit from the user as an integer. 2. You can assume the user input will be positive and smaller than INT MAX 3. Go from 1 to the number. If you happen to find a number that is prime, print it. The input...

  • Write a program to print all the prime numbers below a certain given number. A prime...

    Write a program to print all the prime numbers below a certain given number. A prime number is defined as a number that can only be divided by 1 and itself. Requirements: 1. Accept the upper limit from the user as an integer. 2. You can assume the user input will be positive and smaller than INT MAX 3. Go from 1 to the number. If you happen to find a number that is prime, print it. The input and...

  • In this assignment you are asked to write a Python program to determine all the prime...

    In this assignment you are asked to write a Python program to determine all the prime numbers in between a range and store them in a list that will be printed when the range is searched. The user is prompted for two positive integer values as input, one is the start of the range and the other is the end of the range. If the user gives a negative value or enters a second number that is lower than the...

  • A perfect number is a positive integer that equals the sum of all of its divisors...

    A perfect number is a positive integer that equals the sum of all of its divisors (including the divisor 1 but excluding the number itself). For example 6, 28 and 496 are perfect numbers because 6=1+2+3 28 1 + 2 + 4 + 7 + 14 496 1 + 2 + 4 + 8 + 16 + 31 + 62 + 124 + 248 Write a program to read a positive integer value, N, and find the smallest perfect number...

  • I must create a Loop in C++ that will print all prime numbers less than the...

    I must create a Loop in C++ that will print all prime numbers less than the number entertered by the user. 1. Accept the upper limit from the user (as an integer). (5 points) 2. Make sure the number is positive. If it is not, terminate the program. (5 points). 3. Go from 1 to the number. If you happen to find a number that is prime, print it. (35 points) example: Enter the upper limit: 25 The prime numbers...

  • For Python: A prime number is defined as an integer greater than 1 that has no...

    For Python: A prime number is defined as an integer greater than 1 that has no positive divisors other than 1 and itself. Write a program that prompts the user for an integer > 1. Validate the value is > 1 (if not, ask for another). Use a loop to determine if the number is prime or not. Issue an appropriate message. [complete this part before proceeding]. Add a loop that continues to ask the user if they would like...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT