12.7.. 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 P12700052 {
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();
int i=0;
while (i<= n;){
s = s + znak * (i+1) /(double)(k + i); // s1+=znak * /(double)(i + 1);
znak = -znak;
i++;
}
System.out.println("n = " + n + " k = " + k + " s = " + s);
}
}
II Varijanta
package zadatak;
import java.util.Scanner;
public class P12700052_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();
int i=0;
while ( i<= n; ){
if (i%2 != 0)
s1 = s1 + (i+1)/(double)(k+i);
else
s2 = s2 + (i+1)/(double)(k+i);
s = s1 - s2;
i++;
}
System.out.println("n = " + n + " k = " + k + " s = " + s);
}
}
Ispis na ekranu:
Index
|
|