Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.*;
- class Calc{
- private static String getInput(String prompt){
- BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in));
- System.out.print(prompt);
- System.out.flush();
- try{
- return stdin.readLine();
- }catch(Exception e){
- return "Error: " + e.getMessage();
- }
- }
- public static void main(String[] args){
- float sum = 1;
- String input = getInput("Enter the limit: ");
- int n = Integer.parseInt(input);
- if(n>0){
- System.out.print("1/(1.0)^2");
- for(float i=2; i<=n; i++){
- System.out.print(" + 1/("+i+")^2");
- sum += 1/(i*i);
- }
- System.out.print(" = " +sum);
- }
- else
- System.out.print("Error: Not a positive number.");
- }
- }
- // Output:-
- // Enter the limit: 5
- // 1/(1.0)^2 + 1/(2.0)^2 + 1/(3.0)^2 + 1/(4.0)^2 + 1/(5.0)^2 = 1.4636111
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement