Computer Science, asked by swati0325, 1 month ago

Write a java program to display...(use for loop)....
a = 1/2+1/3+1/4....10 terms

Answers

Answered by anindyaadhikari13
4

\texttt{\textsf{\large{\underline{Solution}:}}}

The given problem is solved using language - Java.

public class Sum{

   public static void main(String args[]){

       double i,s=0;

       for(i=1.0;i<=10;i++)

           s+=1/(i+1);

       System.out.println("Sum of the series upto 10 terms is: "+s);

   }

}

\texttt{\textsf{\large{\underline{Algorithm}:}}}

  1. Initialize sum = 0, n = 10.
  2. Repeat i = 1 to n.
  3. Add 1/(i + 1) to sum.
  4. Display the sum.

\texttt{\textsf{\large{\underline{O{u}tput}:}}}

Sum of the series upto 10 terms is: 2.019877344877345

See attachment for verification.

Attachments:
Answered by kamalrajatjoshi94
1

Answer:

Program:-

public class Main

{

public static void main(String args[])

{

double s=0;

for(int i=2;i<=10;i++)

{

s=s+1/(double)i;

}

System.out.println("The sum upto 10 terms="+s);

}

}

Logic:-

  • Run a loop from 2 to 10
  • Find the sum of the series use explicit conversion to get the correct value in decimal.
  • I even checked in calculator and also I did it in copy it came same as the output.

Logic 2:-

public class Main

{

public static void main(String args[])

{

double s=0;

for(double i=2.0;i<=10.0;i++)

{

s=s+1/i;

}

System.out.println("The sum upto 10 terms="+s);

}

}

In both I got the same output

But,If you mean upto 10 terms then

Program:-

public class Main

{

public static void main(String args[])

{

double s=0;

for(double i=1.0;i<=10.0;i++)

{

s=s+1/(i+1);

}

System.out.println("The sum upto 10 terms="+s);

}

}

In this case output is different.

Attachments:
Similar questions