GeekInterview.com
  I am new, Sign me up!
 
GeekInterview.com  >  Interview Questions  >  Programming  >  C
Go To First  |  Previous Question  |  Next Question 
 C  |  Question 424 of 453    Print  
Sum of two numbers without using arithmetic operators
ex:
int a=10;
int b=10;
int sum=a+b;
without using "+" operator calculate sum



  
Total Answers and Comments: 11 Last Update: July 18, 2009     Asked by: meda_reddy 
  
 Sponsored Links

 
 Best Rated Answer
Submitted by: jintojos
 
void main()
 {
          int a=10,b=20;
          while(b--) a++;
           printf("Sum is :%d",a);  
 }

Above answer was rated as good by the following members:
ranganath_yadav, PAVAN86, Jaya_Kaja, rajani_vaddepalli15, pachaimuthu84
  Sorting Options  
  Page 1 of 2   « First    1    2    >     Last »  
July 17, 2008 06:41:48   #1  
jintojos Member Since: May 2008   Contribution: 29    

RE: sum of two numbers without using arithmetic operators
void main()
{
int a 10 b 20;
while(b--) a++;
printf("Sum is : d" a);
}

 
Is this answer useful? Yes | NoAnswer is useful 5   Answer is not useful 0Overall Rating: +5    
July 17, 2008 12:44:10   #2  
veeranarayana Member Since: July 2008   Contribution: 1    

RE: sum of two numbers without using arithmetic operators
int i j;
if(i<j)
{
for(i 0;i< j;i++)
j++;
cout<<j;
}
else
{
for(j 0;j< i;j++)
i++;
cout<<i;
}

 
Is this answer useful? Yes | NoAnswer is useful 1   Answer is not useful 0Overall Rating: +1    
July 18, 2008 12:22:08   #3  
ceren6 Member Since: July 2008   Contribution: 2    

RE: sum of two numbers without using arithmetic operators

#include <stdio.h>

int main ()

{

unsigned int t 0xffffffff;

unsigned int z 1;

int a 10 b 10 sum 0 r 0;


while (t) {

int t1 t2;


t1 a & 0x00000001;

t2 b & 0x00000001;


if ((t1 ^ t2 ^ r) 1) {

if (t1 1 && t2 1 && r 1)

r 1;

else

r 0;

sum sum | z;

} else {

if (t1 0 && t2 0 && r 0)

r 0;

else

r 1;

}


a >> 1;

b >> 1;

z << 1;

t >> 1;

}


printf ("sum: dn" sum);


return 0;

}


 
Is this answer useful? Yes | No
July 21, 2008 05:48:29   #4  
ceren6 Member Since: July 2008   Contribution: 2    

RE: sum of two numbers without using arithmetic operators
Arithmetic operators
Operator NameSyntaxOverloadableIncluded in C
Unary Plus +a Yes Yes
Addition (Sum) a + b Yes Yes
Prefix Increment ++a Yes Yes
Postfix Increment a++ Yes Yes
Assignment by Addition a + b Yes Yes
Unary Minus (Negation) -a Yes Yes
Subtraction (Difference) a - b Yes Yes
Prefix Decrement --a Yes Yes
Postfix Decrement a-- Yes Yes
Assignment by Subtraction a - b Yes Yes
Multiplication (Product) a * b Yes Yes
Assignment by Multiplication a * b Yes Yes
Division (Quotient) a / b Yes Yes
Assignment by Division a / b Yes Yes
Modulus (Remainder) a b Yes Yes
Assignment by Modulus a b Yes Yes

 
Is this answer useful? Yes | NoAnswer is useful 1   Answer is not useful 0Overall Rating: +1    
August 14, 2008 13:18:18   #5  
Saurabh Sharma Member Since: August 2008   Contribution: 1    

RE: sum of two numbers without using arithmetic operators
Use minus operator :-
int a 10 b 20 c;
c a-(-b);


 
Is this answer useful? Yes | No
August 22, 2008 01:50:32   #6  
sriranga.ch Member Since: August 2008   Contribution: 5    

RE: sum of two numbers without using arithmetic operators
void main()
{
int a 10 b 10;
a a<<1;
b b<<1;
printf("Sum of a&b is dn" (a&b));
}

 
Is this answer useful? Yes | No
August 25, 2008 00:36:04   #7  
shiva chitta Member Since: August 2008   Contribution: 2    

RE: sum of two numbers without using arithmetic operators
#include add(int a int b)
{
if(!a) return b;
else return add((a&b)<<1 a^b);
}
void main()
{ int a 2 b 5 c; c add(a b);
cout<<c;
}


hope it 'll help you right...........

 
Is this answer useful? Yes | No
September 09, 2008 15:45:00   #8  
rashmi.mohanty Member Since: September 2008   Contribution: 8    

RE: sum of two numbers without using arithmetic operators
void main(){
int a 5 b 7 i;
for(i 1;i< a;i++)
b++;
printf("sum value: d" b);
}

 
Is this answer useful? Yes | No
November 02, 2008 13:51:52   #9  
npatwardhan Member Since: November 2008   Contribution: 1    

RE: sum of two numbers without using arithmetic operators
for(i 0; i < a ;i++)
{

count++;
}
for(j 0;j< b;j++)
{
count++;
}

printf( d count);

 
Is this answer useful? Yes | No
July 10, 2009 11:53:01   #10  
sarav957 Member Since: February 2008   Contribution: 2    

RE: Sum of two numbers without using arithmetic operators

#include<stdio.h>

int main( )

{

int i 1 j 20 c;

c (i^j);

printf(" d" c);

return 0;

}



 
Is this answer useful? Yes | No
  Page 1 of 2   « First    1    2    >     Last »  


 
Go To Top


 Sponsored Links

 
About Us -  Privacy Policy -  Terms and Conditions -  Contact -  Ask Question -  Propose Category -  Site Updates 

Copyright © 2005 - 2009 GeekInterview.com. All Rights Reserved

Page copy protected against web site content infringement by Copyscape