Code

darpan.kattel

Darpan Kattel

• April 16, 2022, 3:59 p.m.

A program to check whether the input is a triangular number or not

The numbers formed by the sum of the natural numbers are the triangular numbers. For example, 1, 3=1+2, 6=1+2+3, 10=1+2+3+4

C

// a program to check whether the input number is traingular or not
#include <stdio.h>
int main(){
    int num, i, sum=0, flag=0;
    printf("Enter the number\n");
    scanf("%d",&num);
    for(i=0; i<=num; i++){
        sum += i;
        if(sum == num){
            printf("%d is a triangular number\n",num);
            flag = 1;
            break;
        }
    }
    if(flag == 0){
        printf("%d is not a triangular number\n",num);
    }
    return 0;
}
Output:
A program to check whether the input is a triangular number or not | Output of C Language Code

Add comment:

Total 0 comments