C# Basic
C# Interview Programs
- C# Interview Programs
- C# Abundant Number
- C# Amicable Number
- C# Armstrong Number
- C# Average of N Numbers
- C# Automorphic Number
- C# Biggest of three numbers
- C# Binary to Decimal
- C# Common Divisors
- C# Composite Number
- C# Condense a Number
- C# Cube Number
- C# Decimal to Binary
- C# Decimal to Octal
- C# Disarium Number
- C# Even Number
- C# Evil Number
- C# Factorial of a Number
- C# Fibonacci Series
- C# GCD
- C# Happy Number
- C# Harshad Number
- C# LCM
- C# Leap Year
- C# Magic Number
- C# Matrix Addition
- C# Matrix Division
- C# Matrix Multiplication
- C# Matrix Subtraction
- C# Matrix Transpose
- C# Maximum Value of an Array
- C# Minimum Value of an Array
- C# Multiplication Table
- C# Natural Number
- C# Number Combination
- C# Odd Number
- C# Palindrome Number
- C# Pascalβs Triangle
- C# Perfect Number
- C# Perfect Square
- C# Power of 2
- C# Power of 3
- C# Pronic Number
- C# Prime Factor
- C# Prime Number
- C# Smith Number
- C# Strong Number
- C# Sum of Array
- C# Sum of Digits
- C# Swap Two Numbers
- C# Triangular Number
C# Program to check Natural Number
Photo Credit to CodeToFun
π Introduction
In the realm of programming, understanding and validating numbers is a fundamental task. One common classification is that of natural numbers.
A natural number is a positive integer greater than zero.
In this tutorial, we will explore a simple C# program that checks whether a given number is a natural number or not.
π Example
Let's delve into the C# code that accomplishes this functionality.
using System;
class Program {
// Function to check if a number is a natural number
static bool IsNaturalNumber(int num) {
return (num > 0);
}
// Driver program
static void Main() {
// Replace this value with your desired number
int number = 42;
// Check if the number is a natural number
if (IsNaturalNumber(number)) {
Console.WriteLine($"{number} is a natural number.");
} else {
Console.WriteLine($"{number} is not a natural number.");
}
}
}
π» Testing the Program
To test the program with different numbers, replace the value of number in the Main method.
42 is a natural number.
Compile and run the program to see if the given number is a natural number.
π§ How the Program Works
- The program defines a function IsNaturalNumber that takes a number as input and returns true if it's a natural number (greater than 0) and false otherwise.
- In the Main method, replace the value of number with the desired number.
- The program then checks if the number is a natural number using the IsNaturalNumber function.
- It prints whether the number is a natural number or not based on the result.
π€ Considerations
- The program assumes the input number is an integer.
- For user input scenarios, additional validation may be required to handle non-integer inputs.
π Between the Given Range
Let's dive into the C# code that checks and displays natural numbers in the range from 1 to 10.
using System;
class Program {
// Function to check if a number is natural
static bool IsNaturalNumber(int number) {
return (number > 0);
}
// Driver program
static void Main() {
// Define the range from 1 to 10
int startRange = 1;
int endRange = 10;
Console.WriteLine($"Natural Numbers in the range {startRange} to {endRange}:");
// Iterate through the range and check for natural numbers
for (int i = startRange; i <= endRange; i++) {
if (IsNaturalNumber(i)) {
Console.Write($"{i} ");
}
}
Console.WriteLine();
}
}
π» Testing the Program
Natural Numbers in the range 1 to 10: 1 2 3 4 5 6 7 8 9 10
Simply compile and run the program to see the natural numbers in the range from 1 to 10 displayed in the desired format.
π§ How the Program Works
- The program defines a function IsNaturalNumber that checks if a given number is a natural number (greater than 0).
- In the Main method, it defines the range from 1 to 10 and prints the header.
- It then iterates through the range, checking each number for naturality using the defined function.
- Natural numbers within the specified range are displayed in the requested format.
π§ Understanding the Concept of Natural Numbers
Natural numbers are a set of positive integers that start from 1 and continue indefinitely. They are often used for counting and ordering.
In this program, we check whether a given number is a natural number by verifying if it is greater than zero.
π Conclusion
This C# program serves as a foundational example for checking whether a number is a natural number or not. Understanding the characteristics of natural numbers and incorporating such checks can be essential in various programming scenarios
Feel free to incorporate and modify this code as needed for your specific use case. Happy coding!
π¨βπ» Join our Community:
Author
For over eight years, I worked as a full-stack web developer. Now, I have chosen my profession as a full-time blogger at codetofun.com.
Buy me a coffee to make codetofun.com free for everyone.
Buy me a Coffee
If you have any doubts regarding this article (C# Program to check Natural Number), please comment here. I will help you immediately.