📌  相关文章
📜  检查八进制数字的十进制表示是否可被7整除

📅  最后修改于: 2021-05-04 10:04:01             🧑  作者: Mango

给定八进制数N。任务是编写一个程序来检查给定八进制数N的十进制表示形式是否可被7整除。
例子

Input: N = 112
Output: NO
Equivalent Decimal = 74
7410 = 7 * 10 1 + 4 * 100
1128 = 1 * 82 + 1 * 81 + 2 * 80

Input: N = 25
Output: YES
Decimal Equivalent = 21

这个想法是要注意的是,8%7将返回1。因此,当我们扩展八进制表示形式并将其取模7时,单个术语8的所有幂将减少为1。因此,如果八进制表示形式中所有数字的总和可以被7整除,那么相应的十进制数将被7整除。
下面是上述方法的实现:

C++
// CPP program to check if Decimal representation
// of an Octal number is divisible by 7 or not
 
#include 
using namespace std;
 
// Function to check Divisibility
int check(int n)
{
    int sum = 0;
 
    // Sum of all individual digits
    while (n != 0) {
        sum += n % 10;
        n = n / 10;
    }
 
    // Condition
    if (sum % 7 == 0)
        return 1;
    else
        return 0;
}
 
// Driver Code
int main()
{
    // Octal number
    int n = 25;
 
    (check(n) == 1) ? cout << "YES"
                    : cout << "NO";
 
    return 0;
}


Java
// Java program to check if Decimal
// representation of an Octal number
// is divisible by 7 or not
import java.util.*;
import java.lang.*;
import java.io.*;
 
class GFG
{
     
// Function to check Divisibility
static int check(int n)
{
    int sum = 0;
 
    // Sum of all individual digits
    while (n != 0)
    {
        sum += n % 10;
        n = n / 10;
    }
 
    // Condition
    if (sum % 7 == 0)
        return 1;
    else
        return 0;
}
 
// Driver Code
public static void main(String args[])
{
    // Octal number
    int n = 25;
 
    String s=(check(n) == 1) ?
                       "YES" : "NO";
    System.out.println(s);
}
}
 
// This code is contributed
// by Subhadeep


Python 3
# Python 3 program to check if
# Decimal representation of an
# Octal number is divisible by
# 7 or not
 
# Function to check Divisibility
def check(n):
 
    sum = 0
 
    # Sum of all individual digits
    while n != 0 :
        sum += n % 10
        n = n // 10
 
    # Condition
    if sum % 7 == 0 :
        return 1
    else:
        return 0
 
# Driver Code
if __name__ == "__main__":
    # Octal number
    n = 25
 
    print(("YES") if check(n) == 1
                  else print("NO"))
 
# This code is contributed
# by ChitraNayal


C#
// C# program to check if Decimal
// representation of an Octal
// number is divisible by 7 or not
using System;
 
class GFG
{
     
    // Function to check Divisibility
    static int check(int n)
    {
            int sum = 0;
     
        // Sum of all individual digits
        while (n != 0)
        {
            sum += n % 10;
            n = n / 10;
        }
     
    // Condition
    if (sum % 7 == 0)
        return 1;
    else
        return 0;
}
 
// Driver Code
public static void Main(String []args)
{
    // Octal number
    int n = 25;
     
    String s=(check(n) == 1) ?
                       "YES" : "NO";
    Console.WriteLine(s);
}
}
 
// This code is contributed
// by Kirti_Mangal


PHP


Javascript


输出:
YES