Advertisement
Guest User

Untitled

a guest
Dec 19th, 2014
168
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.59 KB | None | 0 0
  1. import acm.program.*;
  2. import acm.util.RandomGenerator;
  3.  
  4. public class Loop extends Program{
  5.  
  6. public void run() {
  7.  
  8. RandomGenerator rgen = RandomGenerator.getInstance();
  9. rgen.setSeed(System.nanoTime());
  10. println("Give me a non negative integer:");
  11. int n = readInt();
  12. while (n<0){
  13. println("Give me a non negative integer:");
  14. n = readInt();
  15. }
  16. int i = 1;
  17. int c = 0;
  18. int d = 0;
  19. println("Give me an integer ,other than zero");
  20. int x = readInt();
  21. if(x==0){
  22. println("Illegal Input" );
  23. int a = factorial(i,n);
  24. int b = power(i,n,x);
  25. println("Factorial: " + a + "Power: " + b);
  26. /* results of 5a and 5b*/
  27. double y = System.nanoTime();
  28. int k = rgen.nextInt(-1,1);
  29. int j =1;
  30. while( j < 1000000) {
  31. c = power(i,k,j)/factorial(i,j)+c;
  32. d = (power(i,-1,n)*power(i,k,2*n+1)/factorial(i,2*n+1))+d;
  33. j = j+1;
  34. }
  35. double l = System.nanoTime();
  36. double z = l - y;
  37. println("The base of e in the power of x equals: " + c);
  38. println("The sinus of x equals: " + d);
  39. println("Total Time equals: " + z);
  40. }
  41. }
  42.  
  43. public int factorial(int a, int b) {
  44. int product = 1;
  45. while (a<=b) {
  46. product = product * a;
  47. a = a + 1;
  48. }
  49. return product;
  50. }
  51.  
  52. public int power(int a, int b, int c){
  53. int product =1;
  54. if(b == 0) {
  55. product = 0 ;
  56. println ("Illegal Input");
  57. while (a<=b) {
  58. product = product * c;
  59. a = a + 1;
  60. }
  61. }
  62. return product;
  63. }
  64. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement