I just need someone to write this program for me so I can compare to mine. She n
ID: 3812124 • Letter: I
Question
I just need someone to write this program for me so I can compare to mine. She normally gives an answer key so we can correct it after we turn it in, but she didn't have time to post one this time. (This program is already done and turned in, I just need someone to write it out so I can compare).
Here is the assignment:
Recursive solutions can each be coded as iterative solutions, yet recursion sometimes simplifies the problem or makes it more recognizable as a mathematical problem.
You are to write four programs, each in its own source code file. For full credit, all four Java files should meet all requirements and be put into your handin directory by the start of class on the due date. The
handin directory will contain: Prog6a.java, Prog6b.java, Prog6c.java, Prog6d.java.
The first line of output from each program will be the identification string used in previous programs:
Program 6, [Name], [csscID]
Prog6a (10 pts)
Write a recursive program that prints whole numbers from N down to 1 with one space between. Read N from standard input, but do not prompt for it. Example (input in green text):
10
10 9 8 7 6 5 4 3 2 1
Prog6b (10 pts)
Write a recursive program that prints the digits backwards. Read a whole positive number N from statndard input. Do not prompt for N. Example (input in green text):
8743
3478
Prog6c (15 pts)
Given user-provided values x and n, write a recursive program that computes Xn. Examples (input in green text), where output is rounded to thousandths:
4.0 0.0 1.000
Prog6d (15 pts)
Write a recursive program, that outputs N!, with 3-space indented print statements. . Read a whole positive number N from standard input. Do not prompt for N. Example:
5
Explanation / Answer
1)prints whole numbers
import java.io.*;
import java.util.*;
class Prog6a
{
public static void main(String args[])
{
Scanner s=new Scanner(System.in) //standard input
int n=s.nextInt();
//int n=System.in.read(); //standard input
System.out.println("whole numbers from " + n +" down to 1");
}
public static int print(int n) //recursively print whole numbers
{
System.out.println(n+" ");
print(n-1);
}
}
output: 10
whole numbers from 10 down to 1
10 9 8 7 6 5 4 3 2 1
2) prints the digits backwards
import java.io.*;
import java.util.*;
class Prog6b
{
public static void main(String args[])
{
int rev=0;
Scanner s=new Scanner(System.in) //standard input
int n=s.nextInt();
System.out.println("Reverse of " + n +"is");
System.out.println(reverse(n));
}
public static int reverse(int n)
{
int n1=0;
if(n<10)
return n;
else{
n1= (n1*10)+n%10;
reverse(n/10);
}
return n1;
}
output: 123
Reverse of 123 is
321
3) program that computes Xn.
import java.io.*;
import java.util.*;
import java.util.Scanner;
class Prog6c
{
public static void main(String args[])
{
float x,n;
Scanner s=new Scanner(System.in);
System.out.println("Enter x and n value:");
x=s.nextFloat();
n=s.nextFloat() ;//standard input
System.out.println(x +" power" + n +"is"+ nprog6c.power(x,n));
}
public static float power(float x, float n)
{
double n1=0;
if( n==0)
return 1;
else if (n<0)
n1 = n1* power(x,n+1);
else (n>0)
n1=n1*power(x,n-1);
return (String.format("%.3f", n1));
}
}
output:
Enter x and n value: 3.123
2.0
3.123 power 2.0 is 9.753
3.14
4)indented print statements.
import java.io.*;
import java.util.*;
class Prog6d
{
public static void main(String args[])
{
int n;
Scanner s=new Scanner(System.in) //standard input
n=s.nextInt();
int h=n;
for(int i=0;i<n;i++)
{
for(int j=i;j>0;j--)
System.out.print(" ");
System.out.println("factorial("+h+")");
h--;
}
for(int j=6;j>1;j--)
System.out.print(" ");
System.out.println("return 1");
for(inti=0;i<n;i++)
{
int t=i+1;int k=
for(int j=5;j>i;j--)
System.out.print(" ");
System.out.println("return "+t+"*"+fact(t-1)+"= "+fact(1));
}
}
public static int fact(int n)
{
int x= n * fact(n-1);
return x;
}
}
output: 5
factorial(5)
factorial(4)
factorial(3)
factorial(2)
factorial(1)
return 1
return 1*1 = 1
return 2*1 = 2
return 3*2 = 6
return 4*6 = 24
return 5*24 = 120