In: Computer Science
Please submit
1) the source code (.java file), and
2) the screenshot of running results of each question.
(Factorials) Write an application that calculates the factorial of 20, and display the results. Note: 1) The factorial of a positive integer n (written n!) is equal to the product of the positive integers from 1 to n. 2) Use type long.
(Largest and Smallest Integers) Write an application that reads five integers and determines and prints the largest and smallest integers in the group. Use for loop.
//import for Scanner
import java.util.Scanner;
//main class
class Main {
//main method
public static void main(String arg[])
{
//declaration of number
long n,fact=1;
//create scanner object
Scanner sc = new Scanner(System.in);
//take input from user
System.out.println("Enter number :");
n = sc.nextLong();
//calculate factorial from number
for(int i=1;i<=n;i++)
{
fact=fact*i;
}
//print factorial on console
System.out.println("factorial = "+fact);
}
}
output of above code
Largest and Smallest Integers
//import for Scanner
import java.util.Scanner;
//main class
class Main {
//main method
public static void main(String arg[])
{
// declaring and creating array objects
int[] arr = new int[5];
int min=0, max=0, i=0;
//create scanner object
Scanner sc = new Scanner(System.in);
//take input from user
System.out.println("Enter five numbers :");
for(i=0; i<arr.length; i++)
{
// read input
arr[i] = sc.nextInt();
}
max=arr[0];
min=arr[0];
for(i =0; i<arr.length ; i++)
if(arr[i]>max)
{
max=arr[i];
}
else if(arr[i]<min)
{
min=arr[i];
}
//print factorial on console
System.out.println(" smallest integers = "+min);
System.out.println(" largest integers = "+max);
}
}
output: