Use Java for the following! 1. Write a recursive function that returns the maxim
ID: 664737 • Letter: U
Question
Use Java for the following!
1. Write a recursive function that returns the maximum odd number in an array of integers. If there are no odd numbers in the array, then the function returns 1.
2. Write a recursive function int sumSquares(int i, int n) that sums up the squares of all the numbers i through and inclusive of n. Example: sumSquares(2,4)=2^2+3^2+4^2=29
public static int sumSquares( int i int n ){
...
}
3. Write the function int add(int x, int y) that returns the sum of x and y. You may not use the + operator at all in this function. You may ONLY use an ‘if’ statement, a recursive call and operator ++ and to implement this function.
public static int add( int x, int y ){
...
}
Explanation / Answer
1.
import java .util.scanner
Public class Largestodd
{
Scanner scanner = new scanner(system.in)
System.out.println("enter the size of array");
int length = scanner.nextint();
int[] array = new int[length];
Systtem.out.println("enter the elements of A");
for(int i=0; i<length; i++)
{
array[i] = scanner.nextint();
}
int largestodd = -1;
boolean largestoddValid == false){a
for(int i=0; i< array.length; i++){
if(array[i] % 2! ==0){
if(largestoddvalid == false){
largestodd = array[i];
larestoddvalid = true;}
else{
largestodd = Math.max(largestodd, array[i]);
}
}
}
}
}
2. Public static int sumofsquares(int i, int n)
{
if(i< =n)
return m*m + sumofsquares( i+1, n); // recursive call
else
return i*i;
}
3.int add(int x, int y)
{
x =0;
y = 0;
int sum = 0;
if (sum == 0)
return 0;
else
sum+ = intadd(x, y);
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.