Question

Question 3 (10 marks) An integer number is said to be a perfect number if its factors, including 1 (but not the number itself

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

Find the code for the above question below, read the comments provides in the code for better understanding. If found helpful please do upvote this.

Code

#include<stdio.h>//standard header fiel for c programs
#include<math.h>//for performing mathematical opeartions , in this program it is the square root

//method which returns 1 if the num is perfect else returns 0
int isPerfect(int num){
//declare and initialize a variable total to store sum of all factors
int total=0;
//run a loop from 1 till square root of the number
//beaucse the factors can be found using those numbers only
//this saves time and numebr of comparisons
for(int i=1;i<=sqrt(num);i++){
//if the num is divisible by i then it is one of teh factors
if(num%i==0){
//teh other factor will be num/i
//if both i and num/i are same then take only one i.e. i
if(num/i==i)
total += i;//add it to total
else{
//else add both i and num/i to the total
total += i;
total += num/i;
}
}
}
//the total now has the sum of all factors including teh numebr itself ,
//so subtract the number form the total
total -= num;
//now chekc if total is equal to the numebr if yes then it is a poerfect num
//in that case return 1
if(total == num)
return 1;
//else return 0
else
return 0;
}

//main driver code of the program
int main(){
//print a header
printf("Perfect Numbers from 1 - 1000 are :\n");
//run a loop from 1 to 1000 and make method call for each
for(int i=1;i<=1000;i++){
//call the isPerfect() method , if it returns 1 then it is a perfect num
//so show it
if(isPerfect(i)==1){
printf("%d ",i);
}
}
  
}

Output

The output shows all the perfect numbers from 1 to 1000 and as per the output only 3 such number exist.

Add a comment
Know the answer?
Add Answer to:
Question 3 (10 marks) An integer number is said to be a perfect number if its...
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.

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

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

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

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

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

  • Write java code to create 3 methods that accomplish the below concepts according to simple number...

    Write java code to create 3 methods that accomplish the below concepts according to simple number theory. 1. Create a file PositiveInteger.java that houses a class called PositiveInteger 2. This class should have one single instance variable called num This is what I have for the constructor: public PositiveInteger(int number) { num = number; } The three methods should start with the italicized sections shown below to accomplish the corresponding tasks: 1. public boolean isPerfect () - A number is...

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

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

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