Check whether the number is Triangular or not. A number is termed as triangular number if we can represent it in the form of triangular grid of points such that the points form an equilateral triangle and each row contains as many points as the row number, i.e., the first row has one point, second row has two points, third row has three points and so on. The starting triangular numbers are 1, 3 (1+2), 6 (1+2+3), 10 (1+2+3+4).
Input:
The first line of input contains an integer T denoting the number of test cases. The first line of each test case is N, N is the number to be checked if it is traingular or not.
Output:
If the number is Triangular then display 1 otherwise 0.
Constraints:
The first line of input contains an integer T denoting the number of test cases. The first line of each test case is N, N is the number to be checked if it is traingular or not.
Output:
If the number is Triangular then display 1 otherwise 0.
Constraints:
1<= T <= 100
1<= N <= 1000000
Example:
Input:
5
3
4
6
55
345
1<= N <= 1000000
Example:
Input:
5
3
4
6
55
345
Output:
1
0
1
1
0
1
0
1
1
0
SOURCE CODE:
#include <stdio.h>
int main() {
int t,n;
scanf("%d",&t);
while(t--)
{
scanf("%d",&n);
// printf("%d",t);
int flag=0,i,k=0;
for(i=1;k<=n;i++)
{
k=k+i;
if(k==n)
{
printf("1\n");flag=1;break;
}
}
if(flag==0)
printf("0\n");
}
return 0;
}
@suggest me if there is a optimal code
No comments:
Post a Comment