# Check if Decimal representation of an Octal number is divisible by 7

Given an Octal number N. The task is to write a program to check if the Decimal representation of the given octal number N is divisible by 7 or not.
Examples

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

The idea is to note that, 8 % 7 will return 1. Thus, when we expand octal representation and take its modulo 7 all powers of 8 in individual terms will reduce to 1. So, if the sum of all the digits in octal representation is divisible by 7 then, the corresponding decimal number will be divisible by 7.
Below is the implementation of the above approach:

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

 ``

Output:

`YES`

Time Complexity: O(log10n)

Auxiliary Space: O(1)

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 geeks!

Previous
Next