TheBeginner'sQuestion

TheBeginner'sQuestion

0+2+4+6+8+.............+n

View Answers

December 6, 2010 at 5:26 PM

Hi Friend,

Try the following code:

import java.util.*;
class Series 
{
    public static void main(String[] args) 
    {
        int sum=0;
        StringBuffer buffer=new StringBuffer();
        Scanner input=new Scanner(System.in);
        System.out.print("Enter value of n: ");
        int num=input.nextInt();
            for(int i=0;i<=num;i++){
                if(i%2==0){
                    buffer.append(Integer.toString(i));
                    buffer.append("+");
                    sum+=i;
                }
            }
        String st=buffer.toString();
        int len=st.length()-1;
        String str=st.substring(0,len);
        System.out.println(str+" = "+sum);

    }
}

Thanks









Related Tutorials/Questions & Answers:
TheBeginner'sQuestion
TheBeginner'sQuestion  0+2+4+6+8+.............+n   Hi Friend, Try the following code:ADS_TO_REPLACE_1 import java.util.*; class Series { public static void main(String[] args) { int sum=0

Ads