Check if a given number is perfect number or not

A perfect number is a number which will have all divisor sum are equal to given number.

perfect number

Here given code implementation process.

//C Program
//Check if a given number is perfect number or not
#include <stdio.h>

//Check given number is perfect or not
int is_perfect_no(int n)
{
  
  if(n<0)
  {
    return 0;
  }
  int sum = 0;

  for (int i = (n/2); i>=1 ; i--)
  {
    if(n % i == 0)
    {
      sum+=i;
    }
  }

  if(sum==n)
  {
    return 1;
  }
  else
  {
    return 0;
  }
  
}



void perfect_number(int n)
{
  
  if(is_perfect_no(n))
  {
    printf("Is Perfect Number : %d",n );
  }
  else
  {
    printf("Is not a Perfect Number : %d",n );
  }
  
 printf("\n");
}


int main()
{
  perfect_number(6);
  perfect_number(24);
  perfect_number(8128);
  perfect_number(33550336);
  return 0;
}

Output

Is Perfect Number : 6
Is not a Perfect Number : 24
Is Perfect Number : 8128
Is Perfect Number : 33550336
/*
  C++ Program
  Check if a given number is perfect number or not
*/
#include <iostream>

using namespace std;
class Number {
  public:
    bool is_perfect_no(int n) {
      if (n < 0) {
        return false;
      }
      int sum = 0;
      for (int i = (n / 2); i >= 1; i--) {
        if (n % i == 0) {
          sum += i;
        }
      }
      if (sum == n) {
        return true;
      } else {
        return false;
      }
    }
  void perfect_number(int n) {
    if (this->is_perfect_no(n)) {
      cout << "Is Perfect Number : " << n << endl;
    } else {
      cout << "Is not a Perfect Number : " << n << endl;
    }
  }
};

int main() {
  Number obj;
  obj.perfect_number(6);
  obj.perfect_number(24);
  obj.perfect_number(8128);
  obj.perfect_number(33550336);
}

Output

Is Perfect Number : 6
Is not a Perfect Number : 24
Is Perfect Number : 8128
Is Perfect Number : 33550336
/*
  Java Program
  Check if a given number is perfect number or not
*/
public class Number {

  //Check given number is perfect or not
  public boolean is_perfect_no(int n) {

    if (n < 0) {
      return false;
    }
    int sum = 0;

    for (int i = (n / 2); i >= 1; i--) {
      if (n % i == 0) {
        sum += i;
      }
    }

    if (sum == n) {
      return true;
    } else {
      return false;
    }

  }
  void perfect_number(int n) {

    if (is_perfect_no(n)) {
      System.out.println("Is Perfect Number : " + n);
    } else {
      System.out.println("Is not a Perfect Number : " + n);
    }

  }


  public static void main(String[] args) {

    Number obj = new Number();
    obj.perfect_number(6);
    obj.perfect_number(24);
    obj.perfect_number(8128);
    obj.perfect_number(33550336);
   
  }
}

Output

Is Perfect Number : 6
Is not a Perfect Number : 24
Is Perfect Number : 8128
Is Perfect Number : 33550336
#Python3
#Check if a given number is perfect number or not
class Number :
  def is_perfect_no(self, n) :
    if (n < 0) :
      return False
    
    sumValue = 0
    i = int(n / 2)
    while ( i >= 1 ) :
      if (n % i == 0) :
        sumValue += i

      i -= 1
      
    
    if (sumValue == n) :
      return True
    else :
      return False
    
  
  def perfect_number(self, n) :
    if (self.is_perfect_no(n)) :
      print("Is Perfect Number : ", n)
    else :
      print("Is not a Perfect Number : ", n)
    
  
def main() :
  obj = Number()
  obj.perfect_number(6)
  obj.perfect_number(24)
  obj.perfect_number(8128)
  obj.perfect_number(33550336)
  

if __name__ == "__main__":
  main()

Output

Is Perfect Number :  6
Is not a Perfect Number :  24
Is Perfect Number :  8128
Is Perfect Number :  33550336
/*
  C# Program
  Check if a given number is perfect number or not
*/
using System;
public class Number {

  //Check given number is perfect or not
  public Boolean is_perfect_no(int n) {

    if (n < 0) {
      return false;
    }
    int sum = 0;

    for (int i = (n / 2); i >= 1; i--) {
      if (n % i == 0) {
        sum += i;
      }
    }

    if (sum == n) {
      return true;
    } else {
      return false;
    }

  }
  void perfect_number(int n) {

    if (is_perfect_no(n)) {
      Console.WriteLine("Is Perfect Number : " + n);
    } else {
      Console.WriteLine("Is not a Perfect Number : " + n);
    }

  }


  public static void Main(String[] args) {

    Number obj = new Number();
    obj.perfect_number(6);
    obj.perfect_number(24);
    obj.perfect_number(8128);
    obj.perfect_number(33550336);

  }
}

Output

Is Perfect Number : 6
Is not a Perfect Number : 24
Is Perfect Number : 8128
Is Perfect Number : 33550336
<?php
/*
  Php Program
  find Next Sparse Number
*/
class Number {
  public function is_perfect_no($n) {
    if ($n < 0) {
      return false;
    }
    $sum = 0;
    for ($i = intval($n / 2); $i >= 1; $i--) {
      if ($n % $i == 0) {
        $sum += $i;
      }
    }
    if ($sum == $n) {
      return true;
    } else {
      return false;
    }
  }

  function perfect_number($n) {
    if ($this->is_perfect_no($n)) {
      echo ("Is Perfect Number : ". $n."\n");
    } else {
      echo ("Is not a Perfect Number : " . $n."\n");
    }
  }
  
  
}
function main() {
  $obj = new Number();
  $obj->perfect_number(6);
  $obj->perfect_number(24);
  $obj->perfect_number(8128);
  $obj->perfect_number(33550336);
}
main();

Output

Is Perfect Number : 6
Is not a Perfect Number : 24
Is Perfect Number : 8128
Is Perfect Number : 33550336

Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.

New Comment







© 2021, kalkicode.com, All rights reserved