Java Program to multiply two numbers without using * operator

Get two inputs num1 and num2, compute the product of num1 and num2 without using * operator

Sample Input 1:

5 6

Sample Output 1:

30

Program or Solution

				
				
					

import java.util.*;

class MulTab

 {

     public static void main(String args[])

     {

          int ans=0,i,num,numberoftimes;

          System.out.println("Enter The Number Of Times And Table value");

          Scanner sc=new Scanner(System.in);

          num=sc.nextInt();

          numberoftimes=sc.nextInt();

          for(i=1;i<=numberoftimes;i++)

            {

ans+=num;

            }

System.out.println(ans);

      }

}

Program Explanation

1. Get num and numberoftimes

2. Here the logic is add num to ans for numberoftimes

    Example

    3 * 4 =12

    3 + 3 + 3 + 3 =12 (adding 3 for 4 times)

    for(i=1;i<=numberoftimes;i++)

3. i is initialized to 1 and incremented by 1. iteration stops when i is greater than               numberoftimes.


4. in each iteration num is added to the ans. ans is initially 0.


5. finally print ans.