In this article, I am going to discuss the Buzz Number Program in C# with examples This detailed article will cover the following topics.
- Introduction
- What is Buzz Number?
- Algorithm to check Buzz Number
- Program to check whether a given number is a Buzz Number or not in C#
- Program to print Buzz Numbers between 1 to 100
- Conclusion
What is Buzz Number?
A Buzz number is a number that satisfies one of the following two conditions:
- The number should be divisible by 7.
- The number should end with the digit 7.
Examples
7 is a buzz number
Explanation: 7 is divisible by 7
27 is a buzz number
Explanation: 27 ends with 7
63 is a buzz number
Explanation: 63 is divisible by 7
97 is a buzz number
Explanation: 27 ends with 7
72 is not a buzz number
Explanation: 72 is neither divisible by 7 nor it ends with 7
Algorithm to check Buzz Number
To check whether the given number is a Buzz number or not, below are the steps.
- Get the input from the user that you want to check if it is a Buzz number
- Check if the number ends with 7 or is divisible by 7
- If the condition is true then it will print a buzz number
- If the condition is not true then print that it is not a buzz number.
Program to check whether a given number is a Buzz Number or not in C#
The following C# program will enable the user to enter a number and check whether the number is a Buzz number or not. Let's see.
internal class BuzzNumberIncSharp
{
public static void Main()
{
try
{
Console.Write("Enter a number: ");
var number = Convert.ToInt32(Console.ReadLine());
if (IsBuzzNumber(number))
{
Console.WriteLine($"{number} is a Buzz Number!");
}
else
{
Console.WriteLine($"{number} is not a Buzz Number!");
}
Console.ReadLine();
}
catch (FormatException)
{
Console.WriteLine("Invalid input! Please enter a valid integer.");
}
}
static bool IsBuzzNumber(int num)
{
return (num % 7 == 0 || num % 10 == 7); // Check if divisible by 7 or ends with 7
}
}
Output
Program to print Buzz Numbers between 1 to 100
The following C# program will enable the user to print Buzz Numbers between 1 to 100. Let's see.
internal class BuzzNumberBetween1to100
{
public static void Main()
{
Console.WriteLine("!! To Print Buzz Numbers Between 1 to 100 !!");
Console.Write("Buzz Numbers: ");
for (int num = 1; num <= 100; num++)
{
if (num % 7 == 0 || num % 10 == 7)
{
Console.Write(num + ", ");
}
}
Console.ReadLine();
}
}
Output
See you in the next article, till then, take care and be happy learning.
You may also visit my other article.
You can connect with me @.
Conclusion
In this article, we have discussed how to check whether a given number is a buzz number or not in C# with examples.
I hope you enjoyed this blog. Follow C# Corner to learn more new and amazing things about C#.
Thanks for reading.