CoolInterview.com - World's Largest Collection of Interview Questions
Send Free SMS
 Interview Questions  
 Our Services  


INTERVIEW QUESTIONS LANGUAGES C DETAILS
Question :
How I can add two numbers in c language without using Arithmetic operators?

Posted by: Shahul on 11/25/2007

Contact Shahul  Contact Shahul
Category C Interview Questions
Rating (4.9) By 11 users
Added on 11/25/2007
Views 8447
Rate it!
Answers:

by using bitwise operators it is possible, for example a=8,ie in binary(00001000) & b=4 in binary (00000100)if we add a,b then we get 12 in binary(00001100)..if u add the binary a,b & convert the answer in to decimal we get 12.. in the c code is:
void main()
{
int a=8,b=4,c;
clrscr();
c=a^b;
printf("%d",c);
getch();
}
so,by using ^ operator we can do that in c.



 Posted by: murali    

Contact murali  Contact murali

main()
{
int i,j,k;
printf("Enter the value of i and j");
scanf("%d%d",&i,&j);
if(i<j)
{
for(k=0;k<i;k++)
j++;
}
else
{
for(k=0;k<j;k++)
i++;
}
printf("The required sum is : ",i)
}



 Posted by: bablu    

Contact bablu  Contact bablu

#include<stdio.h>
unsigned int add(unsigned int a, unsigned int b)
{
unsigned int c=0;
unsigned int r=0;
unsinged int t=~0;
for (t=~0;t;t>>=1)
{
r<<=1;
r|=(a|b)&c|a&b)&1;
a>>=11;
b>>=1;
}
for(t=~0,c=~t;t;t>>=1)
{
c<<=1;
c|=r&1;
r>>1;
}
int main(int argc,char*argv[])
{
printf("%d+%d=%d
",atoi(argv[1]),atoi(argv[2]),add(atoi(argv[1]),atoi(argv[2])));
return(0);
}



 Posted by: Nirman    

Contact Nirman  Contact Nirman

int add(int a, int b)
{
int sum, carry;
sum = a ^ b;
carry = a & b;
while (carry != 0)
{
carry <<= 1;
a = sum;
b = carry;
sum = a ^ b;
carry = a & b;
}
return sum;
}



 Posted by: Dharmesh    

Contact Dharmesh  Contact Dharmesh

You should perform OR operation between these numbers.



 Posted by: Leon    

Contact Leon  Contact Leon

int main()
{
int i,a, b, sum=0,temp1,temp2,temp3;
int cin=0;

printf("enter input a an b");
scanf("%d
%d",&a,&b);
for(i=0;i<32;i++)
{
temp1=(a>>i)&(1);
temp2=(b>>i)&(1);

temp3 =cin^temp1^temp2;
sum=sum|temp3<<i;
if((temp1&&temp2)||(temp1&&cin)||(cin&&temp2)) cin=1;
else cin=0;
}
printf("%d",sum);
return 0;
}



 Posted by: vikram patel and preetam singh    

Contact vikram patel and preetam singh  Contact vikram patel and preetam singh

#include<stdio.h>
void main()
{
int num1 = 10; //First Number
int num2 = 5; //Second Number
int i; // loop counter
int s=0;
for (i=0;i<5;i++)
{
i++;
s++;
}
printf("Sum = %d",s);
}



 Posted by: Rohtash    

Contact Rohtash  Contact Rohtash

#include <stdio.h>
int add(int a, int b){
char carry=0;
int result=0,bitshift=0;
while ((a!=0)||(b!=0)){
result=(((a&0x1)^(b&0x1)^carry)<<bitshift)|result;
if (((a&0x1)&(b&0x1))||((a&0x1)&carry)||((b&0x1)&carry)) carry=1;else carry=0;
a=a>>1;b=b>>1;bitshift++;
}
result=(carry<<bitshift)|result;
return result;
}
int main(int argc, char **argv)
{int a=255,b=12;
printf("Total of %d and %d is: %d",a,b,add(a,b));getchar();
return 0;
}



 Posted by: DL    

Contact DL  Contact DL

int main()
{
int i,a, b, sum=0,temp1,temp2,temp3;
int cin=0;

printf("enter input a an b");
scanf("%d
%d",&a,&b);
for(i=0;i<32;i++)
{
temp1=(a>>i)&(1);
temp2=(b>>i)&(1);

temp3 =cin^temp1^temp2;
sum=sum|temp3<<i;
if((temp1&&temp2)||(temp1&&cin)||(cin&&temp2)) cin=1;
else cin=0;
}
printf("%d",sum);
return 0;
}



 Posted by: preetam singh ,vikram patel and pawan choursia    

Contact preetam singh ,vikram patel and pawan choursia  Contact preetam singh ,vikram patel and pawan choursia

sum of variables a, b
sum = a XOR b;



 Posted by: Hari Prasad M R    

Contact Hari Prasad M R  Contact Hari Prasad M R

two variables a,b

sum = (a^b)|((a&b)<<1);



 Posted by: Hari Prasad M R    

Contact Hari Prasad M R  Contact Hari Prasad M R

void main()
{
int n1,n2,i;
clrscr();
printf("enter the two no");
scanf("%d %d",&n1,&n2);
for(i=1;i<=n2;i++)
n2=n2+1;
printf("%d",n2);
}



 Posted by: vijay jain    

Contact vijay jain  Contact vijay jain

by using vadd() function



 Posted by: D.N.Pani    

Contact D.N.Pani  Contact D.N.Pani

#include<stdio.h>
main()
{
int i,j,k;
printf("Enter the value of i and j");
scanf("%d%d",&i,&j);
if(i>j)
{
for(k=0;k<i;k++)
{j++;
}
printf("The required sum is : %d",j);
}
else
{
for(k=0;k<j;k++)
{
i++;
}
printf("The required sum is : %d",i);
}
}



 Posted by: Rohtash    

Contact Rohtash  Contact Rohtash

By using Bitwise Operators, we can add 2 nos. But the usage of " ^ " bitwise will not give the correct answer for all values.



 Posted by: Subramanian N.    

Contact Subramanian N.  Contact Subramanian N.

#include<stdio.h>
main()
{
int a,b,i;
clrscr();
printf("enter the value of a and b");
scanf("%d %d",&a,&b);
int d=a;
for(i=1;i<=b;i++)
{
a++;
}
if(b==0)
printf("%d",d);
else
printf("%d",a);
}



 Posted by: sridevi    

Contact sridevi  Contact sridevi

void main()
{
int n1,n2,i;
scanf("%d %d",&n1,&n2);
for(i=0;i<n2;i++,n1++);
printf("%d",n1);
}



 Posted by: mohan    

Contact mohan  Contact mohan

main()
{
int a,b,c;
printf("enter any 2 numbers ");
scanf("%d%d",&a,&b);
while(b)
{
c= (a^b);
b= (a&b)<<1;
a=c;
}
printf("Sum of a and b is %d",c);
}



 Posted by: Sreenivas    

Contact Sreenivas  Contact Sreenivas

#include<stdio.h>
main()
{
int a,b;
clrscr();
scanf("%d %d",&a,&b);
a=a+b;
printf("a=%d,b=%d",a=a-b,b=a-b);
}



 Posted by: Govind    

Contact Govind  Contact Govind

main()
{
int a = 2, b = 3;
printf("%d",((a ^ b) | (a & b) << 1));
}



 Posted by: abhishek    

Contact abhishek  Contact abhishek

main()
{
int a,b,c;
scanf("%d%d",&a,&b);
for(i=1;i<=b;i++)
c=++a;
printf("Sum = %d",c);
}



 Posted by: Arasan    

Contact Arasan  Contact Arasan


If you have the better answer, then send it to us. We will display your answer after the approval.
Name :*
Email Id :*
Answer :*
Verification Code Code Image - Please contact webmaster if you have problems seeing this image code Not readable? Load New Code
Process Verification  Enter the above shown code:*
Inform me about updated answers to this question

   
Related Questions
View Answer
What is pointers and its uses?
View Answer
What is the difference between structure and union?
View Answer
What is pointer?
View Answer
Can we use functions within a structure?
View Answer
void main()
{
float a= 0.7;
if (a < 0.7)
printf("c");
else
printf("c++");
}
Output of the above program is c. Why? Whereas the same program with 0.8 instead of 0.7 gives c++ as the output? Why explain?
View Answer
Code for swapping of two numbers without using temporary variable using C.
View Answer
code To draw a three dimensional graph using c graphics
View Answer
How to write a C program for displaying a sentence without output command?
View Answer
What is difference between the test effort and the test procedure?
View Answer
What are the disadvantages of using Pointers.
View Answer

Please Note: We keep on updating better answers to this site. Subscribe to our newsletter to get notified when better answer is posted.

Notify me when better answer is posted!
Email:

View ALL C Interview Questions

User Options
Sponsored Links


Copyright ©2003-2010 CoolInterview.com, All Rights Reserved.
Privacy Policy | Terms and Conditions
Page URL: http://www.coolinterview.com/interview/12525/default.asp?cachecommand=bypass


Download Yahoo Messenger | Placement Papers| FREE SMS | ASP .Net Tutorial | Web Hosting | Free SMS | Dedicated Servers | Joke of the Day

0.72