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 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 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 which asks the user to enter a name of a person and...

    Write a C program which asks the user to enter a name of a person and the program finds the number of that person. The number is displayed. Otherwise output "Not found". Your program have to check only given names. The program declares and initializes a two-dimensional array of characters that holds pairs of names and numbers. The array can hold 10 strings, each capable of holding up to 79 characters. Array elements are following: "Adam", "585-5622", "Mark", "505-8446", "Austin",...

  • Please paste your code and a screenshot of your output! 1. An integer n is divisible...

    Please paste your code and a screenshot of your output! 1. An integer n is divisible by 9 if the sum of its digits is divisible by 9. Develop a program to determine whether or not the following numbers are divisible by 9: n= 154368 n 621594 n-123456 2. A number is said to be perfect if the sum of its divisors (except for itself) is equal to itself. For example, 6 is a perfect number because the sum of...

  • Write a C Program that inputs an array of integers from the user along-with the length...

    Write a C Program that inputs an array of integers from the user along-with the length of array. The program then prints out the array of integers in reverse. (You do not need to change (re-assign) the elements in the array, only print the array in reverse.) The program uses a function call with array passed as call by reference. Part of the Program has been given here. You need to complete the Program by writing the function. #include<stdio.h> void...

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
ADVERTISEMENT