By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,956 Members | 1,683 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,956 IT Pros & Developers. It's quick & easy.

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

P: 2
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
Apr 13 '16 #1

✓ answered by weaknessforcats

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

What you have is a * I.

I changed your loop to:

Expand|Select|Wrap|Line Numbers
  1. b = a;
  2.     do{
  3.         b = b*a;
  4.         i++;
  5.     } while (i < 2);
and I get 42.875.

Share this Question
Share on Google+
1 Reply


weaknessforcats
Expert Mod 5K+
P: 9,197
You are not calculating the cube. That would be a * a* a.

What you have is a * I.

I changed your loop to:

Expand|Select|Wrap|Line Numbers
  1. b = a;
  2.     do{
  3.         b = b*a;
  4.         i++;
  5.     } while (i < 2);
and I get 42.875.
Apr 13 '16 #2

Post your reply

Sign in to post your reply or Sign up for a free account.