Question

In: Computer Science

*Code in C* Write a function that checks if a number is a perfect cube. Write...

*Code in C*

Write a function that checks if a number is a perfect cube.

Write another function that calculates the integer cubic root.

Under the main program:

  • Prompt the user to input a number
  • Tell the user if the number is a perfect cube or not
  • Print the cubic root if the inputted number is a perfect cube.

Solutions

Expert Solution

Solution :-

Here in this problem, we need to find whether the number that will be provided to us by the user is a perfect cube and also the cube root of the number if the number has an integer cube root.

In the C program written below, there is a function called isperfectcube ( int ) to find if the number that is given as input is a perfect cube or not, hence returns 0 or 1 accordingly. 0 if the number is not a perfect cube and 1 if it is a perfect cube.

The function uses binary search whose start is 1 and the end is the number that is given as input in the main function.

Now using the while loop we are implementing the binary search that is shown below.

The function cuberoot ( int ), uses the same concept that is binary search to find the cuberoot of the number.

The program with both the function is shown below.

#include <stdio.h> // for the standard input and output stream 

int isperfectcube(int n) // function to check whether the number is a perfect cube or not using binary search.
{
    int start = 1, end = n; // initializing start and the end of the search. 
    
        int mid = (start + end)/2;  // the middle of the searching range is calculated.
        
    while (start <= end)
    {
        if ((mid * mid * mid) == n)
        {
            return 1;   // returning is the mid is equal to the cube root.
        }
        
        if((mid * mid * mid) > n)
            end = mid - 1;
            
        else
            start = mid + 1;
        
        mid = (start + end)/2;
    }
    return 0;
}

int cuberoot (int n)  // function to search the cube root if there exist one using binary search.
{
    int start = 1, end = n;
    int mid = (start + end)/2;
    
    while (start <= end)
    {
        if ((mid * mid * mid) == n)
        {
            return mid;
        }
        
        if((mid * mid * mid) > n)
            end = mid - 1;
            
        else
            start = mid + 1;
        
        mid = (start + end)/2;
    }
    
    return 0;
}

int main()
{
    int number;

    printf("Enter the number : ");

    scanf("%d", & number);
    
    if(isperfectcube(number))
    {
        printf("The number is a perfect cube.\n");
        int croot = cuberoot(number);
        printf("The cube root of the number %d is %d", number, croot);
    }
    
    else
    {
        printf("The number is not a perfect cube.");
    }
    
    return 0;
}

Related Solutions

Please write a pep/9 assembly code that checks if a word or number is a palindrome
Please write a pep/9 assembly code that checks if a word or number is a palindrome
Write a function name as 'checkEvenOrOdd' that checks the input value is odd or even number....
Write a function name as 'checkEvenOrOdd' that checks the input value is odd or even number. The main function is given: int main(){     int number;     cout << "Check number input: ";     cin >> number;     cout << "The input number " << number << " is " << checkEvenOrOdd(number) << endl;     return 0; } simple c++ code please
C PROGRAMMING 1. Write a C Language inline function that computes the cube of float X,...
C PROGRAMMING 1. Write a C Language inline function that computes the cube of float X, if X is greater than 1 and X is less than 100. Put the inline function in a main program that reads X from the keyboard, calls the function, and then outputs the result. 2. Show an empty statement and detail what it does? 3. A collection of predefined functions is called a Database                    C) Subroutine                       E) None of these Library                       D) Directive 4....
C++ Write the C++ code for a void function that prompts the user to enter a...
C++ Write the C++ code for a void function that prompts the user to enter a name, and then stores the user's response in the string variable whose address is passed to the function. Name the function getName.
Please write the code in c++ Write a function with one input parameter that is a...
Please write the code in c++ Write a function with one input parameter that is a vector of strings. The function should count and return the number of strings in the vector that have either an 'x' or a 'z' character in them. For example, when the function is called, if the vector argument contains the 6 string values, "enter", "exit", "zebra", "tiger", "pizza", "zootaxy" the function should return a count of 4. ("exit", "zebra", "pizza", and "zootaxy" all have...
Write c code to determine if a binary number is even or odd. If it is...
Write c code to determine if a binary number is even or odd. If it is odd, it outputs 1, and if it is even, it outputs 0. It has to be less than 12 operations. The operations have to be logical, bitwise, and arithmetic.
Write a C++ program to print all the perfect numbers below a certain given number. A...
Write a C++ program to print all the perfect numbers below a certain given number. A perfect number is a number that that is equal too the sum of it's divisors other than itself . For example, 6 and 28 are all perfect numbers. 6 = ( 1+2 +3) 28 = (1+2+4+7+14) Make sure your program conforms to the following requirements: 1. Accept the upper limit from the user (as an integer). 2. Make sure the number is positive (at...
[PLEASE USE C++] Write a function to read values of a number of rows, number of...
[PLEASE USE C++] Write a function to read values of a number of rows, number of columns, 2 dimensional (2D) array elements and display the 2D array in a matrix form. Input 2 3 1 4 5 2 3 0 Where, First line of represents the number of rows. Second line of input represents the number of columns. Third line contains array elements of the 1st row and so on. Output 1 4 5 2 3 0 where There must...
Write c code program for the following Write a function, circle, which takes the radius of...
Write c code program for the following Write a function, circle, which takes the radius of a circle from the main function and assign the area of the circle to the variable, area, and the perimeter of the circle to the variable, perimeter. Hint: The function should have three variables as input. Since the contents of the variables are to be modified by a function, it is necessary to use pointers. Please type out the full usable program. Thank you.
Code needed in C++ (nOT IN STEP BY STEP EITHER)    Write a recursive function that...
Code needed in C++ (nOT IN STEP BY STEP EITHER)    Write a recursive function that computes the sum of the digits in an integer. Use the following function header: int sumDigits(int n) For example, sumDigits(234) returns 2 + 3 + 4 = 9. Write a test program that prompts the user to enter an integer and displays its sum.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT