Monday, June 4, 2012

Product of two numbers without using Multiplication.

   Sounds difficult, right? But if you will first take a second to understand, It would be easier for you to know what would be the best way to perform the operation. Simple logic will be applied, its simpler if you will use the operation itself. But for the sake of the logic and a basic looping technique, the program will look like this.

Code:
import javax.swing.JOptionPane;

public class Product{

public static void main (String [] args){

String firstNum, secondNum;
int num1, num2,  product=0;

//convert string to int type variable
firstNum=JOptionPane.showInputDialog("enter first number");
num1=Integer.parseInt(firstNum);

secondNum=JOptionPane.showInputDialog("enter second number");
num2=Integer.parseInt(secondNum);

for(int i=0;i<num2;i++)
product += num1;
     }
}//end

No comments:

Post a Comment