Posted on by Kalkicode
Code Mathematics

Check that if large number is divisible by 30

A number is divisible by 30 if and only if it is divisible by both 2 and 3 and 5.

To check if a large number is divisible by 30, you can follow these steps:

  1. Check if the number is even. If the last digit of the number is even, then it is divisible by 2. Otherwise, it is not divisible by 2 and therefore not divisible by 30.

  2. Check if the sum of the digits is divisible by 3. If the sum of the digits is divisible by 3, then the number is divisible by 3. Otherwise, it is not divisible by 3 and therefore not divisible by 30.

  3. Check if the number ends in 0 or 5. If the last digit of the number is either 0 or 5, then it is divisible by 5. Otherwise, it is not divisible by 5 and therefore not divisible by 30.

If the number is divisible by 2, 3, and 5, then it is divisible by 30.

Other Approach

Check if a large number is divisible by 30 using the numbers 3 and 10. Here's how:

  1. Check if the sum of the digits is divisible by 3. If the sum of the digits is divisible by 3, then the number is divisible by 3.

  2. Check if the last digit is 0. If the last digit is 0, then the number is divisible by 10.

If the number is divisible by both 3 and 10, then it is divisible by 30.

Program Solution

// Java program for
// Check that if large number is divisible by 30
public class Divisibility
{
	public void isDivisibleBy30(String num)
	{
		// A number is divisible by 30 when satisfies two conditions.
		// ➀ is divisible by 10.
		// ➁ is divisible by 3.
		boolean result = false;
		int n = num.length();
		if (n == 1 && num.charAt(0) == '0')
		{
			// When number is zero
			result = true;
		}
		else if (n > 1)
		{
			// Check condition 1
			if (num.charAt(n - 1) == '0')
			{
				// When number is divisible by 10
				// 2nd condition check number is divisible by 3 or not
				int sum = 0;
				// Calculate sum of all digit
				for (int i = 0; i < n; ++i)
				{
					sum += (num.charAt(i) - 48);
				}
				if (sum % 3 == 0)
				{
					// When number is divisible by 3
					result = true;
				}
			}
		}
		if (result == true)
		{
			System.out.println(" Given number (" + 
                               num + ") is divisible by 30");
		}
		else
		{
			System.out.println(" Given number (" + 
                               num + ") is not divisible by 30");
		}
	}
	public static void main(String[] args)
	{
		Divisibility task = new Divisibility();
		// Test
		task.isDivisibleBy30("434534533434230");
		task.isDivisibleBy30("43453453343432323423230");
		task.isDivisibleBy30("895434230");
		task.isDivisibleBy30("32340");
	}
}

Output

 Given number (434534533434230) is not divisible by 30
 Given number (43453453343432323423230) is divisible by 30
 Given number (895434230) is not divisible by 30
 Given number (32340) is divisible by 30
// Include header file
#include <iostream>
#include <string>
using namespace std;
// C++ program for
// Check that if large number is divisible by 30
class Divisibility
{
	public: void isDivisibleBy30(string num)
	{
		// A number is divisible by 30 when satisfies two conditions.
		// ➀ is divisible by 10.
		// ➁ is divisible by 3.
		bool result = false;
		int n = num.length();
		if (n == 1 && num[0] == '0')
		{
			// When number is zero
			result = true;
		}
		else if (n > 1)
		{
			// Check condition 1
			if (num[n - 1] == '0')
			{
				// When number is divisible by 10
				// 2nd condition check number is divisible by 3 or not
				int sum = 0;
				// Calculate sum of all digit
				for (int i = 0; i < n; ++i)
				{
					sum += (num[i] - 48);
				}
				if (sum % 3 == 0)
				{
					// When number is divisible by 3
					result = true;
				}
			}
		}
		if (result == true)
		{
			cout << " Given number (" 
                 << num << ") is divisible by 30" << endl;
		}
		else
		{
			cout << " Given number (" 
                 << num << ") is not divisible by 30" << endl;
		}
	}
};
int main()
{
	Divisibility *task = new Divisibility();
	// Test
	task->isDivisibleBy30("434534533434230");
	task->isDivisibleBy30("43453453343432323423230");
	task->isDivisibleBy30("895434230");
	task->isDivisibleBy30("32340");
	return 0;
}

Output

 Given number (434534533434230) is not divisible by 30
 Given number (43453453343432323423230) is divisible by 30
 Given number (895434230) is not divisible by 30
 Given number (32340) is divisible by 30
// Include namespace system
using System;
// Csharp program for
// Check that if large number is divisible by 30
public class Divisibility
{
	public void isDivisibleBy30(String num)
	{
		// A number is divisible by 30 when satisfies two conditions.
		// ➀ is divisible by 10.
		// ➁ is divisible by 3.
		Boolean result = false;
		int n = num.Length;
		if (n == 1 && num[0] == '0')
		{
			// When number is zero
			result = true;
		}
		else if (n > 1)
		{
			// Check condition 1
			if (num[n - 1] == '0')
			{
				// When number is divisible by 10
				// 2nd condition check number is divisible by 3 or not
				int sum = 0;
				// Calculate sum of all digit
				for (int i = 0; i < n; ++i)
				{
					sum += (num[i] - 48);
				}
				if (sum % 3 == 0)
				{
					// When number is divisible by 3
					result = true;
				}
			}
		}
		if (result == true)
		{
			Console.WriteLine(" Given number (" + num + ") is divisible by 30");
		}
		else
		{
			Console.WriteLine(" Given number (" + num + ") is not divisible by 30");
		}
	}
	public static void Main(String[] args)
	{
		Divisibility task = new Divisibility();
		// Test
		task.isDivisibleBy30("434534533434230");
		task.isDivisibleBy30("43453453343432323423230");
		task.isDivisibleBy30("895434230");
		task.isDivisibleBy30("32340");
	}
}

Output

 Given number (434534533434230) is not divisible by 30
 Given number (43453453343432323423230) is divisible by 30
 Given number (895434230) is not divisible by 30
 Given number (32340) is divisible by 30
package main
import "fmt"
// Go program for
// Check that if large number is divisible by 30

func isDivisibleBy30(num string) {
	// A number is divisible by 30 when satisfies two conditions.
	// ➀ is divisible by 10.
	// ➁ is divisible by 3.
	var result bool = false
	var n int = len(num)
	if n == 1 && num[0] == '0' {
		// When number is zero
		result = true
	} else if n > 1 {
		// Check condition 1
		if num[n - 1] == '0' {
			// When number is divisible by 10
			// 2nd condition check number is divisible by 3 or not
			var sum int = 0
			// Calculate sum of all digit
			for i := 0 ; i < n ; i++ {
				sum += (int(num[i]) - 48)
			}
			if sum % 3 == 0 {
				// When number is divisible by 3
				result = true
			}
		}
	}
	if result == true {
		fmt.Println(" Given number (", num, ") is divisible by 30")
	} else {
		fmt.Println(" Given number (", num, ") is not divisible by 30")
	}
}
func main() {

	// Test
	isDivisibleBy30("434534533434230")
	isDivisibleBy30("43453453343432323423230")
	isDivisibleBy30("895434230")
	isDivisibleBy30("32340")
}

Output

 Given number (434534533434230) is not divisible by 30
 Given number (43453453343432323423230) is divisible by 30
 Given number (895434230) is not divisible by 30
 Given number (32340) is divisible by 30
<?php
// Php program for
// Check that if large number is divisible by 30
class Divisibility
{
	public	function isDivisibleBy30($num)
	{
		// A number is divisible by 30 when satisfies two conditions.
		// ➀ is divisible by 10.
		// ➁ is divisible by 3.
		$result = false;
		$n = strlen($num);
		if ($n == 1 && $num[0] == '0')
		{
			// When number is zero
			$result = true;
		}
		else if ($n > 1)
		{
			// Check condition 1
			if ($num[$n - 1] == '0')
			{
				// When number is divisible by 10
				// 2nd condition check number is divisible by 3 or not
				$sum = 0;
				// Calculate sum of all digit
				for ($i = 0; $i < $n; ++$i)
				{
					$sum += (ord($num[$i]) - 48);
				}
				if ($sum % 3 == 0)
				{
					// When number is divisible by 3
					$result = true;
				}
			}
		}
		if ($result == true)
		{
			echo(" Given number (".$num.
				") is divisible by 30".
				"\n");
		}
		else
		{
			echo(" Given number (".$num.
				") is not divisible by 30".
				"\n");
		}
	}
}

function main()
{
	$task = new Divisibility();
	// Test
	$task->isDivisibleBy30("434534533434230");
	$task->isDivisibleBy30("43453453343432323423230");
	$task->isDivisibleBy30("895434230");
	$task->isDivisibleBy30("32340");
}
main();

Output

 Given number (434534533434230) is not divisible by 30
 Given number (43453453343432323423230) is divisible by 30
 Given number (895434230) is not divisible by 30
 Given number (32340) is divisible by 30
// Node JS program for
// Check that if large number is divisible by 30
class Divisibility
{
	isDivisibleBy30(num)
	{
		// A number is divisible by 30 when satisfies two conditions.
		// ➀ is divisible by 10.
		// ➁ is divisible by 3.
		var result = false;
		var n = num.length;
		if (n == 1 && num.charAt(0) == '0')
		{
			// When number is zero
			result = true;
		}
		else if (n > 1)
		{
			// Check condition 1
			if (num.charAt(n - 1) == '0')
			{
				// When number is divisible by 10
				// 2nd condition check number is divisible by 3 or not
				var sum = 0;
				// Calculate sum of all digit
				for (var i = 0; i < n; ++i)
				{
					sum += (num.charAt(i).charCodeAt(0) - 48);
				}
				if (sum % 3 == 0)
				{
					// When number is divisible by 3
					result = true;
				}
			}
		}
		if (result == true)
		{
			console.log(" Given number (" + num + ") is divisible by 30");
		}
		else
		{
			console.log(" Given number (" + num + ") is not divisible by 30");
		}
	}
}

function main()
{
	var task = new Divisibility();
	// Test
	task.isDivisibleBy30("434534533434230");
	task.isDivisibleBy30("43453453343432323423230");
	task.isDivisibleBy30("895434230");
	task.isDivisibleBy30("32340");
}
main();

Output

 Given number (434534533434230) is not divisible by 30
 Given number (43453453343432323423230) is divisible by 30
 Given number (895434230) is not divisible by 30
 Given number (32340) is divisible by 30
#  Python 3 program for
#  Check that if large number is divisible by 30
class Divisibility :
	def isDivisibleBy30(self, num) :
		#  A number is divisible by 30 when satisfies two conditions.
		#  ➀ is divisible by 10.
		#  ➁ is divisible by 3.
		result = False
		n = len(num)
		if (n == 1 and num[0] == '0') :
			#  When number is zero
			result = True
		elif (n > 1) :
			#  Check condition 1
			if (num[n - 1] == '0') :
				#  When number is divisible by 10
				#  2nd condition check number is divisible by 3 or not
				sum = 0
				i = 0
				#  Calculate sum of all digit
				while (i < n) :
					sum += (ord(num[i]) - 48)
					i += 1
				
				if (sum % 3 == 0) :
					#  When number is divisible by 3
					result = True
				
			
		
		if (result == True) :
			print(" Given number (", num ,") is divisible by 30")
		else :
			print(" Given number (", num ,") is not divisible by 30")
		
	

def main() :
	task = Divisibility()
	#  Test
	task.isDivisibleBy30("434534533434230")
	task.isDivisibleBy30("43453453343432323423230")
	task.isDivisibleBy30("895434230")
	task.isDivisibleBy30("32340")

if __name__ == "__main__": main()

Output

 Given number ( 434534533434230 ) is not divisible by 30
 Given number ( 43453453343432323423230 ) is divisible by 30
 Given number ( 895434230 ) is not divisible by 30
 Given number ( 32340 ) is divisible by 30
#  Ruby program for
#  Check that if large number is divisible by 30
class Divisibility 
	def isDivisibleBy30(num) 
		#  A number is divisible by 30 when satisfies two conditions.
		#  ➀ is divisible by 10.
		#  ➁ is divisible by 3.
		result = false
		n = num.length
		if (n == 1 && num[0] == '0') 
			#  When number is zero
			result = true
		elsif (n > 1) 
			#  Check condition 1
			if (num[n - 1] == '0') 
				#  When number is divisible by 10
				#  2nd condition check number is divisible by 3 or not
				sum = 0
				i = 0
				#  Calculate sum of all digit
				while (i < n) 
					sum += (num[i].ord - 48)
					i += 1
				end

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

			end

		end

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

	end

end

def main() 
	task = Divisibility.new()
	#  Test
	task.isDivisibleBy30("434534533434230")
	task.isDivisibleBy30("43453453343432323423230")
	task.isDivisibleBy30("895434230")
	task.isDivisibleBy30("32340")
end

main()

Output

 Given number (434534533434230) is not divisible by 30
 Given number (43453453343432323423230) is divisible by 30
 Given number (895434230) is not divisible by 30
 Given number (32340) is divisible by 30
import scala.collection.mutable._;
// Scala program for
// Check that if large number is divisible by 30
class Divisibility()
{
	def isDivisibleBy30(num: String): Unit = {
		// A number is divisible by 30 when satisfies two conditions.
		// ➀ is divisible by 10.
		// ➁ is divisible by 3.
		var result: Boolean = false;
		var n: Int = num.length();
		if (n == 1 && num.charAt(0) == '0')
		{
			// When number is zero
			result = true;
		}
		else if (n > 1)
		{
			// Check condition 1
			if (num.charAt(n - 1) == '0')
			{
				// When number is divisible by 10
				// 2nd condition check number is divisible by 3 or not
				var sum: Int = 0;
				var i: Int = 0;
				// Calculate sum of all digit
				while (i < n)
				{
					sum += (num.charAt(i).toInt - 48);
					i += 1;
				}
				if (sum % 3 == 0)
				{
					// When number is divisible by 3
					result = true;
				}
			}
		}
		if (result == true)
		{
			println(" Given number (" + 
                    num + ") is divisible by 30");
		}
		else
		{
			println(" Given number (" + 
                    num + ") is not divisible by 30");
		}
	}
}
object Main
{
	def main(args: Array[String]): Unit = {
		var task: Divisibility = new Divisibility();
		// Test
		task.isDivisibleBy30("434534533434230");
		task.isDivisibleBy30("43453453343432323423230");
		task.isDivisibleBy30("895434230");
		task.isDivisibleBy30("32340");
	}
}

Output

 Given number (434534533434230) is not divisible by 30
 Given number (43453453343432323423230) is divisible by 30
 Given number (895434230) is not divisible by 30
 Given number (32340) is divisible by 30
import Foundation;
// Swift 4 program for
// Check that if large number is divisible by 30
class Divisibility
{
	func isDivisibleBy30(_ v: String)
	{
      	let num = Array(v);
		// A number is divisible by 30 when satisfies two conditions.
		// ➀ is divisible by 10.
		// ➁ is divisible by 3.
		var result: Bool = false;
		let n: Int = num.count;
		if (n == 1 && num[0] == "0")
		{
			// When number is zero
			result = true;
		}
		else if (n > 1)
		{
			// Check condition 1
			if (num[n - 1] == "0")
			{
				// When number is divisible by 10
				// 2nd condition check number is divisible by 3 or not
				var sum: Int = 0;
				var i: Int = 0;
				// Calculate sum of all digit
				while (i < n)
				{
					sum += (Int(UnicodeScalar(String(num[i]))!.value) - 48);
					i += 1;
				}
				if (sum % 3 == 0)
				{
					// When number is divisible by 3
					result = true;
				}
			}
		}
		if (result == true)
		{
			print(" Given number (", v ,") is divisible by 30");
		}
		else
		{
			print(" Given number (", v ,") is not divisible by 30");
		}
	}
}
func main()
{
	let task: Divisibility = Divisibility();
	// Test
	task.isDivisibleBy30("434534533434230");
	task.isDivisibleBy30("43453453343432323423230");
	task.isDivisibleBy30("895434230");
	task.isDivisibleBy30("32340");
}
main();

Output

 Given number ( 434534533434230 ) is not divisible by 30
 Given number ( 43453453343432323423230 ) is divisible by 30
 Given number ( 895434230 ) is not divisible by 30
 Given number ( 32340 ) is divisible by 30
// Kotlin program for
// Check that if large number is divisible by 30
class Divisibility
{
	fun isDivisibleBy30(num: String): Unit
	{
		// A number is divisible by 30 when satisfies two conditions.
		// ➀ is divisible by 10.
		// ➁ is divisible by 3.
		var result: Boolean = false;
		val n: Int = num.length;
		if (n == 1 && num.get(0) == '0')
		{
			// When number is zero
			result = true;
		}
		else if (n > 1)
		{
			// Check condition 1
			if (num.get(n - 1) == '0')
			{
				// When number is divisible by 10
				// 2nd condition check number is divisible by 3 or not
				var sum: Int = 0;
				var i: Int = 0;
				// Calculate sum of all digit
				while (i < n)
				{
					sum += (num.get(i).toInt() - 48);
					i += 1;
				}
				if (sum % 3 == 0)
				{
					// When number is divisible by 3
					result = true;
				}
			}
		}
		if (result == true)
		{
			println(" Given number (" + 
                    num + ") is divisible by 30");
		}
		else
		{
			println(" Given number (" + 
                    num + ") is not divisible by 30");
		}
	}
}
fun main(args: Array < String > ): Unit
{
	val task: Divisibility = Divisibility();
	// Test
	task.isDivisibleBy30("434534533434230");
	task.isDivisibleBy30("43453453343432323423230");
	task.isDivisibleBy30("895434230");
	task.isDivisibleBy30("32340");
}

Output

 Given number (434534533434230) is not divisible by 30
 Given number (43453453343432323423230) is divisible by 30
 Given number (895434230) is not divisible by 30
 Given number (32340) is divisible by 30

Comment

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