Program to check Plus Perfect Number

Given a ‘n’ digit number x, check if it is a plus perfect number or not. A number is plus perfect number if it is equal to the sum of its digits raised to the nth power.

Examples :

Input : x  = 371
Output : Yes
Explanation :  
Number of digits n = 3
(3*3*3) + (7*7*7) + (1*1*1) = 371

Input : x = 9474
Output : Yes
Explanation : 
Number of digits n = 4
(9*9*9*9) + (4*4*4*4) + (7*7*7*7) + 
(4*4*4*4) = 9474

Input : x = 9473
Output : No
Explanation : 
Number of digits n = 4
(9*9*9*9) + (4*4*4*4) + (7*7*7*7) +
(3*3*3*3) != 9474

Below is the implementation to check if a number is plus perfect number or not.

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// CPP implementation to check
// if the number is plus perfect 
// or not
#include <bits/stdc++.h>
using namespace std;
  
// function to check plus perfect number
bool checkplusperfect(int x)
{
    int temp = x;
      
    // calculating number of digits
    int n = 0;
    while (x != 0) {
        x /= 10;
        n++;
    }   
      
    // calculating plus perfect number
    x = temp;
    int sum = 0;
    while (x != 0) {
        sum += pow(x % 10, n);
        x /= 10;
    }
      
    // checking whether number 
    // is plus perfect or not
    return (sum == temp);
}
  
// driver program
int main()
{    
    int x = 9474;
    if (checkplusperfect(x))
        cout << "Yes";
    else
        cout << "No";        
    return 0;

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// java implementation to check
// if the number is plus perfect 
// or not
import java.io.*;
  
class GFG {
      
    // function to check plus perfect number
    static boolean checkplusperfect(int x)
    {
        int temp = x;
          
        // calculating number of digits
        int n = 0;
        while (x != 0
        {
            x /= 10;
            n++;
        
          
        // calculating plus perfect number
        x = temp;
        int sum = 0;
        while (x != 0
        {
            sum += Math.pow(x % 10, n);
            x /= 10;
        }
          
        // checking whether number 
        // is plus perfect or not
        return (sum == temp);
    }
      
    // Driver program
    public static void main (String[] args) 
    {
        int x = 9474;
        if (checkplusperfect(x))
            System.out.println ( "Yes");
        else
            System.out.println ( "No"); 
      
    }
}
  
// This code is contributed by vt_m

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python 3 implementation to check
# if the number is plus perfect 
# or not
import math
  
# function to check plus perfect number
def checkplusperfect(x) :
    temp = x
      
    # calculating number of digits
    n = 0
    while (x != 0) :
        x = x // 10
        n = n + 1
      
      
    # calculating plus perfect number
    x = temp
    sm = 0
    while (x != 0) :
        sm = sm + (int)(math.pow(x % 10, n))
        x = x // 10
      
      
    # checking whether number 
    # is plus perfect or not
    return (sm == temp)
  
  
# driver program
x = 9474
if (checkplusperfect(x)) :
    print("Yes")
else :
    print("No")
      
  
# This code is contributed by Nikita Tiwari.

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# implementation to check
// if the number is plus perfect 
// or not
using System;
  
class GFG {
      
    // function to check plus perfect number
    static bool checkplusperfect(int x)
    {
        int temp = x;
          
        // calculating number of digits
        int n = 0;
        while (x != 0) 
        {
            x /= 10;
            n++;
        
          
        // calculating plus perfect number
        x = temp;
        int sum = 0;
        while (x != 0) 
        {
            sum += (int)Math.Pow(x % 10, n);
            x /= 10;
        }
          
        // checking whether number 
        // is plus perfect or not
        return (sum == temp);
    }
      
    // Driver program
    public static void Main () 
    {
        int x = 9474;
        if (checkplusperfect(x))
            Console.WriteLine ( "Yes");
        else
            Console.WriteLine ( "No"); 
      
    }
}
  
// This code is contributed by vt_m

chevron_right


PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// PHP implementation to check
// if the number is plus perfect 
// or not
  
// function to check plus
// perfect number
function checkplusperfect($x)
{
    $temp = $x;
      
    // calculating number
    // of digits
    $n = 0;
    while ($x != 0)
    {
        $x /= 10;
        $n++;
    
      
    // calculating plus 
    // perfect number
    $x = $temp;
    $sum = 0;
    while ($x != 0)
    {
        $sum += pow($x % 10, $n);
        $x /= 10;
    }
      
    // checking whether number 
    // is plus perfect or not
    return ($sum == $temp);
}
  
    // Driver Code
    $x = 9474;
    if (checkplusperfect(!$x))
        echo "Yes";
    else
        echo "No"
      
// This code is contributed by ajit 
?>

chevron_right



Output :

Yes


My Personal Notes arrow_drop_up


If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



Improved By : jit_t



Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.