Check if a given number is sparse or not

Here given code implementation process.

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


//Handle request of sparse number
void is_sparse(int number)
{
  if(((number>>1) & number) >=1)
  {
    printf("%d is not a sparse number\n",number);
  }
  else
  {
   
    printf("%d is sparse number\n",number);
  }
}

int main()
{
  //Test case
  is_sparse(52);
  is_sparse(12);
  is_sparse(5);
  is_sparse(11);
  is_sparse(10);
  return 0;
}

Output

52 is not a sparse number
12 is not a sparse number
5 is sparse number
11 is not a sparse number
10 is sparse number
/*
 C++ Program
 Check if a given number is sparse or not
*/
#include<iostream>

using namespace std;

class MyNumber {
	public:

		//Handle request of sparse number
		void is_sparse(int number) {
			//Check the adjacent 1 in it's binary representation

			if (((number >> 1) & number) >= 1) {
				cout << number << " is not a sparse number\n";
			} else {
				cout << number << " is sparse number\n";
			}
		}
};
int main() {
	MyNumber obj;
	//Test case
	obj.is_sparse(52);
	obj.is_sparse(12);
	obj.is_sparse(5);
	obj.is_sparse(11);
	obj.is_sparse(10);
	return 0;
}

Output

52 is not a sparse number
12 is not a sparse number
5 is sparse number
11 is not a sparse number
10 is sparse number
/*
  Java Program
  Check if a given number is sparse or not
*/

public class MyNumber {


  //Handle request of sparse number
  public void is_sparse(int number)
  {
    //Check the adjacent 1 in it's binary representation
    if(((number>>1) & number) >=1)
    {
      System.out.print(number+" is not a sparse number\n");
    }
    else
    {
      System.out.print(number+" is sparse number\n");
    }
  }

  public static void main(String[] args) {

    MyNumber obj = new MyNumber();
    //Test case
    obj.is_sparse(52);
    obj.is_sparse(12);
    obj.is_sparse(5);
    obj.is_sparse(11);
    obj.is_sparse(10);
  }
}

Output

52 is not a sparse number
12 is not a sparse number
5 is sparse number
11 is not a sparse number
10 is sparse number
/*
  C# Program
  Check if a given number is sparse or not
*/
using System;
public class MyNumber {


	//Handle request of sparse number
	public void is_sparse(int number) {
		//Check the adjacent 1 in it's binary representation
		if (((number >> 1) & number) >= 1) {
			Console.Write(number + " is not a sparse number\n");
		} else {
			Console.Write(number + " is sparse number\n");
		}
	}

	public static void Main(String[] args) {

		MyNumber obj = new MyNumber();
		//Test case
		obj.is_sparse(52);
		obj.is_sparse(12);
		obj.is_sparse(5);
		obj.is_sparse(11);
		obj.is_sparse(10);
	}
}

Output

52 is not a sparse number
12 is not a sparse number
5 is sparse number
11 is not a sparse number
10 is sparse number
# Python 3 Program
# Check if a given number is sparse or not
class MyNumber :
	#Handle request of sparse number
	def is_sparse(self, number) :
		#Check the adjacent 1 in it's binary representation
		if (((number >> 1) & number) >= 1) :
			print(number ," is not a sparse number\n")
		else :
			print(number ," is sparse number\n")
		
	

def main() :
	obj = MyNumber()
	#Test case
	obj.is_sparse(52)
	obj.is_sparse(12)
	obj.is_sparse(5)
	obj.is_sparse(11)
	obj.is_sparse(10)


if __name__ == "__main__":
	main()

Output

52 is not a sparse number
12 is not a sparse number
5 is sparse number
11 is not a sparse number
10 is sparse number
# Ruby Program 
# Check if a given number is sparse or not
class MyNumber 
	#Handle request of sparse number
	def is_sparse(number) 
		#Check the adjacent 1 in it's binary representation

		if (((number >> 1) & number) >= 1) 
			print(number ," is not a sparse number\n")
		else 
			print(number ," is sparse number\n")
		end
	end
end
def main() 
	obj = MyNumber.new()
	#Test case
	obj.is_sparse(52)
	obj.is_sparse(12)
	obj.is_sparse(5)
	obj.is_sparse(11)
	obj.is_sparse(10)
end
main()

Output

52 is not a sparse number
12 is not a sparse number
5 is sparse number
11 is not a sparse number
10 is sparse number
/*
 Scala Program
 Check if a given number is sparse or not
*/
class MyNumber {
	//Handle request of sparse number
	def is_sparse(number: Int): Unit = {
		//Check the adjacent 1 in it's binary representation

		if (((number >> 1) & number) >= 1) {
			print(s"$number is not a sparse number\n");
		} else {
			print(s"$number is sparse number\n");
		}
	}
}
object Main {
	def main(args: Array[String]): Unit = {
		var obj: MyNumber = new MyNumber();
		//Test case
		obj.is_sparse(52);
        obj.is_sparse(12);
        obj.is_sparse(5);
        obj.is_sparse(11);
        obj.is_sparse(10);
	}
}

Output

52 is not a sparse number
12 is not a sparse number
5 is sparse number
11 is not a sparse number
10 is sparse number
/*
  Swift 4 Program
  Check if a given number is sparse or not
*/
class MyNumber {
	//Handle request of sparse number
	func is_sparse(_ number: Int) {
		//Check the adjacent 1 in it's binary representation

		if (((number >> 1) & number) >= 1) {
			print(number ," is not a sparse number");
		} else {
			print(number ," is sparse number");
		}
	}
}
func main() {
	let obj: MyNumber = MyNumber();
	//Test case
	obj.is_sparse(52);
	obj.is_sparse(12);
	obj.is_sparse(5);
	obj.is_sparse(11);
	obj.is_sparse(10);
}
main();

Output

52  is not a sparse number
12  is not a sparse number
5  is sparse number
11  is not a sparse number
10  is sparse number
<?php
/*
  Php Program
  Check if a given number is sparse or not
*/
class MyNumber {
	//Handle request of sparse number

	public 	function is_sparse($number) {
		//Check the adjacent 1 in it's binary representation

		if ((($number >> 1) & $number) >= 1) {
			echo($number ." is not a sparse number\n");
		} else {
			echo($number ." is sparse number\n");
		}
	}
};
function main() {
	$obj = new MyNumber();
	//Test case

	$obj->is_sparse(52);
	$obj->is_sparse(12);
	$obj->is_sparse(5);
	$obj->is_sparse(11);
	$obj->is_sparse(10);
}
main();

Output

52 is not a sparse number
12 is not a sparse number
5 is sparse number
11 is not a sparse number
10 is sparse number
/*
 Node Js Program
 Check if a given number is sparse or not
*/
class MyNumber {
	//Handle request of sparse number
	is_sparse(number) {
		//Check the adjacent 1 in it's binary representation

		if (((number >> 1) & number) >= 1) {
			process.stdout.write(number + " is not a sparse number\n");
		} else {
			process.stdout.write(number + " is sparse number\n");
		}
	}
}

function main(args) {
	var obj = new MyNumber();
	//Test case
	obj.is_sparse(52);
	obj.is_sparse(12);
	obj.is_sparse(5);
	obj.is_sparse(11);
	obj.is_sparse(10)
}
main();

Output

52 is not a sparse number
12 is not a sparse number
5 is sparse number
11 is not a sparse number
10 is sparse 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