forked from prashantkalokhe/Hacktoberfest2022
-
Notifications
You must be signed in to change notification settings - Fork 0
/
PRODUCT OF ELEMENT USING O(1).c
55 lines (46 loc) · 1.06 KB
/
PRODUCT OF ELEMENT USING O(1).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
// 1.8 Write a program to display an array of n integers (n>1), where at every index of the
// array should contain the product of all elements in the array except the element at the
// given index. Solve this problem by taking single loop and without an additional array.
// Input Array : 3 4 5 1 2
// Output Array :40 30 24 120 60
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int pro(int ar[],int n)
{
int p=1;
for(int i=0;i<n;i++)
{
p=ar[i]*p;
}
for(int i=0;i<n;i++)
{
ar[i]=p/ar[i];
}
printf("\nOUTPUT ARRAY=");
for(int i=0;i<n;i++)
{
printf("%d ",ar[i]);
}
return 0;
}
int main() {
int n;
printf("Enter Array Size=");
scanf("%d",&n);
int ar[n];
for (int i = 0; i < n; ++i) {
ar[i]=i+1;
}
printf("INPUT ARRAY=");
for(int i=0;i<n;i++)
{
printf("%d ",ar[i]);
}
clock_t t;
t = clock();
pro(ar, n);
t = clock() - t;
double time_taken = ((double)t)/CLOCKS_PER_SEC; // in seconds
printf("\npro() function took %f seconds to execute \n", time_taken);
}