Question

In: Computer Science

{ /* Write a program in C++ to find the perfect numbers between 1 and 500....

{
/* Write a program in C++ to find the perfect numbers between 1 and 500.
The perfect numbers between 1 to 500 are:
6
28
496*/
   int sum = 0;
   int i = 1;
   int j = 1;
   for (i ; i <= 100; i++)
   {
       for (j; j <= 100; j++)
       {
           if (j < i)//Line 55
           {
              
           if (i % j == 0)
               sum = sum + j;
           }
          
       }

       if (sum == i)
      
           cout << i << " ";
       j = 1;//line 66
       sum = 0;
      
   }
  
   return 0;
}

Can someone please explain the code above and how it works. what is the purpose of the comparison on line 55 and I on line 66?

Solutions

Expert Solution

#include<iostream>

using namespace std;

int main()
{
   int sum = 0;
   int i = 1;
   int j = 1;

   //perfect number is equal to sum of its divisors
   //Example : number 6
   //divisors of 6 are : 1,2,3 = sum of these values is 1+2+3 = 6
   //below code is implement by using above logic
   //Here i is the number to check,if it is perfect number or not
   for (i ; i <= 500; i++)
   {
       //here j is used to divide i value
       for (j; j < i; j++)
       {
           //This condition used to check the value j is less than i
           //We have to find the divisor of i upto i-1 value.So we have to wrote the condition (j<i)
          //For example i value is 6
          //To check is perfect number or not.we have to divide i value with j value and it gives reminder zero and also some of those j values should equal to i value
         //suppose i = 6
         //6%1 = reminder 0
         //6%2 = reminder 0
         //6%3 = reminder 0
         //6%4 = remimder 2
         //6%5 = remimder 1.After this the condition will fail.
           if (j < i)//Line 55
           {
               //This condition is used to find the number i is divisible by j are not.Then it add the j value with sum.
               //In above example,sum the value of j which got the reminder 0.
               //1+2+3 = 6.So 6 is the perfect number
               if (i % j == 0)
                 sum = sum + j;
           }
       }
       //if sum equal to i
       //At i = 6,sum will become 6.Then it prints the sum
       if (sum == i)
          cout << i << " ";
       //we have to divide every number (i)starting from 1 to i-1.But after completion of inner for loop, j value will different not equal to 1.So we have to initialize j value with 1 before going to outer for loop.So here it writtern as j = 1.Otherwise from next iteration,i will not divide from 1.
       j = 1;//line 66
       sum = 0;
     }
     return 0;
}


Related Solutions

. (a) Write a C++ program to find Fibonacci numbers. (For the definition of Fibonacci numbers...
. (a) Write a C++ program to find Fibonacci numbers. (For the definition of Fibonacci numbers and the recursive formula please refer to problem 5 of Chapter 2 at page 81 in the text-book.) The program asks the user to input an integer and outputs the corresponding Fibonacci number. For example, if the user inputs 4, the program outputs the fifth Fibonacci number, which is 3. Introduce two local variables in the recursion function (suppose we call it fib(n)), one...
Question : Write a C++ program to find all prime numbers between 10 to 100 by...
Question : Write a C++ program to find all prime numbers between 10 to 100 by using while loop. Hint: a prime number is a number that is divisible by 1 and itself. For example 3, 5, 7, 11, 13 are prime numbers because they are only divisible by 1 and themselves.
Write a C++ program to print all the perfect numbers below a certain given number. A...
Write a C++ program to print all the perfect numbers below a certain given number. A perfect number is a number that that is equal too the sum of it's divisors other than itself . For example, 6 and 28 are all perfect numbers. 6 = ( 1+2 +3) 28 = (1+2+4+7+14) Make sure your program conforms to the following requirements: 1. Accept the upper limit from the user (as an integer). 2. Make sure the number is positive (at...
Write a program in C++ that computes the sum of odd numbers between 1 and 117....
Write a program in C++ that computes the sum of odd numbers between 1 and 117. Execute the program and submit a screen capture of the program and its results.
Write a program in C++ coding that generates a random number between 1 and 500 and...
Write a program in C++ coding that generates a random number between 1 and 500 and asks the user to guess what the number is. If the user’s guess is higher than the random number, the program should display “Too high, try again.” If the user’s guess is lower than the random number, the program should display “Too low, try again.” The program should use a loop that repeats until the user correctly guesses the random number. Count the number...
Write a program in C++ that prints out the even numbers between 1 and 21 using...
Write a program in C++ that prints out the even numbers between 1 and 21 using WHILE loop. Also, find the sum AND product of these numbers and display the resulting sum and product.
Write a C++ program to read N numbers. Find sum, product, and average of N numbers
Write a C++ program to read N numbers. Find sum, product, and average of N numbers
write C program to create 4 threads for summing the numbers between 1 and 40 where...
write C program to create 4 threads for summing the numbers between 1 and 40 where the first thread computes the sum between 1 and 10; the second thread computes the sum between 11 and 20; the third thread computes the sum between 21 and 30; and the fourth thread compute the sum between 31 and 40. The output should be similar to the following. The sum from 1 to 10 = 55 The sum from 11 to 20 =...
write pseudocode not c program If- else programming exercises 1.    Write a C program to find...
write pseudocode not c program If- else programming exercises 1.    Write a C program to find maximum between two numbers. 2.    Write a C program to find maximum between three numbers. 3.    Write a C program to check whether a number is negative, positive or zero. 4.    Write a C program to check whether a number is divisible by 5 and 11 or not. 5.    Write a C program to check whether a number is even or odd. 6.    Write...
Write a c++ program that prints the count of all prime numbers between A and B...
Write a c++ program that prints the count of all prime numbers between A and B (inclusive), where A and B are defined as follows: A = Any 5 digit unique number B = A + 1000 Just a recap on prime numbers: A prime number is any number, greater or equal to 2, that is divisible ONLY by 1 and itself. Here are the first 10 prime numbers: 2, 5, 7, 11, 13, 17, 19, 23, and 29. Rules:...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT