Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a program to calculate the factorial of a number. The factorial n! of a po

ID: 3842482 • Letter: W

Question

Write a program to calculate the factorial of a number. The factorial n! of a positive number (integer) is defined by n! = n*(n-1)*(n-2)*...*3*2*1, where 0! = 1. In function main prompt the user for a non-negative integer less than 15 for which the factorial is to be found. Use a while validation loop to assure the number entered is in the proper range. Then call a function to calculate the factorial. The input to the function is the integer n and the return value is n factorial. Use data type double for the factorial. In the function use a for loop to calculate the factorial. Make provision for the situation in which n = 0. Return the value of n factorial to function main and print. Test with n = 14.

Explanation / Answer

/* package whatever; // don't place package name! */

import java.util.*;
import java.lang.*;
import java.io.*;

class FactNumber
{
   public static void main (String[] args)
   {
       int i;
       double fact=1;
       Scanner sc = new Scanner(System.in);
       int number = sc.nextInt();
       if(number==0){
           fact=1;
           System.out.println("Factorial of "+number+" is: "+fact);
       }
       else if(number>0 && number<15){
           for(i=1;i<=number;i++){
               fact=fact*i;
           }
           System.out.println("Factorial of "+number+" is: "+fact);
       }
       else{
           System.out.println("Please enter number between 1 to 14");

       }
   }
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote