Composite Number

Here given code implementation process.

// C Program 
// Composite Number
#include <stdio.h>

// Check that whether given number is prime or not
int isPrime(int num)
{
	if (num == 2 || num == 3 || num == 5)
	{
		return 1;
	}
	if (num <= 1 || (num % 2 == 0) || (num % 3 == 0) || (num % 5 == 0))
	{
		return 0;
	}
	int i = 11;
	while ((i *i) <= num)
	{
		if (num % i == 0)
		{
			//When number is divisible of current i value
			return 0;
		}
		else if (num % (i + 2) == 0)
		{
			//When number is divisible of current i + 2 value
			return 0;
		}
		i = i + 6;
	}
	return 1;
}
void isComposite(int number)
{
	int result = 0;
	if (number > 3 && (isPrime(number) == 0))
	{
		// When number is greater than 3 and it's not prime number
		result = 1;
	}
	if (result == 1)
	{
		printf("  Number %d is Composite \n", number);
	}
	else
	{
		printf("  Number %d is not Composite\n", number);
	}
}
int main(int argc, char const *argv[])
{
	// Test Cases
	isComposite(4);
	isComposite(13);
	isComposite(22);
	isComposite(15);
	isComposite(31);
	isComposite(9);
	return 0;
}

Output

  Number 4 is Composite
  Number 13 is not Composite
  Number 22 is Composite
  Number 15 is Composite
  Number 31 is not Composite
  Number 9 is Composite
/*
    Java Program
    Composite Number
*/
public class CompositeNumber
{
	// Check that whether given number is prime or not
	public boolean isPrime(int num)
	{
		if (num == 2 || num == 3 || num == 5)
		{
			return true;
		}
		if (num <= 1 || (num % 2 == 0) || (num % 3 == 0) || (num % 5 == 0))
		{
			return false;
		}
		int i = 11;
		while ((i * i) <= num)
		{
			if (num % i == 0)
			{
				//When number is divisible of current i value
				return false;
			}
			else if (num % (i + 2) == 0)
			{
				//When number is divisible of current i + 2 value
				return false;
			}
			i = i + 6;
		}
		return true;
	}
	public void isComposite(int number)
	{
		boolean result = false;
		if (number > 3 && (isPrime(number) == false))
		{
			// When number is greater than 3 and it's not prime number
			result = true;
		}
		if (result == true)
		{
			System.out.print(" Number " + number + " is Composite \n");
		}
		else
		{
			System.out.print(" Number " + number + " is not Composite\n");
		}
	}
	public static void main(String[] args)
	{
		CompositeNumber task = new CompositeNumber();
		// Test Cases
		task.isComposite(4);
		task.isComposite(13);
		task.isComposite(22);
		task.isComposite(15);
		task.isComposite(31);
		task.isComposite(9);
	}
}

Output

 Number 4 is Composite
 Number 13 is not Composite
 Number 22 is Composite
 Number 15 is Composite
 Number 31 is not Composite
 Number 9 is Composite
// Include header file
#include <iostream>
using namespace std;

/*
    C++ Program
    Composite Number
*/

class CompositeNumber
{
	public:
		// Check that whether given number is prime or not
		bool isPrime(int num)
		{
			if (num == 2 || num == 3 || num == 5)
			{
				return true;
			}
			if (num <= 1 || (num % 2 == 0) || (num % 3 == 0) || (num % 5 == 0))
			{
				return false;
			}
			int i = 11;
			while ((i *i) <= num)
			{
				if (num % i == 0)
				{
					//When number is divisible of current i value
					return false;
				}
				else if (num % (i + 2) == 0)
				{
					//When number is divisible of current i + 2 value
					return false;
				}
				i = i + 6;
			}
			return true;
		}
	void isComposite(int number)
	{
		bool result = false;
		if (number > 3 && (this->isPrime(number) == false))
		{
			// When number is greater than 3 and it's not prime number
			result = true;
		}
		if (result == true)
		{
			cout << " Number " << number << " is Composite \n";
		}
		else
		{
			cout << " Number " << number << " is not Composite\n";
		}
	}
};
int main()
{
	CompositeNumber task = CompositeNumber();
	// Test Cases
	task.isComposite(4);
	task.isComposite(13);
	task.isComposite(22);
	task.isComposite(15);
	task.isComposite(31);
	task.isComposite(9);
	return 0;
}

Output

 Number 4 is Composite
 Number 13 is not Composite
 Number 22 is Composite
 Number 15 is Composite
 Number 31 is not Composite
 Number 9 is Composite
// Include namespace system
using System;
/*
    C# Program
    Composite Number
*/
public class CompositeNumber
{
	// Check that whether given number is prime or not
	public Boolean isPrime(int num)
	{
		if (num == 2 || num == 3 || num == 5)
		{
			return true;
		}
		if (num <= 1 || (num % 2 == 0) || (num % 3 == 0) || (num % 5 == 0))
		{
			return false;
		}
		int i = 11;
		while ((i * i) <= num)
		{
			if (num % i == 0)
			{
				//When number is divisible of current i value
				return false;
			}
			else if (num % (i + 2) == 0)
			{
				//When number is divisible of current i + 2 value
				return false;
			}
			i = i + 6;
		}
		return true;
	}
	public void isComposite(int number)
	{
		Boolean result = false;
		if (number > 3 && (isPrime(number) == false))
		{
			// When number is greater than 3 and it's not prime number
			result = true;
		}
		if (result == true)
		{
			Console.Write(" Number " + number + " is Composite \n");
		}
		else
		{
			Console.Write(" Number " + number + " is not Composite\n");
		}
	}
	public static void Main(String[] args)
	{
		CompositeNumber task = new CompositeNumber();
		// Test Cases
		task.isComposite(4);
		task.isComposite(13);
		task.isComposite(22);
		task.isComposite(15);
		task.isComposite(31);
		task.isComposite(9);
	}
}

Output

 Number 4 is Composite
 Number 13 is not Composite
 Number 22 is Composite
 Number 15 is Composite
 Number 31 is not Composite
 Number 9 is Composite
<?php
/*
    Php Program
    Composite Number
*/
class CompositeNumber
{
	// Check that whether given number is prime or not
	public	function isPrime($num)
	{
		if ($num == 2 || $num == 3 || $num == 5)
		{
			return true;
		}
		if ($num <= 1 || ($num % 2 == 0) || ($num % 3 == 0) || ($num % 5 == 0))
		{
			return false;
		}
		$i = 11;
		while (($i * $i) <= $num)
		{
			if ($num % $i == 0)
			{
				//When number is divisible of current i value
				return false;
			}
			else if ($num % ($i + 2) == 0)
			{
				//When number is divisible of current i + 2 value
				return false;
			}
			$i = $i + 6;
		}
		return true;
	}
	public	function isComposite($number)
	{
		$result = false;
		if ($number > 3 && ($this->isPrime($number) == false))
		{
			// When number is greater than 3 and it's not prime number
			$result = true;
		}
		if ($result == true)
		{
			echo " Number ". $number ." is Composite \n";
		}
		else
		{
			echo " Number ". $number ." is not Composite\n";
		}
	}
}

function main()
{
	$task = new CompositeNumber();
	// Test Cases
	$task->isComposite(4);
	$task->isComposite(13);
	$task->isComposite(22);
	$task->isComposite(15);
	$task->isComposite(31);
	$task->isComposite(9);
}
main();

Output

 Number 4 is Composite
 Number 13 is not Composite
 Number 22 is Composite
 Number 15 is Composite
 Number 31 is not Composite
 Number 9 is Composite
/*
    Node Js Program
    Composite Number
*/
class CompositeNumber
{
	// Check that whether given number is prime or not
	isPrime(num)
	{
		if (num == 2 || num == 3 || num == 5)
		{
			return true;
		}
		if (num <= 1 || (num % 2 == 0) || (num % 3 == 0) || (num % 5 == 0))
		{
			return false;
		}
		var i = 11;
		while ((i * i) <= num)
		{
			if (num % i == 0)
			{
				//When number is divisible of current i value
				return false;
			}
			else if (num % (i + 2) == 0)
			{
				//When number is divisible of current i + 2 value
				return false;
			}
			i = i + 6;
		}
		return true;
	}
	isComposite(number)
	{
		var result = false;
		if (number > 3 && (this.isPrime(number) == false))
		{
			// When number is greater than 3 and it's not prime number
			result = true;
		}
		if (result == true)
		{
			process.stdout.write(" Number " + number + " is Composite \n");
		}
		else
		{
			process.stdout.write(" Number " + number + " is not Composite\n");
		}
	}
}

function main()
{
	var task = new CompositeNumber();
	// Test Cases
	task.isComposite(4);
	task.isComposite(13);
	task.isComposite(22);
	task.isComposite(15);
	task.isComposite(31);
	task.isComposite(9);
}
main();

Output

 Number 4 is Composite
 Number 13 is not Composite
 Number 22 is Composite
 Number 15 is Composite
 Number 31 is not Composite
 Number 9 is Composite
#  Python 3 Program
#  Composite Number

class CompositeNumber :
	#  Check that whether given number is prime or not
	def isPrime(self, num) :
		if (num == 2 or num == 3 or num == 5) :
			return True
		
		if (num <= 1 or(num % 2 == 0) or(num % 3 == 0) or(num % 5 == 0)) :
			return False
		
		i = 11
		while ((i * i) <= num) :
			if (num % i == 0) :
				# When number is divisible of current i value
				return False
			
			elif(num % (i + 2) == 0) :
				# When number is divisible of current i + 2 value
				return False
			
			i = i + 6
		
		return True
	
	def isComposite(self, number) :
		result = False
		if (number > 3 and(self.isPrime(number) == False)) :
			#  When number is greater than 3 and it's not prime number
			result = True
		
		if (result == True) :
			print(" Number ", number ," is Composite ")
		else :
			print(" Number ", number ," is not Composite")
		
	

def main() :
	task = CompositeNumber()
	#  Test Cases
	task.isComposite(4)
	task.isComposite(13)
	task.isComposite(22)
	task.isComposite(15)
	task.isComposite(31)
	task.isComposite(9)

if __name__ == "__main__": main()

Output

 Number  4  is Composite
 Number  13  is not Composite
 Number  22  is Composite
 Number  15  is Composite
 Number  31  is not Composite
 Number  9  is Composite
#  Ruby Program
#  Composite Number

class CompositeNumber 
	#  Check that whether given number is prime or not
	def isPrime(num) 
		if (num == 2 || num == 3 || num == 5) 
			return true
		end

		if (num <= 1 || (num % 2 == 0) || (num % 3 == 0) || (num % 5 == 0)) 
			return false
		end

		i = 11
		while ((i * i) <= num) 
			if (num % i == 0) 
				# When number is divisible of current i value
				return false
			elsif(num % (i + 2) == 0) 
				# When number is divisible of current i + 2 value
				return false
			end

			i = i + 6
		end

		return true
	end

	def isComposite(number) 
		result = false
		if (number > 3 && (self.isPrime(number) == false)) 
			#  When number is greater than 3 and it's not prime number
			result = true
		end

		if (result == true) 
			print(" Number ", number ," is Composite \n")
		else 
			print(" Number ", number ," is not Composite\n")
		end

	end

end

def main() 
	task = CompositeNumber.new()
	#  Test Cases
	task.isComposite(4)
	task.isComposite(13)
	task.isComposite(22)
	task.isComposite(15)
	task.isComposite(31)
	task.isComposite(9)
end

main()

Output

 Number 4 is Composite 
 Number 13 is not Composite
 Number 22 is Composite 
 Number 15 is Composite 
 Number 31 is not Composite
 Number 9 is Composite 
/*
    Scala Program
    Composite Number
*/
class CompositeNumber
{
	// Check that whether given number is prime or not
	def isPrime(num: Int): Boolean = {
		if (num == 2 || num == 3 || num == 5)
		{
			return true;
		}
		if (num <= 1 || (num % 2 == 0) || (num % 3 == 0) || (num % 5 == 0))
		{
			return false;
		}
		var i: Int = 11;
		while ((i * i) <= num)
		{
			if (num % i == 0)
			{
				//When number is divisible of current i value
				return false;
			}
			else if (num % (i + 2) == 0)
			{
				//When number is divisible of current i + 2 value
				return false;
			}
			i = i + 6;
		}
		return true;
	}
	def isComposite(number: Int): Unit = {
		var result: Boolean = false;
		if (number > 3 && (this.isPrime(number) == false))
		{
			// When number is greater than 3 and it's not prime number
			result = true;
		}
		if (result == true)
		{
			print(" Number " + number + " is Composite \n");
		}
		else
		{
			print(" Number " + number + " is not Composite\n");
		}
	}
}
object Main
{
	def main(args: Array[String]): Unit = {
		var task: CompositeNumber = new CompositeNumber();
		// Test Cases
		task.isComposite(4);
		task.isComposite(13);
		task.isComposite(22);
		task.isComposite(15);
		task.isComposite(31);
		task.isComposite(9);
	}
}

Output

 Number 4 is Composite
 Number 13 is not Composite
 Number 22 is Composite
 Number 15 is Composite
 Number 31 is not Composite
 Number 9 is Composite
/*
    Swift 4 Program
    Composite Number
*/
class CompositeNumber
{
	// Check that whether given number is prime or not
	func isPrime(_ num: Int)->Bool
	{
		if (num == 2 || num == 3 || num == 5)
		{
			return true;
		}
		if (num <= 1 || (num % 2 == 0) || (num % 3 == 0) || (num % 5 == 0))
		{
			return false;
		}
		var i: Int = 11;
		while ((i * i) <= num)
		{
			if (num % i == 0)
			{
				//When number is divisible of current i value
				return false;
			}
			else if (num % (i + 2) == 0)
			{
				//When number is divisible of current i + 2 value
				return false;
			}
			i = i + 6;
		}
		return true;
	}
	func isComposite(_ number: Int)
	{
		var result: Bool = false;
		if (number > 3 && (self.isPrime(number) == false))
		{
			// When number is greater than 3 and it"s not prime number
			result = true;
		}
		if (result == true)
		{
			print(" Number ", number ," is Composite ");
		}
		else
		{
			print(" Number ", number ," is not Composite");
		}
	}
}
func main()
{
	let task: CompositeNumber = CompositeNumber();
	// Test Cases
	task.isComposite(4);
	task.isComposite(13);
	task.isComposite(22);
	task.isComposite(15);
	task.isComposite(31);
	task.isComposite(9);
}
main();

Output

 Number  4  is Composite
 Number  13  is not Composite
 Number  22  is Composite
 Number  15  is Composite
 Number  31  is not Composite
 Number  9  is Composite
/*
    Kotlin Program
    Composite Number
*/
class CompositeNumber
{
	// Check that whether given number is prime or not
	fun isPrime(num: Int): Boolean
	{
		if (num == 2 || num == 3 || num == 5)
		{
			return true;
		}
		if (num <= 1 || (num % 2 == 0) || (num % 3 == 0) || (num % 5 == 0))
		{
			return false;
		}
		var i: Int = 11;
		while ((i * i) <= num)
		{
			if (num % i == 0)
			{
				//When number is divisible of current i value
				return false;
			}
			else if (num % (i + 2) == 0)
			{
				//When number is divisible of current i + 2 value
				return false;
			}
			i = i + 6;
		}
		return true;
	}
	fun isComposite(number: Int): Unit
	{
		var result: Boolean = false;
		if (number > 3 && (this.isPrime(number) == false))
		{
			// When number is greater than 3 and it's not prime number
			result = true;
		}
		if (result == true)
		{
			print(" Number " + number + " is Composite \n");
		}
		else
		{
			print(" Number " + number + " is not Composite\n");
		}
	}
}
fun main(args: Array < String > ): Unit
{
	var task: CompositeNumber = CompositeNumber();
	// Test Cases
	task.isComposite(4);
	task.isComposite(13);
	task.isComposite(22);
	task.isComposite(15);
	task.isComposite(31);
	task.isComposite(9);
}

Output

 Number 4 is Composite
 Number 13 is not Composite
 Number 22 is Composite
 Number 15 is Composite
 Number 31 is not Composite
 Number 9 is Composite


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