给定一个整数N,我们的任务是确定整数N是否为奇数。如果是,则打印“是”,否则输出“否”。
特殊数字是数字的数字总和的三倍。
例子:
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.
方法:
为了解决上述问题,我们必须首先找到数字N的位数之和。然后检查该数字的位数之和乘以3是否实际上是数字N本身。如果是,则打印“是”,否则输出为“否”。
下面是上述方法的实现:
C++
// C++ implementation to check if the
// number is peculiar
#include
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
输出:
No