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 :
Attention reader! Don’t stop learning now. Get hold of all the important mathematical concepts for competitive programming with the Essential Maths for CP Course at a student-friendly price. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.
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++
// CPP implementation to check// if the number is plus perfect// or not#include <bits/stdc++.h>using namespace std;// function to check plus perfect numberbool 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 programint main(){ int x = 9474; if (checkplusperfect(x)) cout << "Yes"; else cout << "No"; return 0;} |
Java
// java implementation to check// if the number is plus perfect// or notimport 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 |
Python3
# Python 3 implementation to check# if the number is plus perfect# or notimport math# function to check plus perfect numberdef 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 programx = 9474if (checkplusperfect(x)) : print("Yes")else : print("No") # This code is contributed by Nikita Tiwari. |
C#
// C# implementation to check// if the number is plus perfect// or notusing 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 |
PHP
<?php// PHP implementation to check// if the number is plus perfect// or not// function to check plus// perfect numberfunction 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?> |
Javascript
<script>// Javascript implementation to check// if the number is plus perfect// or not// Function to check plus perfect numberfunction checkplusperfect(x){ let temp = x; // Calculating number of digits let n = 0; while (x != 0) { x = parseInt(x / 10); n++; } // Calculating plus perfect number x = temp; let sum = 0; while (x != 0) { sum += Math.pow(x % 10, n); x = parseInt(x/10); } // Checking whether number // is plus perfect or not return (sum == temp);}// Driver Codelet x = 9474;if (checkplusperfect(x)) document.write("Yes");else document.write("No"); // This code is contributed by souravmahato348</script> |
Output :
Yes

