#include<iostream>
using namespace std;
int * getno(int n)
{
int a[10],value;
int no=n;
for(int i=0;i<10;i++)
a[i]=0;
for(int i=9;i>=0 && no!=0;i--)
{
value=no%10;
no=no/10;
a[i]=value;
}
return a;
}
bool addno(int *a,int *b)
{
int c[10],r,carry=0,n;
for(int i=9;i>=0;i--)
{
c[i]=a[i]+b[i]+carry;
if(c[i]>9)
{
n=c[i];
r=n%10;
n=n/10;
c[i]=r;
carry=n;
}
if(c[0]>9)
break;
}
if(c[0]>9)
return false;
else
return true;
}
bool subno(int *a,int *b)
{
int c[10],flag=0;
int borrow;
for(int i=9;i>=0;i--)
{
if(a[i]<b[i])
{
a[i]=a[i]+10;
a[i-1]=a[i-1]-1;
}
c[i]=a[i]-b[i];
if(a[0]<b[0])
{
flag=1;
break;
}
}
if(flag==1)
return false;
else
return true;
}
int main()
{
long long int n,m;
cout << "\nenter any two nos:";
cin>>n>> m;
int *a=getno(n);
int *b=getno(m);
cout<< "first no is:";
for(int i=0;i<10;i++)
cout<< a[i];
cout<< "\n";
for(int i=0;i<10;i++)
cout<< b[i];
if(addno(a,b))
cout<< "\ntwo nos can be added :)";
else
cout<< "\noverflow";
if(subno(a,b))
cout<< "\ntwo nos can be subtracted:)";
else
cout<< "\nunderflow";
return 0;
}
Get Answers For Free
Most questions answered within 1 hours.