Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Semrush - keyword research tool

Turn ChatGPT, Claude, Gemini, And CoPilot Into Your Personal Assistant, Business Coach, Content Creator, And More

AFFILIATE MARKETING Your all-in-one performance engine Manage affiliates, creators, and customer referrals in one unified platform—turning every partnership into measurable growth
Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

42,849 questions

55,678 answers

573 users

How to find the next number in the series 1, 8, 27, 64, 125, 216 using C

1 Answer

0 votes
#include <stdio.h>
#include <math.h>   // for cbrt() and round()

/*
    This program finds the next number in a sequence of perfect cubes.
    The input series is: 1, 8, 27, 64, 125, 216

    These values correspond to:
    1^3, 2^3, 3^3, 4^3, 5^3, 6^3

    The next value is 7^3 = 343.

    The approach:
    - Verify each number is a perfect cube.
    - Extract the cube root of the last number.
    - Compute (root + 1)^3 to get the next number.
*/

// Check whether a number is a perfect cube.
// Uses cbrt() and rounding to verify the cube root.
int isPerfectCube(int value) {
    double root = cbrt((double)value);
    int rounded = (int)round(root);

    return rounded * rounded * rounded == value;
}

// Compute the next cube in the series.
int nextCubeInSeries(const int *series, int length) {
    int lastValue = series[length - 1];

    // Compute the integer cube root of the last value.
    int lastRoot = (int)round(cbrt((double)lastValue));

    // The next number is (lastRoot + 1)^3.
    int next = (lastRoot + 1) * (lastRoot + 1) * (lastRoot + 1);

    return next;
}

int main(void) {
    int series[] = {1, 8, 27, 64, 125, 216};
    int length = sizeof(series) / sizeof(series[0]);

    // Validate the pattern before computing the next value.
    for (int i = 0; i < length; ++i) {
        if (!isPerfectCube(series[i])) {
            printf("Series contains a non-cube value.\n");
            return 1;
        }
    }

    int nextValue = nextCubeInSeries(series, length);

    printf("Next number in the series: %d\n", nextValue);
    
    return 0;
}



/*
run:

Next number in the series: 343

*/

 



answered 5 days ago by avibootz
...