Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import acm.program.*;
- import acm.util.RandomGenerator;
- public class Loop extends Program{
- public void run() {
- RandomGenerator rgen = RandomGenerator.getInstance();
- rgen.setSeed(System.nanoTime());
- println("Give me a non negative integer:");
- int n = readInt();
- while (n<0){
- println("Give me a non negative integer:");
- n = readInt();
- }
- int i = 1;
- int c = 0;
- int d = 0;
- println("Give me an integer ,other than zero");
- int x = readInt();
- if(x==0){
- println("Illegal Input" );
- int a = factorial(i,n);
- int b = power(i,n,x);
- println("Factorial: " + a + "Power: " + b);
- /* results of 5a and 5b*/
- double y = System.nanoTime();
- int k = rgen.nextInt(-1,1);
- int j =1;
- while( j < 1000000) {
- c = power(i,k,j)/factorial(i,j)+c;
- d = (power(i,-1,n)*power(i,k,2*n+1)/factorial(i,2*n+1))+d;
- j = j+1;
- }
- double l = System.nanoTime();
- double z = l - y;
- println("The base of e in the power of x equals: " + c);
- println("The sinus of x equals: " + d);
- println("Total Time equals: " + z);
- }
- }
- public int factorial(int a, int b) {
- int product = 1;
- while (a<=b) {
- product = product * a;
- a = a + 1;
- }
- return product;
- }
- public int power(int a, int b, int c){
- int product =1;
- if(b == 0) {
- product = 0 ;
- println ("Illegal Input");
- while (a<=b) {
- product = product * c;
- a = a + 1;
- }
- }
- return product;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement