Question:3.
Program to take input for 5 elements using an array, display all the elements also calculate and print product of all the elements.
Problem In Hand
To take input for 5 elements using an array further display all the elements amd also calculate and print product of all the elements
Process and Solution Of Problem
* Using a for loop(any loop can be used) we take input for 5 elements
* Further we use for loop to display all the elements and calculate product of all the elements as the elements are traversed.
Program/Source Code
#include<stdio.h> int main() { int a[5],i,p=1; for(i=0;i<5;i++) { printf("Enter %d element ",i); scanf("%d",&a[i]); } for(i=0;i<5;i++) { printf("%d\n",a[i]); p=p*a[i]; } printf("Product = %d",p); return 0; }
Program Explanation
- As we have to take input for 5 elements we declare an array of size 5.
- memory locations are created from 0 to 4 i.e. a[0] to a[4].
- Now using a for loop we take input for elements
- value of variable is initilized as 1 i.e. p=1 (as product is to be calculated in the variable)
- further we use for loop again to dislay the elements and as elements are traversed their product is calculated in the variable p. (p=p*a[i];)
- And after the full traversal the product of all the elements is calculated in variable “p”.
- Now we display the value of variable “p” .
Output:
Enter 0 element 1
Enter 1 element 2
Enter 2 element 3
Enter 3 element 4
Enter 4 element 5
1
2
3
4
5
Product = 120