Friday, June 7, 2013

Program to print sum of the series S=1+x+x2+x3+…………+xn

PROGRAM
WAP to find  sum of the series S=1+x+x2+x3+…………+xn


/**
 * class sumseries finds sum of the series.
 * This program dosn't use built-in method pow().
 * @author: Nitendra Verma 
 * @version: June 8,2013
 */
import java.util.Scanner;
public class sumseries
{
    int num,limit;     //instance variables
    public sumseries() //default constructor, initializes both variables to 0 
    {
        num=0;
        limit=0;
    } 
    public void getlimit(int n,int l) //method, assigns parameter values to variables 
    {
        num=n;
        limit=l;
    }
    
    public int findpow(int x,int p)  //method, returns power value of number
    {
    int value=1;
        for(int i=0;i<p;i++)
        {
            value*=x;
        }
        return value;   //returning value
    }
    
    public void findsumseries()  //method, finds sum of the series
    {
        int sum=0;
        for(int i=0;i<limit;i++)
        {
            sum+=findpow(num,i);
        }
        System.out.println("Sum of the series= "+sum);
    }
    
    public static void main()
    {
        sumseries ob=new sumseries();   //creating object of class sumseries
        Scanner in=new Scanner(System.in);  //creating object of class Scanner
        System.out.println("Enetr the number");
        int num=in.nextInt();               //inputting value
        System.out.println("Enetr the limit");
        int limit=in.nextInt();             //inputting value
        ob.getlimit(num,limit);             //calling method
        ob.findsumseries();                 //calling method
    }
}

OUTPUT:
Enetr the number
3
Enetr the limit
3
Sum of the series= 13

/*---------------Program developed by: Nitendra Verma---------------*/
//For more details visit http://javawithnitendra.blogspot.in

No comments:

Post a Comment

Ur comments r most welcome...