Print squares of first n natural numbers without using *, / and –
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++
// C++ program to print squares of first 'n' natural numbers // wothout 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
// Java program to print squares // of first 'n' natural numbers // wothout 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
# 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#
// C# program to print squares // of first 'n' natural numbers // wothout 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 |
PHP
<?php // PHP program to print squares // of first 'n' natural numbers // wothout 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
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++
// C++ program to print squares of first 'n' natural numbers // wothout 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
// 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
# Python3 program to print squares # of first 'n' natural numbers # wothout 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#
// 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.. |
PHP
<?php // PHP program to print squares // of first 'n' natural numbers // wothout 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
This article is contributed by Sachin. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above
Recommended Posts:
- How to swap two numbers without using a temporary variable?
- Lucky Numbers
- Ugly Numbers
- Write a program to print all permutations of a given string
- Write a program to add two numbers in base 14
- Print all combinations of balanced parentheses
- Print all combinations of points that can compose a given number
- Count of Binary Digit numbers smaller than N
- Program for Fibonacci numbers
- Average of a stream of numbers
- Print all sequences of given length
- Add two numbers without using arithmetic operators
- To find sum of two numbers without using any operator
- Count numbers that don't contain 3
- Print all permutations in sorted (lexicographic) order



