# Find the sum of elements of the Matrix generated by the given rules

Given three integers **A**, **B** and **R**, the task is to find the sum of all the elements of the matrix generated by the given rules:

- The first row will contain a single element which is
**A**and the rest of the elements will be**0**. - The next row will contain
**two elements**all of which are**(A + B)**and the rest are**0s**. - Third row will contain
**(A + B + B)**three times and the rest are**0s**. - …..
- The matrix will contain only R rows.

**For example, ** if **A = 5**, **B = 3** and **R = 3** then the matrix will be:

5 0 0

8 8 0

11 11 11

**Examples:**

Input:A = 5, B = 3, R = 3

Output:54

5 + 8 + 8 + 11 + 11 + 11 = 54

Input:A = 7, B = 56, R = 1

Output:7

**Approach:** Initialise **sum = 0** and for every **1 ≤ i ≤ R** update **sum = sum + (i * A)**. After every iteration update **A = A + B**. Print the final sum in the end.

Below is the implementation of the above approach:

## C++

`// C++ implementation of the approach ` `#include <iostream> ` `using` `namespace` `std; ` ` ` `// Function to return the required sum ` `int` `sum(` `int` `A, ` `int` `B, ` `int` `R) ` `{ ` ` ` ` ` `// To store the sum ` ` ` `int` `sum = 0; ` ` ` ` ` `// For every row ` ` ` `for` `(` `int` `i = 1; i <= R; i++) { ` ` ` ` ` `// Update the sum as A appears i number ` ` ` `// of times in the current row ` ` ` `sum = sum + (i * A); ` ` ` ` ` `// Update A for the next row ` ` ` `A = A + B; ` ` ` `} ` ` ` ` ` `// Return the sum ` ` ` `return` `sum; ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` ` ` `int` `A = 5, B = 3, R = 3; ` ` ` `cout << sum(A, B, R); ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// JAVA implementation of the approach ` `import` `java.util.*; ` `import` `java.lang.*; ` `import` `java.io.*; ` ` ` `class` `GFG ` `{ ` ` ` `// Function to return the required sum ` `static` `int` `sum(` `int` `A, ` `int` `B, ` `int` `R) ` `{ ` ` ` ` ` `// To store the sum ` ` ` `int` `sum = ` `0` `; ` ` ` ` ` `// For every row ` ` ` `for` `(` `int` `i = ` `1` `; i <= R; i++) ` ` ` `{ ` ` ` ` ` `// Update the sum as A appears i number ` ` ` `// of times in the current row ` ` ` `sum = sum + (i * A); ` ` ` ` ` `// Update A for the next row ` ` ` `A = A + B; ` ` ` `} ` ` ` ` ` `// Return the sum ` ` ` `return` `sum; ` `} ` ` ` `// Driver code ` `public` `static` `void` `main (String[] args) ` ` ` `throws` `java.lang.Exception ` `{ ` ` ` `int` `A = ` `5` `, B = ` `3` `, R = ` `3` `; ` ` ` ` ` `System.out.print(sum(A, B, R)); ` `} ` `} ` ` ` `// This code is contributed by nidhiva ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 implementation of the approach ` ` ` `# Function to return the required ssum ` `def` `Sum` `(A, B, R): ` ` ` ` ` `# To store the ssum ` ` ` `ssum ` `=` `0` ` ` ` ` `# For every row ` ` ` `for` `i ` `in` `range` `(` `1` `, R ` `+` `1` `): ` ` ` ` ` `# Update the ssum as A appears i number ` ` ` `# of times in the current row ` ` ` `ssum ` `=` `ssum ` `+` `(i ` `*` `A) ` ` ` ` ` `# Update A for the next row ` ` ` `A ` `=` `A ` `+` `B ` ` ` ` ` `# Return the ssum ` ` ` `return` `ssum ` ` ` `# Driver code ` `A, B, R ` `=` `5` `, ` `3` `, ` `3` `print` `(` `Sum` `(A, B, R)) ` ` ` `# This code is contributed by Mohit Kumar ` |

*chevron_right*

*filter_none*

## C#

`// C# implementation of the approach ` `using` `System; ` ` ` `class` `GFG ` `{ ` ` ` `// Function to return the required sum ` `static` `int` `sum(` `int` `A, ` `int` `B, ` `int` `R) ` `{ ` ` ` ` ` `// To store the sum ` ` ` `int` `sum = 0; ` ` ` ` ` `// For every row ` ` ` `for` `(` `int` `i = 1; i <= R; i++) ` ` ` `{ ` ` ` ` ` `// Update the sum as A appears i number ` ` ` `// of times in the current row ` ` ` `sum = sum + (i * A); ` ` ` ` ` `// Update A for the next row ` ` ` `A = A + B; ` ` ` `} ` ` ` ` ` `// Return the sum ` ` ` `return` `sum; ` `} ` ` ` `// Driver code ` `public` `static` `void` `Main () ` `{ ` ` ` `int` `A = 5, B = 3, R = 3; ` ` ` ` ` `Console.Write(sum(A, B, R)); ` `} ` `} ` ` ` `// This code is contributed by anuj_67.. ` |

*chevron_right*

*filter_none*

**Output:**

54

## Recommended Posts:

- Find the element in the matrix generated by given rules
- Find determinant of matrix generated by array rotation
- Find sum of all elements in a matrix except the elements in row and/or column of given cell?
- Find the value at kth position in the generated array
- Find the Kth smallest element in the sorted generated array
- Find unique elements in a matrix
- Find the sum of the diagonal elements of the given N X N spiral matrix
- Given a Boolean Matrix, find k such that all elements in k'th row are 0 and k'th column are 1.
- Find a Square Matrix such that sum of elements in every row and column is K
- Find distinct elements common to all rows of a matrix
- Program to print elements of a Matrix row-wise skipping alternate elements
- Minimum difference between adjacent elements of array which contain elements from each row of a matrix
- Move matrix elements in given direction and add elements with same value
- Program to swap upper diagonal elements with lower diagonal elements of matrix.
- Find trace of matrix formed by adding Row-major and Column-major order of same matrix

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.