Watson95 Watson95 - 1 month ago 11
C Question

write "isTriangular" function, which takes a positive integer and returns 1 if the given number is a triangular number; returns 0, otherwise

Write isTriangular function, which takes a positive integer and returns 1 if the given number is a triangular number; returns 0, otherwise.

The main function is okay, but there is some errors in "isTriangular" function which i can not see, please help me.Triangular numbers are shown below:

enter image description here

#include<stdio.h>
int isTriangular(int N)
{
int sum = 0, n = 0;
for (n; sum <= N; n++)
sum += n;

if (sum == N)
return 1;
else
return 0;
}


int main()
{
int x;
do
{
scanf("%d", &x);
if (x > 0)
{
if (isTriangular(x))
printf("%d is a triangular number\n", x);
else
printf("%d is NOT a triangular number\n", x);
}
}
while (x > 0);
}

Answer

Change termination condition from sum <= N to sum < N.