Question

In: Computer Science

Use Java for the following; Part 1 n!= n * (n –1)* (n–2)* ...* 3 *...

Use Java for the following;

Part 1

n!= n * (n –1)* (n–2)* ...* 3 * 2 * 1

For example, 5! = 5 * 4 * 3 * 2 * 1 = 120

Write a function called factorial that takes as input an integer. Your function should verify that the input is positive (i.e. it is greater than 0). Then, it should compute the value of the factorial using a for loop and return the value. In main, display a table of the integers from 0 to 30 along with their factorials. At some point around 15, you will probably see that the answers are not correct anymore. Think about why this is happening.

Solutions

Expert Solution

/*Make sure you hit like if solution works for you else comment in the comment section*/

public class Factorial {
        public static void main(String[]args) {
                for(int i=0;i<31;i++) {
                        System.out.println("Factorial of " + i + ": " + factorial(i));
                }
        }

        private static int factorial(int num) {
                int fact = 1;
                if(num < 0) {
                        System.out.println("Number should be greater than 0");
                        return -1;
                }
                if(num == 0 || num == 1) return 1;
                for(int i = num; i > 0; i--) {
                        fact = fact * i;
                }
                return fact;
        }
}

It will start giving wrong factorial after 12, it is because factorial of 13 goes out of range of an int data type. If you will use long instead of int you will get correct factorial upto value 20 since it's range is greater than int data type.


Related Solutions

(a) Find the limit of {(1/(n^(3/2)))-(3/n)+2} and use an epsilon, N argument to show that this...
(a) Find the limit of {(1/(n^(3/2)))-(3/n)+2} and use an epsilon, N argument to show that this is indeed the correct limit. (b) Use an epsilon, N argument to show that {1/(n^(1/2))} converges to 0. (c) Let k be a positive integer. Use an epsilon, N argument to show that {a/(n^(1/k))} converges to 0. (d) Show that if {Xn} converges to x, then the sequence {Xn^3} converges to x^3. This has to be an epsilon, N argument [Hint: Use the difference...
Show that (a)Sn=<(1 2),(1 3),……(1 n)>. (b)Sn=<(1 2),(2 3),……(n-1 n)> (c)Sn=<(1 2),(1 2 …… n-1 n)>
Show that (a)Sn=<(1 2),(1 3),……(1 n)>. (b)Sn=<(1 2),(2 3),……(n-1 n)> (c)Sn=<(1 2),(1 2 …… n-1 n)>
Prove that 1^3 + 2^3 + · · · + n^3 = (1 + 2 +...
Prove that 1^3 + 2^3 + · · · + n^3 = (1 + 2 + · · · + n)^2 for every n ∈ N. That is, the sum of the first n perfect cubes is the square of the sum of the first n natural numbers. (As a student, I found it very surprising that the sum of the first n perfect cubes was always a perfect square at all.)
A researcher has a strange habit to use a sample size between N^(1/3) and N^(1/2) ,...
A researcher has a strange habit to use a sample size between N^(1/3) and N^(1/2) , where N is the size of the population that she investigates. Under what circumstances, do you suggest her to use a correction factor for the variance of the sampling distribution while estimating a confidence interval for the population mean? What if she does not take your suggestion seriously? Describe the potential problems involved.
1- Show that (n^3+3n^2+3n+1) / (n+1) is O (n2 ). Use the definition and proof of...
1- Show that (n^3+3n^2+3n+1) / (n+1) is O (n2 ). Use the definition and proof of big-O notation. 2- Prove using the definition of Omega notation that either 8 n is Ω (5 n ) or not. please help be with both
3. The Hofstadter Conway sequence is defined by a(1)=a(2)=1 and (for n>2 by) a(n)=a(a(n-1))+a(n-a(n-1)). Write a...
3. The Hofstadter Conway sequence is defined by a(1)=a(2)=1 and (for n>2 by) a(n)=a(a(n-1))+a(n-a(n-1)). Write a function to quickly compute this sequence. >>> [hc(i) for i in range(1,20)] [1, 1, 2, 2, 3, 4, 4, 4, 5, 6, 7, 7, 8, 8, 8, 8, 9, 10, 11]
Qno.1 Part(A). IN jAVA if 1.Int abc; 2. Int def = 8; 3. abc = def;...
Qno.1 Part(A). IN jAVA if 1.Int abc; 2. Int def = 8; 3. abc = def; ➢ Describe the procedure how much memory will be allocated when these three lines of codes will execute. ➢ Describe what will happened after execution of each of these line of code in term of memory allocation and data storage Qno.1 Part(B) A capacitor is constructed from two conductive metal plates 30cm x 50cm which are spaced 6mm apart from each other, and uses...
Which codes add 1 to integer n? 1) n=n+1; 2) n++; 3)++n; 4) n+=1; 5) n=--n+2
Which codes add 1 to integer n? 1) n=n+1; 2) n++; 3)++n; 4) n+=1; 5) n=--n+2
Write a combinatorial proof for 1 n + 2 ( n − 1 ) + 3...
Write a combinatorial proof for 1 n + 2 ( n − 1 ) + 3 ( n − 2 ) + ⋯ + ( n − 1 ) 2 + n 1 = ( n + 2 choose 3 ) .
Use the following information for part a and b. The 1-year, 2-year, 3-year and 4-year zero...
Use the following information for part a and b. The 1-year, 2-year, 3-year and 4-year zero rates are 2%, 3%, 4% and 5% per annum (APR) with quarterly compounding/payment. A)What are the zero coupon bond prices with maturities of 1 year, 2 years, 3 years and 4 years? (Assume that you receive a face value of $100 for each of these bonds on their maturity dates). B)What are the corresponding per annum zero rates with continuous compounding? Please show work...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT