Related Articles
Squares of numbers with repeated single digits | Set 1 (3, 6 and 9)
• Difficulty Level : Medium
• Last Updated : 24 Dec, 2018

Given a number made of single digits, find its square. It may be assumed that the single digits are 3, 6 and 9. Numbers can be very large i.e. can exceed long long int.

Examples:

```Input : 33 66 99
Output :
Square of 33 is : 1089
Square of 66 is : 4356
Square of 99 is : 9801

Input : 333 666 999
Output :
Square of 333 is : 110889
Square of 666 is : 443556
Square of 999 is : 998001```

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

Method 1 (Writing as multiples of 1111…1)
An interesting fact is, every such number can be represented as a multiple of 1111…1. For example, 33333 = 3 * 11111. Squares of 11, 111, 1111, 11111 … are 121, 12321, 1234321, 123454321, … respectively. So a simple solution is find square of 111…11 then multiply the result with 3 or 6 or 9 (We can use multiplication with large number).

Method 2 (Using digit patterns)
For 333….333 Count the no. of digits and print in the below manner:
Suppose no. of digit is n then write the n-1 times 1 and then write one time 0 and then write n-1 time 8 and in last write 9.
Example :
{ 3333 } = 11108889

For 666….666 Count the no. of digits and print in the below manner:
Suppose no of digit is n then write the n-1 times 4 and then write one time 3 and then write n-1 time 5 and in last write 6.
Example :
{ 6666 } = 44435556

For 999….999 Count the no. of digits and print in the below manner:
Suppose no of digit is n then write the n-1 times 9 and then write one time 8 and then write n-1 time 0 and in last write 1.
Example :
{ 9999 } = 99980001
Below is the implementation of above approach:

## C++

 `// C++ program to find square of ``// these large numbers``#include ``using` `namespace` `std;`` ` `// Function to find the square of``// 333...333, 666...666 and 999...999``string find_Square_369(string num)``{``    ``char` `a, b, c, d;`` ` `    ``// if the number is 333...333``    ``if` `(num == ``'3'``)``        ``a = ``'1'``, b = ``'0'``, c = ``'8'``, d = ``'9'``;`` ` `    ``// if the number is 666...666``    ``else` `if` `(num == ``'6'``)``        ``a = ``'4'``, b = ``'3'``, c = ``'5'``, d = ``'6'``;`` ` `    ``// if the number is 999...999``    ``else``        ``a = ``'9'``, b = ``'8'``, c = ``'0'``, d = ``'1'``;`` ` `    ``// variable for hold result``    ``string result = ``""``;`` ` `    ``// find the no of digit``    ``int` `size = num.size();`` ` `    ``// add size-1 time a in result``    ``for` `(``int` `i = 1; i < num.size(); i++)``        ``result += a;`` ` `    ``// add one time b in result``    ``result += b;`` ` `    ``// add size-1 time c in result``    ``for` `(``int` `i = 1; i < num.size(); i++)``        ``result += c;`` ` `    ``// add one time d in result``    ``result += d;`` ` `    ``// return result``    ``return` `result;``}`` ` `// Drivers code``int` `main()``{``    ` `    ``string num_3, num_6, num_9;``    ``num_3 = ``"3333"``;``    ``num_6 = ``"6666"``;``    ``num_9 = ``"9999"``;`` ` `    ``string result = ``""``;`` ` `    ``// find square of 33..33``    ``result = find_Square_369(num_3);``    ``cout << ``"Square of "` `<< num_3 << ``" is : "` `<< result << endl;`` ` `    ``// find square of 66..66``    ``result = find_Square_369(num_6);``    ``cout << ``"Square of "` `<< num_6 << ``" is : "` `<< result << endl;`` ` `    ``// find square of 66..66``    ``result = find_Square_369(num_9);``    ``cout << ``"Square of "` `<< num_9 << ``" is : "` `<< result << endl;`` ` `    ``return` `0;``}`

## Java

 `// Java program to find square of ``// these large numbers``class` `GFG {``     ` `    ``// Function to find the square of``    ``// 333...333, 666...666 and 999...999``    ``static` `String find_Square_369(String num)``    ``{``        ``char` `a, b, c, d;``     ` `        ``// if the number is 333...333``        ``if` `(num.charAt(``0``) == ``'3'``)``            ``{a = ``'1'``; b = ``'0'``; c = ``'8'``; d = ``'9'``;}``     ` `        ``// if the number is 666...666``        ``else` `if` `(num.charAt(``0``) == ``'6'``)``            ``{a = ``'4'``; b = ``'3'``; c = ``'5'``; d = ``'6'``;}``     ` `        ``// if the number is 999...999``        ``else``            ``{a = ``'9'``; b = ``'8'``; c = ``'0'``; d = ``'1'``;}``     ` `        ``// variable for hold result``        ``String result = ``""``;``     ` `        ``// find the no of digit``        ``int` `size = num.length();``     ` `        ``// add size-1 time a in result``        ``for` `(``int` `i = ``1``; i < size; i++)``            ``result += a;``     ` `        ``// add one time b in result``        ``result += b;``     ` `        ``// add size-1 time c in result``        ``for` `(``int` `i = ``1``; i < size; i++)``            ``result += c;``     ` `        ``// add one time d in result``        ``result += d;``     ` `        ``// return result``        ``return` `result;``    ``}``     ` `    ``// Drivers code``    ``public` `static` `void` `main(String[] args)``    ``{`` ` `        ``String num_3, num_6, num_9;``        ``num_3 = ``"3333"``;``        ``num_6 = ``"6666"``;``        ``num_9 = ``"9999"``;``     ` `        ``String result = ``""``;``     ` `        ``// find square of 33..33``        ``result = find_Square_369(num_3);``        ``System.out.println(``"Square of "` `+ num_3 ``                            ``+ ``" is : "` `+ result);``     ` `        ``// find square of 66..66``        ``result = find_Square_369(num_6);``        ``System.out.println(``"Square of "` `+ num_9``                            ``+ ``" is : "` `+ result);``     ` `        ``// find square of 66..66``        ``result = find_Square_369(num_9);``        ``System.out.println(``"Square of "` `+ num_9``                            ``+ ``" is : "` `+ result);``     ` `    ``}``}`` ` `// This code is contributed by Smitha.`

## Python 3

 `# Pyhton 3 program to find square of ``# these large numbers`` ` `# Function to find the square of``# 333...333, 666...666 and 999...999``def` `find_Square_369(num):`` ` `    ``# if the number is 333...333``    ``if` `(num[``0``] ``=``=` `'3'``):``        ``a ``=` `'1'``        ``b ``=` `'0'``        ``c ``=` `'8'``        ``d ``=` `'9'`` ` `    ``# if the number is 666...666``    ``elif` `(num[``0``] ``=``=` `'6'``):``        ``a ``=` `'4'``        ``b ``=` `'3'``        ``c ``=` `'5'``        ``d ``=` `'6'`` ` `    ``# if the number is 999...999``    ``else``:``        ``a ``=` `'9'``        ``b ``=` `'8'``        ``c ``=` `'0'``        ``d ``=` `'1'`` ` `    ``# variable for hold result``    ``result ``=` `""`` ` `    ``# find the no of digit``    ``size ``=` `len``(num)`` ` `    ``# add size-1 time a in result``    ``for` `i ``in` `range``(``1``, size):``        ``result ``+``=` `a`` ` `    ``# add one time b in result``    ``result ``+``=` `b`` ` `    ``# add size-1 time c in result``    ``for` `i ``in` `range``(``1``, size):``        ``result ``+``=` `c`` ` `    ``# add one time d in result``    ``result ``+``=` `d`` ` `    ``# return result``    ``return` `result`` ` ` ` `# Drivers code``# Your Python 3 Code`` ` `num_3 ``=` `"3333"``num_6 ``=` `"6666"``num_9 ``=` `"9999"`` ` `result ``=` `""`` ` `# find square of 33..33``result ``=` `find_Square_369(num_3)``print``(``"Square of "` `+` `num_3 ``+` `" is : "``                            ``+` `result);`` ` `# find square of 66..66``result ``=` `find_Square_369(num_6)``print``(``"Square of "` `+` `num_6 ``+` `" is : "``                            ``+` `result);`` ` `# find square of 66..66``result ``=` `find_Square_369(num_9)``print``(``"Square of "` `+` `num_9 ``+` `" is : "``                           ``+` `result);`` ` `# This code is contributed by Smitha`

## C#

 `// C# program to find square of ``// these large numbers``using` `System;`` ` `class` `GFG {``     ` `    ``// Function to find the square of``    ``// 333...333, 666...666 and 999...999``    ``static` `string` `find_Square_369(``string` `num)``    ``{``        ``char` `a, b, c, d;``     ` `        ``// if the number is 333...333``        ``if` `(num == ``'3'``)``            ``{a = ``'1'``; b = ``'0'``; c = ``'8'``; d = ``'9'``;}``     ` `        ``// if the number is 666...666``        ``else` `if` `(num == ``'6'``)``            ``{a = ``'4'``; b = ``'3'``; c = ``'5'``; d = ``'6'``;}``     ` `        ``// if the number is 999...999``        ``else``            ``{a = ``'9'``; b = ``'8'``; c = ``'0'``; d = ``'1'``;}``     ` `        ``// variable for hold result``        ``string` `result = ``""``;``     ` `        ``// find the no of digit``        ``int` `size = num.Length;``     ` `        ``// add size-1 time a in result``        ``for` `(``int` `i = 1; i < size; i++)``            ``result += a;``     ` `        ``// add one time b in result``        ``result += b;``     ` `        ``// add size-1 time c in result``        ``for` `(``int` `i = 1; i < size; i++)``            ``result += c;``     ` `        ``// add one time d in result``        ``result += d;``     ` `        ``// return result``        ``return` `result;``    ``}``     ` `    ``// Drivers code``    ``public` `static` `void` `Main()``    ``{``        ``string` `num_3, num_6, num_9;``        ``num_3 = ``"3333"``;``        ``num_6 = ``"6666"``;``        ``num_9 = ``"9999"``;``     ` `        ``string` `result = ``""``;``     ` `        ``// find square of 33..33``        ``result = find_Square_369(num_3);``        ``Console.Write(``"Square of "` `+ num_3 ``                ``+ ``" is : "` `+ result + ``"\n"``);``     ` `        ``// find square of 66..66``        ``result = find_Square_369(num_6);``        ``Console.Write(``"Square of "` `+ num_9 ``                ``+ ``" is : "` `+ result + ``"\n"``);``     ` `        ``// find square of 66..66``        ``result = find_Square_369(num_9);``        ``Console.Write(``"Square of "` `+ num_9``                ``+ ``" is : "` `+ result + ``"\n"``);``    ``}``}`` ` `// This code is contributed by Smitha`

## PHP

 ``

Output :
```Square of 3333 is : 11108889
Square of 6666 is : 44435556
Square of 9999 is : 99980001
```

Attention reader! Don’t stop learning now. Get hold of all the important mathematical concepts for competitive programming with the Essential Maths for CP Course at a student-friendly price. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

My Personal Notes arrow_drop_up