Question

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 is less than 1, output is 0.
The input 1 is equal to 1, output is 0.
The input 4: 4x1=4 and 2x2=4 the divisors other than itself are 2 and 1 so 2+1=3. Output is 3.
The input 5: 5x1=5 the only divisors of a prime number are itself and 1. Output is a 1.
The input 6: 6x1=6 and 3x2=6 the divisors other than itself are 1, 2 and 3. Output is 6=1+2+3.
The input 12: 12x1=12 and 6x2=12 and 3x4=12. Output=16=1+6+2+3+4.
Comment:
Primes should generate a 1. and perfect numbers should echo themselves.
(In number theory, a perfect number is a positive integer that is equal to the sum of its proper positive divisors, that is, the sum of its positive divisors excluding the number itself. Wikipedia)
Objectives:
1. Create this routine as a function, sum_divisors() and call it from main.
2. By successively applying sum_divisors() you can compute the trajectory of the function starting with a particular value. For example, if we compute the trajectory of function on the value 1, we get 1 0 0 . . .since n is 1, sum_divisors(n) is 0, sum_divisor(sum_divisors(n)) is 0, etc. Clearly, once we see any value repeated in the sequence, we know the entire pattern. For 1, this happens after two calls to the function.
3. Prime numbers, which immediately reduce to 1 always, have a short trajectory. For example, from the value 43 we have: 43 1 0 0...which loops at 3 applications of sum_divisors().
4. Perfect numbers repeat immediately 6..6…6…6 ...so only one application of sum_divisor() detects a cycle.
5. Enhance your program so that for each integer read, it prints the number of times sum_divisor() is called in order to detect a cycle.
6. The final program( the program that should be submitted) should allow the user to enter up to 10 integers. If the user enters:
23 -4 6 45
the output should be the following:
23, 1, 0, 0 3 cycles
-4 , 0, 0 2 cycles
6, 6, 1 cycle
45, 33, 15, 9, 4, 3, 1, 0, 0 8 cycles
If the user enters an invalid input (number greater than 100 or a character) the program should end, exit(1) command ends the program.

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

#include<stdio.h>
#include<conio.h>

int sum_divisors(int n)
{
   int sum=0, i,c;
   if(n<=1)
   {
       return 0;
   }
   for(i=2;i<n;i++)
   {
       c=n%i;
       if(c==0)
       {
           sum=sum+i;
       }
   }
   return sum;
}

void main()
{
   int n[10],i,c;

   for(i=0;i<10;i++)
   {
       printf("Enter number");
       scanf("%d",&n[i]);
   }

   for(i=0;i<10;i++)
   {
       c=sum_divisors(n[i]);
       printf("\nThe input %d has sum of

divisors %d",n[i],c);

   }
   getch();

}

Add a comment
Know the answer?
Add Answer to:
Write a program “hw4.c” that reads integer (less than or equal 100) from the keyboard and,...
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...

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

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

  • I got a C++ problem. Let n be a positive integer and let S(n) denote the...

    I got a C++ problem. Let n be a positive integer and let S(n) denote the number of divisors of n. For example, S(1)- 1, S(4)-3, S(6)-4 A positive integer p is called antiprime if S(n)くS(p) for all positive n 〈P. In other words, an antiprime is a number that has a larger number of divisors than any number smaller than itself. Given a positive integer b, your program should output the largest antiprime that is less than or equal...

  • Use C programming Make sure everything works well only upload Write a program that takes an...

    Use C programming Make sure everything works well only upload Write a program that takes an integer from standard input and prints all prime numbers that are smaller than it to standard output. Recall that a prime number is a natural number greater than 1 whose only positive divisors are 1 and itself. At the start of the program, prompt the user to input a number by printing "Input a number greater than 2:\n". If the user's input is less...

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

  • A positive integer is said to be a perfect number if it equals the sum of...

    A positive integer is said to be a perfect number if it equals the sum of its positive divisors (excluding the number itself). As an example, 6 is aperfect number because its divisors, 1, 2, and 3 sum up to 6. The first four perfect numbers are 6, 28, 496, 8128. Write a C program that asks the user to enter a number and checks if the number is perfect. Your program should run interactively until the user quits. Try...

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

  • C++ 2. (a) Write a function, printdixisors, that takes a single integer parameter and prints all...

    C++ 2. (a) Write a function, printdixisors, that takes a single integer parameter and prints all the numbers less that the parameter that are divisors of the parameter (i.e. divides it without a remainder) including 1. So printdivisors(6) will print 1,2,3. Note you may use a wrapper function or default parameters. (b) Write a function, sumdixisors, that takes a single integer parameter and returns the sum of all the divisors of the parameter (including 1). So sumdivisors(6) will return 6...

  • This Java program reads an integer from a keyboard and prints it out with other comments....

    This Java program reads an integer from a keyboard and prints it out with other comments. Modify the comments at the top of the program to reflect your personal information. Submit Assignment1.java for Assignment #1 using Gradescope->Assignemnt1 on canvas.asu.edu site. You will see that the program has a problem with our submission. Your program is tested with 4 testcases (4 sets of input and output files). In order to pass all test cases, your program needs to produce the same...

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