java iteration hands on 2.
question:
write the program to read the 5 numbers and find the
factorial of each.
input:
2
3
4
5
6
output:
2
6
24
120
720.
answer:
import java.util.Scanner;
public class factorial {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int arr[]=new int[5];
int arrforfact[]=new int[5];
for (int i = 0; i < 5; i++) {
int num=sc.nextInt();
arr[i]=num;
}
for (int i = 0; i < 5; i++) {
int numm=arr[i];
int temp=1;
for (int j = 1; j <=numm ; j++) {
temp=temp*j;
}
arrforfact[i]=temp;
}
for (int i = 0; i < 5; i++) {
System.out.println(arrforfact[i]);
}
}
}
Comments
Post a Comment