Monday, 25 August 2014

Filled Under: ,

C Program to Add two numbers without using arithmetic Operators

Share

C Program to Add two numbers without using arithmetic Operators

C Program to Add two numbers without using arithmetic Operators

Program 1 : Using Recursive Function


#include<stdio.h>
int add(int, int);
int main()
{
   int num1, num2; 
   printf("\n ");
   scanf("%d %d", &num1, &num2); 
   printf("\nAddition of two number 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);

}
Output: 
Enter the two Numbers : 12 43
Addition of two number is : 55

program 2 : Using While Loop

 #include<stdio.h>
int main() {
   int num1 = 20, num2 = 8, i;
   while (num2 > 0) {
      num1++;
      num2--;
   }
   printf("%d", num1);
   return (0);
}


Output: 

Sum is :28

program 3 : Using While Loop

  #include<stdio.h>
int main() {
    int num1 = 20, num2 = 8, i;
    while (num2--) {
        num1++;
    }
    printf("Sum is : %d", num1);
    return (0);
}

Output: 
Sum is :28
program 4 : Using for Loop

  #include<stdio.h>
int sum(int, int);
int main() {
   int a, b;
   printf("Enter the two Numbers: ");
   scanf("%d %d", &a, &b);
   printf("Addition of two number is : %d", add(a, b));
   return(0);
}
int add(int num1, int num2) {
   int i;
   for (i = 0; i < num2; i++)
      num1++;
   return num1;
}

Output
 Enter the two Numbers:32 45
Addition of two number is :77