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)>
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.
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 ) .
Check all of the following that are true for the series ∑n=1∞(n−3)cos(n*π)n^2 A. This series converges...
Check all of the following that are true for the series ∑n=1∞(n−3)cos(n*π)n^2 A. This series converges B. This series diverges C. The integral test can be used to determine convergence of this series. D. The comparison test can be used to determine convergence of this series. E. The limit comparison test can be used to determine convergence of this series. F. The ratio test can be used to determine convergence of this series. G. The alternating series test can be...
convergent or divergent infinity sigma n = 1 sqrt(n^5+ n^3 -7) / (n^3-n^2+n)
convergent or divergent infinity sigma n = 1 sqrt(n^5+ n^3 -7) / (n^3-n^2+n)
IN JAVA Part 1 The use of computers in education is referred to as computer-assisted instruction...
IN JAVA Part 1 The use of computers in education is referred to as computer-assisted instruction (CAI). Write a program that will help an elementary school student learn multiplication. Use a SecureRandom object to produce two positive one-digit integers (you will need to look up how to do this). The program should then prompt the user with a question, such as How much is 6 times 7? The student then inputs the answer. Next, the program checks the student’s answer....
Part 1                                          &n
Part 1                                                                                                                            Technology Inc. predicted 2017 variable and fixed costs are as follows:                                                                                                                                                                                                       Variable costs    Fixed costs                                                                                                    Manufacturing                                 480,000               315900                                                                                            Selling and Administrative             216,000               60500                                                                                              Total                                                   696,000               376,400                                                                                                                                                                                                                                 Technology Inc. produces a wide variety of computer interface devices. Per unit                                                                                                                                        manufacturing cost information about one of these products, a high-capacity flash drive is as follows:                                                                                                                                                                                                                                                                              Direct material                                                $10                                                                                                                ...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT