Write a program in java to calculate and display the factorials of all the numbers between 'm' and 'n' (where m 0, n>0).
Answers
Answer:
import java.util.Scanner;
public class SumOfFactorial
{
public static void main(String[] args)
{
// create scanner class object.
Scanner sc = new Scanner(System.in);
// enter the number.
System.out.print("Enter number : ");
int n = sc.nextInt();
int total=0;
int i=1;
// calculate factorial here.
while(i <= n)
{
int factorial=1;
int j=1;
while(j <= i)
{
factorial=factorial*j;
j = j+1;
}
// calculate sum of factorial of the number.
total = total + factorial;
i=i+1;
}
// print the result here.
System.out.println("Sum : " + total);
}
}
Question:-
Write a program in Java to calculate and display the factorial of all the numbers between m and n.
Program:-
import java.util.*;
class Factorial
{
static long factorial(int n)
{
long f=1L;
int i=1;
while(i++<=n)
f*=i;
return f;
}
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
System.out.print("Enter the starting range: ");
int m=sc.nextInt();
System.out.print("Enter the ending range: ");
int n=sc.nextInt();
for(int i=m;i<=n;i++)
System.out.println("Factorial of "+i+" is: "+fact(i));
}
}