Java Program class C14e1 public static void mainString args

Java Program:

class C14e1
{
public static void main(String[] args)
{
numbersToOne(10);
}
//----------------------------------
public static void numbersToOne(int n)
{
if (n > 0)
{
System.out.println(n);
numbersToOne(n - 1);
}
}
}

Write a program that is equivalent to the program shown above, but implement numbersToOne using a while loop instead of recursion.

Also, implement the fib method shown below using a loop instead of recursion.

public static long fib(long i)
{
if (i < 2)
return 1;
return fib(i - 1) + fib(i - 2);
}

Solution

class C14e1
{
public static void main(String[] args)
{
numbersToOne(10);
}
//----------------------------------
public static void numbersToOne(int n)
{
while(int i=n;i>=1;i--)

{

System. out.println(i);

}
}

public static void fib(long i)

{

long a=0,b=1,c=0;

System.out.println(a+\" \"+b);

while(c<=i)

{

c=a+b;

System.out.println(c);

a=b;

b=c;

}

}

Java Program: class C14e1 { public static void main(String[] args) { numbersToOne(10); } //---------------------------------- public static void numbersToOne(in
Java Program: class C14e1 { public static void main(String[] args) { numbersToOne(10); } //---------------------------------- public static void numbersToOne(in

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site