Problem : Given an array, construct another array where i-th element
is the product of all but i-th elementof original array
Example : array size = 5
array = 5 1 2 3 4
resultant array = 24 120 60 40 30
1*2*3*4 = 24, 5*2*3*4 = 120, 5*1*3*4 = 60, 5*1*2*4 = 40, 5*1*2*3 =30
#include<stdio.h>
int main()
{
int a[50], b[50], i, j, n, prod;
printf("Enter the size of the array ( < 50 ): ");
scanf("%d", &n);
printf("\nEnter the elements of the array : ");
for(i = 0; i < n; i++)
scanf("%d", &a[i]);
for(i = 0; i < n; i++)
{
prod = 1;
for(j = 0; j < n; j++)
{
if(j != i)
prod = prod * a[j];
}
b[i] = prod;
}
printf("\nThe resultant array\n");
for(i = 0; i < n; i++)
printf("%d ", b[i]);
return 0;
}
Written by Munia Balayil
No comments:
Post a Comment