Check if large number is divisible by 3

Here given code implementation process.

/*
    C program for
    Check if large number is divisible by 3
*/
#include <stdio.h>

#include <string.h>

void divisibleBy3(const char *num)
{
	// Get the length of number
	int n = strlen(num);
	if (n == 0)
	{
		return;
	}
	int sum = 0;
	for (int i = 0; i < n; ++i)
	{
		// Sum of number digits
		sum += num[i] - '0';
	}
	if ((sum % 3) == 0)
	{
		printf("\n Number %s divisible by 3", num);
	}
	else
	{
		printf("\n Number %s is not divisible by 3", num);
	}
}
int main(int argc, char const *argv[])
{
	// Test Inputs
	divisibleBy3("123");
	divisibleBy3("93782639377588932984");
	divisibleBy3("336235332343");
	divisibleBy3("32456613423");
	return 0;
}

Output

 Number 123 divisible by 3
 Number 93782639377588932984 divisible by 3
 Number 336235332343 is not divisible by 3
 Number 32456613423 divisible by 3
/*
    Java program for
    Check if large number is divisible by 3
*/
class Divisibility
{
    public void divisibleBy3(String num)
    {
        // Get the length of number
        int n = num.length();
        if (n == 0)
        {
            return;
        }
        int sum = 0;
        for (int i = 0; i < n; ++i)
        {
            // Sum of number digits
            sum += num.charAt(i) - '0';
        }
        if ((sum % 3) == 0)
        {
            System.out.print("\n Number " + num + " divisible by 3");
        }
        else
        {
            System.out.print("\n Number " + num + " is not divisible by 3");
        }
    }
    public static void main(String[] args)
    {
        Divisibility task = new Divisibility();
        // Test Inputs
        task.divisibleBy3("123");
        task.divisibleBy3("93782639377588932984");
        task.divisibleBy3("336235332343");
        task.divisibleBy3("32456613423");
    }
}

Output

 Number 123 divisible by 3
 Number 93782639377588932984 divisible by 3
 Number 336235332343 is not divisible by 3
 Number 32456613423 divisible by 3
// Include header file
#include <iostream>
#include <string>
using namespace std;
/*
    C++ program for
    Check if large number is divisible by 3
*/
class Divisibility
{
	public: void divisibleBy3(string num)
	{
		// Get the length of number
		int n = num.length();
		if (n == 0)
		{
			return;
		}
		int sum = 0;
		for (int i = 0; i < n; ++i)
		{
			// Sum of number digits
			sum += num[i] - '0';
		}
		if ((sum % 3) == 0)
		{
			cout << "\n Number " << num << " divisible by 3";
		}
		else
		{
			cout << "\n Number " << num << " is not divisible by 3";
		}
	}
};
int main()
{
	Divisibility *task = new Divisibility();
	// Test Inputs
	task->divisibleBy3("123");
	task->divisibleBy3("93782639377588932984");
	task->divisibleBy3("336235332343");
	task->divisibleBy3("32456613423");
	return 0;
}

Output

 Number 123 divisible by 3
 Number 93782639377588932984 divisible by 3
 Number 336235332343 is not divisible by 3
 Number 32456613423 divisible by 3
// Include namespace system
using System;
/*
    Csharp program for
    Check if large number is divisible by 3
*/
public class Divisibility
{
	public void divisibleBy3(String num)
	{
		// Get the length of number
		int n = num.Length;
		if (n == 0)
		{
			return;
		}
		int sum = 0;
		for (int i = 0; i < n; ++i)
		{
			// Sum of number digits
			sum += num[i] - '0';
		}
		if ((sum % 3) == 0)
		{
			Console.Write("\n Number " + num + " divisible by 3");
		}
		else
		{
			Console.Write("\n Number " + num + " is not divisible by 3");
		}
	}
	public static void Main(String[] args)
	{
		Divisibility task = new Divisibility();
		// Test Inputs
		task.divisibleBy3("123");
		task.divisibleBy3("93782639377588932984");
		task.divisibleBy3("336235332343");
		task.divisibleBy3("32456613423");
	}
}

Output

 Number 123 divisible by 3
 Number 93782639377588932984 divisible by 3
 Number 336235332343 is not divisible by 3
 Number 32456613423 divisible by 3
package main
import "fmt"
/*
    Go program for
    Check if large number is divisible by 3
*/

func divisibleBy3(num string) {
	// Get the length of number
	var n int = len(num)
	if n == 0 {
		return
	}
	var sum int = 0
	for i := 0 ; i < n ; i++ {
		// Sum of number digits
		sum += int(num[i] - '0')
	}
	if (sum % 3) == 0 {
		fmt.Print("\n Number ", num, " divisible by 3")
	} else {
		fmt.Print("\n Number ", num, " is not divisible by 3")
	}
}
func main() {

	// Test Inputs
	divisibleBy3("123")
	divisibleBy3("93782639377588932984")
	divisibleBy3("336235332343")
	divisibleBy3("32456613423")
}

Output

 Number 123 divisible by 3
 Number 93782639377588932984 divisible by 3
 Number 336235332343 is not divisible by 3
 Number 32456613423 divisible by 3
<?php
/*
    Php program for
    Check if large number is divisible by 3
*/
class Divisibility
{
	public	function divisibleBy3($num)
	{
		// Get the length of number
		$n = strlen($num);
		if ($n == 0)
		{
			return;
		}
		$sum = 0;
		for ($i = 0; $i < $n; ++$i)
		{
			// Sum of number digits
			$sum += ord($num[$i]) - ord('0');
		}
		if (($sum % 3) == 0)
		{
			echo("\n Number ".$num.
				" divisible by 3");
		}
		else
		{
			echo("\n Number ".$num.
				" is not divisible by 3");
		}
	}
}

function main()
{
	$task = new Divisibility();
	// Test Inputs
	$task->divisibleBy3("123");
	$task->divisibleBy3("93782639377588932984");
	$task->divisibleBy3("336235332343");
	$task->divisibleBy3("32456613423");
}
main();

Output

 Number 123 divisible by 3
 Number 93782639377588932984 divisible by 3
 Number 336235332343 is not divisible by 3
 Number 32456613423 divisible by 3
/*
    Node JS program for
    Check if large number is divisible by 3
*/
class Divisibility
{
	divisibleBy3(num)
	{
		// Get the length of number
		var n = num.length;
		if (n == 0)
		{
			return;
		}
		var sum = 0;
		for (var i = 0; i < n; ++i)
		{
			// Sum of number digits
			sum += num.charCodeAt(i) - '0'.charCodeAt(0);
		}
		if ((sum % 3) == 0)
		{
			process.stdout.write("\n Number " + num + " divisible by 3");
		}
		else
		{
			process.stdout.write("\n Number " + num + " is not divisible by 3");
		}
	}
}

function main()
{
	var task = new Divisibility();
	// Test Inputs
	task.divisibleBy3("123");
	task.divisibleBy3("93782639377588932984");
	task.divisibleBy3("336235332343");
	task.divisibleBy3("32456613423");
}
main();

Output

 Number 123 divisible by 3
 Number 93782639377588932984 divisible by 3
 Number 336235332343 is not divisible by 3
 Number 32456613423 divisible by 3
#    Python 3 program for
#    Check if large number is divisible by 3
class Divisibility :
	def divisibleBy3(self, num) :
		#  Get the length of number
		n = len(num)
		if (n == 0) :
			return
		
		sum = 0
		i = 0
		while (i < n) :
			#  Sum of number digits
			sum += ord(num[i]) - ord('0')
			i += 1
		
		if ((sum % 3) == 0) :
			print("\n Number", num ,"divisible by 3", end = "")
		else :
			print("\n Number", num ,"is not divisible by 3", end = "")
		
	

def main() :
	task = Divisibility()
	#  Test Inputs
	task.divisibleBy3("123")
	task.divisibleBy3("93782639377588932984")
	task.divisibleBy3("336235332343")
	task.divisibleBy3("32456613423")

if __name__ == "__main__": main()

Output

 Number 123 divisible by 3
 Number 93782639377588932984 divisible by 3
 Number 336235332343 is not divisible by 3
 Number 32456613423 divisible by 3
#    Ruby program for
#    Check if large number is divisible by 3
class Divisibility 
	def divisibleBy3(num) 
		#  Get the length of number
		n = num.length
		if (n == 0) 
			return
		end

		sum = 0
		i = 0
		while (i < n) 
			#  Sum of number digits
			sum += num[i].ord - '0'.ord
			i += 1
		end

		if ((sum % 3) == 0) 
			print("\n Number ", num ," divisible by 3")
		else
 
			print("\n Number ", num ," is not divisible by 3")
		end

	end

end

def main() 
	task = Divisibility.new()
	#  Test Inputs
	task.divisibleBy3("123")
	task.divisibleBy3("93782639377588932984")
	task.divisibleBy3("336235332343")
	task.divisibleBy3("32456613423")
end

main()

Output

 Number 123 divisible by 3
 Number 93782639377588932984 divisible by 3
 Number 336235332343 is not divisible by 3
 Number 32456613423 divisible by 3
/*
    Scala program for
    Check if large number is divisible by 3
*/
class Divisibility()
{
	def divisibleBy3(num: String): Unit = {
		// Get the length of number
		var n: Int = num.length();
		if (n == 0)
		{
			return;
		}
		var sum: Int = 0;
		var i: Int = 0;
		while (i < n)
		{
			// Sum of number digits
			sum += num.charAt(i).toInt - '0'.toInt;
			i += 1;
		}
		if ((sum % 3) == 0)
		{
			print("\n Number " + num + " divisible by 3");
		}
		else
		{
			print("\n Number " + num + " is not divisible by 3");
		}
	}
}
object Main
{
	def main(args: Array[String]): Unit = {
		var task: Divisibility = new Divisibility();
		// Test Inputs
		task.divisibleBy3("123");
		task.divisibleBy3("93782639377588932984");
		task.divisibleBy3("336235332343");
		task.divisibleBy3("32456613423");
	}
}

Output

 Number 123 divisible by 3
 Number 93782639377588932984 divisible by 3
 Number 336235332343 is not divisible by 3
 Number 32456613423 divisible by 3
import Foundation;
/*
    Swift 4 program for
    Check if large number is divisible by 3
*/
class Divisibility
{
	func divisibleBy3(_ data: String)
	{
      	let num = Array(data);
		// Get the length of number
		let n: Int = num.count;
		if (n == 0)
		{
			return;
		}
		var sum: Int = 0;
		var i: Int = 0;
		while (i < n)
		{
			// Sum of number digits
			sum += Int(UnicodeScalar(String(num[i]))!.value) - 
              Int(UnicodeScalar(String("0"))!.value);
			i += 1;
		}
		if ((sum % 3) == 0)
		{
			print("\n Number", data ,"divisible by 3", terminator: "");
		}
		else
		{
			print("\n Number", data ,"is not divisible by 3", terminator: "");
		}
	}
}
func main()
{
	let task: Divisibility = Divisibility();
	// Test Inputs
	task.divisibleBy3("123");
	task.divisibleBy3("93782639377588932984");
	task.divisibleBy3("336235332343");
	task.divisibleBy3("32456613423");
}
main();

Output

 Number 123 divisible by 3
 Number 93782639377588932984 divisible by 3
 Number 336235332343 is not divisible by 3
 Number 32456613423 divisible by 3
/*
    Kotlin program for
    Check if large number is divisible by 3
*/
class Divisibility
{
	fun divisibleBy3(num: String): Unit
	{
		// Get the length of number
		val n: Int = num.length;
		if (n == 0)
		{
			return;
		}
		var sum: Int = 0;
		var i: Int = 0;
		while (i < n)
		{
			// Sum of number digits
			sum += num.get(i).toInt() - '0'.toInt();
			i += 1;
		}
		if ((sum % 3) == 0)
		{
			print("\n Number " + num + " divisible by 3");
		}
		else
		{
			print("\n Number " + num + " is not divisible by 3");
		}
	}
}
fun main(args: Array < String > ): Unit
{
	val task: Divisibility = Divisibility();
	// Test Inputs
	task.divisibleBy3("123");
	task.divisibleBy3("93782639377588932984");
	task.divisibleBy3("336235332343");
	task.divisibleBy3("32456613423");
}

Output

 Number 123 divisible by 3
 Number 93782639377588932984 divisible by 3
 Number 336235332343 is not divisible by 3
 Number 32456613423 divisible by 3


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







© 2021, kalkicode.com, All rights reserved