Check if a number is prime or not

Here given code implementation process.

//C Program
//Check if a number is prime or not
#include <stdio.h>

int is_prime(int n)
{
  if(n<=1)
  {
    return 0;
  }
  //Base case
  if(n==2 || n ==3 || n==5)
  {
    return 1;
  }
  //Check divisibility of a number
  for (int i = n/2; i > 1 ; --i)
  {
    if(n%i == 0)
    {
      return 0;
    }
  }
  
  return 1;
}

void prime_number(int number)
{


  if(is_prime(number)==1)
  {
    printf("%d is a prime number\n",number );
  }
  else
  {
    printf("%d not a prime number\n",number );
  }
  
}


int main()
{
  prime_number(37);
  prime_number(46);
  return 0;
}

Output

37 is a prime number
46 not a prime number
#include<iostream>

using namespace std;
/*
 C++ Program
 Check if a number is prime or not
*/
class MyNumber {
	public:
		bool is_prime(int n) {
			if (n <= 1) {
				return false;
			}
			//Base case

			if (n == 2 || n == 3 || n == 5) {
				return true;
			}
			for (int i = n / 2; i > 1; --i) {
				//Check divisibility of a number

				if (n % i == 0) {
					return false;
				}
			}
			return true;
		}
	void prime_number(int number) {
		if (this->is_prime(number)) {
			cout << number << " is a prime number\n";
		} else {
			cout << number << " number not a prime number\n";
		}
	}
};
int main() {
	MyNumber obj;
	//Test case
	obj.prime_number(37);
	obj.prime_number(46);
	return 0;
}

Output

37 is a prime number
46 number not a prime number
/*
  Java Program
  Check if a number is prime or not
*/

public class MyNumber {


  
  public boolean is_prime(int n)
  {
    if(n<=1)
    {
      return false;
    }
    //Base case
    if(n==2 || n ==3 || n==5)
    {
      return true;
    }
    
    for (int i = n/2; i > 1 ; --i)
    {
      //Check divisibility of a number
      if(n%i == 0)
      {
        return false;
      }
    }
    
    return true;
  }

  public void prime_number(int number)
  {


    if(is_prime(number))
    {
      System.out.print(number+" is a prime number\n");
    }
    else
    {
      System.out.print(number+" number not a prime number\n");
    }
    
  }


  public static void main(String[] args) {

    MyNumber obj = new MyNumber();
    //Test case
    obj.prime_number(37);
    obj.prime_number(46);

  }
}

Output

37 is a prime number
46 number not a prime number
/*
  C# Program
  Check if a number is prime or not
*/
using System;
public class MyNumber {



	public Boolean is_prime(int n) {
		if (n <= 1) {
			return false;
		}
		//Base case
		if (n == 2 || n == 3 || n == 5) {
			return true;
		}

		for (int i = n / 2; i > 1; --i) {
			//Check divisibility of a number
			if (n % i == 0) {
				return false;
			}
		}

		return true;
	}

	public void prime_number(int number) {


		if (is_prime(number)) {
			Console.Write(number + " is a prime number\n");
		} else {
			Console.Write(number + " number not a prime number\n");
		}

	}


	public static void Main(String[] args) {

		MyNumber obj = new MyNumber();
		//Test case
		obj.prime_number(37);
		obj.prime_number(46);

	}
}

Output

37 is a prime number
46 number not a prime number
# Python 3 Program
# Check if a number is prime or not
class MyNumber :
	def is_prime(self, n) :
		if (n <= 1) :
			return False
		
		#Base case
		if (n == 2 or n == 3 or n == 5) :
			return True
		
		i = n / 2
		while (i > 1) :
			#Check divisibility of a number
			if (n % i == 0) :
				return False
			
			i -= 1
		
		return True
	
	def prime_number(self, number) :
		if (self.is_prime(number)) :
			print(number ," is a prime number\n")
		else :
			print(number ," number not a prime number\n")
		
	

def main() :
	obj = MyNumber()
	obj.prime_number(37)
	obj.prime_number(46)


if __name__ == "__main__":
	main()

Output

37 is a prime number
46 number not a prime number
# Ruby Program 
# Check if a number is prime or not
class MyNumber 
	def is_prime(n) 
		if (n <= 1) 
			return false
		end
		#Base case

		if (n == 2 or n == 3 or n == 5) 
			return true
		end
		i = n / 2
		while (i > 1) 
			#Check divisibility of a number

			if (n % i == 0) 
				return false
			end
			i -= 1
		end
		return true
	end
	def prime_number(number) 
		if (self.is_prime(number)) 
			print(number ," is a prime number\n")
		else 
			print(number ," number not a prime number\n")
		end
	end
end
def main() 
	obj = MyNumber.new()
	obj.prime_number(37)
	obj.prime_number(46)
end
main()

Output

37 is a prime number
46 number not a prime number
/*
 Scala Program
 Check if a number is prime or not
*/
class MyNumber {
	def is_prime(n: Int): Boolean = {
		if (n <= 1) {
			return false;
		}
		//Base case

		if (n == 2 || n == 3 || n == 5) {
			return true;
		}
		var i: Int = n / 2;
		while (i > 1) {
			//Check divisibility of a number

			if (n % i == 0) {
				return false;
			}
			i -= 1;
		}
		return true;
	}
	def prime_number(number: Int): Unit = {
		if (this.is_prime(number)) {
			print(s" $number is a prime number\n");
		} else {
			print(s" $number not a prime number\n");
		}
	}
}
object Main {
	def main(args: Array[String]): Unit = {
		var obj: MyNumber = new MyNumber();obj.prime_number(37);obj.prime_number(46);
	}
}

Output

 37 is a prime number
 46 not a prime number
/*
  Swift 4 Program
  Check if a number is prime or not
*/
class MyNumber {
	func is_prime(_ n: Int) -> Bool {
		if (n <= 1) {
			return false;
		}
		//Base case

		if (n == 2 || n == 3 || n == 5) {
			return true;
		}
		var i: Int = n / 2;
		while (i > 1) {
			//Check divisibility of a number

			if (n % i == 0) {
				return false;
			}
			i -= 1;
		}
		return true;
	}
	func prime_number(_ number: Int) {
		if (self.is_prime(number)) {
			print(number ," is a prime number");
		} else {
			print(number ," number not a prime number");
		}
	}
}
func main() {
	let obj: MyNumber = MyNumber();
	obj.prime_number(37);
	obj.prime_number(46);
}
main();

Output

37  is a prime number
46  number not a prime number
<?php
/*
  Php Program
  Check if a number is prime or not
*/
class MyNumber {
	public 	function is_prime($n) {
		if ($n <= 1) {
			return false;
		}
		//Base case

		if ($n == 2 || $n == 3 || $n == 5) {
			return true;
		}
		for ($i = intval($n / 2); $i > 1; --$i) {
			//Check divisibility of a number

			if ($n % $i == 0) {
				return false;
			}
		}
		return true;
	}
	public 	function prime_number($number) {
		if ($this->is_prime($number)) {
			echo($number ." is a prime number\n");
		} else {
			echo($number ." number not a prime number\n");
		}
	}
};

function main() {
	$obj = new MyNumber();
	//Test case

	$obj->prime_number(37);
	$obj->prime_number(46);
}
main();

Output

37 is a prime number
46 number not a prime number
/*
 Node Js Program
 Check if a number is prime or not
*/
class MyNumber {
	is_prime(n) {
		if (n <= 1) {
			return false;
		}
		//Base case

		if (n == 2 || n == 3 || n == 5) {
			return true;
		}
		for (var i = parseInt( n / 2); i > 1; --i) {
			//Check divisibility of a number

			if (n % i == 0) {
				return false;
			}
		}
		return true;
	}
	prime_number(number) {
		if (this.is_prime(number)) {
			process.stdout.write(number + " is a prime number\n");
		} else {
			process.stdout.write(number + " number not a prime number\n");
		}
	}
}

function main(args) {
	var obj = new MyNumber();
	//Test case
	obj.prime_number(37);
	obj.prime_number(46)
}
main();

Output

37 is a prime number
46 number not a prime number


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