Interviews Questions, Algorithms, Aptitude, C Interview Program, C Theory Question, Aptitude Tricks, Test Series,

Saturday, 6 July 2019

Add two number Without Using Addition Operator

Question:













Solution:

1. Recursion

int add(int, int);
int main() {
   int num1, num2;

   printf("\nEnter the two Numbers : ");
   scanf("%d %d", &num1, &num2);

   printf("\nAddition of two num is : %d", add(num1, num2));
   return (0);
}

int add(int num1, int num2) {
   if (!num1)
      return num2;
   else
      return add((num1 & num2) << 1, num1 ^ num2);
}


2. Looping 1

int main() {
   int num1 = 10, num2 = 5, i;

   while (num2 > 0) {
      num1++;
      num2--;
   }

   printf("%d", num1);
   return (0);
}


3. Looping 2

int main() {
int num1 = 10, num2 = 5, i;

while (num2--) {
num1++;
}

printf("Sum is : %d", num1);
return (0);
}


4. Looping 3

int sum(int, int);
int main() {
   int a, b;

   printf("Enter the two Numbers: ");
   scanf("%d %d", &a, &b);

   printf("Addition of two num. is : %d", add(a, b));
   return(0);
}

int add(int num1, int num2) {
   int i;
   for (i = 0; i < num2; i++)
      num1++;
   return num1;
}


5. Using Subtraction for Addition!

int main() {
   int num1 = 10, num2 = 5;

   num1 = num1 - (-num2);
   printf("Sum is : %d",num1);

   return (0);
}

6. Using Multiplication and Subtraction for Addition!
//Rakshita

#include<stdio.h> int main() { int w,x,y,z; printf("enter 2 numbers\n"); scanf(%d%d",x,y); w=x*y-x-y; z=x*y-w; printf("%d",z); return 0; }

7. Using Complimentary function
//Shravya Shetty

#include<stdio.h> void main(){ int a=5,b=0; int res; res=a - ~b -1; printf("Answer%d",res); }

0 comments:

Post a Comment