Posted on by Kalkicode
Code Number

# Nonagonal number

In mathematics, the nonagonal numbers are a sequence of figurate numbers that represent the number of dots that can be arranged in a nonagon (a nine-sided polygon) with increasing side lengths. These numbers are a type of polygonal number and are denoted by N(n), where 'n' represents the position of the nonagonal number in the sequence.

## Problem Statement

The task is to calculate the first 'k' nonagonal numbers using the formula for nonagonal numbers and print them. The formula to calculate the 'n-th' nonagonal number is given by: N(n) = n(7n - 5) / 2

## Explanation with Suitable Example

Let's consider 'k = 5' to demonstrate the calculation of the first five nonagonal numbers.

1. For n = 1: N(1) = 1(7 * 1 - 5) / 2 = 1(2) / 2 = 1

2. For n = 2: N(2) = 2(7 * 2 - 5) / 2 = 2(9) / 2 = 9

3. For n = 3: N(3) = 3(7 * 3 - 5) / 2 = 3(16) / 2 = 24

4. For n = 4: N(4) = 4(7 * 4 - 5) / 2 = 4(23) / 2 = 46

5. For n = 5: N(5) = 5(7 * 5 - 5) / 2 = 5(30) / 2 = 75

The first five nonagonal numbers are: 1, 9, 24, 46, 75

## Standard Pseudocode

The pseudocode to find the 'k' nonagonal numbers is as follows:

``````nonagonalNo(k):
for n from 1 to k:
result = n * (7 * n - 5) / 2
print result
``````

## Algorithm Explanation

1. Start the function nonagonalNo, which takes an integer 'k' as input, to find the first 'k' nonagonal numbers.
2. Initialize a loop for 'n' ranging from 1 to 'k'.
3. Calculate the 'n-th' nonagonal number using the formula (n * (7 * n - 5) / 2) and store it in the variable 'result'.
4. Print the calculated 'result' for each 'n', which represents the 'n-th' nonagonal number.
5. Repeat the process until 'n' reaches 'k'.

## Code Solution

Here given code implementation process.

``````// C Program for
// Nonagonal number
#include <stdio.h>

void nonagonalNo(int k)
{
// Print all initial k nonagonal number
for (int n = 1; n <= k; ++n)
{
// Formula
//  n(7n-5)
// ————————
//    2

// Calculate nth nonagonal number
int result = (n *(7 *n - 5)) / 2;

// Display calculated result
printf("  %d", result);
}
}
int main()
{
//  Nonagonal number are
// —————————————————————————————————————————————
//  1, 9, 24, 46, 75, 111, 154, 204, 261, 325, 396,
// 474, 559, 651, 750, 856, 969, 1089, 1216, 1350,
// 1491, 1639, 1794, 1956, 2125, 2301, 2484, 2674,
// 2871, 3075, 3286, 3504, 3729, 3961, 4200, 4446,
// 4699, 4959, 5226, 5500, 5781, 6069 ... etc

// k = 10
nonagonalNo(10);
return 0;
}``````

#### Output

``  1  9  24  46  75  111  154  204  261  325``
``````// Java program for
// Nonagonal number
public class NonagonalNumber
{
public void nonagonalNo(int k)
{
// Print all initial k nonagonal number
for (int n = 1; n <= k; ++n)
{
// Formula
//  n(7n-5)
// ————————
//    2

// Calculate nth nonagonal number
int result = (n * (7 * n - 5)) / 2;

// Display calculated result
System.out.print(" " + result);
}
}
public static void main(String[] args)
{
//  Nonagonal number are
// —————————————————————————————————————————————
//  1, 9, 24, 46, 75, 111, 154, 204, 261, 325, 396,
// 474, 559, 651, 750, 856, 969, 1089, 1216, 1350,
// 1491, 1639, 1794, 1956, 2125, 2301, 2484, 2674,
// 2871, 3075, 3286, 3504, 3729, 3961, 4200, 4446,
// 4699, 4959, 5226, 5500, 5781, 6069 ... etc

// k = 10
}
}``````

#### Output

`` 1 9 24 46 75 111 154 204 261 325``
``````// Include header file
#include <iostream>
using namespace std;
// C++ program for
// Nonagonal number

class NonagonalNumber
{
public: void nonagonalNo(int k)
{
// Print all initial k nonagonal number
for (int n = 1; n <= k; ++n)
{
// Formula
//  n(7n-5)
// ————————
//     2

// Calculate nth nonagonal number
int result = (n *(7 *n - 5)) / 2;

// Display calculated result
cout << " " << result;
}
}
};
int main()
{
//  Nonagonal number are
// —————————————————————————————————————————————
//  1, 9, 24, 46, 75, 111, 154, 204, 261, 325, 396,
// 474, 559, 651, 750, 856, 969, 1089, 1216, 1350,
// 1491, 1639, 1794, 1956, 2125, 2301, 2484, 2674,
// 2871, 3075, 3286, 3504, 3729, 3961, 4200, 4446,
// 4699, 4959, 5226, 5500, 5781, 6069 ... etc

// k = 10
return 0;
}``````

#### Output

`` 1 9 24 46 75 111 154 204 261 325``
``````package main
import "fmt"
// Go program for
// Nonagonal number

func nonagonalNo(k int) {
// Print all initial k nonagonal number
for n := 1 ; n <= k ; n++ {
// Formula
//  n(7n-5)
// ————————
//    2

// Calculate nth nonagonal number
var result int = (n * (7 * n - 5)) / 2

// Display calculated result
fmt.Print(" ", result)
}
}
func main() {

//  Nonagonal number are
// —————————————————————————————————————————————
//  1, 9, 24, 46, 75, 111, 154, 204, 261, 325, 396,
// 474, 559, 651, 750, 856, 969, 1089, 1216, 1350,
// 1491, 1639, 1794, 1956, 2125, 2301, 2484, 2674,
// 2871, 3075, 3286, 3504, 3729, 3961, 4200, 4446,
// 4699, 4959, 5226, 5500, 5781, 6069 ... etc

// k = 10
nonagonalNo(10)
}``````

#### Output

`` 1 9 24 46 75 111 154 204 261 325``
``````// Include namespace system
using System;
// Csharp program for
// Nonagonal number
public class NonagonalNumber
{
public void nonagonalNo(int k)
{
// Print all initial k nonagonal number
for (int n = 1; n <= k; ++n)
{
// Formula
//  n(7n-5)
// ————————
//    2

// Calculate nth nonagonal number
int result = (n * (7 * n - 5)) / 2;

// Display calculated result
Console.Write(" " + result);
}
}
public static void Main(String[] args)
{
//  Nonagonal number are
// —————————————————————————————————————————————
//  1, 9, 24, 46, 75, 111, 154, 204, 261, 325, 396,
// 474, 559, 651, 750, 856, 969, 1089, 1216, 1350,
// 1491, 1639, 1794, 1956, 2125, 2301, 2484, 2674,
// 2871, 3075, 3286, 3504, 3729, 3961, 4200, 4446,
// 4699, 4959, 5226, 5500, 5781, 6069 ... etc

// k = 10
}
}``````

#### Output

`` 1 9 24 46 75 111 154 204 261 325``
``````<?php
// Php program for
// Nonagonal number
class NonagonalNumber
{
public	function nonagonalNo(\$k)
{
// Print all initial k nonagonal number
for (\$n = 1; \$n <= \$k; ++\$n)
{
// Formula
//  n(7n-5)
// ————————
//    2

// Calculate nth nonagonal number
\$result = (int)((\$n * (7 * \$n - 5)) / 2);

// Display calculated result
echo(" ".\$result);
}
}
}

function main()
{
//  Nonagonal number are
// —————————————————————————————————————————————
//  1, 9, 24, 46, 75, 111, 154, 204, 261, 325, 396,
// 474, 559, 651, 750, 856, 969, 1089, 1216, 1350,
// 1491, 1639, 1794, 1956, 2125, 2301, 2484, 2674,
// 2871, 3075, 3286, 3504, 3729, 3961, 4200, 4446,
// 4699, 4959, 5226, 5500, 5781, 6069 ... etc

// k = 10
}
main();``````

#### Output

`` 1 9 24 46 75 111 154 204 261 325``
``````// Node JS program for
// Nonagonal number
class NonagonalNumber
{
nonagonalNo(k)
{
// Print all initial k nonagonal number
for (var n = 1; n <= k; ++n)
{
// Formula
//  n(7n-5)
// ————————
//    2

// Calculate nth nonagonal number
var result = parseInt((n * (7 * n - 5)) / 2);

// Display calculated result
process.stdout.write(" " + result);
}
}
}

function main()
{
//  Nonagonal number are
// —————————————————————————————————————————————
//  1, 9, 24, 46, 75, 111, 154, 204, 261, 325, 396,
// 474, 559, 651, 750, 856, 969, 1089, 1216, 1350,
// 1491, 1639, 1794, 1956, 2125, 2301, 2484, 2674,
// 2871, 3075, 3286, 3504, 3729, 3961, 4200, 4446,
// 4699, 4959, 5226, 5500, 5781, 6069 ... etc

// k = 10
}
main();``````

#### Output

`` 1 9 24 46 75 111 154 204 261 325``
``````#  Python 3 program for
#  Nonagonal number
class NonagonalNumber :
def nonagonalNo(self, k) :
n = 1
#  Print all initial k nonagonal number
while (n <= k) :
#  Formula
#   n(7n-5)
#  ————————
#     2

#  Calculate nth nonagonal number
result = int((n * (7 * n - 5)) / 2)

#  Display calculated result
print(" ", result, end = "")
n += 1

def main() :
#   Nonagonal number are
#  —————————————————————————————————————————————
#   1, 9, 24, 46, 75, 111, 154, 204, 261, 325, 396,
#  474, 559, 651, 750, 856, 969, 1089, 1216, 1350,
#  1491, 1639, 1794, 1956, 2125, 2301, 2484, 2674,
#  2871, 3075, 3286, 3504, 3729, 3961, 4200, 4446,
#  4699, 4959, 5226, 5500, 5781, 6069 ... etc

#  k = 10

if __name__ == "__main__": main()``````

#### Output

``  1  9  24  46  75  111  154  204  261  325``
``````#  Ruby program for
#  Nonagonal number
class NonagonalNumber
def nonagonalNo(k)
n = 1
#  Print all initial k nonagonal number
while (n <= k)
#  Formula
#   n(7n-5)
#  ————————
#     2

#  Calculate nth nonagonal number
result = (n * (7 * n - 5)) / 2

#  Display calculated result
print(" ", result)
n += 1
end

end

end

def main()
#   Nonagonal number are
#  —————————————————————————————————————————————
#   1, 9, 24, 46, 75, 111, 154, 204, 261, 325, 396,
#  474, 559, 651, 750, 856, 969, 1089, 1216, 1350,
#  1491, 1639, 1794, 1956, 2125, 2301, 2484, 2674,
#  2871, 3075, 3286, 3504, 3729, 3961, 4200, 4446,
#  4699, 4959, 5226, 5500, 5781, 6069 ... etc

#  k = 10
end

main()``````

#### Output

`` 1 9 24 46 75 111 154 204 261 325``
``````// Scala program for
// Nonagonal number
class NonagonalNumber()
{
def nonagonalNo(k: Int): Unit = {
var n: Int = 1;
// Print all initial k nonagonal number
while (n <= k)
{
// Formula
//  n(7n-5)
// ————————
//    2

// Calculate nth nonagonal number
var result: Int = (n * (7 * n - 5)) / 2;

// Display calculated result
print(" " + result);
n += 1;
}
}
}
object Main
{
def main(args: Array[String]): Unit = {
var task: NonagonalNumber = new NonagonalNumber();
//  Nonagonal number are
// —————————————————————————————————————————————
//  1, 9, 24, 46, 75, 111, 154, 204, 261, 325, 396,
// 474, 559, 651, 750, 856, 969, 1089, 1216, 1350,
// 1491, 1639, 1794, 1956, 2125, 2301, 2484, 2674,
// 2871, 3075, 3286, 3504, 3729, 3961, 4200, 4446,
// 4699, 4959, 5226, 5500, 5781, 6069 ... etc

// k = 10
}
}``````

#### Output

`` 1 9 24 46 75 111 154 204 261 325``
``````// Swift 4 program for
// Nonagonal number
class NonagonalNumber
{
func nonagonalNo(_ k: Int)
{
var n: Int = 1;
// Print all initial k nonagonal number
while (n <= k)
{
// Formula
//  n(7n-5)
// ————————
//    2

// Calculate nth nonagonal number
let result: Int = (n * (7 * n - 5)) / 2;

// Display calculated result
print(" ", result, terminator: "");
n += 1;
}
}
}
func main()
{
//  Nonagonal number are
// —————————————————————————————————————————————
//  1, 9, 24, 46, 75, 111, 154, 204, 261, 325, 396,
// 474, 559, 651, 750, 856, 969, 1089, 1216, 1350,
// 1491, 1639, 1794, 1956, 2125, 2301, 2484, 2674,
// 2871, 3075, 3286, 3504, 3729, 3961, 4200, 4446,
// 4699, 4959, 5226, 5500, 5781, 6069 ... etc

// k = 10
}
main();``````

#### Output

``  1  9  24  46  75  111  154  204  261  325``
``````// Kotlin program for
// Nonagonal number
class NonagonalNumber
{
fun nonagonalNo(k: Int): Unit
{
var n: Int = 1;
// Print all initial k nonagonal number
while (n <= k)
{
// Formula
//  n(7n-5)
// ————————
//    2

// Calculate nth nonagonal number
val result: Int = (n * (7 * n - 5)) / 2;

// Display calculated result
print(" " + result);
n += 1;
}
}
}
fun main(args: Array < String > ): Unit
{
//  Nonagonal number are
// —————————————————————————————————————————————
//  1, 9, 24, 46, 75, 111, 154, 204, 261, 325, 396,
// 474, 559, 651, 750, 856, 969, 1089, 1216, 1350,
// 1491, 1639, 1794, 1956, 2125, 2301, 2484, 2674,
// 2871, 3075, 3286, 3504, 3729, 3961, 4200, 4446,
// 4699, 4959, 5226, 5500, 5781, 6069 ... etc

// k = 10
}``````

#### Output

`` 1 9 24 46 75 111 154 204 261 325``

## Resultant Output Explanation

When we run the provided C code with 'k = 10', the output will be as follows:

``1  9  24  46  75  111  154  204  261  325``

These are the first ten nonagonal numbers, calculated using the formula mentioned in the pseudocode.

## Time Complexity

The time complexity of the given code is O(k) since it has a loop that runs 'k' times to calculate and print the first 'k' nonagonal numbers. As the value of 'k' increases, the time taken to find the nonagonal numbers will grow linearly.

## Comment

Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.

Categories
Relative Post