Question

Using the C Programming Language: An integer number is said to be a perfect number if...

Using the C Programming Language:

An integer number is said to be a perfect number if its factors, including 1 (but not the number itself), sum to the number. For example, 6 is a perfect number because 6 = 1 + 2 + 3. Write a function perfect that determines if parameter number is a perfect number. Use this function in a program that determines and prints all the perfect numbers between 1 and 1000. Print the factors of each perfect number to confirm that the number is indeed perfect.

Note that the function perfect should be a boolean function (returns a value of 'true' i.e. 1 or 'false' i.e. 0).

Example:

The perfect numbers between 1 and 1000 are:

6 is a perfect number; It's factors are = 1, 2, 3

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

ANSWER :

#include<stdio.h>

int test(int);
void printfactors(int);
int main(){
int number,i;
int perfect;
printf("Enter a number: ");
scanf("%d",&number);
do{
perfect=test(number);
if (perfect==1)
{
printf("\n%d ",number);
printfactors(number);
}
number++;
}while(number<1001);

printf("\nPress any key to quit ");
getch();
return 0;
}
int test(int NUM){
int result=0;
int larger=NUM, smaller=1;//saves the larger and smaller factor of NUM
int sum=0-NUM;// sum set to negative of NUM to not 'add' NUM to calculation

while(larger>smaller){
if(NUM%smaller==0){// if a factor, add both, the smaller and larger factor to sum
larger=NUM/smaller;
sum+=larger+smaller;
}
smaller++;//increment smaller factor by one
}

if(sum==NUM && NUM!=0){
result=1;
}

return result;
}

void printfactors(int number){
int i;
printf(" [ ");
for(i=1;i<=number;i++){
if(number%i==0){
printf("%d ",i);
}
}
puts("]");
return;
}

( OR )

ANOTHER ANSWER IS--------

#include <stdio.h>

void perfect(int num)

{

int i = 1;

int sum = 0;

while (i < num)

{

if (num % i == 0)

sum = sum + i;

i = i + 1;

}

i = 1;

if (sum == num)

{

printf ("%d = ", num);

while (i < num)

{

if (num % i == 0)

printf(" %d + ", i);

i = i + 1;

}

printf("\n%d is perfect number\n", num);

}

else

printf("%d is NOT perfect number\n", num);

}

int main()

{

int i = 2;

while (i < 1000)

{

perfect(i);

i = i + 1;

}

return 0;

}

Add a comment
Know the answer?
Add Answer to:
Using the C Programming Language: An integer number is said to be a perfect number if...
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
  • An integer number is said to be a perfect number if its factors, including 1 (but...

    An integer number is said to be a perfect number if its factors, including 1 (but not the number itself), sum to the number. For example, 6 is a perfect number, because 6 = 1+2+3. Write a method isPerfect that determines if parameter number is a perfect number. Use this method in a test program to display all the perfect numbers between 1 and 1000. Display the factors of each perfect number to confirm that the number is indeed perfect.

  • it should be written in c or c++ program Question 1 (25 points) An integer number...

    it should be written in c or c++ program Question 1 (25 points) An integer number is said to be a perfect number if its factors, including 1 (but not the number itself), sum to the number. For example, 6 is a perfect number because 6 -1 + 2 + 3. Write a function perfect that determines if parameter number is a perfect number. Use this function in a program that determines and prints all the perfect numbers between 1...

  • Question 3 (10 marks) An integer number is said to be a perfect number if its...

    Question 3 (10 marks) An integer number is said to be a perfect number if its factors, including 1 (but not the number itself), sum to the number. For example, 6 is a perfect number because its factors, except itself, are 3, 2, and 1, and 6 = 3+2+1, but 12 is not a perfect number because its factors, except itself, are 6, 4, 3, 2, and 1, but 12 # 6+4+3+2+1. First. Write a C function, called isperfect, that...

  • An integer is said to be a perfect number if the sum of its divisors, including...

    An integer is said to be a perfect number if the sum of its divisors, including 1(but not the number itself), is equal to the number. For example, 6 is a perfect number because 6 = 1+2+3. A) Write a function numPerfect( number ) that returns true when the number is a perfect number, false when it is not.        B) Write a C# console program that calls the function in A) to determine and print all the perfect numbers...

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

  • A positive integer n is “perfect” if the sum of its positive factors, excluding itself, equals...

    A positive integer n is “perfect” if the sum of its positive factors, excluding itself, equals n.  Write a perfect function in Haskell that takes a single integer argument and returns the list of all perfect numbers up to that argument. Report all of the perfect numbers up to 1000 (i.e. call 1000)

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

  • For C++: A perfect number has these properties 1) positive integer 2) the sum of its...

    For C++: A perfect number has these properties 1) positive integer 2) the sum of its proper divisors is the number itself 6 and 28 are perfect numbers Write two functions that return true if its value parameter is a perfect number and false if its value parameter is not a perfect numbers. The two function look like bool isPerfect(int) and void isPerfect(int, bool&) If you want more perfect number you find them on the Internet.

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

  • 1. An integer is said to be perfect if the sum of its factors, including 1,...

    1. An integer is said to be perfect if the sum of its factors, including 1, is equal to the number itself. Write a Prolog predicate perfect(N, F), which determines if integer N is perfect and if so, return it’s list of factors F. If N is not perfect, the predicate fails. You may find the arithmetic operation M mod N to be useful. Test your predicate on the following. • perfect(6, Factors) ⇒ Factors = [1, 2, 3] •...

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