473,396 Members | 1,927 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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
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.

1 1297
weaknessforcats
9,208 Expert Mod 8TB
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

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

Similar topics

6
by: John Bentley | last post by:
John Bentley writes at this level: If we think about our savings accounts then division never comes in (as far as I can see). We deposit and withdraw exact amounts most of the time. Occasionaly...
3
by: shez | last post by:
Hi, Is there any way to output a double value with different number of decimal places (apart from using 'sprintf'). I looked up the reference for C++ streams and found the 'setprecision'...
27
by: Gaijinco | last post by:
Sooner or later everytime I found recreational programming challenges I stumble with how I test if a number is has decimal places differnt than 0? For example if I want to know if a number is a...
3
by: android | last post by:
I require a function that takes a double as a parameter and returns the number of decimal places. What is the most efficient way in c# to find out the number of decimal places I have written a...
14
by: dharmdeep | last post by:
Hi friends, I need a sample code in C which will convert a Hexadecimal number into decimal number. I had written a code for that but it was too long, I need a small code, so request u all to...
17
by: scan87 | last post by:
Can somone please, please give me the solution for the following problem. I need to submit it on Monday. Write a global function called format, which formats numbers with a given number of decimal...
7
by: Gonzchris | last post by:
What would be the easiest way of going about converting a 3 digit hexadecimal number to decimal, I think that a switch statement is out of the question due to multiple digits. Would it be easiest...
1
by: mertoman | last post by:
Hello, In Adobe LiveCycle Designer 8 I have a form field that users should be able to us to enter a whole number followed by a fraction or a decimal number such as: 45 1/8 or 45.125 I...
4
by: DixitSolanki | last post by:
I want to convert ASCII hex number to decimal conversion. For Example : 1: F01A is converted into Decimal: 61466 2: FFFC64F0 is converted into Decimal: 4294730992 What is the best...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.