You are given a number n, the task is to find nth octagonal number. Also, find the Octagonal series till n.
An octagonal number is the figure number that represent octagonal. Octagonal numbers can be formed by placing triangular numbers on the four sides of a square. Octagonal number is calculated by using the formula (3n2 – 2n).
Examples :
Input : 5
Output : 65
Input : 10
Output : 280
Input : 15
Output : 645
C++
#include <bits/stdc++.h>
using namespace std;
int octagonal( int n)
{
return 3 * n * n - 2 * n;
}
int main()
{
int n = 10;
cout << n << "th octagonal number :"
<< octagonal(n);
return 0;
}
|
Java
import java.util.*;
import java.lang.*;
public class GfG {
public static int octagonal( int n)
{
return 3 * n * n - 2 * n;
}
public static void main(String argc[])
{
int n = 10 ;
System.out.println(n + "th octagonal" +
" number :" + octagonal(n));
}
}
|
Python
def octagonal(n):
return 3 * n * n - 2 * n
n = 10
print (n, "th octagonal number :" ,
octagonal(n))
|
C#
using System;
public class GfG {
public static int octagonal( int n)
{
return 3 * n * n - 2 * n;
}
public static void Main()
{
int n = 10;
Console.WriteLine(n + "th octagonal"
+ " number :" + octagonal(n));
}
}
|
PHP
<?php
function octagonal( $n )
{
return 3 * $n * $n - 2 * $n ;
}
$n = 10;
echo $n , "th octagonal number :"
, octagonal( $n );
?>
|
Javascript
<script>
function octagonal(n)
{
return 3 * n * n - 2 * n;
}
let n = 10;
document.write(n + "th octagonal" +
" number :" + octagonal(n));
</script>
|
Output :
10th octagonal number : 280
Time Complexity: O(1)
Auxiliary Space: O(1)
Given number n, find the octagonal series till n.
We can also find the octagonal series. Octagonal series contains the points on octagonal.
Octagonal series 1, 8, 21, 40, 65, 96, 133, 176, 225, 280, . . .
C++
#include <bits/stdc++.h>
using namespace std;
void octagonalSeries( int n)
{
for ( int i = 1; i <= n; i++)
cout << (3 * i * i - 2 * i);
}
int main()
{
int n = 10;
octagonalSeries(n);
return 0;
}
|
Java
import java.util.*;
import java.lang.*;
public class GfG {
public static void octagonalSeries( int n)
{
for ( int i = 1 ; i <= n; i++)
System.out.print( 3 * i * i - 2 * i);
}
public static void main(String argc[])
{
int n = 10 ;
octagonalSeries(n);
}
}
|
Python
def octagonalSeries(n):
for i in range ( 1 , n + 1 ):
print ( 3 * i * i - 2 * i,
end = ", " )
n = 10
octagonalSeries(n)
|
C#
using System;
public class GfG {
public static void octagonalSeries( int n)
{
for ( int i = 1; i <= n; i++)
Console.Write(3 * i * i - 2 * i + ", " );
}
public static void Main()
{
int n = 10;
octagonalSeries(n);
}
}
|
PHP
<?php
function octagonalSeries( $n )
{
for ( $i = 1; $i <= $n ; $i ++)
echo (3 * $i * $i - 2 * $i ), "," ;
}
$n = 10;
octagonalSeries( $n );
?>
|
Javascript
<script>
function octagonalSeries(n)
{
for (let i = 1; i <= n; i++)
document.write(3 * i * i - 2 * i + ", " );
}
let n = 10;
octagonalSeries(n);
</script>
|
Output :
1, 8, 21, 40, 65, 96, 133, 176, 225, 280
Time Complexity: O(n)
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 geeks!