Please comment the two codes below. (//1)The first is writing numbers vertically and second is on pins (//2) class Main { public static void main(String[] args) { //example1(); example2(6); writeVertical(120); System.out.println(numPins(5)); } //Infintie Recursion public static void example1() { } //Recursion with base case public static void example2(int n) { if(n ==1) { return; } else { System.out.println(n); example2(n-1); } } //Makes the number vertical public static void writeVertical(int num) { if(num ==0) { return; } else { writeVertical(num/10); System.out.println(num%10); } } //2 public static int numPins (int rows) { if(rows == 0) { return rows; } return numPins(rows - 1)+ rows; } }
Please comment the two codes below. (//1)The first is writing numbers vertically and second is on pins (//2)
class Main
{
public static void main(String[] args)
{
//example1();
example2(6);
writeVertical(120);
System.out.println(numPins(5));
}
//Infintie Recursion
public static void example1()
{
}
//Recursion with base case
public static void example2(int n)
{
if(n ==1)
{
return;
}
else
{
System.out.println(n);
example2(n-1);
}
}
//Makes the number vertical
public static void writeVertical(int num)
{
if(num ==0)
{
return;
}
else
{
writeVertical(num/10);
System.out.println(num%10);
}
}
//2
public static int numPins (int rows)
{
if(rows == 0)
{
return rows;
}
return numPins(rows - 1)+ rows;
}
}
Step by step
Solved in 7 steps with 2 images