//  Scott DeRuiter 9/22/2002
//  Fourth.java
//  User inputs the coefficients for a fourth degree polynomial and an x value.
//  The function value is calculated and printed.
//  7/15/2011 - Greenstein: Changed TextReader to Scanner, added import Math

import java.util.Scanner;
import java.lang.Math;

public class Fourth   
{
	public static void main ( String [] args )   
	{
	
		int A, B, C, D, E;
		double x, result;
		Scanner pickitup = new Scanner ( System.in );

		System.out.print ( "\n\nEnter the fourth degree coefficient   -> " );
		A = pickitup.nextInt ( );
		System.out.print ( "\n\nEnter the cubic coefficient           -> " );
		B = pickitup.nextInt ( );
		System.out.print ( "\n\nEnter the quadratic coefficient       -> " );
		C = pickitup.nextInt ( );
		System.out.print ( "\n\nEnter the linear coefficient          -> " );
		D = pickitup.nextInt ( );
		System.out.print ( "\n\nEnter the constant coefficient        -> " );
		E = pickitup.nextInt ( );
		System.out.print ( "\n\nEnter the input value (x)             -> " );
		x = pickitup.nextDouble ( );

		result = (double)A * Math.pow ( x, 4 ) + (double)B * Math.pow ( x, 3 ) + 
			(double)C * Math.pow ( x, 2 ) + (double)D * x + E;
		System.out.println ( A + "x^4  + " + B + "x^3  + " + C + "x^2  + " +
			D + "x  + " + E + "  =  " + result );  
		System.out.println ( "when x = " + x );
	}
}

Back to Lesson 3 Examples

Back to Java Main Page