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
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....
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++ 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.
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.
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.
[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 code in C please. #1 Write a function multiples() which will take an integer input...
Write code in C please. #1 Write a function multiples() which will take an integer input and it will print out all the multiples of this number starting from 2 but not including itself. For example, multiples(10) will print 2, 5 and multiples(100) will print 2, 4, 5, 10, 20, 25, 50 #2 Write and test a Fibonacci() function that uses a loop instead of recursion to calculate Fibonacci numbers.
Using pseudocode or C++ code, write a function to compute and return the product of two...
Using pseudocode or C++ code, write a function to compute and return the product of two sums. The first is the sum of the elements of the first half of an array A. The second is the sum of the elements of the second half of A. The function receives A and N ≥ 2, the size of A, as parameters. (in c++)
Write C code that contains a function called triangle_generator that outputs a triangle wave at a...
Write C code that contains a function called triangle_generator that outputs a triangle wave at a specified frequency (in hertz) using a specified sample rate (in hertz), and for a specified time duration (in seconds). These parameters are float type. The output you generate are floating point numbers between -1 and 1, one number per output line. The math trig functions in C use radians while all the specifications here are in hertz (cycles per second). One hertz is 2*Pi...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT