Home Answers Viewqa Java-Beginners TheBeginner'sQuestion

 
 


snowingday
TheBeginner'sQuestion
1 Answer(s)      2 years and 5 months ago
Posted in : Java Beginners

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 Pages:
TheBeginner'sQuestion
TheBeginner'sQuestion  0+2+4+6+8+.............+n   Hi Friend, Try the following code: import java.util.*; class Series { public static void main(String[] args) { int sum=0; StringBuffer

Ask Questions?

If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.

Ask your questions, our development team will try to give answers to your questions.