Home > Back-end >  Taking unlimited input
Taking unlimited input

Time:11-22

import java.util.*;
class SeriesSum
{
    int x,n,i,j,k,s=1;
    double f=0.0;
    void accept()
    {
        Scanner sc=new Scanner(System.in);
        System.out.println("Enter the value of x and n");
        x=sc.nextInt();
        n=sc.nextInt();
    }
    void compute()
    {
        for(i=2;i<=n;i=i 2)
        {
            for(j=1;j<n;j  )
            {
                for(k=1;k<=j;j  )
                {
                    s=s*k;
                }
                f=f ((Math.pow(x,i))/s);
            }
        }
        System.out.println("Sum of series :" f);
    }
    public static void main()
    {
        SeriesSum ob=new SeriesSum();
        ob.accept();
        ob.compute();
    }
}

Well this is the code to find the sum of this series: (x ^ 2)/(1!) (x ^ 4)/(3!) (x ^ 6)/(5!) (x ^ n)/((n - 1)!)

The only problem is that it's taking unlimited input

What should I do

CodePudding user response:

Look at the line for (k = 1; k <= j; j ). You have set up an infinite loop. That's why nothing seems to return. I think you meant to write for (k = 1; k <= j; k ).

Here's an enhanced version:

import java.util.*;

public class UnlimitedInput {

    private Scanner sc;
    private boolean ok = true;

    void accept() {
        sc = new Scanner(System.in);
        System.out.print("Enter the value of x and n, separated by a comma: ");
        try {
            String[] split = sc.nextLine().split(",");
            int x = Integer.valueOf(split[0].trim());
            int n = Integer.valueOf(split[1].trim());
            compute(x, n);
        } catch (NumberFormatException e) {
            System.out.println("Bad input. Quitting.");
            ok = false;
        }
    }

    // The commented out print statements show how I debugged this.
    void compute(int x, int n) {
        // System.out.println(x   "^"   n); <- printed once
        int i, j, k, s = 1;
        double f = 0.0;
        
        for (i = 2; i <= n; i = i   2) {
            // System.out.println("i: "   i); <- printed once
            for (j = 1; j < n; j  ) {
                for (k = 1; k <= j; k  ) { // <- you had j   (copy-paste error?)
                    s = s * k;
                    // System.out.println("s: "   s); <- with j  , this just printed forever
                }
                f = f   ((Math.pow(x, i)) / s);
            }
        }
        System.out.println("Sum of series: "   f);
    }
    
    void close() {
        if (sc != null) {
            sc.close();
        }
    }

    public static void main(String[] args) {
        UnlimitedInput input = new UnlimitedInput();
        System.out.println("Hit Ctrl-break to quit.");
        do {
            input.accept();
        } while (input.ok);
        input.close();
    }
}

CodePudding user response:

Your loop is looping Infinitely. try this code

import java.util.*;

class SeriesOne {
int x, n, sum;

double calFact(int m) {
    int fact = 1;
    for (int i = 1; i <= m; i  )
        fact = fact * i;
    return fact;
}

double calPower(int x, int y) {
    double res;
    res = Math.pow(x, y);
    return res;
}

void cal() {
    double term = 0;
    for (int i = 2; i <= n; i = i   2) {
        term = calPower(x, i) / calFact(i - 1);
        sum  = term;
    }

    System.out.println("Sum of series :"   sum);
}

void accept() {
    Scanner sc = new Scanner(System.in);
    System.out.println("Enter the value of x and n");
    x = sc.nextInt();
    n = sc.nextInt();
}

public static void main(String[] args) {
    SeriesOne so = new SeriesOne();

    so.accept();
    so.cal();
}

}

  • Related