GeekInterview.com
Series: Subject: Topic:
Question: 177 of 827

Sum of two numbers without using arithmetic operators

Ex:
int a=10;
int b=10;
int sum=a+b;
without using "+" operator calculate sum
Asked by: meda_reddy | Member Since Jul-2008 | Asked on: Jul 15th, 2008

View all questions by meda_reddy

Editorial / Best Answer

Answered by: jintojos

View all answers by jintojos

Member Since May-2008 | Answered On : Jul 17th, 2008

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

Showing Answers 1 - 18 of 18 Answers
jintojos

Answered On : Jul 17th, 2008

View all answers by jintojos

Void main() {          int a=10,b=20;          while(b--) a++;           printf("Sum is :%d",a);   }

Yes  9 Users have rated as useful.
  
Login to rate this answer.
veeranarayana

Answered On : Jul 17th, 2008

View all answers by veeranarayana

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;}

Yes  1 User has rated as useful.
  
Login to rate this answer.
ceren6

Answered On : Jul 18th, 2008

View all answers by ceren6

#include 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; } printf("sum: %dn", sum); return 0;}

  
Login to rate this answer.
ceren6

Answered On : Jul 21st, 2008

View all answers by ceren6

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

Yes  1 User has rated as useful.
  
Login to rate this answer.
Saurabh Sharma

Answered On : Aug 14th, 2008

View all answers by Saurabh Sharma

Use minus operator :-int a=10,b=20,c;c=a-(-b);

Yes  3 Users have rated as useful.
  
Login to rate this answer.
sriranga.ch

Answered On : Aug 22nd, 2008

View all answers by sriranga.ch

Void main(){int a=10,b=10;a=a<<1;b=b<<1;printf("Sum of a&b is %dn",(a&b));}

  
Login to rate this answer.
shiva chitta

Answered On : Aug 25th, 2008

View all answers by shiva chitta

#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...........

Yes  1 User has rated as useful.
  
Login to rate this answer.

Void main(){    int a=5,b=7,i;    for(i=1;i<=a;i++)         b++;    printf("sum value:%d",b);}

  
Login to rate this answer.
npatwardhan

Answered On : Nov 2nd, 2008

View all answers by npatwardhan

Code
  1. for (i = 0; i < a; i++)
  2. {
  3.     count++;
  4. }
  5. for (j = 0; j < b; j++)
  6. {
  7.     count++;
  8. }
  9. printf("%d", count);
  10.  

  
Login to rate this answer.
sarav957

Answered On : Jul 10th, 2009

View all answers by sarav957

Code
  1. #include<stdio.h>
  2. int main()
  3. {
  4.     int i = 1, j = 20, c;
  5.     c = (i ^ j);
  6.     printf("%d", c);
  7.     return 0;
  8. }
  9.  

  
Login to rate this answer.
ratnapaul

Answered On : Jul 18th, 2009

View all answers by ratnapaul

Code
  1. main()<br />{<br />cout<<"Enter the numbers to be added";<br /> int a,b,x,y;<br /> cin>>a>>b;<br /> <br />do{<br /> x=a&b;<br /> y=a^b;<br /> x=a<<1;<br /> y=b;<br />}while(a);<br />cotu<<"The sum is:"<<y;<br />}

  
Login to rate this answer.
luckypavan

Answered On : Oct 1st, 2010

View all answers by luckypavan

Code
  1. #include<stdio.h>
  2. main()
  3. {
  4.     int a, b, c, d, sum;
  5.     printf("n Please enter a,b values:");
  6.     scanf("%d %d", &a, &b);
  7.     if (b > a) {
  8.         c = b - a;
  9.     } else {
  10.         c = a - b;              // using minus operator
  11.     }
  12.     d = square(b) - square(a);  // using square function and - minus operator
  13.     sum = d / c;                // using division operator
  14.     printf("The sum of a and b is:%d", sum);
  15.     return 0;
  16. }

  
Login to rate this answer.
indravardhans

Answered On : Jan 15th, 2011

View all answers by indravardhans

Code
  1. #include<stdio.h>
  2.  
  3. int result = 0X0;
  4. int a, b, C = 0X0, aB, bB;
  5.  
  6. int main()
  7. {
  8.     int val = 0X1;
  9.  
  10.     scanf("%d%d", &a, &b);
  11.     while (val) {
  12.         aB = !!(a & val);
  13.         bB = !!(b & val);
  14.  
  15.         if (aB ^ bB ^ C)
  16.             result |= val;
  17.  
  18.         C = (aB & bB) | ((aB ^ bB) & C);
  19.  
  20.         val <<= 1;
  21.     }
  22.     printf("%d", result);
  23.  
  24.     return 0;
  25. }

  
Login to rate this answer.
rome2all

Answered On : Aug 16th, 2011

View all answers by rome2all

Code
  1. class Test {
  2.     public static void main(String ... z) {
  3.         System.out.println(4 || 8);     //use logical or symbol u can add it
  4. }}

  
Login to rate this answer.
sravan

Answered On : Aug 20th, 2011

Code
  1.          #include<iostream.h>
  2.          void main()
  3.  {
  4.       int i=20,j=30,m,n;
  5.                          m=i,n=j;
  6. if(i<j)
  7. {
  8.           for(i=1;i<=m;i++)
  9.                         j++;
  10.          cout<<j;
  11. }
  12. else
  13. {
  14.         for(j=1;j<=n;j++)
  15.                  i++;
  16.   cout<<i;
  17. }
  18.  }

  
Login to rate this answer.
mukesh kumar

Answered On : Mar 25th, 2012

Code
  1. int main(){
  2. int a,b;
  3. printf("Enter the two numbers:
  4. ");
  5.  
  6. scanf("%d",&a);
  7. scanf("%d",&b);
  8. printf("Sum is: %d",add(a,b));
  9. }

  
Login to rate this answer.
mahamad

Answered On : Jun 14th, 2012

Code
  1. #include<stdio.h>
  2. #include<conio.h>
  3. main()
  4. {
  5.     int a=20,b=10,c;
  6.     clrscr();
  7.     c=a-~b-1;             //it will change the sign of operator truly magic
  8.     printf("sum is %d",c);
  9.     getch();
  10.     return 0;
  11. }

  
Login to rate this answer.
And

Answered On : Aug 28th, 2012

For two positive numbers:

int main(){
int a = 10;
int b = 10;

printf("%d",a ^ b | ((a & b)

  
Login to rate this answer.

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

Related Open Questions

Ads

Connect

twitter fb Linkedin GPlus RSS

Ads

Interview Question

 Ask Interview Question?

 

Latest Questions

Interview & Career Tips

Get invaluable Interview and Career Tips delivered directly to your inbox. Get your news alert set up today, Once you confirm your Email subscription, you will be able to download Job Inteview Questions Ebook . Please contact me if you there is any issue with the download.