# C: Squaring string represented as positive integer

Tags:
1. Dec 30, 2015

### gruba

1. The problem statement, all variables and given/known data
Write a program that will square the input positive integer represented as a string.
2. Relevant equations
3. The attempt at a solution

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 (Text):

#include<stdio.h>
#define MAX 100000

typedef struct
{
int arr[MAX];
}NUMBER;

{
int i,digit=0;
char ch[101];
scanf("%s",ch);
while(ch[digit])
digit++;
for(i=0;i < MAX;i++)
{
digit--;
if(digit >= 0)
else
}
}

{
int carry=0;
int i,temp;
for(i=0;i < MAX;i++)
{
temp=a.arr[i]+b.arr[i]+carry;
carry=temp / 10;
}
}

{
int i;
for(;i>=0;i--)
}

int main()
{
NUMBER x,y,z;
printf("enter two positive integers: \n");
print(&z);
return 0;
}

2. Dec 30, 2015

### Staff: Mentor

No, there isn't.
To square a number represented as a string, you're going to have to do multiplication, digit by digit. For example, to multiply 12345 by 345, you need to do this:
1. multiply 12345 by 5.
2. multiply 12345 by 4, and multiply that result by 10.
3. multiply 12345 by 3, and then multiply that result by 100.
4. add together all of the partial products.