1.
A number n is triangular if n == 1 + 2 +…+j for some j. Write a function
int isTriangular(int n)
that returns 1 if n is a triangular number, otherwise it returns 0. The first 4 triangular numbers are 1 (j=1), 3 (j=2), 6, (j=3), 10 (j=4).
package com.zzy;
/**
* 更多请关注: http://huamaodashu.com
* Created by hadoopall on 17/08/2018.
*/
public class triangular {
public static void main(String[] args) {
System.out.println(istriangular(10));
}
public static int istriangular(int n){
int sum = 0;
for (int i = 0; i <= n; i++) {
sum = sum+ i;
System.out.println("sum"+sum);
if(sum == n){
System.out.println("sum"+sum);
return 1;
}else if(sum > n ){
return 0;
}
}
return 1;
}
}