SlideShare a Scribd company logo
1 of 5
Download to read offline
import java.util.Scanner;
public class HornersPolynomial {
/**
* Calculate the polynomial using Horner's method
* return
*/
public static double hornersMeth(double[] coeff, int n, double x) {
double finalVal = coeff[n];
for(int i = (n - 1) ; i >= 0; i--) {
finalVal = (finalVal * x) + coeff[i];
}
return finalVal;
}
/**
* Prints the polynomial
* param coeff
* param n
* param x
*/
public static void printPoly(double[] coeff, int n, double x) {
System.out.println(" Polynomial : ");
System.out.print(coeff[n] + "x^" + n);
for(int i = (n - 1) ; i >= 0; i--) {
if(coeff[i] < 0)
System.out.print(" - " + ((-1) * coeff[i]) + "x^" + i);
else if(coeff[i] >= 0)
System.out.print(" + " + coeff[i] + "x^" + i);
}
System.out.println(" where x = " + x);
}
public static void main(String[] args) {
//Scanner for user input
Scanner sc = new Scanner(System.in);
//Get degree of the polynomial
System.out.println(" Enter the degree of the polynomial: ");
int n = sc.nextInt();
//Coefficicnets array
double[] coeff = new double[n + 1];
for(int i = n ; i >= 0; i--) {
System.out.println("Enter coefficient " + i + ": ");
coeff[i] = sc.nextDouble();
}
//Get value of x
System.out.println(" Enter the value of x: ");
double x = sc.nextDouble();
printPoly(coeff, n, x);
System.out.println(" Value of the polynomila is " + hornersMeth(coeff, n, x));
//Close scanner
sc.close();
}
}
SAMPLE OUTPUT:
Enter the degree of the polynomial:
3
Enter coefficient 3:
2
Enter coefficient 2:
-6
Enter coefficient 1:
2
Enter coefficient 0:
-1
Enter the value of x:
3
Polynomial :
2.0x^3 - 6.0x^2 + 2.0x^1 - 1.0x^0
where x = 3.0
Value of the polynomila is 5.0
Solution
import java.util.Scanner;
public class HornersPolynomial {
/**
* Calculate the polynomial using Horner's method
* return
*/
public static double hornersMeth(double[] coeff, int n, double x) {
double finalVal = coeff[n];
for(int i = (n - 1) ; i >= 0; i--) {
finalVal = (finalVal * x) + coeff[i];
}
return finalVal;
}
/**
* Prints the polynomial
* param coeff
* param n
* param x
*/
public static void printPoly(double[] coeff, int n, double x) {
System.out.println(" Polynomial : ");
System.out.print(coeff[n] + "x^" + n);
for(int i = (n - 1) ; i >= 0; i--) {
if(coeff[i] < 0)
System.out.print(" - " + ((-1) * coeff[i]) + "x^" + i);
else if(coeff[i] >= 0)
System.out.print(" + " + coeff[i] + "x^" + i);
}
System.out.println(" where x = " + x);
}
public static void main(String[] args) {
//Scanner for user input
Scanner sc = new Scanner(System.in);
//Get degree of the polynomial
System.out.println(" Enter the degree of the polynomial: ");
int n = sc.nextInt();
//Coefficicnets array
double[] coeff = new double[n + 1];
for(int i = n ; i >= 0; i--) {
System.out.println("Enter coefficient " + i + ": ");
coeff[i] = sc.nextDouble();
}
//Get value of x
System.out.println(" Enter the value of x: ");
double x = sc.nextDouble();
printPoly(coeff, n, x);
System.out.println(" Value of the polynomila is " + hornersMeth(coeff, n, x));
//Close scanner
sc.close();
}
}
SAMPLE OUTPUT:
Enter the degree of the polynomial:
3
Enter coefficient 3:
2
Enter coefficient 2:
-6
Enter coefficient 1:
2
Enter coefficient 0:
-1
Enter the value of x:
3
Polynomial :
2.0x^3 - 6.0x^2 + 2.0x^1 - 1.0x^0
where x = 3.0
Value of the polynomila is 5.0

More Related Content

Similar to import java.util.Scanner;public class HornersPolynomial {   .pdf

This is Function Class public abstract class Function {    .pdf
This is Function Class public abstract class Function {    .pdfThis is Function Class public abstract class Function {    .pdf
This is Function Class public abstract class Function {    .pdfamitbagga0808
 
III MCS python lab (1).pdf
III MCS python lab (1).pdfIII MCS python lab (1).pdf
III MCS python lab (1).pdfsrxerox
 
การหาปริมาตรของปร ซึม
การหาปริมาตรของปร ซึมการหาปริมาตรของปร ซึม
การหาปริมาตรของปร ซึมPairy Little
 
PROGRAM 2 – Fraction Class Problem For this programming as.pdf
PROGRAM 2 – Fraction Class Problem For this programming as.pdfPROGRAM 2 – Fraction Class Problem For this programming as.pdf
PROGRAM 2 – Fraction Class Problem For this programming as.pdfezonesolutions
 
import java.util.Scanner;public class ArrayOperation {    inp.pdf
import java.util.Scanner;public class ArrayOperation {     inp.pdfimport java.util.Scanner;public class ArrayOperation {     inp.pdf
import java.util.Scanner;public class ArrayOperation {    inp.pdfangelsfashion1
 
OrderTest.javapublic class OrderTest {       Get an arra.pdf
OrderTest.javapublic class OrderTest {         Get an arra.pdfOrderTest.javapublic class OrderTest {         Get an arra.pdf
OrderTest.javapublic class OrderTest {       Get an arra.pdfakkhan101
 
Go ahead, make my day
Go ahead, make my dayGo ahead, make my day
Go ahead, make my dayTor Ivry
 
SumNumbers.java import java.util.Scanner;public class SumNumbe.pdf
SumNumbers.java import java.util.Scanner;public class SumNumbe.pdfSumNumbers.java import java.util.Scanner;public class SumNumbe.pdf
SumNumbers.java import java.util.Scanner;public class SumNumbe.pdfankkitextailes
 
PickerApp CLass.pdf
 PickerApp CLass.pdf PickerApp CLass.pdf
PickerApp CLass.pdfankitcom
 
Import java
Import javaImport java
Import javaheni2121
 
ch05-program-logic-indefinite-loops.ppt
ch05-program-logic-indefinite-loops.pptch05-program-logic-indefinite-loops.ppt
ch05-program-logic-indefinite-loops.pptMahyuddin8
 
Example of JAVA Program
Example of JAVA ProgramExample of JAVA Program
Example of JAVA ProgramTrenton Asbury
 
Lab01.pptx
Lab01.pptxLab01.pptx
Lab01.pptxKimVeeL
 
Newton cotes method
Newton cotes methodNewton cotes method
Newton cotes methodFaisal Saeed
 

Similar to import java.util.Scanner;public class HornersPolynomial {   .pdf (16)

This is Function Class public abstract class Function {    .pdf
This is Function Class public abstract class Function {    .pdfThis is Function Class public abstract class Function {    .pdf
This is Function Class public abstract class Function {    .pdf
 
III MCS python lab (1).pdf
III MCS python lab (1).pdfIII MCS python lab (1).pdf
III MCS python lab (1).pdf
 
การหาปริมาตรของปร ซึม
การหาปริมาตรของปร ซึมการหาปริมาตรของปร ซึม
การหาปริมาตรของปร ซึม
 
PROGRAM 2 – Fraction Class Problem For this programming as.pdf
PROGRAM 2 – Fraction Class Problem For this programming as.pdfPROGRAM 2 – Fraction Class Problem For this programming as.pdf
PROGRAM 2 – Fraction Class Problem For this programming as.pdf
 
import java.util.Scanner;public class ArrayOperation {    inp.pdf
import java.util.Scanner;public class ArrayOperation {     inp.pdfimport java.util.Scanner;public class ArrayOperation {     inp.pdf
import java.util.Scanner;public class ArrayOperation {    inp.pdf
 
OrderTest.javapublic class OrderTest {       Get an arra.pdf
OrderTest.javapublic class OrderTest {         Get an arra.pdfOrderTest.javapublic class OrderTest {         Get an arra.pdf
OrderTest.javapublic class OrderTest {       Get an arra.pdf
 
Go ahead, make my day
Go ahead, make my dayGo ahead, make my day
Go ahead, make my day
 
SumNumbers.java import java.util.Scanner;public class SumNumbe.pdf
SumNumbers.java import java.util.Scanner;public class SumNumbe.pdfSumNumbers.java import java.util.Scanner;public class SumNumbe.pdf
SumNumbers.java import java.util.Scanner;public class SumNumbe.pdf
 
PickerApp CLass.pdf
 PickerApp CLass.pdf PickerApp CLass.pdf
PickerApp CLass.pdf
 
Import java
Import javaImport java
Import java
 
ch05-program-logic-indefinite-loops.ppt
ch05-program-logic-indefinite-loops.pptch05-program-logic-indefinite-loops.ppt
ch05-program-logic-indefinite-loops.ppt
 
Example of JAVA Program
Example of JAVA ProgramExample of JAVA Program
Example of JAVA Program
 
Faisal
FaisalFaisal
Faisal
 
Lab01.pptx
Lab01.pptxLab01.pptx
Lab01.pptx
 
Newton cotes method
Newton cotes methodNewton cotes method
Newton cotes method
 
Class 5 2ciclo
Class 5 2cicloClass 5 2ciclo
Class 5 2ciclo
 

More from aptex1

Name- Gold leaved white PoplarGenus- Populusspecies- albaAutho.pdf
Name- Gold leaved white PoplarGenus- Populusspecies- albaAutho.pdfName- Gold leaved white PoplarGenus- Populusspecies- albaAutho.pdf
Name- Gold leaved white PoplarGenus- Populusspecies- albaAutho.pdfaptex1
 
MinOfFourNumbers.javaimport java.util.Scanner;public class MinOf.pdf
MinOfFourNumbers.javaimport java.util.Scanner;public class MinOf.pdfMinOfFourNumbers.javaimport java.util.Scanner;public class MinOf.pdf
MinOfFourNumbers.javaimport java.util.Scanner;public class MinOf.pdfaptex1
 
molarity = 0.252101.1 =0.001236 M percent =0.251002 =12.5 .pdf
molarity = 0.252101.1 =0.001236 M percent =0.251002 =12.5 .pdfmolarity = 0.252101.1 =0.001236 M percent =0.251002 =12.5 .pdf
molarity = 0.252101.1 =0.001236 M percent =0.251002 =12.5 .pdfaptex1
 
Initial concentration of Br- = 2.25 x 10e-3M1V1=M2V2therefore M2.pdf
Initial concentration of Br- = 2.25 x 10e-3M1V1=M2V2therefore M2.pdfInitial concentration of Br- = 2.25 x 10e-3M1V1=M2V2therefore M2.pdf
Initial concentration of Br- = 2.25 x 10e-3M1V1=M2V2therefore M2.pdfaptex1
 
Direct memory access (DMA) is a function that allows an inputoutput.pdf
Direct memory access (DMA) is a function that allows an inputoutput.pdfDirect memory access (DMA) is a function that allows an inputoutput.pdf
Direct memory access (DMA) is a function that allows an inputoutput.pdfaptex1
 
Ethanol is miscible in water because of the hydroxyl functional grou.pdf
Ethanol is miscible in water because of the hydroxyl functional grou.pdfEthanol is miscible in water because of the hydroxyl functional grou.pdf
Ethanol is miscible in water because of the hydroxyl functional grou.pdfaptex1
 
the diluted concentration of HCl = (0.120 M x 25..pdf
                     the diluted concentration of HCl = (0.120 M x 25..pdf                     the diluted concentration of HCl = (0.120 M x 25..pdf
the diluted concentration of HCl = (0.120 M x 25..pdfaptex1
 
Simple Nobody Knows The Answer .pdf
                     Simple Nobody Knows The Answer                   .pdf                     Simple Nobody Knows The Answer                   .pdf
Simple Nobody Knows The Answer .pdfaptex1
 
AnswerThe net equation of the reaction catalyzed by the protein c.pdf
AnswerThe net equation of the reaction catalyzed by the protein c.pdfAnswerThe net equation of the reaction catalyzed by the protein c.pdf
AnswerThe net equation of the reaction catalyzed by the protein c.pdfaptex1
 
Answer i command.The vi editor always starts in command mode. For.pdf
Answer i command.The vi editor always starts in command mode. For.pdfAnswer i command.The vi editor always starts in command mode. For.pdf
Answer i command.The vi editor always starts in command mode. For.pdfaptex1
 
Answer (b) systems security and control.The businesses are now a .pdf
Answer (b) systems security and control.The businesses are now a .pdfAnswer (b) systems security and control.The businesses are now a .pdf
Answer (b) systems security and control.The businesses are now a .pdfaptex1
 
A. Temeperature of refrigeratorsIntervalB. Horspower of .pdf
A. Temeperature of refrigeratorsIntervalB. Horspower of .pdfA. Temeperature of refrigeratorsIntervalB. Horspower of .pdf
A. Temeperature of refrigeratorsIntervalB. Horspower of .pdfaptex1
 
48. The answer is Botulism. Botulism ia an illness that brings about.pdf
48. The answer is Botulism. Botulism ia an illness that brings about.pdf48. The answer is Botulism. Botulism ia an illness that brings about.pdf
48. The answer is Botulism. Botulism ia an illness that brings about.pdfaptex1
 
2) b) Understory is rich in humidity hence supports fungi3) a) can.pdf
2) b) Understory is rich in humidity hence supports fungi3) a) can.pdf2) b) Understory is rich in humidity hence supports fungi3) a) can.pdf
2) b) Understory is rich in humidity hence supports fungi3) a) can.pdfaptex1
 
1.Discuss in detail Emile Durkheim and his contributions to the fiel.pdf
1.Discuss in detail Emile Durkheim and his contributions to the fiel.pdf1.Discuss in detail Emile Durkheim and his contributions to the fiel.pdf
1.Discuss in detail Emile Durkheim and his contributions to the fiel.pdfaptex1
 
1) YELLOW FEVERGroup Group IV ((+) ssRNA)Order   Unassigned.pdf
1) YELLOW FEVERGroup Group IV ((+) ssRNA)Order   Unassigned.pdf1) YELLOW FEVERGroup Group IV ((+) ssRNA)Order   Unassigned.pdf
1) YELLOW FEVERGroup Group IV ((+) ssRNA)Order   Unassigned.pdfaptex1
 
Thermodynamic entropy is a non-conserved state fu.pdf
                     Thermodynamic entropy is a non-conserved state fu.pdf                     Thermodynamic entropy is a non-conserved state fu.pdf
Thermodynamic entropy is a non-conserved state fu.pdfaptex1
 
I is oxidized by 6 electrons as the oxidation sta.pdf
                     I is oxidized by 6 electrons as the oxidation sta.pdf                     I is oxidized by 6 electrons as the oxidation sta.pdf
I is oxidized by 6 electrons as the oxidation sta.pdfaptex1
 
The main difference is that Democritus was not a .pdf
                     The main difference is that Democritus was not a .pdf                     The main difference is that Democritus was not a .pdf
The main difference is that Democritus was not a .pdfaptex1
 
for acidic nature is should give H+ equation H.pdf
                     for acidic nature is should give H+  equation  H.pdf                     for acidic nature is should give H+  equation  H.pdf
for acidic nature is should give H+ equation H.pdfaptex1
 

More from aptex1 (20)

Name- Gold leaved white PoplarGenus- Populusspecies- albaAutho.pdf
Name- Gold leaved white PoplarGenus- Populusspecies- albaAutho.pdfName- Gold leaved white PoplarGenus- Populusspecies- albaAutho.pdf
Name- Gold leaved white PoplarGenus- Populusspecies- albaAutho.pdf
 
MinOfFourNumbers.javaimport java.util.Scanner;public class MinOf.pdf
MinOfFourNumbers.javaimport java.util.Scanner;public class MinOf.pdfMinOfFourNumbers.javaimport java.util.Scanner;public class MinOf.pdf
MinOfFourNumbers.javaimport java.util.Scanner;public class MinOf.pdf
 
molarity = 0.252101.1 =0.001236 M percent =0.251002 =12.5 .pdf
molarity = 0.252101.1 =0.001236 M percent =0.251002 =12.5 .pdfmolarity = 0.252101.1 =0.001236 M percent =0.251002 =12.5 .pdf
molarity = 0.252101.1 =0.001236 M percent =0.251002 =12.5 .pdf
 
Initial concentration of Br- = 2.25 x 10e-3M1V1=M2V2therefore M2.pdf
Initial concentration of Br- = 2.25 x 10e-3M1V1=M2V2therefore M2.pdfInitial concentration of Br- = 2.25 x 10e-3M1V1=M2V2therefore M2.pdf
Initial concentration of Br- = 2.25 x 10e-3M1V1=M2V2therefore M2.pdf
 
Direct memory access (DMA) is a function that allows an inputoutput.pdf
Direct memory access (DMA) is a function that allows an inputoutput.pdfDirect memory access (DMA) is a function that allows an inputoutput.pdf
Direct memory access (DMA) is a function that allows an inputoutput.pdf
 
Ethanol is miscible in water because of the hydroxyl functional grou.pdf
Ethanol is miscible in water because of the hydroxyl functional grou.pdfEthanol is miscible in water because of the hydroxyl functional grou.pdf
Ethanol is miscible in water because of the hydroxyl functional grou.pdf
 
the diluted concentration of HCl = (0.120 M x 25..pdf
                     the diluted concentration of HCl = (0.120 M x 25..pdf                     the diluted concentration of HCl = (0.120 M x 25..pdf
the diluted concentration of HCl = (0.120 M x 25..pdf
 
Simple Nobody Knows The Answer .pdf
                     Simple Nobody Knows The Answer                   .pdf                     Simple Nobody Knows The Answer                   .pdf
Simple Nobody Knows The Answer .pdf
 
AnswerThe net equation of the reaction catalyzed by the protein c.pdf
AnswerThe net equation of the reaction catalyzed by the protein c.pdfAnswerThe net equation of the reaction catalyzed by the protein c.pdf
AnswerThe net equation of the reaction catalyzed by the protein c.pdf
 
Answer i command.The vi editor always starts in command mode. For.pdf
Answer i command.The vi editor always starts in command mode. For.pdfAnswer i command.The vi editor always starts in command mode. For.pdf
Answer i command.The vi editor always starts in command mode. For.pdf
 
Answer (b) systems security and control.The businesses are now a .pdf
Answer (b) systems security and control.The businesses are now a .pdfAnswer (b) systems security and control.The businesses are now a .pdf
Answer (b) systems security and control.The businesses are now a .pdf
 
A. Temeperature of refrigeratorsIntervalB. Horspower of .pdf
A. Temeperature of refrigeratorsIntervalB. Horspower of .pdfA. Temeperature of refrigeratorsIntervalB. Horspower of .pdf
A. Temeperature of refrigeratorsIntervalB. Horspower of .pdf
 
48. The answer is Botulism. Botulism ia an illness that brings about.pdf
48. The answer is Botulism. Botulism ia an illness that brings about.pdf48. The answer is Botulism. Botulism ia an illness that brings about.pdf
48. The answer is Botulism. Botulism ia an illness that brings about.pdf
 
2) b) Understory is rich in humidity hence supports fungi3) a) can.pdf
2) b) Understory is rich in humidity hence supports fungi3) a) can.pdf2) b) Understory is rich in humidity hence supports fungi3) a) can.pdf
2) b) Understory is rich in humidity hence supports fungi3) a) can.pdf
 
1.Discuss in detail Emile Durkheim and his contributions to the fiel.pdf
1.Discuss in detail Emile Durkheim and his contributions to the fiel.pdf1.Discuss in detail Emile Durkheim and his contributions to the fiel.pdf
1.Discuss in detail Emile Durkheim and his contributions to the fiel.pdf
 
1) YELLOW FEVERGroup Group IV ((+) ssRNA)Order   Unassigned.pdf
1) YELLOW FEVERGroup Group IV ((+) ssRNA)Order   Unassigned.pdf1) YELLOW FEVERGroup Group IV ((+) ssRNA)Order   Unassigned.pdf
1) YELLOW FEVERGroup Group IV ((+) ssRNA)Order   Unassigned.pdf
 
Thermodynamic entropy is a non-conserved state fu.pdf
                     Thermodynamic entropy is a non-conserved state fu.pdf                     Thermodynamic entropy is a non-conserved state fu.pdf
Thermodynamic entropy is a non-conserved state fu.pdf
 
I is oxidized by 6 electrons as the oxidation sta.pdf
                     I is oxidized by 6 electrons as the oxidation sta.pdf                     I is oxidized by 6 electrons as the oxidation sta.pdf
I is oxidized by 6 electrons as the oxidation sta.pdf
 
The main difference is that Democritus was not a .pdf
                     The main difference is that Democritus was not a .pdf                     The main difference is that Democritus was not a .pdf
The main difference is that Democritus was not a .pdf
 
for acidic nature is should give H+ equation H.pdf
                     for acidic nature is should give H+  equation  H.pdf                     for acidic nature is should give H+  equation  H.pdf
for acidic nature is should give H+ equation H.pdf
 

Recently uploaded

Alper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentAlper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentInMediaRes1
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptxVS Mahajan Coaching Centre
 
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfEnzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfSumit Tiwari
 
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Celine George
 
Hierarchy of management that covers different levels of management
Hierarchy of management that covers different levels of managementHierarchy of management that covers different levels of management
Hierarchy of management that covers different levels of managementmkooblal
 
How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17Celine George
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)eniolaolutunde
 
Crayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon ACrayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon AUnboundStockton
 
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTiammrhaywood
 
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...Marc Dusseiller Dusjagr
 
Proudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxProudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxthorishapillay1
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxSayali Powar
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxpboyjonauth
 
Presiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsPresiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsanshu789521
 
Meghan Sutherland In Media Res Media Component
Meghan Sutherland In Media Res Media ComponentMeghan Sutherland In Media Res Media Component
Meghan Sutherland In Media Res Media ComponentInMediaRes1
 
internship ppt on smartinternz platform as salesforce developer
internship ppt on smartinternz platform as salesforce developerinternship ppt on smartinternz platform as salesforce developer
internship ppt on smartinternz platform as salesforce developerunnathinaik
 
MARGINALIZATION (Different learners in Marginalized Group
MARGINALIZATION (Different learners in Marginalized GroupMARGINALIZATION (Different learners in Marginalized Group
MARGINALIZATION (Different learners in Marginalized GroupJonathanParaisoCruz
 
How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxmanuelaromero2013
 

Recently uploaded (20)

Alper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentAlper Gobel In Media Res Media Component
Alper Gobel In Media Res Media Component
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
 
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfEnzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
 
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
 
Hierarchy of management that covers different levels of management
Hierarchy of management that covers different levels of managementHierarchy of management that covers different levels of management
Hierarchy of management that covers different levels of management
 
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdfTataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
 
How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)
 
Crayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon ACrayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon A
 
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
 
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
 
Proudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxProudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptx
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptx
 
Presiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsPresiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha elections
 
Meghan Sutherland In Media Res Media Component
Meghan Sutherland In Media Res Media ComponentMeghan Sutherland In Media Res Media Component
Meghan Sutherland In Media Res Media Component
 
internship ppt on smartinternz platform as salesforce developer
internship ppt on smartinternz platform as salesforce developerinternship ppt on smartinternz platform as salesforce developer
internship ppt on smartinternz platform as salesforce developer
 
MARGINALIZATION (Different learners in Marginalized Group
MARGINALIZATION (Different learners in Marginalized GroupMARGINALIZATION (Different learners in Marginalized Group
MARGINALIZATION (Different learners in Marginalized Group
 
ESSENTIAL of (CS/IT/IS) class 06 (database)
ESSENTIAL of (CS/IT/IS) class 06 (database)ESSENTIAL of (CS/IT/IS) class 06 (database)
ESSENTIAL of (CS/IT/IS) class 06 (database)
 
How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptx
 

import java.util.Scanner;public class HornersPolynomial {   .pdf

  • 1. import java.util.Scanner; public class HornersPolynomial { /** * Calculate the polynomial using Horner's method * return */ public static double hornersMeth(double[] coeff, int n, double x) { double finalVal = coeff[n]; for(int i = (n - 1) ; i >= 0; i--) { finalVal = (finalVal * x) + coeff[i]; } return finalVal; } /** * Prints the polynomial * param coeff * param n * param x */ public static void printPoly(double[] coeff, int n, double x) { System.out.println(" Polynomial : "); System.out.print(coeff[n] + "x^" + n); for(int i = (n - 1) ; i >= 0; i--) { if(coeff[i] < 0) System.out.print(" - " + ((-1) * coeff[i]) + "x^" + i); else if(coeff[i] >= 0) System.out.print(" + " + coeff[i] + "x^" + i); } System.out.println(" where x = " + x); }
  • 2. public static void main(String[] args) { //Scanner for user input Scanner sc = new Scanner(System.in); //Get degree of the polynomial System.out.println(" Enter the degree of the polynomial: "); int n = sc.nextInt(); //Coefficicnets array double[] coeff = new double[n + 1]; for(int i = n ; i >= 0; i--) { System.out.println("Enter coefficient " + i + ": "); coeff[i] = sc.nextDouble(); } //Get value of x System.out.println(" Enter the value of x: "); double x = sc.nextDouble(); printPoly(coeff, n, x); System.out.println(" Value of the polynomila is " + hornersMeth(coeff, n, x)); //Close scanner sc.close(); } } SAMPLE OUTPUT: Enter the degree of the polynomial: 3 Enter coefficient 3: 2 Enter coefficient 2: -6 Enter coefficient 1: 2
  • 3. Enter coefficient 0: -1 Enter the value of x: 3 Polynomial : 2.0x^3 - 6.0x^2 + 2.0x^1 - 1.0x^0 where x = 3.0 Value of the polynomila is 5.0 Solution import java.util.Scanner; public class HornersPolynomial { /** * Calculate the polynomial using Horner's method * return */ public static double hornersMeth(double[] coeff, int n, double x) { double finalVal = coeff[n]; for(int i = (n - 1) ; i >= 0; i--) { finalVal = (finalVal * x) + coeff[i]; } return finalVal; } /** * Prints the polynomial * param coeff * param n * param x */ public static void printPoly(double[] coeff, int n, double x) { System.out.println(" Polynomial : "); System.out.print(coeff[n] + "x^" + n);
  • 4. for(int i = (n - 1) ; i >= 0; i--) { if(coeff[i] < 0) System.out.print(" - " + ((-1) * coeff[i]) + "x^" + i); else if(coeff[i] >= 0) System.out.print(" + " + coeff[i] + "x^" + i); } System.out.println(" where x = " + x); } public static void main(String[] args) { //Scanner for user input Scanner sc = new Scanner(System.in); //Get degree of the polynomial System.out.println(" Enter the degree of the polynomial: "); int n = sc.nextInt(); //Coefficicnets array double[] coeff = new double[n + 1]; for(int i = n ; i >= 0; i--) { System.out.println("Enter coefficient " + i + ": "); coeff[i] = sc.nextDouble(); } //Get value of x System.out.println(" Enter the value of x: "); double x = sc.nextDouble(); printPoly(coeff, n, x); System.out.println(" Value of the polynomila is " + hornersMeth(coeff, n, x)); //Close scanner sc.close(); } }
  • 5. SAMPLE OUTPUT: Enter the degree of the polynomial: 3 Enter coefficient 3: 2 Enter coefficient 2: -6 Enter coefficient 1: 2 Enter coefficient 0: -1 Enter the value of x: 3 Polynomial : 2.0x^3 - 6.0x^2 + 2.0x^1 - 1.0x^0 where x = 3.0 Value of the polynomila is 5.0