06-04-2011, 01:09 PM
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