Pages

Wednesday, April 2, 2014

Java code to find the sum of the following series using recursion 1 2 2 2 3 2 n 2

/*
Program to find the sum of the following series using recursion :
12 + 22 + 32 +.......+ n2
*/

import java.io.*;
class series
{
public static void main(String args[]) throws IOException
{
BufferedReader br = new BufferedReader(new
InputStreamReader(System.in));
series call = new series();
System.out.print("Enter value of n : ");
int n = Integer.parseInt(br.readLine());
System.out.println("Sum of series = " +call.sum(n));
}
int sum(int n)
{
if(n==1)
return 1;
else
return (n*n)+sum(n-1);
}
}


/**
* ALGORITHM :
* ---------
* 1. Start
* 2. Accept a number n from user.
* 3. Using method of recursion, add all the squared numbers upto n.
* 4. Print the sum.
* 5. End
*/

/*
OUTPUT :
------
Enter value of n : 5
Sum of series = 55
*/



Related Posts by Categories

0 comments:

Post a Comment