Problem statement:
How to find out total number of square in chessboard / any other board of n x n.
How to find out total number of square in chessboard / any other board of n x n.
- class A {
- public static void main(String args[]) {
- int n = 8; // here we can give any value of n
- int n2 = n * (n + 1) * (2 * n + 1) / 6;
- System.out.println(n2);
- }
- }
Output:
204
Explanation - for n = 8, it will be
1 unit - 8 x 8 = 64
2 unit - 7 x 7 = 49
3 unit - 6 x 6 = 36
4 unit - 5 x 5 = 25
5 unit - 4 x 4 = 16
6 unit - 3 x 3 = 9
7 unit - 2 x 2 = 4
8 unit - 1 x 1 = 1
---------------------
total = 204
Asked in - Synechron technologiesExplanation - for n = 8, it will be
1 unit - 8 x 8 = 64
2 unit - 7 x 7 = 49
3 unit - 6 x 6 = 36
4 unit - 5 x 5 = 25
5 unit - 4 x 4 = 16
6 unit - 3 x 3 = 9
7 unit - 2 x 2 = 4
8 unit - 1 x 1 = 1
---------------------
total = 204
No comments:
Post a Comment