You are given input as order of graph n (highest number of edges connected to a node), you have to find the number of vertices in a Hypercube graph of order n.
Examples:
Input : n = 3
Output : 8
Input : n = 2
Output : 4
In hypercube graph Q(n), n represents the degree of the graph. Hypercube graph represents the maximum number of edges that can be connected to a graph to make it an n degree graph, every vertex has the same degree n and in that representation, only a fixed number of edges and vertices are added as shown in the figure below:
All hypercube graphs are Hamiltonian, hypercube graph of order n has (2^n) vertices, , for input n as the order of graph we have to find the corresponding power of 2.
Recursive Approach
C++
#include <iostream>
using namespace std;
int power( int n)
{
if (n == 1)
return 2;
return 2 * power(n - 1);
}
int main()
{
int n = 4;
cout << power(n);
return 0;
}
|
Java
class GfG
{
static int power( int n)
{
if (n == 1 )
return 2 ;
return 2 * power(n - 1 );
}
public static void main(String []args)
{
int n = 4 ;
System.out.println(power(n));
}
}
|
Python3
def power(n):
if n = = 1 :
return 2
return 2 * power(n - 1 )
n = 4
print (power(n))
|
C#
using System;
class GfG
{
static int power( int n)
{
if (n == 1)
return 2;
return 2 * power(n - 1);
}
public static void Main()
{
int n = 4;
Console.WriteLine(power(n));
}
}
|
PHP
<?php
{
function power( $n )
{
if ( $n == 1)
return 2;
return 2 * power( $n - 1);
}
{
$n = 4;
echo (power( $n ));
}
}
?>
|
Javascript
<script>
function power(n)
{
if (n == 1)
return 2;
return 2 * power(n - 1);
}
var n = 4;
document.write( power(n));
</script>
|
Iterative Approach
C++
#include <bits/stdc++.h>
using namespace std;
int power( int n)
{
if (n == 0)
return 0;
int pow = 1;
for ( int i = 1; i <= n; i++)
{
pow *= 2;
}
return pow ;
}
int main()
{
int n = 4;
cout << power(n);
return 0;
}
|
Java
import java.io.*;
class GFG {
static int power( int n)
{
if (n== 0 ) return 0 ;
int pow = 1 ;
for ( int i= 1 ;i<=n;i++){
pow *= 2 ;
}
return pow;
}
public static void main (String[] args) {
int n = 4 ;
System.out.println(power(n));
}
}
|
Python3
def power(n):
if n = = 0 :
return 0
pow = 1
for i in range ( 1 , n + 1 ):
pow * = 2
return pow
n = 4
print (power(n))
|
C#
using System;
public class GFG {
public static int power( int n)
{
if (n == 0)
return 0;
int pow = 1;
for ( int i = 1; i <= n; i++) {
pow *= 2;
}
return pow;
}
static public void Main()
{
int n = 4;
Console.WriteLine(power(n));
}
}
|
Javascript
function power(n) {
if (n === 0) return 0;
let pow = 1;
for (let i = 1; i <= n; i++) {
pow *= 2;
}
return pow;
}
const n = 4;
console.log(power(n));
|
Java Using Math.pow()
C++
#include <bits/stdc++.h>
using namespace std;
int power( int n) { return pow (2, n); }
int main()
{
int n = 4;
cout << power(n) << endl;
return 0;
}
|
Java
import java.io.*;
class GFG {
static int power( int n)
{
return ( int )Math.pow( 2 ,n);
}
public static void main (String[] args) {
int n = 4 ;
System.out.println(power(n));
}
}
|
Python3
def power(n):
return pow ( 2 , n)
n = 4
print (power(n))
|
C#
using System;
public class GFG {
static int power( int n) { return ( int )Math.Pow(2, n); }
static public void Main()
{
int n = 4;
Console.WriteLine(power(n));
}
}
|
Javascript
function power(n) { return Math.pow(2, n); }
let n = 4;
console.log(power(n));
|
Last Updated :
08 Feb, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...