Ticker

6/recent/ticker-posts

Perfect number Program in C | C++ | JAVA

Start Practicing LeetCode with Programming ChaskaI have explained my approach to the problems in simple way. I have also shared the optimized code so that you can learn some new. 

Perfect number Program in C | C++ | JAVA

Perfect Number: A natural number that is equal to the sum of its all factors (excluding number as its factor itself), is called a Perfect Number.

For Example: Number --> 6
factors of 6 are -> 1,2 and 3
->1 + 2 + 3 = 6.
So, 6 is a Perfect Number.
Some of the Perfect numbers are 28, 496, and 8128.

Algorithm to check whether a given number is a Perfect number or not:

Step 1: Accept a number.

Step 2: Find factors of number starting from 1 to number (Do not include the number itself as a factor). If given number is divisible by number smaller than itself, then its a factor.

Step 3: Add all the factors of a number and store it in a variable.

Step 4: If the sum of all the factors of a number is equal to the number itself then Display Perfect number else Display Not a Perfect number.

Step 5: Stop

Implementation in C:

Code 1: This is the general code in C with time complexity of O(n).



#include <stdio.h>
int main()
{
    int numbersum = 0;
    printf("Enter number here: ");
    scanf("%d", &number);
    for (int i = 1i < numberi++)
    {
        if (number % i == 0)
            sum = sum + i;
    }
    if (sum == number)
        printf("The given number %d is a Perfect Number"number);
    else
        printf("The given number %d is not a Perfect Number"number);
    return 0;
}


Implementation in C++:

Code 2: This is the general code in C++ with time complexity of O(n).


#include <iostream>

using namespace std;

int main()

{

  int numbersum = 0;

  cout << "Enter number here: ";

  cin >> number;

  for (int i = 1i < numberi++)

  {

    if (number % i == 0)

      sum = sum + i;
  }

  if (sum == number)

    cout << "The given number" << number << " is a Perfect Number";

  else

    cout << "The given number" << number << " is not a Perfect Number";

  return 0;
}



Implementation in JAVA:

    Code 3: This is the general code in JAVA with time complexity of O(n).



    import java.util.Scanner;
     
    public class PerfectNumber { // class scope starts
     
        public static void main(String[] args) { //main method starts
     
            Scanner in = new Scanner(System.in);
     
            System.out.println("Enter your number");
     
            int number = in.nextInt();
     
            in.close();
     
            int sum = 0;
     
            for (int i = 1; i < number; i++) { //starting for loop from 1 to number
     
                if (number % i == 0)
     
                    sum = sum + i;
     
            }
     
            if (sum == number)
     
                System.out.println("The given number " + number + " is a Perfect number"); //printing statement
     
            else
     
                System.out.println("The given number " + number + " is not a Perfect number"); //printing statement
     
        }
     
    }



    Start Practicing LeetCode with Programming ChaskaI have explained my approach to the problems in simple way. I have also shared the optimized code so that you can learn some new. 


    Optimized Code in C | C++ | JAVA:

    Code 4: This is optimized code to check perfect numbers. Here we are using mathematical property for factor checking. The factors of a number starting repeating when we go beyond its square root.

    Example:

    Factors of 6 : 

    6 = 1 * 6 

    6 = 2 * 3 

    ------------ (crossed square root of 6)

    6 = 3 * 2

    6 = 6 * 1

    We can see that 2*3 is same as 3*2, so we will not go beyond square root. 

    Time complexity - O(sqrt(n))


        // Logic in C | C++ | JAVA

        public bool checkPerfectNumber(int num) { // change return type bool to boolean for JAVA
            if(num == 1) return false;

            int sum = 1;
            for(int i = 2; i * i <= num; i++) {
                if(num % i == 0) {
                    sum += i + (num / i); // num/i to find other factor
                }
            }

            return sum == num; // if sum equals num return true else false
        }


    Start Practicing LeetCode with Programming ChaskaI have explained my approach to the problems in simple way. I have also shared the optimized code so that you can learn some new. 


        If you have any doubts/questions related to the Program, if you want to give any suggestions, or want an explanation of the codefeel free to comment below. I hope that my code is understandable, if not then you can tell me in the comments so that I will try to make it easier to understand.

        Post a Comment

        0 Comments