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
Try your Solution
Strongly recommended to Solve it on your own, Don't directly go to the solution given below.
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.
Comments
Related Programs
- Java Program to print N Natural numbers
- Java Program to print N Whole numbers
- Java Program to print N Natural numbers in reverse
- Java Program to calculate sum of First N Natural numbers
- Java Program to calculate sum of N given numbers
- Java Program to print sum of N ODD numbers
- Java Program to print Sum of N Even numbers
- Java Program to print ODD numbers till N
- Java Program to print Even numbers till N
- Java Program to calculate sum of Even numbers till N
- Java Program to calculate sum of ODD numbers till N
- Java Program to print Hello World for N times
- Java Program to print the first digit of a given number
- Java Program to find whether the first digit of given number is odd or even
- Java Program to print Multiplication table of N
- Java Program to print English Alphabets between two Alphabets
- Java Program to Print the English Alphabets In Lower Case
- Java Program to Print the English Alphabets In Lower Case
- Java Program to find Exponentiation
- Java Program to print alphabets in reverse
- Java Program to N th Multiplication table till M
- Java Program to sum N Postive numbers, Skip Negative Numbers.
- Java Program to find The Sum Of Multiple Positive Number,Stops If user Enters Negative Numbers