Decode School

Decode School - The School for Problem Solvers
Welcome to My Blog.
Need Assignment / Project Guidance (or) Training in Python?

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,table,numberoftimes;
          System.out.println("Enter The Number Of Times And Table value");
          Scanner sc=new Scanner(System.in);
          table=sc.nextInt();
          numberoftimes=sc.nextInt();
          for(i=1;i<=numberoftimes;i++)
            {
			ans+=table;
            }
		System.out.println(ans);
      }
}
					    
					

Program Explanation

Instruction(s) inside the for block{} are executed repeatedly till the second expression (i<=table)is true.

Here i is initialized to 1 and incremented by 1 for each iteration, instructions inside the for block are executed unless i becomes greater than m.

so value of i (1,2,3,...m)will be add num2 with m time and printed in table format.