How to add numbers without using "+" sign?

3 views (last 30 days)
Najeeb Ullah
Najeeb Ullah on 6 Nov 2015
Answered: Image Analyst on 6 Nov 2015
write a function which takes two inputs and returns the sum. Matlab inbuilt function are not allowed, you can only use for,if and while loop if necessary.

Answers (1)

Image Analyst
Image Analyst on 6 Nov 2015
I think the intent is that you can't use the sum() function and the length() or size() functions, not that you can't use mathematical operators like +.
So you can have a for loop to add the vectors. Since you can't use the length() or size() function, it's hard to know when to quit adding elements. I'd use a try catch inside the for loop. It will go to the catch when you try to index beyond the length of the array. In the catch, have a "break" command to exit the loop. Don't use sum as the name of your accumulator variable since it's the name of a built in function.

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!