without using arithmatic operator convert an intger variable
x into x+1
Answer Posted / vadivel t
Hi,
In addition to the Answer#2, which i have posted already,
here i am posting another program which can be used to add
any two nos without using arithmatic operators.
Note: The program written below follows a logic of binary
addition.
#include<stdio.h>
#include<conio.h>
void main()
{
int no1, no2, size, i;
int res = 0, carry = 0, temp1 = 0, temp2 = 0;
size = sizeof(int) * 8;
printf("ENTER THE TWO NOS TO BE ADDED: \n");
scanf("%d %d", &no1, &no2);
for(i = 0; i < size ; i++)
{
temp1 = (no1 & 0x01 << i) ? 1 : 0;
temp2 = (no2 & 0x01 << i) ? 1 : 0;
if((temp1 & temp2) == 1 && (carry == 1))
{
res = res | 0x01 << i;
carry = 1;
}
else if((temp1 & temp2) == 1 && (carry == 0))
{
res = res | 0x00;
carry = 1;
}
else if((temp1 | temp2) == 1 && (carry == 1))
{
res = res | 0x00;
carry = 1;
}
else if((temp1 | temp2) == 1 && (carry == 0))
{
res = res | 0x01 << i;
carry = 0;
}
else if((temp1 | temp2) == 0 && (carry == 1))
{
res = res | 0x01 << i;
carry = 0;
}
else if((temp1 | temp2) == 0 && (carry == 0))
{
res = res | 0x00;
carry = 0;
}
else
{
/*Fatal Error*/
}
}
printf("\nRESULT: %d", res);
_getch();
}
Is This Answer Correct ? | 1 Yes | 0 No |
Post New Answer View All Answers
Is there anything like an ifdef for typedefs?
Is there sort function in c?
Differentiate abs() function from fabs() function.
Add Two Numbers Without Using the Addition Operator
Why is structure padding done in c?
any restrictions have on the number of 'return' statements that may be present in a function. a) no restriction b) only 2 return statements c) only 1 return statements d) none of the above
What is d scanf?
What is array of structure in c programming?
What is array of structure in c?
How can you increase the allowable number of simultaneously open files?
How can I invoke another program (a standalone executable, or an operating system command) from within a c program?
How can I make sure that my program is the only one accessing a file?
What is switch in c?
Write a program to compute the similarity between two strings - The program should get the two strings as input - Then it will output one single number which is the percentage of similarity between the two strings
explain what are actual arguments?