Program to add two numbers of up to 100 digits.Two Numbers should contain the same number of digits
#include<stdio.h>
#include<conio.h>
int getnum(char);
char getcha(int);
void main()
{
char str1[101],str2[101],str3[102]="";
int i,num1,num2,carry=0,sum,tot;
clrscr();
printf("Two numbers must contain the same number of digits:\n");
printf("Enter First number:\n");
gets(str1);
printf("Enter second number:\n");
gets(str2);
if(strlen(str1)==strlen(str2))
{
strrev(str1);
strrev(str2);
for(i=0;str1[i]!='\0'||str2[i]!='\0';i++)
{
num1=getnum(str1[i]);
num2=getnum(str2[i]);
tot=num1+num2+carry;
sum=tot%10;
carry=tot/10;
str3[i]=getcha(sum);
}
str3[i]=getcha(carry);
strrev(str3);
printf("Sum of entered two numbers is:\n");
puts(str3);
}
else
{
printf("Two Numbers are with different number of digits");
}
}
int getnum(char c) //for converting char into number
{
if(c>47&& c<58)
{
return (c-48);
}
else
{
printf("invalid character in Numbers");
exit();
return 10;
}
}
char getcha(int n) //for converting int into char
{
return n+48;
}
0 comments:
Post a Comment