Computer Science, asked by aditig1820, 8 days ago

you are given on integer N and integer array A as the input where N denotes the length of A.write a program to find the product of every element with its next consecutive integers on the number line and return the sum of these products​

Answers

Answered by Equestriadash
1

Co‎de: [in Python language]

\tt import\ numpy\ as\ np\\N\ =\ int(in put("Enter\ the\ number\ of\ elements:\ "))\\print()\\l\ =\ list()

\tt for\ i\ in\ range(N):\\{\ \ \ \ \ }nm\ =\ int(in put("Enter\ the\ number:\ "))\\{\ \ \ \ \ }l.append(nm)\\print()

\tt A\ =\ np.array(l)\\print(A,\ "is\ the\ given\ array.")\\print()\\prod\_dict\ =\ dict()\\for\ i\ in\ A:\\{\ \ \ \ \ }prod\_dict[i]\ =\ i*(i\ +\ 1)\\print(sum(prod\_dict.values()),\ "is\ the\ required\ out put.")

Explanation:

To create an array, the \tt numpy library is required. We retrieve the number of elements required, in the variable 'N'. A list is created to store the numbers that are going to be entered. The numbers are retrieved using a \tt for loop and as they're being entered, they're added to the list earlier created for the same using the \tt append() method. The required array, 'A', is created. We then create a dictionary 'prod_dict' to store the product of the elements with their consecutive numbers on the number line. The final sum is printed in an appropriate \tt print() statement.

Similar questions