Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Multiplying BigNum

  1. #1
    Junior Member
    Join Date
    Nov 2020
    Posts
    10
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Question Multiplying BigNum

    Hello everybody !

    I am writing code to create a BigNum class and being able to add it with others and I also have to write it so I can multiply it with others, but I can't seem to get that working... Here is my code :
    public class BigNum {
        public static boolean showZero = true; // I am not sure if that is relevant
        public static final int MAX_DIGITS = 40;
        private int[] digits = new int [MAX_DIGITS];
        public BigNum(){
            for (int i=0; i < MAX_DIGITS; i++)
                digits[i]=0;
        }
        public BigNum(int a) {
            for (int i=0; i<MAX_DIGITS; i++){
                int stock = (int) (a %10);
                digits[i] = stock;
                a = a/10;
            }   
        }
        public String show(){
            String s ="";
            for (int i = MAX_DIGITS-1; i>=0; i--){
                if (digits[i] != 0) {
                    s = s + digits[i];
                }     
            }
            System.out.println("BigNum = "+s);
            return s;
        }
        public BigNum add(BigNum t){
            BigNum sum = new BigNum ();
            int a = 0;
            for (int i = 0; i< MAX_DIGITS; i++){
                sum.digits[i] = (this.digits[i]+ t.digits[i]+a)%10;
                a = (this.digits[i] + t.digits[i] + a)/10;
            }
            return sum;
        }
        public BigNum multiply(BigNum u){
            BigNum e = new BigNum ();
            for (int i = 0; i< 2; i++){             // the condition i<2 is wrong, but I don't know what to put in else
                BigNum mult = new BigNum ();
                int a = 0;         
                for (int k = 0; k<MAX_DIGITS; k++){
                    mult.digits[k] = (this.digits[i] * u.digits[k] + a)%10;
                    a = (this.digits[i] * u.digits[k] + a)/10;
                }
                e = e.somme(prod);
                System.out.println("e="+e.affiche());
            }
            return e;
        }
     
        public static void main(String[] args) { 
        BigNum a = new BigNum(123); 
        BigNum b = new BigNum(512); 
        a.show();
        b.show();
        BigNum c = a.add(b);
        c.show(); 
        BigNum d = a.multiply(b); 
        d.show(); 
        }
    }
    My results for the add method works good, but I can't seem to get the multiply method going... Somehow in the second cycle for the multiply I get 256 and not 1024... Also I don't know how to get the program to add a 0 at the right whenever a new cycle begins...

    Thank you for helping me in advance !!!

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Multiplying BigNum

    Can you explain the logic for the multiply method?

    The posted code has compiler errors. Please fix them so the code can be compiled and executed for testing.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Multiplying Strings
    By jbflores1115 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: May 31st, 2014, 07:00 PM
  2. Matrix multiplying using separate input classes and a GUI
    By havinFun in forum What's Wrong With My Code?
    Replies: 24
    Last Post: March 17th, 2012, 11:40 AM