without using arithmatic operator convert an intger variable
x into x+1

Answers were Sorted based on User's Feedback



without using arithmatic operator convert an intger variable x into x+1..

Answer / vadivel t

#include<stdio.h>

void main()
{
int no;
int size, i;

printf("ENTER THE NO: ");
scanf("%d",&no);

size = sizeof(int) * 8;
for(i = 0; i < size; i++)
{
if((no & (0x01 << i)) != 0)
{
no = no^(0x01 << i);
}
else
{
no = no |(0x01 << i);
break;
}
}
printf("OUTPUT :%d \n", no);
_getch();
}

Is This Answer Correct ?    1 Yes 0 No

without using arithmatic operator convert an intger variable x into x+1..

Answer / 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

without using arithmatic operator convert an intger variable x into x+1..

Answer / geeta

x++

Is This Answer Correct ?    2 Yes 13 No

Post New Answer

More C Interview Questions

Write a program that takes a 5 digit number and calculates 2 power that number and prints it.

1 Answers  


write a c program to print the values in words eg:- 143 written it has (one hundred and forty three)& 104, 114 are also written words

5 Answers   Captronic, DELL, Google, IBM, Mithi, RCC, Wipro,


There is a 100-story building and you are given two eggs. The eggs (and the building) have an interesting property that if you throw the egg from a floor number less than X, it will not break. And it will always brake if the floor number is equal or greater than X. Assuming that you can reuse the eggs which didn't broke; you got to find X in a minimal number of throws. Give an algorithm to find X in minimal number of throws.

5 Answers   Yahoo,


What is #define in c?

0 Answers  


printf("%d",(printf("Hello")); What it returns?

32 Answers   TCS,






Why is malloc used?

1 Answers  


How do you determine whether to use a stream function or a low-level function?

0 Answers  


what is difference between overriding and overloading?

1 Answers  


What is floating point constants?

0 Answers  


When should we use pointers in a c program?

0 Answers  


Is r written in c?

0 Answers  


What does volatile do?

0 Answers  


Categories