Question

In: Computer Science

Write a program to multiply a polynomial with a given number. Code needed in java.

Write a program to multiply a polynomial with a given number.

Code needed in java.

Solutions

Expert Solution

Write a program to multiply a polynomial with a given number

import java.util.Scanner;
// Import package for scanner
public class Main
{
static int max(int m, int n) {
return (m > n) ? m : n;
}
  // A[] represents coefficients of polynomial, m are the sizes of A[], n is the given number that multiply   
static int[] add(int A[], int m, int n) {
int size = max(m, n);
int sum[] = new int[size];
  // Initialize the polynomial
for (int i = 0; i < m; i++) {
sum[i] = A[i] *n;
}
return sum;
}
  
// method to print a polynomial
static void printPoly(int poly[], int n) {
for (int i = 0; i < n; i++) {
System.out.print(poly[i]);
if (i != 0) {
System.out.print("x^" + i);
}
if (i != n - 1) {
System.out.print(" + ");
}
}
}
public static void main(String[] args)

{
       int n;
Scanner s = new Scanner(System.in);
System.out.print("Enter no. of elements you want in array:");
n = s.nextInt();
int A[] = new int[n];
System.out.println("Enter all the elements:");
for(int i = 0; i < n; i++)
{
A[i] = s.nextInt();
  
}
// array that represents in polynomial 1 + 2x + 4x^2
int B;
System.out.print("Enter Number that want to multiply:");
B = s.nextInt();
int m = A.length;
System.out.println("Polynomial is");
printPoly(A, m);
int sum[] = add(A,m,B);
int size = max(m, B);
System.out.println("\nMultiply polynomial is");
printPoly(sum, m);
   }
}

Output :

I hope u like my effort..... Thank u


Related Solutions

Write a program to multiply a polynomial with a given number. Code needed in java.
Write a program to multiply a polynomial with a given number. Code needed in java.
Write a program to multiply two polynomials. Code needed in Java.
Write a program to multiply two polynomials. Code needed in Java.
I need a java code Write a simple program to prompt the user for a number...
I need a java code Write a simple program to prompt the user for a number between 1 and 12 (inclusive) and print out the corresponding month. For example:   The 12th month is December. Use a java "switch" statement to convert from the number (1-12) to the month. Also use a "do while" loop and conditional checking to validate that the number entered is between 1 and 12 inclusive and if not, prompt the user again until getting the correct...
Write a program in Java with a Scanner. Given an array and a number k where...
Write a program in Java with a Scanner. Given an array and a number k where k is smaller than the size of the array, write a program to find the k'th smallest element in the given array. It is given that all array elements are distinct. Example: Input: arr[] = {7,10,4,3,20,15} k = 3 Output: 7
Must be written in JAVA Code Write a program that takes a whole number input from...
Must be written in JAVA Code Write a program that takes a whole number input from a user and determines whether it’s prime. If the number is not prime, display its unique prime factors. Remember that a prime number’s factors are only 1 and the prime number itself. Every number that’s not prime has a unique prime factorization. For example, consider the number 54. The prime factors of 54 are 2, 3, 3 and 3. When the values are multiplied...
Write a C program to calculate the number of fence panels needed over a given distance....
Write a C program to calculate the number of fence panels needed over a given distance. The fence must be created using two difference size panels which have a 2 foot length difference between them (ie. panels could be 2’ and 4’ or 3’ and 5’ or 5’and 7’). Your program should request the total length of the fence and the smaller panel's size. Your program will then calculate the length of the larger panel and output the number of...
Parse string java code Write a recursive program that can calculate the value of a given...
Parse string java code Write a recursive program that can calculate the value of a given polynomial in a string, which is not more than the tenth order, for the given x. The polynomial will be given in the following format and should display the value of the polynomial for spaced-out x Using index of for -/+
P-3.36 Write a Java program for a matrix class that can add and multiply arbitrary twodimensional...
P-3.36 Write a Java program for a matrix class that can add and multiply arbitrary twodimensional arrays of integers.
Write code in MIPS ,read tow number from the user that do the following: 1- multiply...
Write code in MIPS ,read tow number from the user that do the following: 1- multiply 2- Dividing 3- sum 4- average 5- minimum 6- maximum 7- print message to thank the user for using my program
Write a recursive a Java code that checks if a number is Palindrome. A palindrome number...
Write a recursive a Java code that checks if a number is Palindrome. A palindrome number is a number that reads the same from beginning to end and from end to beginning, in other words, a palindrome number remains the same when its digits are reversed. For example, 13431 is a palindrome number. 2332 is another one. (Note: Your algorithm should define and work with an integer number) The functionality of your code should be commented to explain what you...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT