Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.PrintStream;
- import java.util.Scanner;
- public class Problem4 {
- public static void main(String args[]) throws java.io.UnsupportedEncodingException {
- PrintStream ps = new PrintStream(System.out, true, "UTF-8");
- Scanner in = new Scanner(System.in);
- double pot, x, sum, coeff[];
- int n;
- boolean isNotCorrect;
- isNotCorrect = true;
- sum = 0;
- pot = 1;
- n = 0;
- x = 0;
- ps.println("Введите степень многочлена: ");
- do {
- try {
- n = Integer.parseInt(in.nextLine());
- if (n < 1)
- ps.println("Введите натуральное число!");
- else isNotCorrect = false;
- }
- catch (NumberFormatException err) {
- ps.println("Некорректный ввод данных!");
- }
- } while (isNotCorrect);
- isNotCorrect = true;
- coeff = new double[n];
- ps.println("Введите коэффициенты многочлена: ");
- for (int i = n - 1; i > -1; i--)
- do {
- try {
- coeff[i] = Double.parseDouble(in.nextLine());
- isNotCorrect = false;
- }
- catch (NumberFormatException err) {
- ps.println("Некорректный ввод данных!");
- }
- } while (isNotCorrect);
- isNotCorrect = true;
- ps.println("Введите значение переменной многочлена: ");
- do {
- try {
- x = Double.parseDouble(in.nextLine());
- isNotCorrect = false;
- }
- catch (NumberFormatException err) {
- ps.println("Некорректный ввод данных!");
- }
- } while (isNotCorrect);
- for (int i = 0; i < n; i++){
- for (int k = 0; k < i + 1; k++)
- pot = pot * x;
- sum = sum + coeff[i] * pot;
- pot = 1;
- }
- ps.print("Значение многочлена: ");
- ps.println(sum);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement