Sunday, November 17, 2024
Google search engine
HomeData Modelling & AIDetermine whether the given integer N is a Peculiar Number or not

Determine whether the given integer N is a Peculiar Number or not

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++




// C++ implementation to check if the
// number is peculiar
 
#include <bits/stdc++.h>
using namespace std;
 
// Function to find sum of digits
// of a number
int sumDig(int n)
{
    int s = 0;
 
    while (n != 0) {
        s = s + (n % 10);
 
        n = n / 10;
    }
 
    return s;
}
 
// Function to check if the
// number is peculiar
bool Pec(int n)
{
    // Store a duplicate of n
    int dup = n;
 
    int dig = sumDig(n);
 
    if (dig * 3 == dup)
        return true;
 
    else
        return false;
}
 
// Driver code
int main()
{
    int n = 36;
 
    if (Pec(n) == true)
        cout << "Yes" << endl;
 
    else
        cout << "No" << endl;
 
    return 0;
}


Java




// Java implementation to check if the
// number is peculiar
import java.io.*;
 
class GFG{
 
// Function to find sum of digits
// of a number
static int sumDig(int n)
{
    int s = 0;
 
    while (n != 0)
    {
        s = s + (n % 10);
        n = n / 10;
    }
    return s;
}
 
// Function to check if number is peculiar
static boolean Pec(int n)
{
     
    // Store a duplicate of n
    int dup = n;
    int dig = sumDig(n);
 
    if (dig * 3 == dup)
        return true;
    else
        return false;
}
 
// Driver code
public static void main (String[] args)
{
    int n = 36;
 
    if (Pec(n) == true)
        System.out.println("Yes");
    else
        System.out.println("No");
}
}
 
// This code is contributed by shubhamsingh10


Python3




# Python3 implementation to check if the
# number is peculiar
 
# Function to get sum of digits
# of a number
def sumDig(n):
     
    s = 0
    while (n != 0):
        s = s + int(n % 10)
        n = int(n / 10)
     
    return s
     
# Function to check if the 
# number is peculiar    
def Pec(n):
     
    dup = n
    dig = sumDig(n)
 
    if(dig * 3 == dup):
        return "Yes"
    else :
        return "No"
         
# Driver code
n = 36
 
if Pec(n) == True:
    print("Yes")
else:
    print("No")
 
# This code is contributed by grand_master


C#




// C# implementation to check if the
// number is peculiar
using System;
 
class GFG{
 
// Function to find sum of digits
// of a number
static int sumDig(int n)
{
    int s = 0;
 
    while (n != 0)
    {
        s = s + (n % 10);
        n = n / 10;
    }
    return s;
}
 
// Function to check if the number is peculiar
static bool Pec(int n)
{
     
    // Store a duplicate of n
    int dup = n;
    int dig = sumDig(n);
 
    if (dig * 3 == dup)
        return true;
    else
        return false;
}
 
// Driver code
public static void Main()
{
    int n = 36;
 
    if (Pec(n) == true)
        Console.Write("Yes");
    else
        Console.Write("No");
}
}
 
// This code is contributed by Akanksha_Rai


Javascript




<script>
 
// Javascript implementation to check if the
// number is peculiar
 
// Function to find sum of digits
// of a number
function sumDig(n)
{
    var s = 0;
 
    while (n != 0) {
        s = s + (n % 10);
 
        n = parseInt(n / 10);
    }
 
    return s;
}
 
// Function to check if the
// number is peculiar
function Pec(n)
{
    // Store a duplicate of n
    var dup = n;
 
    var dig = sumDig(n);
 
    if (dig * 3 == dup)
        return true;
 
    else
        return false;
}
 
// Driver code
var n = 36;
if (Pec(n) == true)
    document.write( "Yes");
else
    document.write( "No" );
 
// This code is contributed by noob2000.
</script>


Output

No

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

Feeling lost in the world of random DSA topics, wasting time without progress? It’s time for a change! Join our DSA course, where we’ll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!

RELATED ARTICLES

Most Popular

Recent Comments