C# Topics
- C# Intro
- C# String Methods
- C# Interview Programs
- Abundant Number
- Amicable Number
- Armstrong Number
- Average of N Numbers
- Automorphic Number
- Biggest of three numbers
- Binary to Decimal
- Common Divisors
- Composite Number
- Condense a Number
- Cube Number
- Decimal to Binary
- Decimal to Octal
- Disarium Number
- Even Number
- Evil Number
- Factorial of a Number
- Fibonacci Series
- GCD
- Happy Number
- Harshad Number
- LCM
- Leap Year
- Magic Number
- Matrix Addition
- Matrix Division
- Matrix Multiplication
- Matrix Subtraction
- Matrix Transpose
- Maximum Value of an Array
- Minimum Value of an Array
- Multiplication Table
- Natural Number
- Number Combination
- Odd Number
- Palindrome Number
- Pascalβs Triangle
- Perfect Number
- Perfect Square
- Power of 2
- Power of 3
- Pronic Number
- Prime Factor
- Prime Number
- Smith Number
- Strong Number
- Sum of Array
- Sum of Digits
- Swap Two Numbers
- Triangular Number
- C# Star Pattern
- C# Number Pattern
- C# Alphabet Pattern
C# Program to Check Triangular Number
Photo Credit to CodeToFun
π Introduction
In the realm of programming, various mathematical properties of numbers often pique our interest. One such property is whether a number is a triangular number.
A triangular number or triangle number is the sum of the natural numbers up to a certain value.
In this tutorial, we'll delve into a C# program to check if a given number is a triangular number.
π Example
Let's explore the C# code that checks whether a given number is a triangular number.
using System;
class Program {
// Function to check if a number is a triangular number
static bool IsTriangularNumber(int num) {
// Formula to check triangular number
int n = (int) Math.Sqrt(2 * num);
return (n * (n + 1)) / 2 == num;
}
// Driver program
static void Main() {
// Replace this value with your desired number
int number = 10;
// Check if the number is a triangular number
if (IsTriangularNumber(number)) {
Console.WriteLine($"{number} is a triangular number.");
} else {
Console.WriteLine($"{number} is not a triangular number.");
}
}
}
π» Testing the Program
To test the program with different numbers, simply replace the value of number in the Main method.
10 is a triangular number.
Compile and run the program to check if the given number is a triangular number.
π§ How the Program Works
- The program defines a function IsTriangularNumber that checks if a given number is a triangular number using the formula.
- Inside the Main method, replace the value of number with the desired number to check.
- The program then calls the IsTriangularNumber function and prints whether the number is a triangular number or not.
π Between the Given Range
Let's take a look at the C# code that checks and displays triangular numbers in the range of 1 to 50.
using System;
class Program {
// Function to check if a number is triangular
static bool IsTriangular(int num) {
int sum = 0;
int n = 1;
while (sum < num) {
sum += n;
n++;
}
return sum == num;
}
// Driver program
static void Main() {
Console.WriteLine("Triangular Numbers in the Range 1 to 50:");
// Check and display triangular numbers in the range
for (int i = 1; i <= 50; i++) {
if (IsTriangular(i)) {
Console.Write(i + " ");
}
}
Console.WriteLine();
}
}
π» Testing the Program
Triangular Numbers in the Range 1 to 50: 1 3 6 10 15 21 28 36 45
Compile and run the program to see the triangular numbers in the specified range.
π§ How the Program Works
- The program defines a function IsTriangular that checks if a given number is a triangular number.
- Inside the function, it uses a while loop to find the triangular number by summing natural numbers.
- The Main function iterates through numbers from 1 to 50 and prints those that are triangular.
π§ Understanding the Concept of Triangular Numbers
Before delving into the code, let's take a moment to understand the concept of triangular numbers.
A triangular number is the sum of the natural numbers up to a certain value, and it can be represented by the formula Tn = n * (n + 1) / 2, where n is a non-negative integer.
For example, T3 = 1 + 2 + 3 = 6 is a triangular number.
π’ Optimizing the Program
While the provided program is effective, you can explore and implement optimizations based on the properties of triangular numbers.
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 Triangular Number), please comment here. I will help you immediately.