HomeData Modelling & AIN-th number which is both a square and a cube

N-th number which is both a square and a cube

Given a number n, find the n-th number which is both a square and a cube. First few such numbers are 1, 64, 729, …

Examples : 

Input : 3
Output :729
        729 is square of 27 and cube of 3.
Input :5
Output :15625

The idea is simple, n-th such number is n6  

C++




// C++ program to find n-th number which is both
// square and cube.
#include <bits/stdc++.h>
using namespace std;
  
int nthSquareCube(int n)
{
   return n*n*n*n*n*n;
}
  
// Driver code
int main()
{
    int n = 5;
    cout << nthSquareCube(n);
    return 0;
}


Java




// Java program to find n-th number
// which is both square and cube.
import java.io.*;
  
public class GFG {
      
    static int nthSquareCube(int n)
    {
        return n * n * n * n * n * n;
    }
      
    // Driver code
    public static void main(String[] args)
    {
        int n = 5;
          
        System.out.println(nthSquareCube(n));
    }
}
  
// This code is contributed by
// Smitha Dinesh Semwal


Python3




# program to find n-th number
# which is both square and cube.
  
def nthSquareCube(n):
  
    return n * n * n * n * n * n
  
  
# Driver code
n = 5
print(nthSquareCube(n))
# This code is contributed by
# Smitha Dinesh Semwal


C#




// C# program to find n-th number
// which is both square and cube.
using System;
  
class GFG
{
      
    static int nthSquareCube(int n)
    {
        return n * n * n * n * n * n;
    }
      
    // Driver code
    static public void Main ()
    {
        int n = 5;
          
        Console.WriteLine(nthSquareCube(n));
    }
}
  
// This code is contributed by Ajit.


PHP




<?php
// PHP program to find n-th 
// number which is both
// square and cube.
  
function nthSquareCube($n)
{
    return $n * $n * $n * 
           $n * $n * $n;
}
  
// Driver code
$n = 5;
echo(nthSquareCube($n));
  
// This code is contributed by Ajit.
?>


Javascript




<script>
  
// JavaScript program to find n-th number 
// which is bothsquare and cube.
function nthSquareCube(n)
{
    return n * n * n * n * n * n;
}
  
// Driver code
let n = 5;
  
document.write(nthSquareCube(n));
  
// This code is contributed by Surbhi Tyagi.
  
</script>


Output

15625

Time Complexity: O(1)
Auxiliary Space: O(1)

Feeling lost in the world of random DSA topics, wasting time without progress? It’s time for a change! Join our DSA course, where we’ll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!

RELATED ARTICLES

4 COMMENTS

Most Popular

Dominic
32686 POSTS0 COMMENTS
Milvus
131 POSTS0 COMMENTS
Nango Kala
7053 POSTS0 COMMENTS
Nicole Veronica
12192 POSTS0 COMMENTS
Nokonwaba Nkukhwana
12291 POSTS0 COMMENTS
Shaida Kate Naidoo
7178 POSTS0 COMMENTS
Ted Musemwa
7444 POSTS0 COMMENTS
Thapelo Manthata
7157 POSTS0 COMMENTS
Umr Jansen
7147 POSTS0 COMMENTS