Check if large number is divisible by 11

Here given code implementation process.

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

void divisibleBy11(const char *num)
{
	// Get the length of num
	int n = strlen(num);
	if (n == 0)
	{
		return;
	}
	// Use to collect alternate position digit sum
	int oddDigitSum = 0;
	int evenDigitSum = 0;
	// Execute loop through by length of number
	for (int i = 0; i < n; ++i)
	{
		if (i % 2 == 0)
		{
			evenDigitSum += (num[i] - '0');
		}
		else
		{
			oddDigitSum += (num[i] - '0');
		}
	}
	if ((oddDigitSum - evenDigitSum) % 11 == 0)
	{
		printf("\n Number %s divisible by 11", num);
	}
	else
	{
		printf("\n Number %s is not divisible by 11", num);
	}
}
int main(int argc, char const *argv[])
{
	// Test Inputs
	divisibleBy11("93782639377588932984");
  	divisibleBy11("110");
	divisibleBy11("33612315332343");
	divisibleBy11("382456613423");
	return 0;
}

Output

 Number 93782639377588932984 is not divisible by 11
 Number 110 divisible by 11
 Number 33612315332343 divisible by 11
 Number 382456613423 is not divisible by 11
/*
    Java program for
    Check if large number is divisible by 11
*/
class Divisibility
{
	public void divisibleBy11(String num)
	{
		// Get the length of num
		int n = num.length();
		if (n == 0)
		{
			return;
		}
		// Use to collect alternate position digit sum
		int oddDigitSum = 0;
		int evenDigitSum = 0;
		// Execute loop through by length of number
		for (int i = 0; i < n; ++i)
		{
			if (i % 2 == 0)
			{
				evenDigitSum += (num.charAt(i) - '0');
			}
			else
			{
				oddDigitSum += (num.charAt(i) - '0');
			}
		}
		if ((oddDigitSum - evenDigitSum) % 11 == 0)
		{
			System.out.print("\n Number " + num + " divisible by 11");
		}
		else
		{
			System.out.print("\n Number " + num + " is not divisible by 11");
		}
	}
	public static void main(String[] args)
	{
		Divisibility task = new Divisibility();
		// Test Inputs
		task.divisibleBy11("93782639377588932984");
		task.divisibleBy11("110");
		task.divisibleBy11("33612315332343");
		task.divisibleBy11("382456613423");
	}
}

Output

 Number 93782639377588932984 is not divisible by 11
 Number 110 divisible by 11
 Number 33612315332343 divisible by 11
 Number 382456613423 is not divisible by 11
// Include header file
#include <iostream>
#include <string>

using namespace std;
/*
    C++ program for
    Check if large number is divisible by 11
*/
class Divisibility
{
	public: void divisibleBy11(string num)
	{
		// Get the length of num
		int n = num.length();
		if (n == 0)
		{
			return;
		}
		// Use to collect alternate position digit sum
		int oddDigitSum = 0;
		int evenDigitSum = 0;
		// Execute loop through by length of number
		for (int i = 0; i < n; ++i)
		{
			if (i % 2 == 0)
			{
				evenDigitSum += (num[i] - '0');
			}
			else
			{
				oddDigitSum += (num[i] - '0');
			}
		}
		if ((oddDigitSum - evenDigitSum) % 11 == 0)
		{
			cout << "\n Number " << num << " divisible by 11";
		}
		else
		{
			cout << "\n Number " << num << " is not divisible by 11";
		}
	}
};
int main()
{
	Divisibility *task = new Divisibility();
	// Test Inputs
	task->divisibleBy11("93782639377588932984");
	task->divisibleBy11("110");
	task->divisibleBy11("33612315332343");
	task->divisibleBy11("382456613423");
	return 0;
}

Output

 Number 93782639377588932984 is not divisible by 11
 Number 110 divisible by 11
 Number 33612315332343 divisible by 11
 Number 382456613423 is not divisible by 11
// Include namespace system
using System;
/*
    Csharp program for
    Check if large number is divisible by 11
*/
public class Divisibility
{
	public void divisibleBy11(String num)
	{
		// Get the length of num
		int n = num.Length;
		if (n == 0)
		{
			return;
		}
		// Use to collect alternate position digit sum
		int oddDigitSum = 0;
		int evenDigitSum = 0;
		// Execute loop through by length of number
		for (int i = 0; i < n; ++i)
		{
			if (i % 2 == 0)
			{
				evenDigitSum += (num[i] - '0');
			}
			else
			{
				oddDigitSum += (num[i] - '0');
			}
		}
		if ((oddDigitSum - evenDigitSum) % 11 == 0)
		{
			Console.Write("\n Number " + num + " divisible by 11");
		}
		else
		{
			Console.Write("\n Number " + num + " is not divisible by 11");
		}
	}
	public static void Main(String[] args)
	{
		Divisibility task = new Divisibility();
		// Test Inputs
		task.divisibleBy11("93782639377588932984");
		task.divisibleBy11("110");
		task.divisibleBy11("33612315332343");
		task.divisibleBy11("382456613423");
	}
}

Output

 Number 93782639377588932984 is not divisible by 11
 Number 110 divisible by 11
 Number 33612315332343 divisible by 11
 Number 382456613423 is not divisible by 11
package main
import "fmt"
/*
    Go program for
    Check if large number is divisible by 11
*/

func divisibleBy11(num string) {
	// Get the length of num
	var n int = len(num)
	if n == 0 {
		return
	}
	// Use to collect alternate position digit sum
	var oddDigitSum int = 0
	var evenDigitSum int = 0
	// Execute loop through by length of number
	for i := 0 ; i < n ; i++ {
		if i % 2 == 0 {
			evenDigitSum += int(num[i] - '0')
		} else {
			oddDigitSum += int(num[i] - '0')
		}
	}
	if (oddDigitSum - evenDigitSum) % 11 == 0 {
		fmt.Print("\n Number ", num, " divisible by 11")
	} else {
		fmt.Print("\n Number ", num, " is not divisible by 11")
	}
}
func main() {

	// Test Inputs
	divisibleBy11("93782639377588932984")
	divisibleBy11("110")
	divisibleBy11("33612315332343")
	divisibleBy11("382456613423")
}

Output

 Number 93782639377588932984 is not divisible by 11
 Number 110 divisible by 11
 Number 33612315332343 divisible by 11
 Number 382456613423 is not divisible by 11
<?php
/*
    Php program for
    Check if large number is divisible by 11
*/
class Divisibility
{
	public	function divisibleBy11($num)
	{
		// Get the length of num
		$n = strlen($num);
		if ($n == 0)
		{
			return;
		}
		// Use to collect alternate position digit sum
		$oddDigitSum = 0;
		$evenDigitSum = 0;
		// Execute loop through by length of number
		for ($i = 0; $i < $n; ++$i)
		{
			if ($i % 2 == 0)
			{
				$evenDigitSum += (ord($num[$i]) - ord('0'));
			}
			else
			{
				$oddDigitSum += (ord($num[$i]) - ord('0'));
			}
		}
		if (($oddDigitSum - $evenDigitSum) % 11 == 0)
		{
			echo("\n Number ".$num.
				" divisible by 11");
		}
		else
		{
			echo("\n Number ".$num.
				" is not divisible by 11");
		}
	}
}

function main()
{
	$task = new Divisibility();
	// Test Inputs
	$task->divisibleBy11("93782639377588932984");
	$task->divisibleBy11("110");
	$task->divisibleBy11("33612315332343");
	$task->divisibleBy11("382456613423");
}
main();

Output

 Number 93782639377588932984 is not divisible by 11
 Number 110 divisible by 11
 Number 33612315332343 divisible by 11
 Number 382456613423 is not divisible by 11
/*
    Node JS program for
    Check if large number is divisible by 11
*/
class Divisibility
{
	divisibleBy11(num)
	{
		// Get the length of num
		var n = num.length;
		if (n == 0)
		{
			return;
		}
		// Use to collect alternate position digit sum
		var oddDigitSum = 0;
		var evenDigitSum = 0;
		// Execute loop through by length of number
		for (var i = 0; i < n; ++i)
		{
			if (i % 2 == 0)
			{
				evenDigitSum += (num.charCodeAt(i) - 
                                 '0'.charCodeAt(0));
			}
			else
			{
				oddDigitSum += (num.charCodeAt(i) - 
                                '0'.charCodeAt(0));
			}
		}
		if ((oddDigitSum - evenDigitSum) % 11 == 0)
		{
			process.stdout.write("\n Number " + num + " divisible by 11");
		}
		else
		{
			process.stdout.write("\n Number " + num + " is not divisible by 11");
		}
	}
}

function main()
{
	var task = new Divisibility();
	// Test Inputs
	task.divisibleBy11("9378265675467565654654654756739377588932984");
	task.divisibleBy11("110");
	task.divisibleBy11("33612315332343");
	task.divisibleBy11("382456613423");
}
main();

Output

 Number 9378265675467565654654654756739377588932984 is not divisible by 11
 Number 110 divisible by 11
 Number 33612315332343 divisible by 11
 Number 382456613423 is not divisible by 11
#    Python 3 program for
#    Check if large number is divisible by 11
class Divisibility :
	def divisibleBy11(self, num) :
		#  Get the length of num
		n = len(num)
		if (n == 0) :
			return
		
		#  Use to collect alternate position digit sum
		oddDigitSum = 0
		evenDigitSum = 0
		i = 0
		#  Execute loop through by length of number
		while (i < n) :
			if (i % 2 == 0) :
				evenDigitSum += (ord(num[i]) - ord('0'))
			else :
				oddDigitSum += (ord(num[i]) - ord('0'))
			
			i += 1
		
		if ((oddDigitSum - evenDigitSum) % 11 == 0) :
			print("\n Number", num ,"divisible by 11", end = "")
		else :
			print("\n Number", num ,"is not divisible by 11", end = "")
		
	

def main() :
	task = Divisibility()
	#  Test Inputs
	task.divisibleBy11("93782639377588932984")
	task.divisibleBy11("110")
	task.divisibleBy11("33612315332343")
	task.divisibleBy11("382456613423")

if __name__ == "__main__": main()

Output

 Number 93782639377588932984 is not divisible by 11
 Number 110 divisible by 11
 Number 33612315332343 divisible by 11
 Number 382456613423 is not divisible by 11
#    Ruby program for
#    Check if large number is divisible by 11
class Divisibility 
	def divisibleBy11(num) 
		#  Get the length of num
		n = num.length
		if (n == 0) 
			return
		end

		#  Use to collect alternate position digit sum
		oddDigitSum = 0
		evenDigitSum = 0
		i = 0
		#  Execute loop through by length of number
		while (i < n) 
			if (i % 2 == 0) 
				evenDigitSum += (num[i].ord - '0'.ord)
			else
 
				oddDigitSum += (num[i].ord - '0'.ord)
			end

			i += 1
		end

		if ((oddDigitSum - evenDigitSum) % 11 == 0) 
			print("\n Number ", num ," divisible by 11")
		else
 
			print("\n Number ", num ," is not divisible by 11")
		end

	end

end

def main() 
	task = Divisibility.new()
	#  Test Inputs
	task.divisibleBy11("93782639377588932984")
	task.divisibleBy11("110")
	task.divisibleBy11("33612315332343")
	task.divisibleBy11("382456613423")
end

main()

Output

 Number 93782639377588932984 is not divisible by 11
 Number 110 divisible by 11
 Number 33612315332343 divisible by 11
 Number 382456613423 is not divisible by 11
import scala.collection.mutable._;
/*
    Scala program for
    Check if large number is divisible by 11
*/
class Divisibility()
{
	def divisibleBy11(num: String): Unit = {
		// Get the length of num
		var n: Int = num.length();
		if (n == 0)
		{
			return;
		}
		// Use to collect alternate position digit sum
		var oddDigitSum: Int = 0;
		var evenDigitSum: Int = 0;
		var i: Int = 0;
		// Execute loop through by length of number
		while (i < n)
		{
			if (i % 2 == 0)
			{
				evenDigitSum += (num.charAt(i).toInt - '0'.toInt);
			}
			else
			{
				oddDigitSum += (num.charAt(i).toInt - '0'.toInt);
			}
			i += 1;
		}
		if ((oddDigitSum - evenDigitSum) % 11 == 0)
		{
			print("\n Number " + num + " divisible by 11");
		}
		else
		{
			print("\n Number " + num + " is not divisible by 11");
		}
	}
}
object Main
{
	def main(args: Array[String]): Unit = {
		var task: Divisibility = new Divisibility();
		// Test Inputs
		task.divisibleBy11("93782639377588932984");
		task.divisibleBy11("110");
		task.divisibleBy11("33612315332343");
		task.divisibleBy11("382456613423");
	}
}

Output

 Number 93782639377588932984 is not divisible by 11
 Number 110 divisible by 11
 Number 33612315332343 divisible by 11
 Number 382456613423 is not divisible by 11
import Foundation;
/*
    Swift 4 program for
    Check if large number is divisible by 11
*/
class Divisibility
{
	func divisibleBy11(_ data: String)
	{
      	let num = Array(data);
		// Get the length of num
		let n: Int = num.count;
		if (n == 0)
		{
			return;
		}
		// Use to collect alternate position digit sum
		var oddDigitSum: Int = 0;
		var evenDigitSum: Int = 0;
		var i: Int = 0;
		// Execute loop through by length of number
		while (i < n)
		{
			if (i % 2 == 0)
			{
				evenDigitSum += (Int(UnicodeScalar(String(num[i]))!.value) -
                                 Int(UnicodeScalar(String("0"))!.value));
			}
			else
			{
				oddDigitSum += (Int(UnicodeScalar(String(num[i]))!.value) - 
                                Int(UnicodeScalar(String("0"))!.value));
			}
			i += 1;
		}
		if ((oddDigitSum - evenDigitSum) % 11 == 0)
		{
			print("\n Number", data ,"divisible by 11", terminator: "");
		}
		else
		{
			print("\n Number", data ,"is not divisible by 11", terminator: "");
		}
	}
}
func main()
{
	let task: Divisibility = Divisibility();
	// Test Inputs
	task.divisibleBy11("93782639377588932984");
	task.divisibleBy11("110");
	task.divisibleBy11("33612315332343");
	task.divisibleBy11("382456613423");
}
main();

Output

 Number 93782639377588932984 is not divisible by 11
 Number 110 divisible by 11
 Number 33612315332343 divisible by 11
 Number 382456613423 is not divisible by 11
/*
    Kotlin program for
    Check if large number is divisible by 11
*/
class Divisibility
{
	fun divisibleBy11(num: String): Unit
	{
		// Get the length of num
		val n: Int = num.length;
		if (n == 0)
		{
			return;
		}
		// Use to collect alternate position digit sum
		var oddDigitSum: Int = 0;
		var evenDigitSum: Int = 0;
		var i: Int = 0;
		// Execute loop through by length of number
		while (i < n)
		{
			if (i % 2 == 0)
			{
				evenDigitSum += (num.get(i).toInt() - '0'.toInt());
			}
			else
			{
				oddDigitSum += (num.get(i).toInt() - '0'.toInt());
			}
			i += 1;
		}
		if ((oddDigitSum - evenDigitSum) % 11 == 0)
		{
			print("\n Number " + num + " divisible by 11");
		}
		else
		{
			print("\n Number " + num + " is not divisible by 11");
		}
	}
}
fun main(args: Array < String > ): Unit
{
	val task: Divisibility = Divisibility();
	// Test Inputs
	task.divisibleBy11("93782639377588932984");
	task.divisibleBy11("110");
	task.divisibleBy11("33612315332343");
	task.divisibleBy11("382456613423");
}

Output

 Number 93782639377588932984 is not divisible by 11
 Number 110 divisible by 11
 Number 33612315332343 divisible by 11
 Number 382456613423 is not divisible by 11


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