Aahan is doing homework where he has to find the product of N integers. The problem is, he missed the class of MULTIPLICATION and don’t know how to do it. Can you write a code to help him?
       
 
  
    
     
    
Input Format
The first line of input consist of number of integers, N.
The second line of input consist of N space separated integers.
Constraints
1<= N <=10
1<= Integer <=25
Output Format
Print the product of N integers.
Code
public class Multiply {
    static int product(int arr[], int n) 
    { 
        int result = 1; 
        for (int i = 0; i < n; i++) 
            result = result * arr[i]; 
        return result; 
    } 
    public static void main(String[] args) 
    { 
         Scanner sc = new Scanner(System.in); 
        System.out.println("Enter the number of elements");
        int t = sc.nextInt();
        int []arr = new int[t];
         for(int i=0; i<t; i++ ) {
         arr[i] = sc.nextInt();}
        System.out.printf("%d", product(arr, t)); 
    } 
 }
Answer
 
 

0 Comments