Gegeben sei eine Oktalzahl N. Die Aufgabe besteht darin, ein Programm zu schreiben, das überprüft, ob die Dezimaldarstellung der gegebenen Oktalzahl N durch 7 teilbar ist oder nicht. 
Beispiele
 

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

Die Idee ist, dass 8 % 7 1 zurückgibt. Wenn wir also die Oktaldarstellung erweitern und ihr Modulo 7 nehmen, werden alle Potenzen von 8 einzeln auf 1 reduziert. Also, wenn die Summe aller Ziffern in Oktaldarstellung durch 7 teilbar ist, dann ist die entsprechende Dezimalzahl durch 7 teilbar.
Unten ist die Implementierung des obigen Ansatzes: 
 

C++

// CPP program to check if Decimal representation
// of an Octal number is divisible by 7 or not
 
#include <bits/stdc++.h>
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

<?php
// PHP program to check if
// Decimal representation of
// an Octal number is divisible
// by 7 or not
 
// Function to check Divisibility
function check($n)
{
    $sum = 0;
 
    // Sum of all individual digits
    while ($n != 0)
    {
        $sum += $n % 10;
        $n = (int)($n / 10);
    }
 
    // Condition
    if ($sum % 7 == 0)
        return 1;
    else
        return 0;
}
 
// Driver Code
 
// Octal number
$n = 25;
 
(check($n) == 1) ?
  print("YES\n") :
   print("NO\n");
     
// This Code is contributed
// by mits
?>

Javascript

<script>
 
// Javascript program to check if Decimal representation
// of an Octal number is divisible by 7 or not
 
// Function to check Divisibility
function check(n)
{
    let sum = 0;
 
    // Sum of all individual digits
    while (n != 0) {
        sum += n % 10;
        n = Math.floor(n / 10);
    }
 
    // Condition
    if (sum % 7 == 0)
        return 1;
    else
        return 0;
}
 
// Driver Code
 
    // Octal number
    let n = 25;
 
    (check(n) == 1) ? document.write("YES")
                    : document.write("NO");
 
// This code is contributed by Mayank Tyagi
 
</script>
Ausgabe: 
JA