Front-end Tutorials

Front-end Tutorials

HTMLCSSSassJavaScriptReactJS
CMS Tutorials

CMS Tutorials

WordPress
Tutorials expand

C Program to Check Triangular Number

Posted in C Tutorial
Updated on Oct 27, 2024
By Mari Selvan
πŸ‘οΈ 88 - Views
⏳ 4 mins
πŸ’¬ 1 Comment
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 explore a C program to check if a given number is a triangular number.

πŸ“„ Example

Let's delve into the C code that checks whether a given number is a triangular number.

isTriangularNumber.c
Copied
Copy To Clipboard
#include <stdio.h>
#include <math.h>

// Function to check if a number is a triangular number
int isTriangularNumber(int num) {
  // Formula to check triangular number
  int n = (int) sqrt(2 * num);
  return (n * (n + 1)) / 2 == num;
}

// Driver program
int main() {
  // Replace this value with your desired number
  int number = 10;

  // Check if the number is a triangular number
  if (isTriangularNumber(number)) {
    printf("%d is a triangular number.\n", number);
  } else {
    printf("%d is not a triangular number.\n", number);
  }

  return 0;
}

πŸ’» Testing the Program

To test the program with different numbers, simply replace the value of number in the main function.

Output
10 is a triangular number.

Compile and run the program to check if the given number is a triangular number.

🧠 How the Program Works

  1. The program defines a function isTriangularNumber that checks if a given number is a triangular number.
  2. Inside the main function, replace the value of number with the desired number to check.
  3. 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.

isTriangular.c
Copied
Copy To Clipboard
#include <stdio.h>

// Function to check if a number is triangular
int isTriangular(int num) {
  int sum = 0, n = 1;

  while (sum < num) {
    sum += n;
    ++n;
  }

  return (sum == num);
}

// Driver program
int main() {
  printf("Triangular Numbers in the Range 1 to 50:\n");

  for (int i = 1; i <= 50; ++i) {
    if (isTriangular(i)) {
      printf("%d ", i);
    }
  }

  return 0;
}

πŸ’» Testing the Program

Output
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 range of 1 to 50.

🧠 How the Program Works

  1. The program defines a function isTriangular that checks if a given number is triangular.
  2. Inside the function, it uses a while loop to find the triangular number by iteratively adding natural numbers.
  3. The main function iterates through the 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:

To get interesting news and instant updates on Front-End, Back-End, CMS and other Frameworks. Please Join the Telegram Channel:

Author

author
πŸ‘‹ Hey, I'm Mari Selvan

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

Share Your Findings to All

Subscribe
Notify of
guest
1 Comment
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Mari Selvan
Mari Selvan
10 months ago

If you have any doubts regarding this article (C Program to Check Triangular Number), please comment here. I will help you immediately.

We make use of cookies to improve our user experience. By using this website, you agree with our Cookies Policy
AgreeCookie Policy