12.5.. Napisati program koji će za učitanu vrijednost K iN izračunati vrijednost izraza i ispisati::
Opis rješenja:
Listing programa:
package zadatak;
import java.util.Scanner;
public class P12500052 {
public static void main(String[] args) {
Scanner input = new Scanner (System.in);
System.out.println ("Sumu prema izrazu");
int k, n, znak=1;;
double s=0;
System.out.print("n: ");
n = input.nextInt();
System.out.print("Koeficijen: ");
k = input.nextInt();
for (int i=0; i<= n; i++){
s = s + znak * (i+1) /(double)(k + i); // s1+=znak * /(double)(i + 1);
znak = -znak;
}
System.out.println("n = " + n + " k = " + k + " s = " + s);
}
}
II Varijanta
package zadatak;
import java.util.Scanner;
public class P12500052_2; {
public static void main(String[] args) {
Scanner input = new Scanner (System.in);
System.out.println ("Sumu prema izrazu");
int k, n;
double s=0, s1=0, s2=0;
System.out.print("n: ");
n = input.nextInt();
System.out.print("Koeficijen: ");
k = input.nextInt();
for (int i=0; i<= n; i++){
if (i%2 != 0)
s1 = s1 + (i+1)/(double)(k+i);
else
s2 = s2 + (i+1)/(double)(k+i);
s = s1 - s2;
}
System.out.println("n = " + n + " k = " + k + " s = " + s);
}
}
Ispis na ekranu:
Index
|
|