Special two digit number
Last Updated :
17 Feb, 2023
A special two-digit number is a number such that when the sum of the digits of the number is added to the product of its digits, the result is equal to the original two-digit number.
Examples :
input : 59.
output : 59 is a Special Two-Digit Number
Explanation:
Sum of digits = 5 + 9 = 14
Product of its digits = 5 x 9 = 45
Sum of the sum of digits
and product of digits = 14 + 45 = 59
input: 29
output: 29 is a Special Two-digit Number
Explanation:
Sum of digits = 9 + 2 = 11
Product of digits = 9 * 2 = 18
Sum of the sum of digits
and product of digits = 11 + 18 = 29
Approach:
Extract the first and last digit of the number and add and multiply the digits separately. Then, add the sum and product of the digits of the two-digit number and compare it to the original number. If they are same, then it is a Special Two-Digit Number, else it is not.
Below is the implementation of above approach:
C++
#include<bits/stdc++.h>
using namespace std;
void specialNumber( int n)
{
if (n < 10 || n > 99)
cout << "Invalid Input! Number"
<< " should have 2 digits only" ;
else
{
int first = n / 10;
int last = n % 10;
int sum = first + last;
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" ;
}
}
}
int main()
{
int n = 59;
specialNumber(n);
return 0;
}
|
Java
import java.io.*;
class GFG
{
static void specialNumber( int n)
{
if (n < 10 || n > 99 )
System.out.println( "Invalid Input! " +
"Number should have " +
"2 digits only" );
else
{
int first = n / 10 ;
int last = n % 10 ;
int sum = first + last;
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" );
}
}
}
public static void main(String args[])
{
int n = 59 ;
specialNumber(n);
}
}
|
Python3
def specialNumber(n):
if (n < 10 or n > 99 ):
print ( "Invalid Input! Number" ,
" should have 2 digits only" )
else :
first = n / / 10
last = n % 10
sum = first + last
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" )
n = 59
specialNumber(n)
|
C#
using System;
class GFG
{
static void specialNumber( int n)
{
if (n < 10 || n > 99)
Console.WriteLine( "Invalid Input!" +
" Number should have" +
" 2 digits only" );
else
{
int first = n / 10;
int last = n % 10;
int sum = first + last;
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" );
}
}
}
public static void Main()
{
int n = 59;
specialNumber(n);
}
}
|
PHP
<?php
function specialNumber( $n )
{
if ( $n < 10 || $n > 99)
echo "Invalid Input!
Number should
have 2 digits only";
else
{
$first = $n / 10;
$last = $n % 10;
$sum = $first + $last ;
$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" ;
}
}
}
$n = 59;
specialNumber( $n );
?>
|
Javascript
<script>
function specialNumber(n) {
if (n < 10 || n > 99)
document.write(
"Invalid Input! Number" +
" should have 2 digits only"
);
else {
var first = parseInt(n / 10);
var last = parseInt(n % 10);
var sum = first + last;
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" )
);
}
}
}
var n = 59;
specialNumber(n);
</script>
|
Output
59 is a Special Two-Digit Number
Time Complexity: O(1), As we are performing constant time operations.
Auxiliary Space: O(1), As constant extra space is used.
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...