Check if large number is divisible by 6
In this article, we will discuss a problem: how to check if a large number is divisible by 6. We will provide an explanation of the problem, a suitable example, the algorithm and pseudocode with an explanation, and the resultant output explanation. The time complexity of the code will also be discussed. Let's get started!
Introduction
The problem is to determine whether a given large number is divisible by 6 or not. We will use a C program to solve this problem. The program takes a string representation of a number as input and performs the necessary calculations to check its divisibility by 6.
Problem Statement
Given a large number, we need to determine if it is divisible by 6. If it is divisible by 6, we print a message indicating that the number is divisible by 6. Otherwise, we print a message indicating that the number is not divisible by 6.
Example
Let's consider a few examples to better understand the problem. We have the following test inputs:
- Number: 160
- Number: 93782639377588932984
- Number: 33612315332343
- Number: 3824566134312
The program will process each input number and provide an output indicating whether it is divisible by 6 or not.
Algorithm and Pseudocode
Here is the algorithm for checking if a large number is divisible by 6:
- Calculate the length of the number string.
- If the length is 0, exit the function.
- Check if the last digit of the number is even (divisible by 2).
- If it is odd, the number is not divisible by 6.
- If it is even, calculate the sum of all digits in the number.
- Check if the sum is divisible by 3.
- If it is divisible by 3, the number is divisible by 6.
- Otherwise, the number is not divisible by 6.
Here is the pseudocode representing the algorithm:
function divisibleBy6(num)
n = length(num)
if n = 0
return
if last_digit(num) % 2 != 0
print "Number " + num + " is not divisible by 6"
else
sum = 0
for i = 0 to n-1
sum = sum + digit(num, i)
if sum % 3 = 0
print "Number " + num + " is divisible by 6"
else
print "Number " + num + " is not divisible by 6"
Code Solution
Here given code implementation process.
/*
C program for
Check if large number is divisible by 6
*/
#include <stdio.h>
#include <string.h>
void divisibleBy6(const char *num)
{
// Get the length of num
int n = strlen(num);
if (n == 0)
{
return;
}
if (((num[n - 1] - '0') % 2) != 0)
{
// When number is not divisible by 2
printf("\n Number %s is not divisible by 6", num);
}
else
{
int sum = 0;
// Execute loop through by size n
for (int i = 0; i < n; ++i)
{
// Sum of digits in given num
sum = sum + (num[i] - '0');
}
if ((sum) % 3 == 0)
{
// When sum is not divisible by 3
printf("\n Number %s divisible by 6", num);
}
else
{
printf("\n Number %s is not divisible by 6", num);
}
}
}
int main(int argc, char const *argv[])
{
// Test Inputs
divisibleBy6("160");
divisibleBy6("93782639377588932984");
divisibleBy6("33612315332343");
divisibleBy6("3824566134312");
return 0;
}
Output
Number 160 is not divisible by 6
Number 93782639377588932984 divisible by 6
Number 33612315332343 is not divisible by 6
Number 3824566134312 divisible by 6
/*
Java program for
Check if large number is divisible by 6
*/
class Divisibility
{
public void divisibleBy6(String num)
{
// Get the length of num
int n = num.length();
if (n == 0)
{
return;
}
if (((num.charAt(n - 1) - '0') % 2) != 0)
{
// When number is not divisible by 2
System.out.print("\n Number " + num + " is not divisible by 6");
}
else
{
int sum = 0;
// Execute loop through by size n
for (int i = 0; i < n; ++i)
{
// Sum of digits in given num
sum = sum + (num.charAt(i) - '0');
}
if ((sum % 3) == 0)
{
// When sum is not divisible by 3
System.out.print("\n Number " + num + " divisible by 6");
}
else
{
System.out.print("\n Number " + num + " is not divisible by 6");
}
}
}
public static void main(String[] args)
{
Divisibility task = new Divisibility();
// Test Inputs
task.divisibleBy6("160");
task.divisibleBy6("93782639377588932984");
task.divisibleBy6("33612315332343");
task.divisibleBy6("3824566134312");
}
}
Output
Number 160 is not divisible by 6
Number 93782639377588932984 divisible by 6
Number 33612315332343 is not divisible by 6
Number 3824566134312 divisible by 6
// Include header file
#include <iostream>
#include <string>
using namespace std;
/*
C++ program for
Check if large number is divisible by 6
*/
class Divisibility
{
public: void divisibleBy6(string num)
{
// Get the length of num
int n = num.length();
if (n == 0)
{
return;
}
if (((num[n - 1] - '0') % 2) != 0)
{
// When number is not divisible by 2
cout << "\n Number " << num << " is not divisible by 6";
}
else
{
int sum = 0;
// Execute loop through by size n
for (int i = 0; i < n; ++i)
{
// Sum of digits in given num
sum = sum + (num[i] - '0');
}
if ((sum % 3) == 0)
{
// When sum is not divisible by 3
cout << "\n Number " << num << " divisible by 6";
}
else
{
cout << "\n Number " << num << " is not divisible by 6";
}
}
}
};
int main()
{
Divisibility *task = new Divisibility();
// Test Inputs
task->divisibleBy6("160");
task->divisibleBy6("93782639377588932984");
task->divisibleBy6("33612315332343");
task->divisibleBy6("3824566134312");
return 0;
}
Output
Number 160 is not divisible by 6
Number 93782639377588932984 divisible by 6
Number 33612315332343 is not divisible by 6
Number 3824566134312 divisible by 6
// Include namespace system
using System;
/*
Csharp program for
Check if large number is divisible by 6
*/
public class Divisibility
{
public void divisibleBy6(String num)
{
// Get the length of num
int n = num.Length;
if (n == 0)
{
return;
}
if (((num[n - 1] - '0') % 2) != 0)
{
// When number is not divisible by 2
Console.Write("\n Number " + num + " is not divisible by 6");
}
else
{
int sum = 0;
// Execute loop through by size n
for (int i = 0; i < n; ++i)
{
// Sum of digits in given num
sum = sum + (num[i] - '0');
}
if ((sum % 3) == 0)
{
// When sum is not divisible by 3
Console.Write("\n Number " + num + " divisible by 6");
}
else
{
Console.Write("\n Number " + num + " is not divisible by 6");
}
}
}
public static void Main(String[] args)
{
Divisibility task = new Divisibility();
// Test Inputs
task.divisibleBy6("160");
task.divisibleBy6("93782639377588932984");
task.divisibleBy6("33612315332343");
task.divisibleBy6("3824566134312");
}
}
Output
Number 160 is not divisible by 6
Number 93782639377588932984 divisible by 6
Number 33612315332343 is not divisible by 6
Number 3824566134312 divisible by 6
package main
import "fmt"
/*
Go program for
Check if large number is divisible by 6
*/
func divisibleBy6(num string) {
// Get the length of num
var n int = len(num)
if n == 0 {
return
}
if ((num[n - 1] - '0') % 2) != 0 {
// When number is not divisible by 2
fmt.Print("\n Number ", num, " is not divisible by 6")
} else {
var sum int = 0
// Execute loop through by size n
for i := 0 ; i < n ; i++ {
// Sum of digits in given num
sum = sum + int(num[i] - '0')
}
if (sum % 3) == 0 {
// When sum is not divisible by 3
fmt.Print("\n Number ", num, " divisible by 6")
} else {
fmt.Print("\n Number ", num, " is not divisible by 6")
}
}
}
func main() {
// Test Inputs
divisibleBy6("160")
divisibleBy6("93782639377588932984")
divisibleBy6("33612315332343")
divisibleBy6("3824566134312")
}
Output
Number 160 is not divisible by 6
Number 93782639377588932984 divisible by 6
Number 33612315332343 is not divisible by 6
Number 3824566134312 divisible by 6
<?php
/*
Php program for
Check if large number is divisible by 6
*/
class Divisibility
{
public function divisibleBy6($num)
{
// Get the length of num
$n = strlen($num);
if ($n == 0)
{
return;
}
if (((ord($num[$n - 1]) - ord('0')) % 2) != 0)
{
// When number is not divisible by 2
echo("\n Number ".$num.
" is not divisible by 6");
}
else
{
$sum = 0;
// Execute loop through by size n
for ($i = 0; $i < $n; ++$i)
{
// Sum of digits in given num
$sum = $sum + (ord($num[$i]) - ord('0'));
}
if (($sum % 3) == 0)
{
// When sum is not divisible by 3
echo("\n Number ".$num.
" divisible by 6");
}
else
{
echo("\n Number ".$num.
" is not divisible by 6");
}
}
}
}
function main()
{
$task = new Divisibility();
// Test Inputs
$task->divisibleBy6("160");
$task->divisibleBy6("93782639377588932984");
$task->divisibleBy6("33612315332343");
$task->divisibleBy6("3824566134312");
}
main();
Output
Number 160 is not divisible by 6
Number 93782639377588932984 divisible by 6
Number 33612315332343 is not divisible by 6
Number 3824566134312 divisible by 6
/*
Node JS program for
Check if large number is divisible by 6
*/
class Divisibility
{
divisibleBy6(num)
{
// Get the length of num
var n = num.length;
if (n == 0)
{
return;
}
if (((num.charCodeAt(n - 1) - '0'.charCodeAt(0)) % 2) != 0)
{
// When number is not divisible by 2
process.stdout.write("\n Number " + num + " is not divisible by 6");
}
else
{
var sum = 0;
// Execute loop through by size n
for (var i = 0; i < n; ++i)
{
// Sum of digits in given num
sum = sum + (num.charCodeAt(i) - '0'.charCodeAt(0));
}
if ((sum % 3) == 0)
{
// When sum is not divisible by 3
process.stdout.write("\n Number " + num + " divisible by 6");
}
else
{
process.stdout.write("\n Number " + num + " is not divisible by 6");
}
}
}
}
function main()
{
var task = new Divisibility();
// Test Inputs
task.divisibleBy6("160");
task.divisibleBy6("93782639377588932984");
task.divisibleBy6("33612315332343");
task.divisibleBy6("3824566134312");
}
main();
Output
Number 160 is not divisible by 6
Number 93782639377588932984 divisible by 6
Number 33612315332343 is not divisible by 6
Number 3824566134312 divisible by 6
# Python 3 program for
# Check if large number is divisible by 6
class Divisibility :
def divisibleBy6(self, num) :
# Get the length of num
n = len(num)
if (n == 0) :
return
if (((ord(num[n - 1]) - ord('0')) % 2) != 0) :
# When number is not divisible by 2
print("\n Number ", num ," is not divisible by 6", end = "")
else :
sum = 0
i = 0
# Execute loop through by size n
while (i < n) :
# Sum of digits in given num
sum = sum + (ord(num[i]) - ord('0'))
i += 1
if ((sum % 3) == 0) :
# When sum is not divisible by 3
print("\n Number", num ,"divisible by 6", end = "")
else :
print("\n Number", num ,"is not divisible by 6", end = "")
def main() :
task = Divisibility()
# Test Inputs
task.divisibleBy6("160")
task.divisibleBy6("93782639377588932984")
task.divisibleBy6("33612315332343")
task.divisibleBy6("3824566134312")
if __name__ == "__main__": main()
Output
Number 160 is not divisible by 6
Number 93782639377588932984 divisible by 6
Number 33612315332343 is not divisible by 6
Number 3824566134312 divisible by 6
# Ruby program for
# Check if large number is divisible by 6
class Divisibility
def divisibleBy6(num)
# Get the length of num
n = num.length
if (n == 0)
return
end
if (((num[n - 1].ord - '0'.ord) % 2) != 0)
# When number is not divisible by 2
print("\n Number ", num ," is not divisible by 6")
else
sum = 0
i = 0
# Execute loop through by size n
while (i < n)
# Sum of digits in given num
sum = sum + (num[i].ord - '0'.ord)
i += 1
end
if ((sum % 3) == 0)
# When sum is not divisible by 3
print("\n Number ", num ," divisible by 6")
else
print("\n Number ", num ," is not divisible by 6")
end
end
end
end
def main()
task = Divisibility.new()
# Test Inputs
task.divisibleBy6("160")
task.divisibleBy6("93782639377588932984")
task.divisibleBy6("33612315332343")
task.divisibleBy6("3824566134312")
end
main()
Output
Number 160 is not divisible by 6
Number 93782639377588932984 divisible by 6
Number 33612315332343 is not divisible by 6
Number 3824566134312 divisible by 6
import scala.collection.mutable._;
/*
Scala program for
Check if large number is divisible by 6
*/
class Divisibility()
{
def divisibleBy6(num: String): Unit = {
// Get the length of num
var n: Int = num.length();
if (n == 0)
{
return;
}
if (((num.charAt(n - 1).toInt - '0'.toInt) % 2) != 0)
{
// When number is not divisible by 2
print("\n Number " + num + " is not divisible by 6");
}
else
{
var sum: Int = 0;
var i: Int = 0;
// Execute loop through by size n
while (i < n)
{
// Sum of digits in given num
sum = sum + (num.charAt(i).toInt - '0'.toInt);
i += 1;
}
if ((sum % 3) == 0)
{
// When sum is not divisible by 3
print("\n Number " + num + " divisible by 6");
}
else
{
print("\n Number " + num + " is not divisible by 6");
}
}
}
}
object Main
{
def main(args: Array[String]): Unit = {
var task: Divisibility = new Divisibility();
// Test Inputs
task.divisibleBy6("160");
task.divisibleBy6("93782639377588932984");
task.divisibleBy6("33612315332343");
task.divisibleBy6("3824566134312");
}
}
Output
Number 160 is not divisible by 6
Number 93782639377588932984 divisible by 6
Number 33612315332343 is not divisible by 6
Number 3824566134312 divisible by 6
import Foundation;
/*
Swift 4 program for
Check if large number is divisible by 6
*/
class Divisibility
{
func divisibleBy6(_ data: String)
{
let num = Array(data);
// Get the length of num
let n: Int = num.count;
if (n == 0)
{
return;
}
if (((Int(UnicodeScalar(String(num[n - 1]))!.value) -
Int(UnicodeScalar(String("0"))!.value)) % 2) != 0)
{
// When number is not divisible by 2
print("\n Number", data ,"is not divisible by 6", terminator: "");
}
else
{
var sum: Int = 0;
var i: Int = 0;
// Execute loop through by size n
while (i < n)
{
// Sum of digits in given num
sum = sum + (Int(UnicodeScalar(String(num[i]))!.value) -
Int(UnicodeScalar(String("0"))!.value));
i += 1;
}
if ((sum % 3) == 0)
{
// When sum is not divisible by 3
print("\n Number", data ,"divisible by 6",
terminator: "");
}
else
{
print("\n Number", data ,"is not divisible by 6",
terminator: "");
}
}
}
}
func main()
{
let task: Divisibility = Divisibility();
// Test Inputs
task.divisibleBy6("160");
task.divisibleBy6("93782639377588932984");
task.divisibleBy6("33612315332343");
task.divisibleBy6("3824566134312");
}
main();
Output
Number 160 is not divisible by 6
Number 93782639377588932984 divisible by 6
Number 33612315332343 is not divisible by 6
Number 3824566134312 divisible by 6
/*
Kotlin program for
Check if large number is divisible by 6
*/
class Divisibility
{
fun divisibleBy6(num: String): Unit
{
// Get the length of num
val n: Int = num.length;
if (n == 0)
{
return;
}
if (((num.get(n - 1).toInt() - '0'.toInt()) % 2) != 0)
{
// When number is not divisible by 2
print("\n Number " + num + " is not divisible by 6");
}
else
{
var sum: Int = 0;
var i: Int = 0;
// Execute loop through by size n
while (i < n)
{
// Sum of digits in given num
sum = sum + (num.get(i).toInt() - '0'.toInt());
i += 1;
}
if ((sum % 3) == 0)
{
// When sum is not divisible by 3
print("\n Number " + num + " divisible by 6");
}
else
{
print("\n Number " + num + " is not divisible by 6");
}
}
}
}
fun main(args: Array < String > ): Unit
{
val task: Divisibility = Divisibility();
// Test Inputs
task.divisibleBy6("160");
task.divisibleBy6("93782639377588932984");
task.divisibleBy6("33612315332343");
task.divisibleBy6("3824566134312");
}
Output
Number 160 is not divisible by 6
Number 93782639377588932984 divisible by 6
Number 33612315332343 is not divisible by 6
Number 3824566134312 divisible by 6
Resultant Output Explanation
After executing the program with the given test inputs, we obtain the following output:
- Number 160 is not divisible by 6
- Number 93782639377588932984 is divisible by 6
- Number 33612315332343 is not divisible by 6
- Number 3824566134312 is divisible by 6
The output indicates whether each input number is divisible by 6 or not.
Time Complexity
The time complexity of the given algorithm is O(n), where n is the length of the input number. This is because we iterate through each digit of the number once to calculate the sum. The operations involved in checking divisibility by 6 have a constant time complexity.
Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.
New Comment