without using arithmatic operator convert an intger variable
x into x+1
Answers were Sorted based on User's Feedback
#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 |
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 |
what is ANSI and ISO
How are 16- and 32-bit numbers stored?
What is the difference between a free-standing and a hosted environment?
how can use subset in c program and give more example
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.
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 ?
Write a program to exchange two variaables without temp
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 “****”.
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 .
What are the features of c language?
Why preprocessor should come before source code?