一个特殊的两位数是这样一个数字,当该数字的位数之和加上其位数的乘积时,结果等于原来的两位数。 例如:
null
input : 59.output : 59 is a Special Two-Digit NumberExplanation:Sum of digits = 5 + 9 = 14Product of its digits = 5 x 9 = 45Sum of the sum of digits and product of digits = 14 + 45 = 59input: 29output: 29 is a Special Two-digit NumberExplanation:Sum of digits = 9 + 2 = 11Product of digits = 9 * 2 = 18Sum of the sum of digits and product of digits = 11 + 18 = 29
方法: 提取数字的第一个和最后一个数字,分别相加和相乘。然后,将两位数的和和与乘积相加,并将其与原始数字进行比较。如果它们是相同的,那么它是一个特殊的两位数,否则就不是。 以下是上述方法的实施情况:
C++
// CPP program to find if number is // a Special Two-Digit number or not #include<bits/stdc++.h> using namespace std; // function to find if number // is special or not void specialNumber( int n) { // Checking whether entered // number is 2 digit or not if (n < 10 || n > 99) cout << "Invalid Input! Number" << " should have 2 digits only" ; else { // Finding the first digit int first = n / 10; // Finding the last digit int last = n % 10; // Finding the sum of // the digits int sum = first + last; // Finding the product // of the digits int pro = first * last; if ((sum + pro) == n) { cout << n << " is a Special " << "Two-Digit Number" ; } else { cout << n << " is Not a " << "Special Two-Digit Number" ; } } } // Driver Code int main() { int n = 59; // function calling specialNumber(n); return 0; } |
JAVA
// Java program to find if number is // a Special Two-Digit number or not import java.io.*; class GFG { // function to find if number // is special or not static void specialNumber( int n) { // Checking whether entered // number is 2 digit or not if (n < 10 || n > 99 ) System.out.println( "Invalid Input! " + "Number should have " + "2 digits only" ); else { // Finding the first digit int first = n / 10 ; // Finding the last digit int last = n % 10 ; // Finding the sum // of the digits int sum = first + last; // Finding the product // of the digits int pro = first * last; if ((sum + pro) == n) { System.out.println(n + " is a Special" + " Two-Digit Number" ); } else { System.out.println(n + " is Not a Special" + " Two-Digit Number" ); } } } // Driver Code public static void main(String args[]) { int n = 59 ; specialNumber(n); } } |
Python3
# Python3 code to find if # number is a Special # Two-Digit number or not # Function to find if number # is special or not def specialNumber(n): # Checking whether entered # number is 2 digit or not if (n < 10 or n > 99 ): print ( "Invalid Input! Number" , " should have 2 digits only" ) else : # Finding the first digit first = n / / 10 # Finding the last digit last = n % 10 # Finding the sum # of the digits sum = first + last # Finding the product # of the digits pro = first * last if (( sum + pro) = = n): print (n , " is a Special " , "Two-Digit Number" ) else : print (n , " is Not a " , "Special Two-Digit Number" ) # Driver code n = 59 specialNumber(n) # This code is contributed # by Anant Agarwal. |
C#
// C# program to find if number is // a Special Two-Digit number or not using System; class GFG { // function to find if number // is special or not static void specialNumber( int n) { // Checking whether entered // number is 2 digit or not if (n < 10 || n > 99) Console.WriteLine( "Invalid Input!" + " Number should have" + " 2 digits only" ); else { // Finding the first digit int first = n / 10; // Finding the last digit int last = n % 10; // Finding the sum // of the digits int sum = first + last; // Finding the product // of the digits int pro = first * last; if ((sum + pro) == n) { Console.WriteLine(n + " is a Special" + " Two-Digit Number" ); } else { Console.WriteLine(n + " is Not a Special" + " Two-Digit Number" ); } } } // Driver Code public static void Main() { int n = 59; specialNumber(n); } } // This code is contributed by vt_m. |
PHP
<?php // PHP program to find if number is // a Special Two-Digit number or not // function to find if number // is special or not function specialNumber( $n ) { // Checking whether entered // number is 2 digit or not if ( $n < 10 || $n > 99) echo "Invalid Input! Number should have 2 digits only"; else { // Finding the first digit $first = $n / 10; // Finding the last digit $last = $n % 10; // Finding the sum // of the digits $sum = $first + $last ; // Finding the product // of the digits $pro = $first * $last ; if (( $sum + $pro ) != $n ) { echo $n , " is a Special " . "Two-Digit Number" ; } else { echo $n , " is Not a Special" . " Two-Digit Number" ; } } } // Driver Code $n = 59; // function calling specialNumber( $n ); // This code is contributed by ajit. ?> |
Javascript
<script> // JavaScript program to find if number is // a Special Two-Digit number or not // function to find if number // is special or not function specialNumber(n) { // Checking whether entered // number is 2 digit or not if (n < 10 || n > 99) document.write( "Invalid Input! Number" + " should have 2 digits only" ); else { // Finding the first digit var first = parseInt(n / 10); // Finding the last digit var last = parseInt(n % 10); // Finding the sum of // the digits var sum = first + last; // Finding the product // of the digits var pro = first * last; if (sum + pro === n) { document.write( n + ( " is a Special " + "Two-Digit Number" ) ); } else { document.write( n + ( " is Not a " + "Special Two-Digit Number" ) ); } } } // Driver Code var n = 59; // function calling specialNumber(n); </script> |
输出:
59 is a Special Two-Digit Number
© 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END