Friday, 23 August 2013

Write A Program To Add Two Numbers Without Using Addition Operator

Case 1: For only Positive Numbers

# include < iostream.h >
# include < conio.h >


void main ( )
{
int Num1,Num2,Num3,Num4;
Cout<<"Enter First Number:- ";
Cin>>Num1;
Num4 = Num1;
Cout<<"Enter Second Number:- ";
Cin>>Num2;

While ( Num2 >0 )
{
Num1++;
Num2--;
}
Cout<<"Addition of "<<Num4<<" And "<<Num2<<"is :- "<<Num1;
getch ( ) ;
}

OUTPUT :- 



Case 2 : If Only Second Number is Negative

# include < iostream.h >
# include < conio.h >

void main ( )
{
int Num1,Num2,Num3,Num4;
Cout<<"Enter First Number:- ";
Cin>>Num1;
Num4 = Num1;
Cout<<"Enter Second Number:- ";
Cin>>Num2;

While ( Num2 <0 )
{
Num1--;
Num2++;
}
Cout<<"Addition of "<<Num4<<" And "<<Num2<<"is :- "<<Num1;
getch ( ) ;
}

OUTPUT :-



Case 3: If only First Number is Negative 


# include < iostream.h >
# include < conio.h >

void main ( )
{
int Num1,Num2,Num3,Num4;
Cout<<"Enter First Number:- ";
Cin>>Num1;
Num4 = Num1;
Cout<<"Enter Second Number:- ";
Cin>>Num2;

While ( Num2 > 0 )
{
Num1++;
Num2--;
}
Cout<<"Addition of "<<Num4<<" And "<<Num2<<"is :- "<<Num1;
getch ( ) ;
}

OUTPUT :- 



Case 4 : If First And Second Number Both Are Negative
#include<iostream.h>

#include<conio.h>

main()

{
int a,b,d;
cout<<"Enter First Number:-  ";
cin>>a;
d=a
cout<<"Enter First Number:- ";
cin>>b;
if ((a>0) && (b>0))
{
while(b>0)
{
a++;
b--;
}
}

if ((a<0) && (b<0))
{
while(b<0)
{
a--;
b++;

}

}

 if(a<0)
 {
while(b>0)
   {
 a++;
 b--;
  }
  }

    if (b<0)
    {

while (b<0)
  {
 b++;
 a--;
 }
 }

Cout<<"Addition of "<<d<<" And "<<b<<"is :- "<<a;
getch();
}


Four Possible Output Combinations 







Share this

0 Comment to "Write A Program To Add Two Numbers Without Using Addition Operator"

Post a Comment