Ruby multiplication


#1

Sorry if I’m in the incorrect forum, but I’m new here :slight_smile:

I would need a function “multiplication” which returns the product of
her parametres “a” and “b”.

Can anybody help me please?


#2

Mi Gatt wrote in post #1185675:

Sorry if I’m in the incorrect forum, but I’m new here :slight_smile:

I would need a function “multiplication” which returns the product of
her parametres “a” and “b”.

Can anybody help me please?

def mult(a,b)
res=0
while true
return(res) if b<=0
res=res+a
b=b-1
end
end
p mult 7,7


#3

def add(a,b)
sa=’-’*a
sb=’-’*b
return “#{sa}#{sb}”.size
end


#4

def multiplication(a,b)
a*b
end


#5

Thanks that helped me!!


#6

Ronald F. wrote in post #1185684:

Ah, I love this. This is by far the best answer to the question.

==
def n(a) ‘-’*a end
def s(a) a.size end

def add(a,b) return “#{a}#{b}” end
def moin(a,b) return a[0…-s(add(b,’-’))] end

def mult(a,b)
res=’’
loop {
return(res) if b==’’
res =add(a,res)
b=moin(b,’-’)
}
end

p s( add(’----’,’–’) )
p s( add n(22), n(33) )
p s( moin n(22), n(1) )
p s( moin n(22), n(23) )
p s( mult n(3) , n(5) )


#7

Regis d’Aubarede wrote in post #1185677:

def add(a,b)
sa=’-’*a
sb=’-’*b
return “#{sa}#{sb}”.size
end

Ah, I love this. This is by far the best answer to the question.