In: Computer Science
Write a program to multiply a polynomial with a given number.
Code needed in java.
Here is the solution to above problem in JAVA. Please read the code comments for more information
GIVE A THUMBS UP!!!
Explanation
1) First you need to take input from the user for the maximum degree for the polynomial
2) Than create an array of coffecient for each polynomial component in degree
3) After that input from user all the coffecients for the polynomial
4) Take input for the multiplication factor for the polynomial
5) Traverse the array and multiply coffecient array each element to the user input multiplication factor
6) Traverse the array and display the polynomial
JAVA CODE
import java.io.*;
import java.util.*;
public class Main
{
public static void main(String[] args) {
System.out.println("Enter the degree polynomial:
");
int degree=0;
Scanner sc = new Scanner(System.in);
degree = sc.nextInt(); //take maximum degree as
input
//array to store coffecient
Double coff[]= new Double [degree+1];
for(int i=0;i<=degree;++i)
{
//take input for location i
System.out.print("Enter coffecient of degree " +
(degree-i) + ": ");
coff[i] = sc.nextDouble();
}
//input for a number to multiply with
System.out.print("Enter a number to multiply the
polynomial: " );
double multiply = sc.nextDouble();
//multiply the coeficient array and print the
answer
for(int i=0;i<=degree;++i)
{
coff[i]= coff[i]*multiply;
}
//print the equation
int x;
for(x=0;x<degree;++x)
{
System.out.print(coff[x] + "x^" + (degree-x) +
"+");
}
System.out.print(coff[x]);
}
}
SCREENSHOT OF OUTPUT