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 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
Ads