Write a Java program to multiply N numbers without using * operator

By | 10.03.2017

Java program to multiply N numbers without using * operator


Write a Java program to multiply N numbers without using * multiplication operator.As we all know that there are lot of methods or techniques to solve problem to produce desired output.

Basic idea of this java program to show varies methods of problem solving techniques, although we have standard operator ” * ”  to perform the basic operation of multiplication.

Our aim is to do that without using ” * ”   operator we will show you multiplication operation on integer but you can extend it to your aspect of need.

In this program, first we input N integers from the user and store them into an Array.Then we make iterations inside loop and add the previous element k times to the Current element to produce desired output.

Here, below is the source code of Java Program to multiply N numbers without using * multiplication operator which is successfully compiled and run(Netbeans) on the Windows System to produce particular output.Let’s look at the program below.


SOURCE CODE : :


 

import java.util.Scanner;

public class Multiplication  
 {  
   public static void main(String[] args)   
   {  
       int i,n;
       Scanner sc = new Scanner(System.in);
       System.out.print("How many elements u want to multiply : ");
       n=sc.nextInt();
       
       int a[]=new int[n];
       
       System.out.println("Enter your elements below :---- \n");
       for(i=0;i<n;i++)
       {
           System.out.print("Enter "+(i+1)+" Element : ");
           a[i]=sc.nextInt();
       }
       
       for(i=1;i<n;i++)
       {
           int j=0,sum=0;
           while(j<a[i])
           {
               sum+=a[i-1];
               j++;
           }
           a[i]=sum;
       }
    System.out.println("\nMultiplication of "+n+" Numbers :"+ a[n-1] +" \n");   
   }  
 }

 


OUTPUT : :


 

How many elements u want to multiply : 6
Enter your elements below :---- 

Enter 1 Element : 1
Enter 2 Element : 2
Enter 3 Element : 3
Enter 4 Element : 4
Enter 5 Element : 5
Enter 6 Element : 6

Multiplication of 6 Numbers :720
4.7 3 votes
Article Rating
Subscribe
Notify of
guest

1 Comment
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Belal

The code gives 0 when there is a negative number
This can be corrected by changing the while loop condition ( j < Math.abs(a[i]))
and then you test the result will be negative or positive ( if the number of negative integers is odd >> it will be negative ,,, else >> positive )