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

Write a recursive a c++ code that checks if a number is Palindrome. A palindrome number...
Write a recursive a c++ code that checks if a number is Palindrome. A palindrome number is a number that reads the same from beginning to end and from end to beginning, in other words, a palindrome number remains the same when its digits are reversed. For example, 13431 is a palindrome number. 2332 is another one. (Note: Your algorithm should define and work with an integer number) The functionality of your code should be commented to explain what you...
Write a recursive a Java code that checks if a number is Palindrome. A palindrome number...
Write a recursive a Java code that checks if a number is Palindrome. A palindrome number is a number that reads the same from beginning to end and from end to beginning, in other words, a palindrome number remains the same when its digits are reversed. For example, 13431 is a palindrome number. 2332 is another one. (Note: Your algorithm should define and work with an integer number) The functionality of your code should be commented to explain what you...
All Code should be written in C: 1. A perfect number is defined as a number...
All Code should be written in C: 1. A perfect number is defined as a number whose proper divisors (factors not including the number itself) add up to the same number. For example, 28 is a perfect number because its perfect divisors are 1, 2, 4, 7, 14, which add up to 28. Write a C function called is_perfect that takes a since integer as input, and returns 1 if the number is perfect, or 0 otherwise. 2. Using the...
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 programming Get a number from the user and write a program that checks to see...
C programming Get a number from the user and write a program that checks to see if the inputted number is equal to x*x+x and x must be greater than 0. For example, if the user inputs 12. The program should check to see if 12 = x*x+x. In this case it is true because 3*3+3 = 12.
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.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT