The elements of an integer-valued array can be set to 0 (i.e., the array can be cleared) recursively as follows:
Write a void method named clear that accepts an integer array, and the number of elements in the array and sets the elements of the array to 0.
Need it to be in Java.
Java Code:
import java.util.*;
class Main {
public static void clearArr(int a[],int n){
if(n<=0)
return;
else{
a[n-1]=0;
clearArr(a,n-1);
}
}
public static void main(String[] args) {
int a[]={1,2,3,4,5,6,7,8};
int size=a.length;
System.out.print("Array before clearing bits: ");
for(int i=0;i<size;i++)
System.out.print(a[i]+" ");
System.out.println();
clearArr(a,size);
System.out.print("Array after clearing bits: ");
for(int i=0;i<size;i++)
System.out.print(a[i]+" ");
}
}
if you like the answer please provide a thumbs up
Get Answers For Free
Most questions answered within 1 hours.