Hello, I have been stuck with a C program, which I have to write for my foundation degree. I know it's simple, but can't seem to wrap my head around it.

Write a program that uses a For loop to:
Input three float numbers less than 20
Cube each one and output each cubed value
Output the total of the three cubed values

We are only allowed to use For loops. Nothing else. I do understand that the arithmetic operation needs to be inside the For loop, but can't seem to process how to approach it :(

Isn't the cube just the multiplication of itself by three times?

Isn't the cube just the multiplication of itself by three times?

Er..Isn't it actually multiplication by itself twice? The thing is, I cant for some reason, understand HOW to write the part inside the loop.
Should it look something like
{
for ( i=3. ; i<21; i++)
cube=i*i**i

printf("your value cubed value is %f", cube);
}
?

[pseudocode]

initialize sumOfCubes = 0.0

for  a = 1 to 3
{
   get float input from user
   store in inputValue

   initialize cubedValue = 1.0

   for b = 1 to 3
   {    
       cubedValue =  cubedValue * inputValue
   }

   print "the cube of " inputValue " is " cubedValue "."

   sumOfCubes = sumOfCubes + cubedValue
}

print "the sum of the cubes is " sumOfCubes "."

[/pseudocode]



.

i see part of your problem... you can't use a double asterisk ** like you think you can. and even if you could, they way you're trying to do it would be wrong.

it's simply this:

'i' cubed = i*i*i

'i' to the fourth is i*i*i*i

but in the real world you'd use the <math.h> library and do this:

iCubed = pow(i,3);

iToTheFourth = pow(i,4);

Gyaaaah. Wrote a reply, but the forum ate it.

i see part of your problem... you can't use a double asterisk ** like you think you can. and even if you could, they way you're trying to do it would be wrong.

it's simply this:

'i' cubed = i*i*i

'i' to the fourth is i*i*i*i

but in the real world you'd use the <math.h> library and do this:

iCubed = pow(i,3);

The ** was a typo :( We're not allowed to use the math library as we haven't done it...We've just done variables, IF statements and For loops. So im not sure how to do it :(

Interesting_Hat >We've just done variables, IF statements and For loops. So im not sure how to do it
Whining might get you want you want, eventually, from someone over here, but definitely will not help you learn.

Start simpler.
Show us a loop where you ask user for three input of float type.
Check that those three inputs are less than twenty.

The ** was a typo

oh, i see. well, the double asterisk operator (**) is in some languages the "Power" operator. so that cubeVal = i ** 3 would be a valid expression for some languages, though not in C.

I forget where that's used, maybe in BASIC. that's what i thought you might be trying to do.


We're not allowed to use the math library as we haven't done it...

yeah, i figured as much. that's why i prefaced it with the future-conditional statement "in the real world you would _____"

it's common practice that beginning comp sci classes force you to do things the "long" way so you understand the mechanics before they let you take off and use standard libraries.

anywho, if you follow my pseudo code, you should have this done in no time.

.

commented: Fortran has ** +2

As far i understand the problem ,you can use that solution for you program if i am wrong then plz correct me .

#include<stdio.h>
 #include<conio.h>
 void main()
 {
 int i;
 float no,cube,sum=0;
 for(i=0;i<3;i++)
 {
 printf("\nEnter the no less then 20:-");
 scanf("%f",&no);
 if(no>20)
 {
 printf("\n\nEntered no is not less then 20 plz enter again.");
 i--;
 }
 cube=no*no*no;
 sum=sum+cube;
 printf("\nCube for %d value %f",i+1,cube);
 }
 printf("\n\nTotal =%f",sum);
 }

Thank you all very much for the help! I did figure it out on Friday, but it's very appreciated :)

As far i understand the problem ,you can use that solution for you program if i am wrong then plz correct me .

#include<stdio.h>
 #include<conio.h>
 void main()
 {
 int i;
 float no,cube,sum=0;
 for(i=0;i<3;i++)
 {
 printf("\nEnter the no less then 20:-");
 scanf("%f",&no);
 if(no>20)
 {
 printf("\n\nEntered no is not less then 20 plz enter again.");
 i--;
 }
 cube=no*no*no;
 sum=sum+cube;
 printf("\nCube for %d value %f",i+1,cube);
 }
 printf("\n\nTotal =%f",sum);
 }

Wouldn't this give wrong results in case I enter value greater than 20 coz its not initialized

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.