Check that if large number is divisible by 15

Here given code implementation process.

// Java program for
// Check if number is divisible by 15
public class Divisibility
{
	public void isDivisibleBy15(String num)
	{
		// Number is divisible by 15.
		// When is divisible by 3 and by 5.
		int n = num.length();
		boolean result = false;
		if (n == 1 && num.charAt(0) == '0')
		{
			// When number is zero
			result = true;
		}
		else if (n > 0 && (num.charAt(n - 1) == '5' || 
                           num.charAt(n - 1) == '0'))
		{
			// When number is divisible by 5
			int sum = 0;
			// Sum of all digits of number
			for (int i = 0; i < n; ++i)
			{
				// 48 is ASCII of zero
				sum = sum + (num.charAt(i) - 48);
			}
			if ((sum % 3) == 0)
			{
				// When number is divisible by 3
				result = true;
			}
		}
		if (result)
		{
			System.out.println(" Given number (" + 
                               num + ") is divisible by 15");
		}
		else
		{
			System.out.println(" Given number (" + 
                               num + ") is not divisible by 15");
		}
	}
	public static void main(String[] args)
	{
		Divisibility task = new Divisibility();
		String num1 = "23782368473246823734850";
		String num2 = "125";
		// Test
		task.isDivisibleBy15(num1);
		task.isDivisibleBy15(num2);
	}
}

Output

 Given number (23782368473246823734850) is divisible by 15
 Given number (125) is not divisible by 15
// Include header file
#include <iostream>
#include <string>
using namespace std;

// C++ program for
// Check if number is divisible by 15
class Divisibility
{
	public: void isDivisibleBy15(string num)
	{
		// Number is divisible by 15.
		// When is divisible by 3 and by 5.
		int n = num.length();
		bool result = false;
		if (n == 1 && num[0] == '0')
		{
			// When number is zero
			result = true;
		}
		else if (n > 0 && 
                 (num[n - 1] == '5' || num[n - 1] == '0'))
		{
			// When number is divisible by 5
			int sum = 0;
			// Sum of all digits of number
			for (int i = 0; i < n; ++i)
			{
				// 48 is ASCII of zero
				sum = sum + (num[i] - 48);
			}
			if ((sum % 3) == 0)
			{
				// When number is divisible by 3
				result = true;
			}
		}
		if (result)
		{
			cout << " Given number (" 
                 << num << ") is divisible by 15" << endl;
		}
		else
		{
			cout << " Given number (" 
                 << num << ") is not divisible by 15" << endl;
		}
	}
};
int main()
{
	Divisibility *task = new Divisibility();
	string num1 = "23782368473246823734850";
	string num2 = "125";
	// Test
	task->isDivisibleBy15(num1);
	task->isDivisibleBy15(num2);
	return 0;
}

Output

 Given number (23782368473246823734850) is divisible by 15
 Given number (125) is not divisible by 15
// Include namespace system
using System;
// Csharp program for
// Check if number is divisible by 15
public class Divisibility
{
	public void isDivisibleBy15(String num)
	{
		// Number is divisible by 15.
		// When is divisible by 3 and by 5.
		int n = num.Length;
		Boolean result = false;
		if (n == 1 && num[0] == '0')
		{
			// When number is zero
			result = true;
		}
		else if (n > 0 && 
                 (num[n - 1] == '5' || num[n - 1] == '0'))
		{
			// When number is divisible by 5
			int sum = 0;
			// Sum of all digits of number
			for (int i = 0; i < n; ++i)
			{
				// 48 is ASCII of zero
				sum = sum + (num[i] - 48);
			}
			if ((sum % 3) == 0)
			{
				// When number is divisible by 3
				result = true;
			}
		}
		if (result)
		{
			Console.WriteLine(" Given number (" + 
                              num + ") is divisible by 15");
		}
		else
		{
			Console.WriteLine(" Given number (" + 
                              num + ") is not divisible by 15");
		}
	}
	public static void Main(String[] args)
	{
		Divisibility task = new Divisibility();
		String num1 = "23782368473246823734850";
		String num2 = "125";
		// Test
		task.isDivisibleBy15(num1);
		task.isDivisibleBy15(num2);
	}
}

Output

 Given number (23782368473246823734850) is divisible by 15
 Given number (125) is not divisible by 15
package main
import "fmt"
// Go program for
// Check if number is divisible by 15

func isDivisibleBy15(num string) {
	// Number is divisible by 15.
	// When is divisible by 3 and by 5.
	var n int = len(num)
	var result bool = false
	if n == 1 && num[0] == '0' {
		// When number is zero
		result = true
	} else if n > 0 && (num[n - 1] == '5' || 
		num[n - 1] == '0') {
		// When number is divisible by 5
		var sum int = 0
		// Sum of all digits of number
		for i := 0 ; i < n ; i++ {
			// 48 is ASCII of zero
			sum = sum + (int(num[i]) - 48)
		}
		if (sum % 3) == 0 {
			// When number is divisible by 3
			result = true
		}
	}
	if result {
		fmt.Println(" Given number (", num, ") is divisible by 15")
	} else {
		fmt.Println(" Given number (", num, ") is not divisible by 15")
	}
}
func main() {

	var num1 string = "23782368473246823734850"
	var num2 string = "125"
	// Test
	isDivisibleBy15(num1)
	isDivisibleBy15(num2)
}

Output

 Given number (23782368473246823734850) is divisible by 15
 Given number (125) is not divisible by 15
<?php
// Php program for
// Check if number is divisible by 15
class Divisibility
{
	public	function isDivisibleBy15($num)
	{
		// Number is divisible by 15.
		// When is divisible by 3 and by 5.
		$n = strlen($num);
		$result = false;
		if ($n == 1 && $num[0] == '0')
		{
			// When number is zero
			$result = true;
		}
		else if ($n > 0 && ($num[$n - 1] == '5' || 
                            $num[$n - 1] == '0'))
		{
			// When number is divisible by 5
			$sum = 0;
			// Sum of all digits of number
			for ($i = 0; $i < $n; ++$i)
			{
				// 48 is ASCII of zero
				$sum = $sum + (ord($num[$i]) - 48);
			}
			if (($sum % 3) == 0)
			{
				// When number is divisible by 3
				$result = true;
			}
		}
		if ($result)
		{
			echo(" Given number (".$num.
				") is divisible by 15\n");
		}
		else
		{
			echo(" Given number (".$num.
				") is not divisible by 15\n");
		}
	}
}

function main()
{
	$task = new Divisibility();
	$num1 = "23782368473246823734850";
	$num2 = "125";
	// Test
	$task->isDivisibleBy15($num1);
	$task->isDivisibleBy15($num2);
}
main();

Output

 Given number (23782368473246823734850) is divisible by 15
 Given number (125) is not divisible by 15
// Node JS program for
// Check if number is divisible by 15
class Divisibility
{
	isDivisibleBy15(num)
	{
		// Number is divisible by 15.
		// When is divisible by 3 and by 5.
		var n = num.length;
		var result = false;
		if (n == 1 && num.charAt(0) == '0')
		{
			// When number is zero
			result = true;
		}
		else if (n > 0 && (num.charAt(n - 1) == '5' || 
                           num.charAt(n - 1) == '0'))
		{
			// When number is divisible by 5
			var sum = 0;
			// Sum of all digits of number
			for (var i = 0; i < n; ++i)
			{
				// 48 is ASCII of zero
				sum = sum + (num.charAt(i).charCodeAt(0) - 48);
			}
			if ((sum % 3) == 0)
			{
				// When number is divisible by 3
				result = true;
			}
		}
		if (result)
		{
			console.log(" Given number (" + 
                        num + ") is divisible by 15");
		}
		else
		{
			console.log(" Given number (" + 
                        num + ") is not divisible by 15");
		}
	}
}

function main()
{
	var task = new Divisibility();
	var num1 = "23782368473246823734850";
	var num2 = "125";
	// Test
	task.isDivisibleBy15(num1);
	task.isDivisibleBy15(num2);
}
main();

Output

 Given number (23782368473246823734850) is divisible by 15
 Given number (125) is not divisible by 15
#  Python 3 program for
#  Check if number is divisible by 15
class Divisibility :
	def isDivisibleBy15(self, num) :
		#  Number is divisible by 15.
		#  When is divisible by 3 and by 5.
		n = len(num)
		result = False
		if (n == 1 and num[0] == '0') :
			#  When number is zero
			result = True
		elif (n > 0 and(num[n - 1] == '5'
				or num[n - 1] == '0')) :
			#  When number is divisible by 5
			sum = 0
			i = 0
			#  Sum of all digits of number
			while (i < n) :
				#  48 is ASCII of zero
				sum = sum + (ord(num[i]) - 48)
				i += 1
			
			if ((sum % 3) == 0) :
				#  When number is divisible by 3
				result = True
			
		
		if (result) :
			print(" Given number (", num ,") is divisible by 15")
		else :
			print(" Given number (", num ,") is not divisible by 15")
		
	

def main() :
	task = Divisibility()
	num1 = "23782368473246823734850"
	num2 = "125"
	#  Test
	task.isDivisibleBy15(num1)
	task.isDivisibleBy15(num2)

if __name__ == "__main__": main()

Output

 Given number ( 23782368473246823734850 ) is divisible by 15
 Given number ( 125 ) is not divisible by 15
#  Ruby program for
#  Check if number is divisible by 15
class Divisibility 
	def isDivisibleBy15(num) 
		#  Number is divisible by 15.
		#  When is divisible by 3 and by 5.
		n = num.length
		result = false
		if (n == 1 && num[0] == '0') 
			#  When number is zero
			result = true
		elsif (n > 0 && (num[n - 1] == '5' || 
                         num[n - 1] == '0')) 
			#  When number is divisible by 5
			sum = 0
			i = 0
			#  Sum of all digits of number
			while (i < n) 
				#  48 is ASCII of zero
				sum = sum + (num[i].ord - 48)
				i += 1
			end

			if ((sum % 3) == 0) 
				#  When number is divisible by 3
				result = true
			end

		end

		if (result) 
			print(" Given number (", 
                  num ,") is divisible by 15", "\n")
		else
 
			print(" Given number (", 
                  num ,") is not divisible by 15", "\n")
		end

	end

end

def main() 
	task = Divisibility.new()
	num1 = "23782368473246823734850"
	num2 = "125"
	#  Test
	task.isDivisibleBy15(num1)
	task.isDivisibleBy15(num2)
end

main()

Output

 Given number (23782368473246823734850) is divisible by 15
 Given number (125) is not divisible by 15
import scala.collection.mutable._;
// Scala program for
// Check if number is divisible by 15
class Divisibility()
{
	def isDivisibleBy15(num: String): Unit = {
		// Number is divisible by 15.
		// When is divisible by 3 and by 5.
		var n: Int = num.length();
		var result: Boolean = false;
		if (n == 1 && num.charAt(0) == '0')
		{
			// When number is zero
			result = true;
		}
		else if (n > 0 && (num.charAt(n - 1) == '5' || 
                           num.charAt(n - 1) == '0'))
		{
			// When number is divisible by 5
			var sum: Int = 0;
			var i: Int = 0;
			// Sum of all digits of number
			while (i < n)
			{
				// 48 is ASCII of zero
				sum = sum + (num.charAt(i).toInt - 48);
				i += 1;
			}
			if ((sum % 3) == 0)
			{
				// When number is divisible by 3
				result = true;
			}
		}
		if (result)
		{
			println(" Given number (" + num + ") is divisible by 15");
		}
		else
		{
			println(" Given number (" + num + ") is not divisible by 15");
		}
	}
}
object Main
{
	def main(args: Array[String]): Unit = {
		var task: Divisibility = new Divisibility();
		var num1: String = "23782368473246823734850";
		var num2: String = "125";
		// Test
		task.isDivisibleBy15(num1);
		task.isDivisibleBy15(num2);
	}
}

Output

 Given number (23782368473246823734850) is divisible by 15
 Given number (125) is not divisible by 15
import Foundation;
// Swift 4 program for
// Check if number is divisible by 15
class Divisibility
{
	func isDivisibleBy15(_ v: String)
	{
      	let num = Array(v);
		// Number is divisible by 15.
		// When is divisible by 3 and by 5.
		let n: Int = num.count;
		var result: Bool = false;
		if (n == 1 && num[0] == "0")
		{
			// When number is zero
			result = true;
		}
		else if (n > 0 && (num[n - 1] == "5" || 
                           num[n - 1] == "0"))
		{
			// When number is divisible by 5
			var sum: Int = 0;
			var i: Int = 0;
			// Sum of all digits of number
			while (i < n)
			{
				// 48 is ASCII of zero
				sum = sum + (Int(UnicodeScalar(String(num[i]))!.value) - 48);
				i += 1;
			}
			if ((sum % 3) == 0)
			{
				// When number is divisible by 3
				result = true;
			}
		}
		if (result)
		{
			print(" Given number (", v ,") is divisible by 15");
		}
		else
		{
			print(" Given number (", v ,") is not divisible by 15");
		}
	}
}
func main()
{
	let task: Divisibility = Divisibility();
	let num1: String = "23782368473246823734850";
	let num2: String = "125";
	// Test
	task.isDivisibleBy15(num1);
	task.isDivisibleBy15(num2);
}
main();

Output

 Given number ( 23782368473246823734850 ) is divisible by 15
 Given number ( 125 ) is not divisible by 15
// Kotlin program for
// Check if number is divisible by 15
class Divisibility
{
	fun isDivisibleBy15(num: String): Unit
	{
		// Number is divisible by 15.
		// When is divisible by 3 and by 5.
		val n: Int = num.length;
		var result: Boolean = false;
		if (n == 1 && num.get(0) == '0')
		{
			// When number is zero
			result = true;
		}
		else if (n > 0 && (num.get(n - 1) == '5' || 
                           num.get(n - 1) == '0'))
		{
			// When number is divisible by 5
			var sum: Int = 0;
			var i: Int = 0;
			// Sum of all digits of number
			while (i < n)
			{
				// 48 is ASCII of zero
				sum = sum + (num.get(i).toInt() - 48);
				i += 1;
			}
			if ((sum % 3) == 0)
			{
				// When number is divisible by 3
				result = true;
			}
		}
		if (result)
		{
			println(" Given number (" + 
                    num + ") is divisible by 15");
		}
		else
		{
			println(" Given number (" + 
                    num + ") is not divisible by 15");
		}
	}
}
fun main(args: Array < String > ): Unit
{
	val task: Divisibility = Divisibility();
	val num1: String = "23782368473246823734850";
	val num2: String = "125";
	// Test
	task.isDivisibleBy15(num1);
	task.isDivisibleBy15(num2);
}

Output

 Given number (23782368473246823734850) is divisible by 15
 Given number (125) is not divisible by 15


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