- #1
gruba
- 206
- 1
Homework Statement
Write a program that will square the input positive integer represented as a string.
Homework Equations
3. The Attempt at a Solution [/B]
Is there another way of squaring a string number instead of multiplying it by itself?
Something like a pow() function for strings?
If not, is it possible to the following program that adds two string numbers in order to find the product:
Code:
#include<stdio.h>
#define MAX 100000
typedef struct
{
int arr[MAX];
}NUMBER;
void read(NUMBER *add_num)
{
int i,digit=0;
char ch[101];
scanf("%s",ch);
while(ch[digit])
digit++;
for(i=0;i < MAX;i++)
{
digit--;
if(digit >= 0)
add_num->arr[i]=ch[digit]-'0';
else
add_num->arr[i]=0;
}
}
void addition(NUMBER a,NUMBER b,NUMBER *add_res)
{
int carry=0;
int i,temp;
for(i=0;i < MAX;i++)
{
temp=a.arr[i]+b.arr[i]+carry;
add_res->arr[i]=temp % 10;
carry=temp / 10;
}
}
void print(NUMBER *add_num)
{
int i;
for(i=MAX-1;add_num->arr[i]==0;i--);
for(;i>=0;i--)
printf("%d",add_num->arr[i]);
}
int main()
{
NUMBER x,y,z;
printf("enter two positive integers: \n");
read(&x);
read(&y);
printf("addition result: ");
addition(x,y,&z);
print(&z);
return 0;
}