Saturday, April 5, 2014

Java Program to Implement Iterative Method

import java.util.Scanner;

public class IterativeProcess {

    public static void main(String args[]) {
        Scanner input = new Scanner(System.in);
        float a, x, b, c, sum;

        float result[] = new float[100];

        System.out.println("for the quadratic equation:ax^2+bx+c");
        System.out.println("enter the value of a:?");
        a = input.nextFloat();

        System.out.println("enter the value of b:?");
        b = input.nextFloat();

        System.out.println("enter the value of c:?");
        c = input.nextFloat();

        System.out.println("enter the starting value of x:?");
        x = input.nextFloat();
        x = (-c - a * x * x) / b;
       
        result[0]=x;
        System.out.printf("The equation is: %.2fx^2 + (%.2f)x + %f = 0 \n", a, b, c);

        //   for(int i=x;i<=10;i++){ // loop starts here

        for (int i =0;; i++) { // loop starts here
         
           // System.out.printf("The value at SN:=%d is:%f \n", i+1, result[i]); 
           
            sum =  (-c - a * x * x) / b;
            result[i+1] = sum;

            if (result[i+1] == x) {
                break;
            }
             System.out.printf("The value at SN:=%d is:%f \n", i+1, result[i]); 
            x = sum;
           


        }// loop ends here
        if ((result[1] - result[2]) >(result[2] - result[3])) {
            System.out.println("\n........................... The function is Converging...........................");
        } else {
            System.out.println("\n ........................ ..The function is Diverging..........................");
        }
    }
}

No comments:

Post a Comment