Q:

Write a C program to find cube of an integer number using two different methods

0

C program to find cube of an integer number using two different methods

cube can be found by calculating the power of 3 of any number. For example, the cube of 2 will be 23 (2 to the power of 3).

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

1) Without using pow() function

In this method, we will just read an integer number and multiply it 3 times.

Consider the program:

#include <stdio.h>

int  main()
{

    int a,cube;
    
    printf("Enter any integer number: ");
    scanf("%d",&a);
    //calculating cube
    cube = (a*a*a);
    printf("CUBE is: %d\n",cube);
    
    return 0;
}

Output

Enter any integer number: 8
CUBE is: 512

2) Using pow() function

pow() is a library function of math.h header file, it is used to calculate power of any number. Here, we will use this function to find the cube of a given number.

Consider the program:

#include <stdio.h>
#include <math.h>

int  main()
{

    int a,cube;
    
    printf("Enter any integer number: ");
    scanf("%d",&a);
    //calculating cube
    cube = pow(a,3);
    printf("CUBE is: %d\n",cube);
    
    return 0;
}

Output

Enter any integer number: 8
CUBE is: 512

"This is very basic program in C programming language and will be helpful for those who started learning C programming, if you liked, please write comments..."

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

C programming Basic Input, Output, if else, Ternary Operator based Programs

Similar questions


need a help?


find thousands of online teachers now
Write a C program to find quotient and remainder... >>
<< Write a C program to print ASCII value of a charac...