wap to display series 1,2,6,24,120 in java
Answers
Question:-
- To print in Java the following series:
Series:-
1,2,6,24,120
Logic:-
d=d+d*I; //I is a loop variable
Code:-
package Coder;
public class Series
{
public static void main (String[] args)
{
int i,d=1;
for(i=1;i<=5;i++)
{
System.out.print(d+" ");
d+=d*i;
}
}
}
Variable description:-
• I :- Loop variable
• d :- Local variable used to store the numbers.
Output:-
1 2 6 24 120
•For verification, please check the attachments.
Required Answer:-
Question:
Write a Program to display the series in java.
1 2 6 24 120....N terms.
Program:
This is a simple program. Read this post to get your answer.
Here comes the program.
import java.util.*;
public class Series
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
System.out.print("Enter n - ");
int n=sc.nextInt(), d=1, i;
for(i=1;i<=n;i++,d*=i)
System.out.print(d+" ");
sc.close();
}
}
Logic:
It is a factorial series. Factorial of a number is the product of all the natural numbers from 1 to the number.
1! = 1
2! = 2
3! = 6
4! = 24
In this program, the d variable calculates the factorial and the it is displayed after each iteration.
Output is attached.