Posted on by Kalkicode
Code Mathematics

Check whether the given number is buzz number or not

The Buzz number problem involves determining whether a given number is a Buzz number or not. A Buzz number is a positive integer that meets a specific criterion involving its digits. To check whether a number is a Buzz number, we need to examine the last digit of the number and check if the number is divisible by 7 or if the last digit is 7.

Problem Statement

A Buzz number is a positive integer where at least one of the following conditions holds:

  1. The number is divisible by 7.
  2. The last digit of the number is 7.

Example

Let's take the number 126 as an example to illustrate whether it's a Buzz number or not.

  • 126 is not divisible by 7.
  • The last digit of 126 is not 7.

Since none of the conditions are satisfied, 126 is not a Buzz number.

Idea to Solve

To solve this problem, we only need to implement a function called isBuzzNo that takes a number as input and checks whether it's a Buzz number or not based on the conditions mentioned. We don't need a separate function to check for prime numbers, as was the case in the Moran number problem.

Standard Pseudocode

function isBuzzNo(number)
	if number is divisible by 7 or last digit of number is 7
		return true
	else
		return false

Algorithm with Explanation

  1. Implement the isBuzzNo function as described in the pseudocode. It checks whether the number is divisible by 7 or if the last digit of the number is 7.
  2. In the main function, call isBuzzNo for each test case (numbers 126, 123, 2120, 107, and 170). Print whether each number is a Buzz number or not based on the returned value from isBuzzNo.

Program Solution

// C program for 
// Check whether the given number is buzz number or not
#include <stdio.h>

void isBuzzNo(int number)
{
	if (number % 7 == 0 || number % 10 == 7)
	{
		// When last digit is 7  
		// or number is divisible by 7
		printf(" Number %d is buzz number\n", number);
	}
	else
	{
		printf(" Number %d is not buzz number\n", number);
	}
}
int main(int argc, char
	const *argv[])
{
	// Test Cases
	isBuzzNo(126);
	isBuzzNo(123);
	isBuzzNo(2120);
	isBuzzNo(107);
	isBuzzNo(170);
	return 0;
}

input

 Number 126 is buzz number
 Number 123 is not buzz number
 Number 2120 is not buzz number
 Number 107 is buzz number
 Number 170 is not buzz number
/*
  Java Program for
  Check whether the given number is buzz number or not
*/
public class BuzzNumber
{
	public void isBuzzNo(int number)
	{
		if (number % 7 == 0 || number % 10 == 7)
		{
			// When last digit is 7  
			// or number is divisible by 7
			System.out.println(" Number " + number + " is buzz number");
		}
		else
		{
			System.out.println(" Number " + number + " is not buzz number");
		}
	}
	public static void main(String[] args)
	{
		BuzzNumber task = new BuzzNumber();
		// Test Cases
		task.isBuzzNo(126);
		task.isBuzzNo(123);
		task.isBuzzNo(2120);
		task.isBuzzNo(107);
		task.isBuzzNo(170);
	}
}

input

 Number 126 is buzz number
 Number 123 is not buzz number
 Number 2120 is not buzz number
 Number 107 is buzz number
 Number 170 is not buzz number
// Include header file
#include <iostream>

using namespace std;
/*
  C++ Program for
  Check whether the given number is buzz number or not
*/
class BuzzNumber
{
	public: void isBuzzNo(int number)
	{
		if (number % 7 == 0 || number % 10 == 7)
		{
			// When last digit is 7  
			// or number is divisible by 7
			cout << " Number " << number << " is buzz number" << endl;
		}
		else
		{
			cout << " Number " << number << " is not buzz number" << endl;
		}
	}
};
int main()
{
	BuzzNumber *task = new BuzzNumber();
	// Test Cases
	task->isBuzzNo(126);
	task->isBuzzNo(123);
	task->isBuzzNo(2120);
	task->isBuzzNo(107);
	task->isBuzzNo(170);
	return 0;
}

input

 Number 126 is buzz number
 Number 123 is not buzz number
 Number 2120 is not buzz number
 Number 107 is buzz number
 Number 170 is not buzz number
// Include namespace system
using System;
/*
  Csharp Program for
  Check whether the given number is buzz number or not
*/
public class BuzzNumber
{
	public void isBuzzNo(int number)
	{
		if (number % 7 == 0 || number % 10 == 7)
		{
			// When last digit is 7  
			// or number is divisible by 7
			Console.WriteLine(" Number " + number + " is buzz number");
		}
		else
		{
			Console.WriteLine(" Number " + number + " is not buzz number");
		}
	}
	public static void Main(String[] args)
	{
		BuzzNumber task = new BuzzNumber();
		// Test Cases
		task.isBuzzNo(126);
		task.isBuzzNo(123);
		task.isBuzzNo(2120);
		task.isBuzzNo(107);
		task.isBuzzNo(170);
	}
}

input

 Number 126 is buzz number
 Number 123 is not buzz number
 Number 2120 is not buzz number
 Number 107 is buzz number
 Number 170 is not buzz number
<?php
/*
  Php Program for
  Check whether the given number is buzz number or not
*/
class BuzzNumber
{
	public	function isBuzzNo($number)
	{
		if ($number % 7 == 0 || $number % 10 == 7)
		{
			// When last digit is 7  
			// or number is divisible by 7
			echo " Number ".$number.
			" is buzz number".
			"\n";
		}
		else
		{
			echo " Number ".$number.
			" is not buzz number".
			"\n";
		}
	}
}

function main()
{
	$task = new BuzzNumber();
	// Test Cases
	$task->isBuzzNo(126);
	$task->isBuzzNo(123);
	$task->isBuzzNo(2120);
	$task->isBuzzNo(107);
	$task->isBuzzNo(170);
}
main();

input

 Number 126 is buzz number
 Number 123 is not buzz number
 Number 2120 is not buzz number
 Number 107 is buzz number
 Number 170 is not buzz number
/*
  Node JS Program for
  Check whether the given number is buzz number or not
*/
class BuzzNumber
{
	isBuzzNo(number)
	{
		if (number % 7 == 0 || number % 10 == 7)
		{
			// When last digit is 7  
			// or number is divisible by 7
			console.log(" Number " + number + " is buzz number");
		}
		else
		{
			console.log(" Number " + number + " is not buzz number");
		}
	}
}

function main()
{
	var task = new BuzzNumber();
	// Test Cases
	task.isBuzzNo(126);
	task.isBuzzNo(123);
	task.isBuzzNo(2120);
	task.isBuzzNo(107);
	task.isBuzzNo(170);
}
main();

input

 Number 126 is buzz number
 Number 123 is not buzz number
 Number 2120 is not buzz number
 Number 107 is buzz number
 Number 170 is not buzz number
#  Python 3 Program for
#  Check whether the given number is buzz number or not
class BuzzNumber :
	def isBuzzNo(self, number) :
		if (number % 7 == 0 or number % 10 == 7) :
			#  When last digit is 7  
			#  or number is divisible by 7
			print(" Number", number ,"is buzz number")
		else :
			print(" Number", number ,"is not buzz number")
		
	

def main() :
	task = BuzzNumber()
	#  Test Cases
	task.isBuzzNo(126)
	task.isBuzzNo(123)
	task.isBuzzNo(2120)
	task.isBuzzNo(107)
	task.isBuzzNo(170)

if __name__ == "__main__": main()

input

 Number 126 is buzz number
 Number 123 is not buzz number
 Number 2120 is not buzz number
 Number 107 is buzz number
 Number 170 is not buzz number
#  Ruby Program for
#  Check whether the given number is buzz number or not
class BuzzNumber 
	def isBuzzNo(number) 
		if (number % 7 == 0 || number % 10 == 7) 
			#  When last digit is 7  
			#  or number is divisible by 7
			print(" Number ", number ," is buzz number", "\n")
		else 
			print(" Number ", number ," is not buzz number", "\n")
		end

	end

end

def main() 
	task = BuzzNumber.new()
	#  Test Cases
	task.isBuzzNo(126)
	task.isBuzzNo(123)
	task.isBuzzNo(2120)
	task.isBuzzNo(107)
	task.isBuzzNo(170)
end

main()

input

 Number 126 is buzz number
 Number 123 is not buzz number
 Number 2120 is not buzz number
 Number 107 is buzz number
 Number 170 is not buzz number
/*
  Scala Program for
  Check whether the given number is buzz number or not
*/
class BuzzNumber()
{
	def isBuzzNo(number: Int): Unit = {
		if (number % 7 == 0 || number % 10 == 7)
		{
			// When last digit is 7  
			// or number is divisible by 7
			println(" Number " + number + " is buzz number");
		}
		else
		{
			println(" Number " + number + " is not buzz number");
		}
	}
}
object Main
{
	def main(args: Array[String]): Unit = {
		var task: BuzzNumber = new BuzzNumber();
		// Test Cases
		task.isBuzzNo(126);
		task.isBuzzNo(123);
		task.isBuzzNo(2120);
		task.isBuzzNo(107);
		task.isBuzzNo(170);
	}
}

input

 Number 126 is buzz number
 Number 123 is not buzz number
 Number 2120 is not buzz number
 Number 107 is buzz number
 Number 170 is not buzz number
/*
  Swift 4 Program for
  Check whether the given number is buzz number or not
*/
class BuzzNumber
{
	func isBuzzNo(_ number: Int)
	{
		if (number % 7 == 0 || number % 10 == 7)
		{
			// When last digit is 7  
			// or number is divisible by 7
			print(" Number", number ,"is buzz number");
		}
		else
		{
			print(" Number", number ,"is not buzz number");
		}
	}
}
func main()
{
	let task: BuzzNumber = BuzzNumber();
	// Test Cases
	task.isBuzzNo(126);
	task.isBuzzNo(123);
	task.isBuzzNo(2120);
	task.isBuzzNo(107);
	task.isBuzzNo(170);
}
main();

input

 Number 126 is buzz number
 Number 123 is not buzz number
 Number 2120 is not buzz number
 Number 107 is buzz number
 Number 170 is not buzz number
/*
  Kotlin Program for
  Check whether the given number is buzz number or not
*/
class BuzzNumber
{
	fun isBuzzNo(number: Int): Unit
	{
		if (number % 7 == 0 || number % 10 == 7)
		{
			// When last digit is 7  
			// or number is divisible by 7
			println(" Number " + number + " is buzz number");
		}
		else
		{
			println(" Number " + number + " is not buzz number");
		}
	}
}
fun main(args: Array < String > ): Unit
{
	val task: BuzzNumber = BuzzNumber();
	// Test Cases
	task.isBuzzNo(126);
	task.isBuzzNo(123);
	task.isBuzzNo(2120);
	task.isBuzzNo(107);
	task.isBuzzNo(170);
}

input

 Number 126 is buzz number
 Number 123 is not buzz number
 Number 2120 is not buzz number
 Number 107 is buzz number
 Number 170 is not buzz number

Time Complexity

The time complexity of this code is O(1) for each test case since the checks involved are simple arithmetic operations and comparisons.

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