Sunday, May 12, 2019

how do you write a program to find out total number of square in a chessboard?

Problem statement:
How to find out total number of square in chessboard / any other board of n x n.
  1. class A {
  2.     public static void main(String args[]) {
  3.         int n = 8; // here we can give any value of n
  4.         int n2 = n * (n + 1) * (2 * n + 1) / 6;
  5.         System.out.println(n2);
  6.     }
  7. }
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 technologies

No comments:

Post a Comment

Blueprint for self-improvement

To learn faster: Make the process fun To understand yourself : Write To understand the world better : Read To build deeper connection : Lis...