Question

Write a program that asks the user for a lower limit and an upper limit. The...

Write a program that asks the user for a lower limit and an upper limit. The program finds all pairs of amicable numbers where the first number of the pair is between those limits. The second number of the pair may or may not be between the limits. To do this sensibly, write a function int sumDivisors( int num ); that returns the sum of all the proper divisors of num.

The main program looks at each integer N between the limits. For each N, compute the sum S of its proper divisors. Then check if the sum of the divisors of S is the original number N. If N and S are the same number, then N is perfect.

The program writes out amicable pairs and perfect numbers one per line. Write out just one number if it is perfect. Write out each amicable pair just once. If both numbers of a pair are within the limits, then the pair will be found twice. So when a pair is found check that S is greater than N. If so, write out the pair. If not, then the lower number of the pair S has already been found as part of a pair.

Write sumDivisors()as a loop that checks trial divisors starting at 2 and going up until trial*trial > N. If N%trial == 0, then add trial and N/trial to the sum. Initialize the sum to 1 (since 1 divides N).

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

Source Code: #include <stdio.h> int sumDivisors(int n) int trial; int Sum = 1; for (trial = 2; trial*trial < n; trial++) if (n % trial-0) sum = sum + trial + n / trial; if (trial*trial -- n) Sum - Sum +trial; return Sum int main () int size-0, i, j, lower Limit, upper Limit; printf(Enter the lower limit: ) scanf(%d, &lower Limit); printfENter the upper 1imit: ) scanf(%d, &upper Limit); size -upper Limit - lower Limit int arrIsize]; arr ilower Limit i for (i -0; i < size; +i)Editable Code:

#include <stdio.h>
int sumDivisors(int n)
{
   int trial;
   int Sum = 1;
   for (trial = 2; trial*trial < n; trial++)
   {
       if (n % trial == 0)
           Sum = Sum + trial + n / trial;
   }
   if (trial*trial == n)
       Sum = Sum + trial;
   return Sum;
}
int main()
{
   int size=0, i, j, lower_Limit, upper_Limit;
   printf("Enter the lower limit: ");
   scanf("%d", &lower_Limit);
   printf("ENter the upper limit: ");
   scanf("%d", &upper_Limit);
   size = upper_Limit - lower_Limit;
   int arr[size];
   for (i = 0; i < size; ++i)
       arr[i] = lower_Limit + i;
   for (i = 0; i < size; ++i)
   {
       for (j = 0; j < size; ++j)
       {
           if ((arr[i] == sumDivisors(arr[j])) && (arr[j] == sumDivisors(arr[i])))
               if (arr[i] == arr[j])
                   printf("The Perfect number is: %d\n", arr[i]);
               else
                   printf("The Amicable pair is: (%d %d)\n", arr[i], arr[j]);
       }
   }
   system("pause");
   return 0;
}

Add a comment
Know the answer?
Add Answer to:
Write a program that asks the user for a lower limit and an upper limit. The...
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
  • C++, data structure An integer number’s proper divisor is any positive integer that divides the number...

    C++, data structure An integer number’s proper divisor is any positive integer that divides the number without remainder and is less than the number. Neither zero nor any negative number is a proper divisor. Write a function returns true if its second parameter is a proper divisor of its first parameter. The function’s prototype is bool properDivisor(int number, int candidate) Write a function that returns the sum of a number’s proper divisors. The function’s prototype is int properDivisorSum(int number); Use...

  • C Programming Write a C program that asks the user for a positive number n (n...

    C Programming Write a C program that asks the user for a positive number n (n ≥ 1) then, the program displays all the perfect numbers up to (including) n. A number n is said to be a perfect number if the sum of all its positive divisors (excluding itself) equals n. For example, the divisors of 6 (excluding 6 itself) are 1,2 and 3, and their sum equals 6, thus, 6 is a perfect number. Display the numbers with...

  • Write in C++ Programming (Spring 2020) Question 5 [15pts] Write a function to find the first...

    Write in C++ Programming (Spring 2020) Question 5 [15pts] Write a function to find the first 45 Amicable pairs of numbers (series A259180 on the On-line Encyclopedia of Integer Sequences). A pair of numbers x and y is called amicable if the sum of the proper divisors of either one is equal to the other. For example, 220 and 284 is a pair of amicable numbers. The divisors or 220 are 1, 2, 4, 5, 10, 11, 20, 22, 44,...

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

  • Assignment Develop a program to analyze one or more numbers entered by a user. The user...

    Assignment Develop a program to analyze one or more numbers entered by a user. The user may enter one or more numbers for analysis. Input should be limited to numbers from 1 through 1000. Determine if a number is a prime number or not. A prime number is one whose only exact divisors are 1 and the number itself (such as 2, 3, 5, 7, etc.). For non-prime numbers, output a list of all divisors of the number. Format your...

  • Write a C program to sum up all the odd numbers between a lower limit number...

    Write a C program to sum up all the odd numbers between a lower limit number and an upper limit number provided by a user. The requirements are: First, request the user to provide a lower limit integer number and an upper limit integer number that is larger than the lower limit number, and save them in variables lowerLimit and upperLimit, respectively. If the user has entered an upper limit number (upper Limit) that is NOT larger than the lower...

  • C++ Font Exercise #5: Multiples of a Number Write a C++ program that prompts the user...

    C++ Font Exercise #5: Multiples of a Number Write a C++ program that prompts the user to enter three integers, n, m, and k, where n is the lower limit, m is the upper limit of a range of positive numbers, and k is any positive number. The program then prints all multiples of the number k between n and m. Treb Sample input/ output: Charac nter the lower and upper limits: 12 93 Enter the multiple: 7 the multiples...

  • Please Write the task in c++ Task A perfect number is an integer that is equal...

    Please Write the task in c++ Task A perfect number is an integer that is equal to the sum of its divisors (where 1 is considered a divisor). For example, 6 is perfect because its divisors are 1, 2, and 3, and 1 + 2 + 3 is 6. Similarly, 28 is perfect because it equals 1 + 2 + 4 + 7 + 14. A quite good number is an integer whose badness—the size of the difference between the...

  • C program. Using do-while loop for this question. Question: write a program to calculate the average...

    C program. Using do-while loop for this question. Question: write a program to calculate the average of first n numbers. output: Enter the value of n : 18 The sum of first 18 numbers =171 The average of first 18 numbers = 9.00 my code couldn't give me the right avg and sum. Please help. #include<stdio.h> int main() {     int num;     int count =0;     int sum=0;     float avg;      printf("Enter the value of n: ");    ...

  • Write a C++ program to check the upper and lower limits of different data types: int,...

    Write a C++ program to check the upper and lower limits of different data types: int, unsigned int, long long data type, unsigned long long data type, Bits in char data type, char data type signed char data type, unsigned char data type. Please write this in programming C++ language and please explain what we are doing in each step.

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