How to obtain a perfect value for the cube of a number (Whole/Decimal)

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Gokul619
    New Member
    • Apr 2016
    • 2

    How to obtain a perfect value for the cube of a number (Whole/Decimal)

    Hi friends ,

    I have used the logic below to calculate the cube of a number (whole no. or decimal)

    #include <stdio.h>
    void main(){
    int i=0,n;
    float a,b;
    printf("Enter the number\n");
    scanf("%f",&a);
    n=a*a;
    do{
    b=a*i;
    i++;
    }while(i<=n);
    printf(" The answer is \n%f",b);
    }


    Iam unable to produce the output at a floating point . For eg: if I/P entered is 3.5 , the O/P comes as 42 but the answer is 42.875 . Can someone help me with this
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    You are not calculating the cube. That would be a * a* a.

    What you have is a * I.

    I changed your loop to:

    Code:
    b = a;
    	do{
    		b = b*a;
    		i++;
    	} while (i < 2);
    and I get 42.875.

    Comment

    Working...