How to add two numbers without using arithmetic operators?
Answers were Sorted based on User's Feedback
Answer / selloorhari
#include <stdio.h>
int add(int a, int b)
{
if (!a)
return b;
else
return add((a & b) << 1, a ^ b);
}
int main()
{
unsigned int a,b;
printf("Enter the two numbers: \n");
scanf("%d",&a);
scanf("%d",&b);
printf("Sum is: %d",add(a,b));
}
Is This Answer Correct ? | 168 Yes | 30 No |
Answer / pakalapati vijaya rama raju
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,c=1;
clrscr();
printf("enter the two numbers");
scanf("%d%d",&a,&b);
while(c<=b)
{
a++;
c++;
}
printf("%d",a);
getch();
}
Is This Answer Correct ? | 16 Yes | 4 No |
Answer / selloorhari
Hi,
This is the code for a FULL ADDER circuit.
Is This Answer Correct ? | 22 Yes | 12 No |
Answer / pradeep
prashant answer is wrong suppose add two similar numbers
prashant answers will fail because addtion of two similar
bits according to the bitwise xor fails.
Is This Answer Correct ? | 17 Yes | 10 No |
Answer / selloorhari
Hi Nitish,
If we will do the LOGICAL OR function then we will get
either 1 or 0.
If we will do the BITWISE OR then we will get the largest of
the two..
For
ex:
Let us take, First number as 2 and Second number as 3..
Then as per the first case we will get 1 as the output.
10(2) || 11(3) -> 1(1)
As per the second case the output will be 3..
10(2) | 11(3) -> 11(3).
Ok
Is This Answer Correct ? | 14 Yes | 12 No |
Answer / srinu
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,i,j,k=0;
printf("enter 2 numbers");
scanf("%d%d",&a,&b);
if(a>b)
{
k=b;
for(i=1;i<=a;i++)
k++;
}
else
{
k=a;
for(j=1;j<=b;j++)
k++;
}
printf("sum of 2 numbers is %d",k);
}
Is This Answer Correct ? | 1 Yes | 2 No |
Answer / roopali
#include<stdio.h>
int sum(int num1, int num2);
int main()
{
int num1, num2, result;
printf("Enter the number:");
scanf("%d%d",&num1,num2);
result=sum(num1,num2);
printf("The sum of two numbers is:%d",result);
return 0;
}
int sum(int num1, int num2)
{
int i;
for(i=0;i<num2;i++)
{
num1++;
}
return num1;
}
Is This Answer Correct ? | 3 Yes | 5 No |
Answer / jayanth kothapalli
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,c;
clrscr();
Printf("Enter Two numbers:");
scanf("%d,%d",&a,&b);
c=-(-a-b);
printf("sum is = %d",c);
getch();
}
Is This Answer Correct ? | 2 Yes | 5 No |
Answer / chirantan
//program to add two numbers without using + operator//
#include<stdio.h>
main()
{
int a,b,c;
printf("\n enter two numbers to add\n");
scanf("%d %d", &a,&b);
c=((a*a)-(b*b))/(a-b);
}
Is This Answer Correct ? | 2 Yes | 5 No |
What is the maximum no. of arguments that can be given in a command line in C.?
Explain the Difference between the New and Malloc keyword.
How to removing white spces in c programming only bu using loops
How do you initialize pointer variables?
int main() { int x = (2,3,4); int y = 9,10,11; printf("%d %d",x,y); } what would be the output?
How can I call a function, given its name as a string?
How are Structure passing and returning implemented by the complier?
How do you generate random numbers in C?
how to set Nth bit of variable by using MACRO
what is Array?
Explain what is operator promotion?
If we have an array of Interger values, find out a sub array which has a maximum value of the array and start and end positions of the array..The sub array must be contiguious. Take the start add to be 4000. For Ex if we have an array arr[] = {-1,-2,-5,9,4,3,-6,8,7,6,5,-3} here the sub array of max would be {8,7,6,5} coz the sum of max contiguous array is 8+7+6+5 = 26.The start and end position is 4014(8) and 4020(5).
5 Answers Microsoft, Motorola,