Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



Home Answers Viewqa Java-Beginners TheBeginner'sQuestion
Login         

View Questions and Answers by Category

Advertisements


 
Have Programming Question? Ask it here!
 
 
 


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


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


Advertisement
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: import java.util.*; class Series { public static void main(String[] args) { int sum=0; StringBuffer
 

 

 

DMCA.com