# for the life of me...

• 01-24-2006
vwy
for the life of me...
i cannot figure out how to input the square root of this problem. i just started a class a few days ago at university and my teacher made the assumption that we all have a hale background in computer science:
To test our knowledge of loops and if statements, we shall write a program which will prompt the user to enter an integer n. If n is positive, the user should print out a table of squares, cubes and square roots of the numbers between 1and n (inclusive). If the number is zero or negative, the user should print an appropriate error message.

i've had to largely teach myself with online courses, so i've been hacking away at this for a few hours. it must be noted again that i am a complete novice, so please be liberal with the layman's terms.

this is what i've got so far:
#include <stdio.h>
#include <math.h>
int main(void)
{
int n;
int r=(n-1);
printf("Enter an Integer: ");
scanf("%d", &n);
if (n>0)
{
printf("%d\n",n*n*n);
printf("%d\n",n*n);
printf("%f\n",sqrt(n));
}
while (r>1)
{
printf("%d",r);
r=r*r*r;
r=r*r;
printf("%f",r=sqrt(r));
}
else
{
printf("Sorry, try inputting a positive number next time.\n");
}
return 0;
}

thanks in advance for being reasonable.
• 01-24-2006
peterchen
i think you have a general idea of how to write the program but alittle bit stuck about using the keywords.

1. the format of an if and else statement is shown below (yours is wrong- you can't place the while statement in between the } and else)
Code:

```if (condition) {     statements; } else {     statements; }```
2. might want to use a for statement (similar to while) to output your square roots and so on. general form shown below.
Code:

```for(initiation; condition; increment) {     statement; }```
here is what i would come up with using some of your code (if you input something that is not a number error will occur)
Code:

```#include <stdio.h> #include <math.h> int main(void){     int n;         //get your integer     printf("Enter an Integer: ");     scanf("%d", &n);         if (n>0) {         // prints the table of squares, cube and square root         printf("number\tsquare\tcube\tsquareroot\n");         for (n; n >0; --n) {             printf("%d \t%d \t%d \t%.2f\n", n, n*n, n*n*n, sqrt(n));         }        }     else {         printf("Sorry, try inputting a positive number next time.\n");     }     return 0; }```
• 01-24-2006
Bigbio2002
it looks like youve done a pretty good job getting the basics down. now, you need to get yourself into a cold, computerized mindset. :cool:

first of all, youve tried to initialize r even though n hasnt been initialized yet. after the user inputs n, then you can go ahead and set r to equal n-1.

secondly, you need to nest the while into the if block. the way you currently have your code, the while executes no matter what, probably printing out garbage values in the process.

thirdly, your while loop needs to be rewritten since it looks like its going to do something really crazy anyways. ive rewritten it for you, so it is similar to the code you had right after if. it will print the cube, square, and sqrt of the numbers in order from 1 to n. ive used a for loop, so if you havent learned it yet, ill explain it to you later.

in the process, ive made your variable r obsolete, but im going to leave it in as an example, even if its not needed.

ive also indented your code for you so you can read it easier and understand how the nesting works. afterwards, you can go ahead and format your output as you wish, since it looks like its going to be a jumble of numbers.

Code:

```#include <stdio.h> #include <math.h> int main(void) {     int n;     int r;  //not used anymore     int i;     printf("Enter an Integer: ");     scanf("%d", &n);     r = (n-1);  //initialize r after n     if (n>0)     {         for(i=1;i<=n;i++) //cycle through 1 to n         {             printf("%d\n",i*i*i); //cube             printf("%d\n",i*i); //square             printf("%f\n",sqrt(i)); //square root         }  //set of...     }  //...nested brackets     else     {         printf("Sorry, try inputting a positive number next time.\n");     }     return 0; }```
• 01-24-2006
peterchen
"should print out a table of squares, cubes and square roots of the numbers between 1 and n (inclusive)" your program Bigbio2002 does not do this. instead it prints a column of numbers alternating between cube, square and square root of n.
• 01-24-2006
vwy
thank you so much, guys. i truly am grateful for your prompt and helpful replies!
what is the a function?
and the square root function is in the math library, right? so what would i type to have it produce square roots?
i've tried the tutorials on this site and have searched for square root, but to no avail.
thanks again. i really appreciate your help.
• 01-24-2006
peterchen
"what is the a function?
and the square root function is in the math library, right? so what would i type to have it produce square roots?"

squareroot = sqrt(your_number);

sorry i don't understand - i can't help if i don't understand provide more info
• 01-24-2006
vwy
i think i've now got it.
thanks again.
• 01-24-2006
Bigbio2002
Quote:

Originally Posted by peterchen
"should print out a table of squares, cubes and square roots of the numbers between 1 and n (inclusive)" your program Bigbio2002 does not do this. instead it prints a column of numbers alternating between cube, square and square root of n.

i know thats not the exact output that he wanted. i said that hed have to format it to make it look nice, but thats the gist of what the program is supposed to do.
• 01-25-2006
peterchen
ok, sorry about that, i think i missed that bit

anyway it's a good thing vwy what he/she was looking for