Given a natural number ‘n’, print squares of first n natural numbers without using *, / and -.
Examples :
Input: n = 5
Output: 0 1 4 9 16
Input: n = 6
Output: 0 1 4 9 16 25
We strongly recommend to minimize the browser and try this yourself first.
Method 1: The idea is to calculate next square using previous square value. Consider the following relation between square of x and (x-1). We know square of (x-1) is (x-1)2 – 2*x + 1. We can write x2 as
x2 = (x-1)2 + 2*x - 1
x2 = (x-1)2 + x + (x - 1)
When writing an iterative program, we can keep track of previous value of x and add the current and previous values of x to current value of square. This way we don’t even use the ‘-‘ operator.
Below is the implementation of above approach:
// C++ program to print squares of first 'n' natural numbers // without using *, / and - #include<iostream> using namespace std;
void printSquares( int n)
{ // Initialize 'square' and previous value of 'x'
int square = 0, prev_x = 0;
// Calculate and print squares
for ( int x = 0; x < n; x++)
{
// Update value of square using previous value
square = (square + x + prev_x);
// Print square and update prev for next iteration
cout << square << " " ;
prev_x = x;
}
} // Driver program to test above function int main()
{ int n = 5;
printSquares(n);
} |
// Java program to print squares // of first 'n' natural numbers // without using *, / and import java.io.*;
class GFG
{ static void printSquares( int n)
{ // Initialize 'square' and
// previous value of 'x'
int square = 0 , prev_x = 0 ;
// Calculate and
// print squares
for ( int x = 0 ; x < n; x++)
{
// Update value of square
// using previous value
square = (square + x + prev_x);
// Print square and update
// prev for next iteration
System.out.print( square + " " );
prev_x = x;
}
} // Driver Code public static void main (String[] args)
{ int n = 5 ;
printSquares(n);
} } // This code is contributed // by akt_mit |
# Python 3 program to print squares of first # 'n' natural numbers without using *, / and - def printSquares(n):
# Initialize 'square' and previous
# value of 'x'
square = 0 ; prev_x = 0 ;
# Calculate and print squares
for x in range ( 0 , n):
# Update value of square using
# previous value
square = (square + x + prev_x)
# Print square and update prev
# for next iteration
print (square, end = " " )
prev_x = x
# Driver Code n = 5 ;
printSquares(n); # This code is contributed # by Akanksha Rai |
// C# program to print squares // of first 'n' natural numbers // without using *, / and using System;
public class GFG{
static void printSquares( int n)
{ // Initialize 'square' and
// previous value of 'x'
int square = 0, prev_x = 0;
// Calculate and
// print squares
for ( int x = 0; x < n; x++)
{
// Update value of square
// using previous value
square = (square + x + prev_x);
// Print square and update
// prev for next iteration
Console.Write( square + " " );
prev_x = x;
}
} // Driver Code static public void Main (){
int n = 5;
printSquares(n);
}
} // This code is contributed // by ajit |
<script> // JavaScript program to print squares of first 'n' natural numbers // without using *, / and - function printSquares(n)
{
// Initialize 'square' and previous value of 'x'
let square = 0, prev_x = 0;
// Calculate and print squares
for (let x = 0; x < n; x++)
{
// Update value of square using previous value
square = (square + x + prev_x);
// Print square and update prev for next iteration
document.write(square + " " );
prev_x = x;
}
}
// Driver program to test above function
let n = 5;
printSquares(n);
// This code is contributed by Surbhi Tyagi </script> |
<?php // PHP program to print squares // of first 'n' natural numbers // without using *, / and - function printSquares( $n )
{ // Initialize 'square' and
// previous value of 'x'
$square = 0; $prev_x = 0;
// Calculate and
// print squares
for ( $x = 0; $x < $n ; $x ++)
{
// Update value of square
// using previous value
$square = ( $square + $x + $prev_x );
// Print square and update
// prev for next iteration
echo $square , " " ;
$prev_x = $x ;
}
} // Driver Code
$n = 5;
printSquares( $n );
// This code is contributed by ajit ?> |
Output:
0 1 4 9 16
Time Complexity: O(n)
Auxiliary Space: O(1)
Method 2: Sum of first n odd numbers are squares of natural numbers from 1 to n. For example 1, 1+3, 1+3+5, 1+3+5+7, 1+3+5+7+9, ….
Following is program based on above concept. Thanks to Aadithya Umashanker and raviteja for suggesting this method.
// C++ program to print squares of first 'n' natural numbers // without using *, / and - #include<iostream> using namespace std;
void printSquares( int n)
{ // Initialize 'square' and first odd number
int square = 0, odd = 1;
// Calculate and print squares
for ( int x = 0; x < n; x++)
{
// Print square
cout << square << " " ;
// Update 'square' and 'odd'
square = square + odd;
odd = odd + 2;
}
} // Driver program to test above function int main()
{ int n = 5;
printSquares(n);
} |
// Java program to print // squares of first 'n' // natural numbers without // using *, / and - import java.io.*;
class GFG
{ static void printSquares( int n)
{ // Initialize 'square'
// and first odd number
int square = 0 , odd = 1 ;
// Calculate and
// print squares
for ( int x = 0 ; x < n; x++)
{
// Print square
System.out.print(square +
" " );
// Update 'square'
// and 'odd'
square = square + odd;
odd = odd + 2 ;
}
} // Driver Code public static void main (String[] args)
{ int n = 5 ;
printSquares(n);
} } // This code is contributed // by ajit |
# Python3 program to print squares # of first 'n' natural numbers # without using *, / and - def printSquares(n):
# Initialize 'square' and
# first odd number
square = 0
odd = 1
# Calculate and print squares
for x in range ( 0 , n):
# Print square
print (square, end = " " )
# Update 'square' and 'odd'
square = square + odd
odd = odd + 2
# Driver Code n = 5 ;
printSquares(n) # This code is contributed # by Rajput-Ji |
// C# program to print squares of first 'n' // natural numbers without using *, / and - using System;
class GFG
{ static void printSquares( int n)
{ // Initialize 'square'
// and first odd number
int square = 0, odd = 1;
// Calculate and
// print squares
for ( int x = 0; x < n; x++)
{
// Print square
Console.Write(square + " " );
// Update 'square'
// and 'odd'
square = square + odd;
odd = odd + 2;
}
} // Driver Code public static void Main ()
{ int n = 5;
printSquares(n);
} } // This code is contributed // by inder_verma.. |
<script> // Javascript program to print squares of first 'n' natural numbers // without using *, / and - function printSquares(n)
{ // Initialize 'square' and first odd number
let square = 0, odd = 1;
// Calculate and print squares
for (let x = 0; x < n; x++)
{
// Print square
document.write(square + " " );
// Update 'square' and 'odd'
square = square + odd;
odd = odd + 2;
}
} // Driver program to test above function let n = 5; printSquares(n); // This code is contributed by subham348. </script> |
<?php // PHP program to print squares // of first 'n' natural numbers // without using *, / and - function printSquares( $n )
{ // Initialize 'square' and
// first odd number
$square = 0; $odd = 1;
// Calculate and print squares
for ( $x = 0; $x < $n ; $x ++)
{
// Print square
echo $square , " " ;
// Update 'square' and 'odd'
$square = $square + $odd ;
$odd = $odd + 2;
}
} // Driver Code $n = 5;
printSquares( $n );
// This code is contributed by m_kit ?> |
Output :
0 1 4 9 16
Time Complexity: O(n)
Auxiliary Space: O(1), since no extra space has been taken.