Check if a number is divisible by 4

Here given code implementation process.

//C Program 
//Check if a number is divisible by 4
#include <stdio.h>

//Assuming that given string text is valid number
void is_divisible_by_4(char text[], int size)
{
	if (size <= 0 || (text[0] == '-' && size == 1))
	{
		return;
	}
	int number = 0;
	int is_negative = 0;
	//Check negative number exist or not
	if (text[0] == '-')
	{
		is_negative = 1;
		if (size == 2)
		{
			//When single negative digit exist
			number = text[size - 1] - '0';
		}
	}
	else if (is_negative == 0 && size == 1)
	{
		//When get a single digit
		number = text[0] - '0';
	}
	if ((is_negative == 1 && size > 2) || (is_negative == 0 && size >= 2))
	{
		//When string contains 2 or more than 2 digit
		//Get second last element
		number = text[size - 2] - '0';
		//Get and combine second last number
		number = (number *10) + (text[size - 1] - '0');
	}
	//Display calculated result
	if (number % 4 == 0)
	{
		printf("\n [%s] Is divisible by 4", text);
	}
	else
	{
		printf("\n [%s] Is not divisible by 4", text);
	}
}
int main()
{
	//Define string number
	char num1[] = "12";
	int size = sizeof(num1) / sizeof(num1[0]) - 1;
	is_divisible_by_4(num1, size);
	char num2[] = "17";
	size = sizeof(num2) / sizeof(num2[0]) - 1;
	is_divisible_by_4(num2, size);
	//Define a negative string number
	char num3[] = "-8";
	size = sizeof(num3) / sizeof(num3[0]) - 1;
	is_divisible_by_4(num3, size);
	char num4[] = "-11";
	size = sizeof(num4) / sizeof(num4[0]) - 1;
	is_divisible_by_4(num4, size);
	//Define a large string number
	char num5[] = "900888800000074144";
	size = sizeof(num5) / sizeof(num5[0]) - 1;
	is_divisible_by_4(num5, size);
	return 0;
}

Output

 [12] Is divisible by 4
 [17] Is not divisible by 4
 [-8] Is divisible by 4
 [-11] Is not divisible by 4
 [900888800000074144] Is divisible by 4
//Java program
//Check if a number is divisible by 4
class MyString
{
	//Assuming that given string text is valid number
	public void is_divisible_by_4(String text)
	{
		int size = text.length();
		if (size <= 0 || (text.charAt(0) == '-' && size == 1))
		{
			return;
		}
		int number = 0;
		boolean is_negative = false;
		//Check negative number exist or not
		if (text.charAt(0) == '-')
		{
			is_negative = true;
			if (size == 2)
			{
				//When single negative digit exist
				number = text.charAt(size - 1) - '0';
			}
		}
		else if (is_negative == false && size == 1)
		{
			//When get a single digit
			number = text.charAt(0) - '0';
		}
		if ((is_negative == true && size > 2) || (is_negative == false && size >= 2))
		{
			//When string contains 2 or more than 2 digit
			//Get second last element
			number = text.charAt(size - 2) - '0';
			//Get and combine second last number
			number = (number * 10) + (text.charAt(size - 1) - '0');
		}
		//Display calculated result
		if (number % 4 == 0)
		{
			System.out.print("\n [" + text + "] Is divisible by 4");
		}
		else
		{
			System.out.print("\n [" + text + "] Is not divisible by 4");
		}
	}
	public static void main(String[] args)
	{
		MyString obj = new MyString();
		//Pass a positive string number
		obj.is_divisible_by_4("12");
		obj.is_divisible_by_4("17");
		//Pass a negative string number
		obj.is_divisible_by_4("-8");
		obj.is_divisible_by_4("-11");
		//Pass a large string number
		obj.is_divisible_by_4("900888800000074144");
	}
}

Output

 [12] Is divisible by 4
 [17] Is not divisible by 4
 [-8] Is divisible by 4
 [-11] Is not divisible by 4
 [900888800000074144] Is divisible by 4
//Include header file
#include <iostream>
#include<string.h>
using namespace std;

//C++ program
//Check if a number is divisible by 4
class MyString
{
	public:
		//Assuming that given string text is valid number
		void is_divisible_by_4(string text)
		{
			int size = text.size();
			if (size <= 0 || (text[0] == '-' && size == 1))
			{
				return;
			}
			int number = 0;
			bool is_negative = false;
			//Check negative number exist or not
			if (text[0] == '-')
			{
				is_negative = true;
				if (size == 2)
				{
					//When single negative digit exist
					number = text[size - 1] - '0';
				}
			}
			else if (is_negative == false && size == 1)
			{
				//When get a single digit
				number = text[0] - '0';
			}
			if ((is_negative == true && size > 2) || (is_negative == false && size >= 2))
			{
				//When string contains 2 or more than 2 digit
				//Get second last element
				number = text[size - 2] - '0';
				//Get and combine second last number
				number = (number * 10) + (text[size - 1] - '0');
			}
			//Display calculated result
			if (number % 4 == 0)
			{
				cout << "\n [" << text << "] Is divisible by 4";
			}
			else
			{
				cout << "\n [" << text << "] Is not divisible by 4";
			}
		}
};
int main()
{
	MyString obj = MyString();
	//Pass a positive string number
	obj.is_divisible_by_4("12");
	obj.is_divisible_by_4("17");
	//Pass a negative string number
	obj.is_divisible_by_4("-8");
	obj.is_divisible_by_4("-11");
	//Pass a large string number
	obj.is_divisible_by_4("900888800000074144");
	return 0;
}

Output

 [12] Is divisible by 4
 [17] Is not divisible by 4
 [-8] Is divisible by 4
 [-11] Is not divisible by 4
 [900888800000074144] Is divisible by 4
//Include namespace system
using System;

//C# program
//Check if a number is divisible by 4
class MyString
{
	//Assuming that given string text is valid number
	public void is_divisible_by_4(String text)
	{
		int size = text.Length;
		if (size <= 0 || (text[0] == '-' && size == 1))
		{
			return;
		}
		int number = 0;
		Boolean is_negative = false;
		//Check negative number exist or not
		if (text[0] == '-')
		{
			is_negative = true;
			if (size == 2)
			{
				//When single negative digit exist
				number = text[size - 1] - '0';
			}
		}
		else if (is_negative == false && size == 1)
		{
			//When get a single digit
			number = text[0] - '0';
		}
		if ((is_negative == true && size > 2) || (is_negative == false && size >= 2))
		{
			//When string contains 2 or more than 2 digit
			//Get second last element
			number = text[size - 2] - '0';
			//Get and combine second last number
			number = (number * 10) + (text[size - 1] - '0');
		}
		//Display calculated result
		if (number % 4 == 0)
		{
			Console.Write("\n [" + text + "] Is divisible by 4");
		}
		else
		{
			Console.Write("\n [" + text + "] Is not divisible by 4");
		}
	}
	public static void Main(String[] args)
	{
		MyString obj = new MyString();
		//Pass a positive string number
		obj.is_divisible_by_4("12");
		obj.is_divisible_by_4("17");
		//Pass a negative string number
		obj.is_divisible_by_4("-8");
		obj.is_divisible_by_4("-11");
		//Pass a large string number
		obj.is_divisible_by_4("900888800000074144");
	}
}

Output

 [12] Is divisible by 4
 [17] Is not divisible by 4
 [-8] Is divisible by 4
 [-11] Is not divisible by 4
 [900888800000074144] Is divisible by 4
<?php
//Php program
//Check if a number is divisible by 4
class MyString
{
	//Assuming that given string text is valid number
	public	function is_divisible_by_4($text)
	{
		$size = strlen($text);
		if ($size <= 0 || ($text[0] == '-' && $size == 1))
		{
			return;
		}
		$number = 0;
		$is_negative = false;
		//Check negative number exist or not
		if ($text[0] == '-')
		{
			$is_negative = true;
			if ($size == 2)
			{
				//When single negative digit exist
				$number = ord($text[$size - 1]) - ord('0');
			}
		}
		else if ($is_negative == false && $size == 1)
		{
			//When get a single digit
			$number = ord($text[0]) - ord('0');
		}
		if (($is_negative == true && $size > 2) || ($is_negative == false && $size >= 2))
		{
			//When string contains 2 or more than 2 digit
			//Get second last element
			$number = ord($text[$size - 2]) - ord('0');
			//Get and combine second last number
			$number = ($number * 10) + (ord($text[$size - 1]) - ord('0'));
		}
		//Display calculated result
		if ($number % 4 == 0)
		{
			echo "\n [". $text ."] Is divisible by 4";
		}
		else
		{
			echo "\n [". $text ."] Is not divisible by 4";
		}
	}
}

function main()
{
	$obj = new MyString();
	//Pass a positive string number
	$obj->is_divisible_by_4("12");
	$obj->is_divisible_by_4("17");
	//Pass a negative string number
	$obj->is_divisible_by_4("-8");
	$obj->is_divisible_by_4("-11");
	//Pass a large string number
	$obj->is_divisible_by_4("900888800000074144");
}
main();

Output

 [12] Is divisible by 4
 [17] Is not divisible by 4
 [-8] Is divisible by 4
 [-11] Is not divisible by 4
 [900888800000074144] Is divisible by 4
//Node Js program
//Check if a number is divisible by 4
class MyString
{
	//Assuming that given string text is valid number
	is_divisible_by_4(text)
	{
		var size = text.length;
		if (size <= 0 || (text[0] == '-' && size == 1))
		{
			return;
		}
		var number = 0;
		var is_negative = false;
		//Check negative number exist or not
		if (text[0] == '-')
		{
			is_negative = true;
			if (size == 2)
			{
				//When single negative digit exist
				number = (text[size - 1]).charCodeAt(0) - '0';
			}
		}
		else if (is_negative == false && size == 1)
		{
			//When get a single digit
			number = (text[0]).charCodeAt(0) - '0';
		}
		if ((is_negative == true && size > 2) || (is_negative == false && size >= 2))
		{
			//When string contains 2 or more than 2 digit
			//Get second last element
			number = (text[size - 2]).charCodeAt(0) - '0';
			//Get and combine second last number
			number = (number * 10) + ((text[size - 1]).charCodeAt(0) - '0');
		}
		//Display calculated result
		if (number % 4 == 0)
		{
			process.stdout.write("\n [" + text + "] Is divisible by 4");
		}
		else
		{
			process.stdout.write("\n [" + text + "] Is not divisible by 4");
		}
	}
}

function main()
{
	var obj = new MyString();
	//Pass a positive string number
	obj.is_divisible_by_4("12");
	obj.is_divisible_by_4("17");
	//Pass a negative string number
	obj.is_divisible_by_4("-8");
	obj.is_divisible_by_4("-11");
	//Pass a large string number
	obj.is_divisible_by_4("900888800000074144");
}
main();

Output

 [12] Is divisible by 4
 [17] Is not divisible by 4
 [-8] Is divisible by 4
 [-11] Is not divisible by 4
 [900888800000074144] Is divisible by 4
# Python 3 program
# Check if a number is divisible by 4
class MyString :
	# Assuming that given string text is valid number
	def is_divisible_by_4(self, text) :
		size = len(text)
		if (size <= 0 or(text[0] == '-'
				and size == 1)) :
			return
		
		number = 0
		is_negative = False
		# Check negative number exist or not
		if (text[0] == '-') :
			is_negative = True
			if (size == 2) :
				# When single negative digit exist
				number = ord(text[size - 1]) - ord('0')
			
		
		elif(is_negative == False and size == 1) :
			# When get a single digit
			number = ord(text[0]) - ord('0')
		
		if ((is_negative == True and size > 2) or(is_negative == False and size >= 2)) :
			# When string contains 2 or more than 2 digit
			# Get second last element
			number = ord(text[size - 2]) - ord('0')
			# Get and combine second last number
			number = (number * 10) + (ord(text[size - 1]) - ord('0'))
		
		# Display calculated result
		if (number % 4 == 0) :
			print(" [", text ,"] Is divisible by 4")
		else :
			print(" [", text ,"] Is not divisible by 4")
		
	

def main() :
	obj = MyString()
	# Pass a positive string number
	obj.is_divisible_by_4("12")
	obj.is_divisible_by_4("17")
	# Pass a negative string number
	obj.is_divisible_by_4("-8")
	obj.is_divisible_by_4("-11")
	# Pass a large string number
	obj.is_divisible_by_4("900888800000074144")

if __name__ == "__main__": main()

Output

 [ 12 ] Is divisible by 4
 [ 17 ] Is not divisible by 4
 [ -8 ] Is divisible by 4
 [ -11 ] Is not divisible by 4
 [ 900888800000074144 ] Is divisible by 4
# Ruby program
# Check if a number is divisible by 4
class MyString

	# Assuming that given string text is valid number
	def is_divisible_by_4(text)
	
		size = text.length()
		if (size <= 0 || (text[0] == '-' && size == 1))
		
			return
		end
		number = 0
		is_negative = false
		# Check negative number exist or not
		if (text[0] == '-')
		
			is_negative = true
			if (size == 2)
			
				# When single negative digit exist
				number = (text[size - 1]).ord - ('0').ord
			end
		elsif(is_negative == false && size == 1)
		
			# When get a single digit
			number = (text[0]).ord - ('0').ord
		end
		if ((is_negative == true && size > 2) || (is_negative == false && size >= 2))
		
			# When string contains 2 or more than 2 digit
			# Get second last element
			number = (text[size - 2]).ord - ('0').ord
			# Get and combine second last number
			number = (number * 10) + ((text[size - 1]).ord - ('0').ord)
		end
		# Display calculated result
		if (number % 4 == 0)
		
			print("\n [", text ,"] Is divisible by 4")
		else
		
			print("\n [", text ,"] Is not divisible by 4")
		end
	end
end
def main()

	obj = MyString.new()
	# Pass a positive string number
	obj.is_divisible_by_4("12")
	obj.is_divisible_by_4("17")
	# Pass a negative string number
	obj.is_divisible_by_4("-8")
	obj.is_divisible_by_4("-11")
	# Pass a large string number
	obj.is_divisible_by_4("900888800000074144")
end
main()

Output

 [12] Is divisible by 4
 [17] Is not divisible by 4
 [-8] Is divisible by 4
 [-11] Is not divisible by 4
 [900888800000074144] Is divisible by 4
//Scala program
//Check if a number is divisible by 4
class MyString
{
	//Assuming that given string text is valid number
	def is_divisible_by_4(text: String): Unit = {
		var size: Int = text.length();
		if (size <= 0 || (text(0) == '-' && size == 1))
		{
			return;
		}
		var number: Int = 0;
		var is_negative: Boolean = false;
		//Check negative number exist or not
		if (text(0) == '-')
		{
			is_negative = true;
			if (size == 2)
			{
				//When single negative digit exist
				number = text(size - 1) - '0';
			}
		}
		else if (is_negative == false && size == 1)
		{
			//When get a single digit
			number = text(0) - '0';
		}
		if ((is_negative == true && size > 2) || (is_negative == false && size >= 2))
		{
			//When string contains 2 or more than 2 digit
			//Get second last element
			number = text(size - 2) - '0';
			//Get and combine second last number
			number = (number * 10) + (text(size - 1) - '0');
		}
		//Display calculated result
		if (number % 4 == 0)
		{
			print("\n [" + text + "] Is divisible by 4");
		}
		else
		{
			print("\n [" + text + "] Is not divisible by 4");
		}
	}
}
object Main
{
	def main(args: Array[String]): Unit = {
		var obj: MyString = new MyString();
		//Pass a positive string number
		obj.is_divisible_by_4("12");
		obj.is_divisible_by_4("17");
		//Pass a negative string number
		obj.is_divisible_by_4("-8");
		obj.is_divisible_by_4("-11");
		//Pass a large string number
		obj.is_divisible_by_4("900888800000074144");
	}
}

Output

 [12] Is divisible by 4
 [17] Is not divisible by 4
 [-8] Is divisible by 4
 [-11] Is not divisible by 4
 [900888800000074144] Is divisible by 4
//Swift program
//Check if a number is divisible by 4
class MyString
{
	//Assuming that given string text is valid number
	func is_divisible_by_4(_ data: String)
	{
      	let text = Array(data);
		let size: Int = text.count;
		if (size <= 0 || (text[0] == "-" && size == 1))
		{
			return;
		}
		var number: Int = 0;
		var is_negative: Bool = false;
        let zero = Int(UnicodeScalar("0")!.value);
		//Check negative number exist or not
		if (text[0] == "-")
		{
			is_negative = true;
			if (size == 2)
			{
				//When single negative digit exist
				number =  Int(UnicodeScalar(String(text[1]))!.value)  - zero;
			}
		}
		else if (is_negative == false && size == 1)
		{
			//When get a single digit
			number =  Int(UnicodeScalar(String(text[0]))!.value)  - zero;
		}
		if ((is_negative == true && size > 2) || (is_negative == false && size >= 2))
		{
			//When string contains 2 or more than 2 digit
			//Get second last element
			number = Int(UnicodeScalar(String(text[size-2]))!.value) - zero;
			//Get and combine second last number
			number = (number * 10) + (Int(UnicodeScalar(String(text[size-1]))!.value) - zero);
		}
		//Display calculated result
		if (number % 4 == 0)
		{
			print(" [", data ,"] Is divisible by 4");
		}
		else
		{
			print(" [", data ,"] Is not divisible by 4");
		}
	}
}
func main()
{
	let obj: MyString = MyString();
	//Pass a positive string number
	obj.is_divisible_by_4("12");
	obj.is_divisible_by_4("17");
	//Pass a negative string number
	obj.is_divisible_by_4("-8");
	obj.is_divisible_by_4("-11");
	//Pass a large string number
	obj.is_divisible_by_4("900888800000074144");
}
main();

Output

 [ 12 ] Is divisible by 4
 [ 17 ] Is not divisible by 4
 [ -8 ] Is divisible by 4
 [ -11 ] Is not divisible by 4
 [ 900888800000074144 ] Is divisible by 4


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