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

How can I call a function, given its name as a string?

4 Answers   ABC Telecom,


I have a function which accepts a pointer to an int. How can I pass a constant like 5 to it?

3 Answers  


what is the value of b if a=5; b=++a + ++a

31 Answers   Infosys, TCS, Tech Mahindra,


What is difference between scanf and gets?

0 Answers  


How can I do graphics in c?

0 Answers  


what would be the output of the following program main() { int a[] = {1,2,3,4,5}; int *ptr = {a,a+1,a+2,a+3,a+4}; printf("%d %d %d %d",a,*ptr,**ptr,ptr); } }

1 Answers  


What are the advantages of using linked list for tree construction?

0 Answers  


An array name contains base address of the array. Can we change the base address of the array?

4 Answers   NMIMS, Wipro,


Write the program with at least two functions to solve the following problem. The members of the board of a small university are considering voting for a pay increase for their 10 faculty members. They are considering a pay increase of 8%. Write a program that will prompt for and accept the current salary for each of the faculty members, then calculate and display their individual pay increases. At the end of the program, print the total faculty payroll before and after the pay increase, and the total pay increase involved.

0 Answers   Convergys,


Write the following function in C. stripos — Find position of first occurrence of a case- insensitive string int stripos ( char* haystack, char* needle, int offset ) Returns the numeric position of the first occurrence of needle in the haystack string. Note that the needle may be a string of one or more characters. If needle is not found, stripos() will return -1. The function should not make use of any C library function calls.

4 Answers   OpenFeel,


typedef struct{ char *; nodeptr next; } * nodeptr ; What does nodeptr stand for?

0 Answers   Wilco,


Is multithreading possible in c?

0 Answers  


Categories