Write a program to find the sum of 1/2 - 2/3 + 3/4 - 4/5 +5/6 in java

 Write a program to find the sum of 1/2 - 2/3 + 3/4 - 4/5 +5/6 in java

public class SumOfSeries
{
     public static void main(String args[]){
        double sum=0;
        int i,N=5;
        for(i =1;i<=N;i++){
            if(i%2!=0){
            System.out.print(i+" / "+(i+1)+" - ");
            sum +=(double)i/(i+1);
                      
            }else{
            System.out.print(i+" / "+(i+1)+" + "); 
            sum -=(double)i/(i+1);
            }
        }
        
        System.out.print(sum);
        
      }
}
Output
1 / 2 - 2 / 3 + 3 / 4 - 4 / 5 + 5 / 6 - 0.6166666666666667

Comments

Popular posts from this blog

How to make simple bill maker by using python tkinter

Make a GST Calculator using HTML and JavaScript

Write a program to find the sum of 1/2+4/3+5/6+8/7+9/10 using loop in java