Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

How to use Recursion to find triangular numbers in java

Shulou Source: shulou.com Published: 2022-06-02 10:41:08 10月01日 Update

This article shows you how to use recursion to find triangular numbers in java. The content is concise and easy to understand. It will definitely brighten your eyes. I hope you can get something through the detailed introduction of this article.

Package com.clean.array;import java.util.Scanner;/** * Recursive triangular digits * / public class TranTest {public static void main (String [] args) {TranTest tranTest = new TranTest (); Scanner scanner = new Scanner (System.in); while (true) {System.out.println ("please input an Integer value:"); String x = scanner.next () If (x.equalsIgnoreCase ("Q")) {System.out.println ("quit!"); break;} int y = Integer.parseInt (x); / / int v = tranTest.triange (y); int v = tranTest.triangeDg (y); System.out.println ("res =" + v) } private int triangeDg (int n) {System.out.println ("enter:" + n); if (n = 1) {System.out.println ("retrun 1"); return 1;} else {int temp = n + triangeDg (n-1); System.out.println ("return:" + temp); return temp * @ param n * @ return * / private int triange (int n) {int total = 0; while (n > 0) {total = total + n;-- n;} return total }} the above is how to use recursion to find triangular numbers in java. Have you learned any knowledge or skills? If you want to learn more skills or enrich your knowledge reserve, you are welcome to follow the industry information channel.

Tags: Numbers triangles recursion content skills knowledge conciseness brevity that is articles more articles industries information channels channels one light reserve cycle Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno MariaDB OPPO Reno MySQL Shulou Information Apple