i'm a newbie in java and i got an assignment to do and i don't know how to start ...

the problem is:

Write a program that will compute for the product of two numbers without using the * operator..

Sample Output:

Enter Num1 :___
Enter num2 :___
Product is :___


hope you'll help me in this one :) :)

Recommended Answers

All 11 Replies

You can always make a loop and add the the first operator to itself second operator times

If you want to be fancier you can << the first operator the largest multiplier of 2 of the second operator and then use the initial loop for the reminder (not multiple of 2)

Dear jsefraijeen,

Try the below program .You will get the output .As a programmer you must try the logic or some coding steps .Then you post that code in daniweb so that it will be more helpfull for you.

import java.io.*;
import java.util.*;
class dani_add{
        public static void main(String args[])throws IOException{
        System.out.println("Enter the first number");
        BufferedReader obj = new BufferedReader(new InputStreamReader((System.in)));
        int a=Integer.parseInt(obj.readLine());
        System.out.println("Enter the Second number");
        int b=Integer.parseInt(obj.readLine());
        int c=0;
        for(int i=0;i<b;i++){
                c=c+a;
        }
        System.out.println("The Product is"+c);
        }
}

Thank you,

With Regards,
prem

Dear jsefraijeen,

Try the below program .You will get the output .As a programmer you must try the logic or some coding steps .Then you post that code in daniweb so that it will be more helpfull for you.

import java.io.*;
import java.util.*;
class dani_add{
        public static void main(String args[])throws IOException{
        System.out.println("Enter the first number");
        BufferedReader obj = new BufferedReader(new InputStreamReader((System.in)));
        int a=Integer.parseInt(obj.readLine());
        System.out.println("Enter the Second number");
        int b=Integer.parseInt(obj.readLine());
        int c=0;
        for(int i=0;i<b;i++){
                c=c+a;
        }
        System.out.println("The Product is"+c);
        }
}

Thank you,

With Regards,
prem

Thaks for your help guys :)

btw ,here's my code

import java.util.Scanner;


class Product
{
    public static void main(String[]args)
    {
        Scanner in = new Scanner(System.in);

        int result=0;
        int x=0;        

        System.out.print("Enter Num1 :");
        int num1 = in.nextInt();

        System.out.print("Enter Numb2 :");
        int num2 = in.nextInt();

        while(x<2)
        {

            result=result+num1;
            x++;


        }

        System.out.print(result);

    }


}
import java.util.Scanner;


class Product
{
public static void main(String[]args)
{
Scanner in = new Scanner(System.in);

int result=0;
int x=0;	

System.out.print("Enter Num1 :");
int num1 = in.nextInt();

System.out.print("Enter Numb2 :");
int num2 = in.nextInt();

while(x<2)
{

result=result+num1;
x++;


}

System.out.print(result);

}


}

Hi,

The above program does't work for negative values .

below is the solution

public static void main(String arg[]){

        System.out.println(""+multiply(-8,8));

    }

    public static  int multiply(int a, int b)
    {
        int temp = 0;
        if (b == 0) return 0;
        for (int ii = 0; ii < abs(b); ++ii) {

            temp = temp + a;
        }

        return b >= 0 ? temp : -temp;
    }

    public static  int abs(int val){

        return val>=0 ? val : -val;
    }
  1. No need for String conversion here: System.out.println(""+multiply(-8,8));
    PrintStream.println has an overload that takes int as argument.

  2. No need to roll your own abs, Math.abs(int) provides one.

  3. Another possible approach that satisfies the (2 years old) request: new BigInteger(a).multiply(new BigInteger(b)) [1] [2] [3]

[1] a and b denote a decimal String representation of BigInteger.
[2] I don't recommend this one though, as it is probably not very efficient, but it does the job.
[3] Some might argue that I'm cheating here ;D

This is code for multiplying two numbers without using multiplication operatorClick Here

import java.util.*;
class MultiplyWithoutMultiplicationOperator{
    public static void main(java.lang.String[] args){


        Scanner scn=new Scanner(System.in);
        System.out.println("Enter first n : ");
        int x=scn.nextInt();
        System.out.println("Enter 2nd n : ");
        int y=scn.nextInt();
        int z=0;
        if(x>0&&y>0){
            for(int i=0;i<y;i++){
                z=z+x;
            }
        }else if(x==0|| y==0){
            z=0;
        }else if(x<0||y<0){
            if(x<0&&y>0){
                for(int i=x;i<0;i++){
                    z+=y;
                }
                z=-z;
            }
            else if(x>0&&y<0){
                for(int i=y;i<0;i++){
                    z=z+x;
                }
                z=-z;
            }
            else if(x<0&&y<0){
                int temp=-x;
                for(int i=y;i<0;i++){
                    z=z+temp;
                }
            }
        }
        System.out.println(x+" * "+y+" = " +z);
    }
}

Do you really think anyone will be interested in trying to decode such a horrible pile of opaque code? Meaningless variable names and no comments. It's also far longer and more repetitive than the solutions already posted.
Nice try, but no cigar.

commented: Yeahhh, look like my code after first Java lecture +16

On that site not only did he post that horrible code, but he either ignored or failed to understand the requirement - which included "no loops".
Hopeless.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.