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

what is ANSI and ISO

7 Answers   HCL,


How are 16- and 32-bit numbers stored?

0 Answers  


What is the difference between a free-standing and a hosted environment?

0 Answers   Aspire,


how can use subset in c program and give more example

0 Answers  


Design a program using an array that searches a number if it is found on the list of the given input numbers and locate its exact location in the list.

4 Answers  


how to convert binary to decimal and decimal to binary in C lanaguage

7 Answers   BPO, Far East Promotions, IBM, RBS,


What does. int *x[](); means ?

0 Answers   Wilco,


Write a program to exchange two variaables without temp

9 Answers   Geometric Software,


Create a registration form application by taking the details like username, address, phone number, email with password and confirm password (should be same as password).Ensure that the password is of 8 characters with only numbers and alphabets. Take such details for 3 users and display the details. While taking input password must appear as “****”.

0 Answers  


when i declare as: void main() { clrscr(); int a=10; printf("%d",a) } my problem that why generate a error in above programs. please tell me answer seriously .

4 Answers  


What are the features of c language?

0 Answers  


Why preprocessor should come before source code?

2 Answers  


Categories