Advertisement
Guest User

Untitled

a guest
Oct 20th, 2018
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.17 KB | None | 0 0
  1. package zadatak2_Polic_Samardzic;
  2. import java.util.Scanner;
  3.  
  4. public class VrijednostPolinoma {
  5.  
  6.     public static void main(String[] args) {
  7.         // TODO Auto-generated method stub
  8.         System.out.print("Unesite broj clanova polinoma: ");
  9.         Scanner num = new Scanner(System.in);
  10.        
  11.         int n = num.nextInt();
  12.         float[] array = new float[n];
  13.        
  14.         for (int i = n-1; i >= 0; i--) {
  15.             if(i == 0) System.out.print("Unesite slobodni clan: ");
  16.             else if(i == 1) System.out.print("Unesite koeficijent uz  x: ");
  17.             else System.out.print("Unesite koeficijent uz  x^" + i + ": ");
  18.             num = new Scanner(System.in);
  19.             array[i] = num.nextFloat();
  20.         }
  21.  
  22.         System.out.print("Unesite tocku x: ");
  23.         num = new Scanner(System.in);
  24.         float x = num.nextFloat();
  25.         float res = calculate(array, n, x);
  26.         num.close();
  27.        
  28.         System.out.print("\n\n");
  29.         System.out.println("Rezultat funkcije za vrijednost x = " + x + " je " + res);
  30.         //System.out.printf("Rezultat funkcije za vrijednost x = %.2f je %.2f", x, res);
  31.     }
  32.  
  33.     public static float calculate(float[] array, int n, float x) {
  34.         float res = 0;
  35.         for (int i = 0; i < n; i++) {
  36.             res += array[i] * Math.pow(x, i);
  37.         }
  38.         return res;
  39.     }
  40. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement