Determine whether the given integer N is a Peculiar Number or not
Last Updated :
01 Nov, 2023
Given an integer N, our task is the determine if the integer N is Peculiar Number. If it is then print “yes” otherwise output “no”.
The peculiar number is the number which is three times the sum of digits of the number.
Examples:
Input: N = 27
Output: Yes
Explanation:
Digit sum for 27 is 9 and 3 * 9 = 27 which is equal to N. Hence the output is yes.
Input: N = 36
Output: No
Explanation:
Digit sum for 36 is 9 and 3 * 9 = 27 which is not equal to N. Hence the output is no.
Approach:
To solve the problem mentioned above we have to first find the sum of the digits of a number N. Then check if the sum of digits of the number multiplied by 3 is actually the number N itself. If it is then print Yes otherwise the output is no.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int sumDig( int n)
{
int s = 0;
while (n != 0) {
s = s + (n % 10);
n = n / 10;
}
return s;
}
bool Pec( int n)
{
int dup = n;
int dig = sumDig(n);
if (dig * 3 == dup)
return true ;
else
return false ;
}
int main()
{
int n = 36;
if (Pec(n) == true )
cout << "Yes" << endl;
else
cout << "No" << endl;
return 0;
}
|
Java
import java.io.*;
class GFG{
static int sumDig( int n)
{
int s = 0 ;
while (n != 0 )
{
s = s + (n % 10 );
n = n / 10 ;
}
return s;
}
static boolean Pec( int n)
{
int dup = n;
int dig = sumDig(n);
if (dig * 3 == dup)
return true ;
else
return false ;
}
public static void main (String[] args)
{
int n = 36 ;
if (Pec(n) == true )
System.out.println( "Yes" );
else
System.out.println( "No" );
}
}
|
Python3
def sumDig(n):
s = 0
while (n ! = 0 ):
s = s + int (n % 10 )
n = int (n / 10 )
return s
def Pec(n):
dup = n
dig = sumDig(n)
if (dig * 3 = = dup):
return "Yes"
else :
return "No"
n = 36
if Pec(n) = = True :
print ( "Yes" )
else :
print ( "No" )
|
C#
using System;
class GFG{
static int sumDig( int n)
{
int s = 0;
while (n != 0)
{
s = s + (n % 10);
n = n / 10;
}
return s;
}
static bool Pec( int n)
{
int dup = n;
int dig = sumDig(n);
if (dig * 3 == dup)
return true ;
else
return false ;
}
public static void Main()
{
int n = 36;
if (Pec(n) == true )
Console.Write( "Yes" );
else
Console.Write( "No" );
}
}
|
Javascript
<script>
function sumDig(n)
{
var s = 0;
while (n != 0) {
s = s + (n % 10);
n = parseInt(n / 10);
}
return s;
}
function Pec(n)
{
var dup = n;
var dig = sumDig(n);
if (dig * 3 == dup)
return true ;
else
return false ;
}
var n = 36;
if (Pec(n) == true )
document.write( "Yes" );
else
document.write( "No" );
</script>
|
Time Complexity: O(log10n), time used to find the sum of digits of a number
Auxiliary Space: O(1), as no extra space is required
Share your thoughts in the comments
Please Login to comment...