how to add numbers with out using any arthematic operators in c

Recommended Answers

All 20 Replies

try:

int sum(int num1,int num2)
{
	for(int i=0;i<num2;i++)
		num1++;
	return num1;
}

how to add numbers with out using any arthematic operators in c

I would be more concern in learning how to add numbers using the adding operator correctly.

Not a good way :( but it works fine.

int sum(int num1, int num2)
{
    int total, temp, temptotal;
    total = num1 ^ num2;
    for(temp = num1 & num2; temp != 0; temp = temptotal & temp) {
        temptotal = total;
        total = total ^ (temp = temp << 1);
    }
    return total;
}
commented: awesome code +1

>Not a good way
I can't really imagine a "good" way to answer this make-work question. Every possible solution is ridiculous and impractical compared to just using the addition operator. :icon_rolleyes:

It's really true.

Upon reflection I can not but wonder: Where does this trend of inquiring for obsolete techniques come from?
Pursuit of such skills are as irrelevant to a programmer as it would be for an aspirant mountaineer to learn how to make ropes out of camel's hair.

To future inquires, searching for the same topic, the following counsel is profitable:
Learn the basics, and learn them well; if you need to ask such a question, the basics you know not yet.

It's usually an effort to "help understand the underlying machine", perhaps get a feel for the hardware side of things. Or perhaps to get the student to better understand binary.

True, often lost is the fact that some of the specifics in this exercise are supposed to be shrugged off.

Pursuit of such skills are as irrelevant to a programmer as it would be for an aspirant mountaineer to learn how to make ropes out of camel's hair.

You wouldn't say that if you were lost in the Sahara Mountains! :icon_mrgreen:

You wouldn't say that if you were lost in the Sahara Mountains! :icon_mrgreen:

Lost and chasing a camel. Not good odds for surviving.

More off topic than previously:
If you plan to get lost, rope can be obtain readily available

>Where does this trend of inquiring for obsolete techniques come from?
Teachers who can't think of better exercises.

>Pursuit of such skills are as irrelevant to a programmer
I'll disagree to a point. Knowledge of the concepts behind computing are important for a well rounded programmer.

>If you plan to get lost, rope can be obtain readily available
Because intelligent people don't leave their houses without a good length of rope...just in case. :icon_rolleyes:

p.s. It's not a bad idea to carry around a flint and multi-tool though.

>If you plan to get lost, rope can be obtain readily available
Because intelligent people don't leave their houses without a good length of rope...just in case. :icon_rolleyes:

p.s. It's not a bad idea to carry around a flint and multi-tool though.

Even when the only connection I thought between getting lost, Sahara mountains, and my metaphor, was Sahara, desert; camels don't mind desert, perhaps primordial skills of survivor; I celebrated the humor intended and as such I went to the ridiculous extreme.
What's your excuse?
Regardless, I digressed.
Given the comment that Mr. Dave Sinkula made, which opinion I value, and now yours, Narue,
I have to conclude that my message was not understood as intended in its entirety.
Therefore, I beg the opportunity to make it clearer without metaphors, this time.
My exhortation in essence was this: Before looking for the knowledge of exotic tricks, and cleverness such:

How to print "Hello world!" without using a semicolon?
How to swap two numbers without using temporary variable?
How to add two numbers without using arithmetic operators?
How to find whether a given number is even or odd without using % (modulus) operator?
How to find whether the given number is a power of 2 in single step (without using loops)?
How to find greatest of two/three/four numbers without using relational operators?
How to print 1 to n without using any kind of loops or recursion?
Etc...

An aspirant programmer would benefit more of learning the basics, than rather pursuing the answer to unusable tricks ( for the most part ). Especially, when a comprehension of the basics will eventually lead to the answer to those questions.

I wasn't endorsing a ban to the understanding of how a computer works at the lowest level, nor at any level.

My exhortation in essence was this: Before looking for the knowledge of exotic tricks, and cleverness such:

How to print "Hello world!" without using a semicolon?
How to swap two numbers without using temporary variable?
How to add two numbers without using arithmetic operators?
How to find whether a given number is even or odd without using % (modulus) operator?
How to find whether the given number is a power of 2 in single step (without using loops)?
How to find greatest of two/three/four numbers without using relational operators?
How to print 1 to n without using any kind of loops or recursion?
Etc...

An aspirant programmer would benefit more of learning the basics, than rather pursuing the answer to unusable tricks ( for the most part ). Especially, when a comprehension of the basics will eventually lead to the answer to those questions.

Although I myself had a similar reaction to this type of homework when I first encountered it, I later recognized it as something a bit different from the usual silly trickery. One of the last times I encountered it was here. There is a bit of similarity with K&R exercises.

The 32-bit assumption may be dealt with separately, but the problem set from which these problem comes from I found ranged from simple to challenging. In essence, it imparted a greater familiarity with the binary operators. I found this to be a measure of good to walk away with, whatever the original intent of these exercises.

I realize the OP did not quite elaborate to clarify my assumption, so perhaps I just read in a little more than was necessary.

#include <iostream>

unsigned int addone(unsigned int x)
{
	unsigned int y = 0;
	unsigned int z = 0;

	for (int i = 0; i <= 31; ++i)
	{
		z = (x<<(31 - i))>>31<<(i);
		if (z > 0)
		{
			x = x ^ z;
		}
		else
		{
			y = x|0xffffffff;
			z = (y<<(31 - i))>>31<<(i);
			x = x ^ z;
			return x;
		}
		
	}
	return 0;
}

int main(int argc, char**argv)
{	
	unsigned int x = 0;


	while (true)
	{
		std::cout<<"enter a value, 0 to exit->";
		std::cin>>x;
		std::cout<<"\n";

		if (x <= 0) break;

		std::cout<<"x->"<<x<<"\n";
		std::cout<<"addone->"<<addone(x)<<"\n";	
	}
	return 0;
}

This will increment with out the math operators

This is a member of the ugly code club

commented: Pay attention. -2

I am afraid you're in the wrong forum. This is C only.

gerard4143, you're still using operator (-)

gerard4143, you're still using operator (-)

Whoops...busted

and the ++ operator in the for statement

  class Addition
  {
            public static void main(String args[])
            {
                     int a=12,b=30,sum;
                     Systen.out.println("ADDITION OF TWO NO.");
                     sum=(a^b)<<1;
                     System.out.println("sum="+sum);
            }
  }
var a = prompt("Enter value of a");
var b = prompt("Enter value of b");
var c = a - (-b);
alert(c);

@Gourav_4: That looks like Javascript, not C. You are using the minus (arithmetic) operator, so the answer is invalid.

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.