Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
PROGRAM TO FIND THE SUM OF ARRAY USING RECURSION java
#1

Code:
import java.io.*;
class Arraysum
{
int temp;
  int sum(int a[],int n)
  {
  if(n==1)
  return a[0];
  else
  {
  temp =sum(a,n-1);
  temp=temp+a[n-1];
  return temp;
  }
  }
}
class Newarray
{
public static void main(String args[])throws IOException
{
DataInputStream dis=new DataInputStream(System.in);
  System.out.println("Enter limit");
  int n=Integer.parseInt(dis.readLine());
  int a[]=new int[10];
  System.out.println("Enter the array");
  

for(int i=0;i<n;i++)
  {  
  a[i]=Integer.parseInt(dis.readLine());
  }
  Arraysum a1=new Arraysum();
  System.out.println("Array sum is"+ a1.sum(a,n));
}
}

OUTPUT
E:\5BCA-B\lijo\java>java Newarray
Enter limit
3
Enter the array
2
3
4
Array sum is 9
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

Powered By MyBB, © 2002-2024 iAndrew & Melroy van den Berg.