fork download
  1. import java.util.Scanner;
  2.  
  3. class JavaApplication17 {
  4.  
  5. public static void main(String[] args) {
  6. Scanner in = new Scanner(System.in);
  7. int num;
  8. double rad30 = Math.toRadians(30);
  9. double rad45 = Math.toRadians(45);
  10. System.out.print("Digite: ");
  11. num = in.nextInt();
  12. System.out.println("Math.sin(30) " + Math.sin(rad30));
  13. System.out.println("sen(30) " + sen(rad30, num) + "\n");
  14. System.out.println("Math.sin(45) " + Math.sin(rad45));
  15. System.out.println("sen(45) " + sen(rad45, num));
  16. }
  17.  
  18. public static double fatorial(int n) {
  19. if (n == 0) {
  20. return 1;
  21. }
  22. return n * fatorial(n - 1);
  23. }
  24.  
  25. public static double potencia(double base, int expoente) {
  26. int i;
  27. double result = 1;
  28. if (expoente == 0) {
  29. return 1;
  30. } else if (base == 0) {
  31. return 0;
  32. } else {
  33. if (expoente < 0) {
  34. expoente = -expoente;
  35. base = 1 / base;
  36. }
  37. for (i = 1; i <= expoente; i = i + 1) {
  38. result = result * base;
  39. }
  40. return result;
  41. }
  42. }
  43.  
  44. public static double sen(double x, int qtdTermos) {
  45. double result = 0;
  46. int n;
  47. for (n = 0; n < qtdTermos; n = n + 1) {
  48. result = result + potencia(-1, n)
  49. / fatorial(2 * n + 1) * potencia(x, 2 * n + 1);
  50. }
  51. return result;
  52. }
  53.  
  54. /*public static double somatorio(int n) {
  55.   double result = 1.0;
  56.   int i, numerador = 2, denominador = 3, expoente = 3;
  57.   for (i = 1; i <= n - 1; i = i + 1) {
  58.   result = result
  59.   + potencia(numerador, expoente) / fatorial(denominador);
  60.   numerador = numerador + 1;
  61.   expoente = expoente + 1;
  62.   denominador = denominador + 2;
  63.   }
  64.   return result;
  65.   }*/
  66. }
Success #stdin #stdout 0.15s 38060KB
stdin
10
stdout
Digite: Math.sin(30) 0.49999999999999994
sen(30) 0.49999999999999994

Math.sin(45) 0.7071067811865475
sen(45) 0.7071067811865475