-
Notifications
You must be signed in to change notification settings - Fork 0
/
multiply_2_polynomial.c
63 lines (52 loc) · 1.09 KB
/
multiply_2_polynomial.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int main() {
int c1,c2,m,n,max;
scanf("%d%d",&c1,&c2);
m=c1+1;
n=c2+1;
int a[m],b[n],arra[m],arrb[n];
for(int i=0;i<m;i++){
scanf("%d",&a[i]);
}
for(int i=0;i<n;i++){
scanf("%d",&b[i]);
}
for(int i=m-1;i>=0;i--){
arra[m-1-i]=a[i];
}
for(int i=n-1;i>=0;i--){
arrb[n-1-i]=b[i];
}
//array sizing ends here
//multiplication starts here
max=m+n-1;
int mul[max],mulf[max];
for(int i=0;i<max;i++){
mul[i]=0;
}
for(int i=0;i<m;i++){
for(int j=0;j<n;j++){
mul[i+j]=mul[i+j]+arra[i]*arrb[j];
}
}
for(int i=max-1;i>=0;i--){
mulf[max-1-i]=mul[i];
}
for(int i=0;i<max;i++){
if(i==max-1){
printf("%d",mulf[i]);
}
else if(i==max-2)
{
printf("%dx + ",mulf[i]);
}
else
{
printf("%dx^%d + ",mulf[i],max-1-i);
}
}
return 0;
}