Question

Write a C++ program divisorsum.cpp which which reads numbers, one at a time, from the terminal...

Write a C++ program divisorsum.cpp which which reads numbers, one at a time, from the terminal input, and outputs the proper divisors and the proper divisor sum for that integer in an equation format.

Here is an example:

> divisorsum
6
6: 1+2+3 = 6
888
888: 1+2+3+4+6+8+12+24+37+74+111+148+222+296+444 = 1392
0
>

Note: spaces will be ignored, but punctuation symbols (:+=) must be in the correct places.

Your program should handle the calculation of proper divisor sums for all numbers between 2 and 2147483647.

Your program should continue answering about the proper divisor sums of numbers until 0 is entered, at which time the program should exit silently.

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

Hello, here is the completed code you wanted. Every important statement is explained using comments. Please check and let me know if you have any doubts. Thanks.

CODE

#include<iostream>

using namespace std;

//main function
int main()
{
   int number;
   unsigned int sum;
      
   //repeat until number is 0
   while(true)
   {
       //prompt for number
       cout<<"\nEnter a number: ";
       cin>>number;
      
       //if number is zero
       if(number == 0)
           //break from the loop
           break;
      
       //initialize sum as 1
       sum = 1;
      
       //printing number and 1
       cout<<number<<": 1";
      
       for(int i = 2; i < number; i++)
       {
           //if i is a proper divisor of number
           if(number % i == 0)
           {
               //print + and i
               cout<<"+"<<i;
              
               //calculating sum
               sum = sum + i;
           }
       }
       //printing sum
       cout<<" = "<<sum;
   }
}

OUTPUT

CODE SCREEN SHOT

Add a comment
Know the answer?
Add Answer to:
Write a C++ program divisorsum.cpp which which reads numbers, one at a time, from the terminal...
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
  • 2 Happy Numbers 2.1 Definition The definition of a happy number, from wikipedia Given a number...

    2 Happy Numbers 2.1 Definition The definition of a happy number, from wikipedia Given a number n = no, define a sequence n1, n2, … where ni+1 1S the sum of the squares of the digits of n,. Then n is happy if and only if there exists i such that ni 1 It can be shown (see wikipedia) that n is unhappy if the number 4 appears in the sequence. 2.2 The program Write a C++ program happy.cpp which...

  • 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 =...

  • Instructions In c++ write a program that reads in a string of zeros and ones without...

    Instructions In c++ write a program that reads in a string of zeros and ones without spaces and for each 8 digits (one byte) outputs the corresponding value in decimal format. The output numbers are separated by spaces. Stop when a character different than 0 or 1 is encountered. The final output may be generated from less than 8 digits but no output should be generated for the final character. Should work for example: input: 00000011000000010010X output: 3 1 2

  • Write a program “hw4.c” that reads integer (less than or equal 100) from the keyboard and,...

    Write a program “hw4.c” that reads integer (less than or equal 100) from the keyboard and, on the output, writes the sum of the divisors of n (other than itself). For integers less than or equal to 1 it should print 0. For example, the input -3 0 1 4 5 6 12 should generate the output 0 0 0 3 1 6 16 Explanation of output: The input -3 is less than 1, output is 0. The input 0...

  • 4. Write a C++ program (or Java program) w that reads two groups of numbers in...

    4. Write a C++ program (or Java program) w that reads two groups of numbers in which each group has random integer numbers with possible duplicates. Your program should display the common numbers without any duplicates in the ascending order. Input format: This is a sample input from a user. cik length WC tontu

  • use python: Write a program that reads in X whole numbers and outputs (1) the sum...

    use python: Write a program that reads in X whole numbers and outputs (1) the sum of all positive numbers, (2) the sum of all negative numbers, and (3) the sum of all positive and negative numbers. The user can enter the X numbers in any different order every time, and can repeat the program if desired. Sample Run How many numbers would you like to enter? 4 Please enter number 1: 3 Please enter number 2: -4 Please enter...

  • I/O program for C Write a program in direct1.c which reads from standard input a line...

    I/O program for C Write a program in direct1.c which reads from standard input a line and then outputs that line immediately to standard output. it should read the first line only ! Then, write another program called direct2.c which reads from standard input every line until end of input and outputs them to standard output. Everything that goes in should come out exactly as it was. Compile both programs and run them on the input files given below The...

  • Write a ( JAVÅ) program which reads (from the keyboard) numbers (doubles) until a zero is...

    Write a ( JAVÅ) program which reads (from the keyboard) numbers (doubles) until a zero is input and computes the maximum and minimum of all the read numbers (excluding the terminating zero). Note: you must allow for negative as well as positive numbers. Hint: look at java.lang.Double.MAX_VALUE and java.lang.Double.MIN_VALUE.

  • Write a program, called wordcount.c, that reads one word at a time from the standard input....

    Write a program, called wordcount.c, that reads one word at a time from the standard input. It keeps track of the words read and the number of occurrences of each word. When it encounters the end of input, it prints the most frequently occurring word and its count. The following screenshot shows the program in action: adminuser@adminuser-VirtualBox~/Desktop/HW8 $ wordCount This is a sample. Is is most frequent, although punctuation and capitals are treated as part of the word. this is...

  • Write a C++ program that reads text from a file and encrypts the file by adding...

    Write a C++ program that reads text from a file and encrypts the file by adding 6 to the ASCII value of each character. See section 5.11 in Starting out with C++ for information on reading and writing to text files. Your program should: 1. Read the provided plain.txt file one line at a time. Because this file has spaces, use getline (see section 3.8). 2. Change each character of the string by adding 6 to it. 3. Write the...

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
Active Questions
ADVERTISEMENT