# I'm new taking my 1st class, and I am having trobble with my homework.. help please!

This is a discussion on I'm new taking my 1st class, and I am having trobble with my homework.. help please! within the C Programming forums, part of the General Programming Boards category; /*Homework2 Wednesday, September 21st, 2011 1:02 PM Homework 2: The program asks user for a positve integer greater than 0 ...

1. ## I'm new taking my 1st class, and I am having trobble with my homework.. help please!

/*Homework2
Wednesday, September 21st, 2011 1:02 PM
Homework 2: The program asks user for a positve integer greater than 0 and less than 10. Program prints out:

1. whether number is even or odd
2. cube of number
3. square root of number
4. Sum of the digits
5. factorial

If an unsuitable number is entered, program exits only printing that an invalid number was entered.*/

Code:
```#include <stdio.h>
#include <math.h> /* for sqrt*/

/* ask user to input a number between 1 and 10.
program will check number and terminate if input invalid
Otherwise the program will prnt out if the number is odd or even
value cubed, sqare root of the number, sum of the digits and factoral*/

int main()
{
int number, i, sumx, productx, cubed, sqrt, factoral;
printf(" enter a postive number between 1 and 10:");
scanf("%d", &number);
/* if ((number > 0 ) && (number < 11)).....*/

{ number/2 = even
printf ("the number %d is even\n");
else
printf ("the number %d is odd\n"); /* 1 */
}

{cubed = (number*number*number)
printf (" the number cubed is %d\n"); /*2*/
}

{sqrt = (sqrt(number))
printf (" the number square root is %d\n"); /*3*/
}

{sumx=0;
for (i=1;i<=number;i=i+1)

sumx =sumx+ i;
printf("the sum of the digits is %d\n", sumx); /* 4 */

for (i=1; i<= number; i=i+1)
productx=productx*i;
}

{factoral=0;
for (i=1;i<=number;i=i*1)

factoral =factoral* i;
printf("factoral of this is %d\n", factoral); /* 5 */

for (i=1; i<= number; i=i*1)
factoral=factoral*i;
}

system("PAUSE");
return 0;
}```

2. What is your question? Does the program work as expected or do you have some problem with it, if so what kind of problem?

3. it doesn't work... I can get only the sum of the numbers to work if I take all the rest out. I need the program to print 5 answers for each part of the assignment. the user enters if the user enters a number 1 though 10 then the program should print out answers to each of these questions...
1. whether number is even or odd
2. cube of number
3. square root of number
4. Sum of the digits
5. factorial

So lets say the user types in 2 I need it to output
2 is odd, the cube is 8 , square root is 1, sum of the digits is 3 and the factorial is 2

4. also dunno if this is a factor but I"m using dev C

5. Start by commenting out everything but the first part (odd/even), then add them in one after one until they are all working. Looking at the first part:

Code:
```    /* if ((number > 0 ) && (number < 11)).....*/

{ number/2 = even
printf ("the number %d is even\n");
else
printf ("the number %d is odd\n"); /* 1 */
}```
That is not even going to compile. The easiest way to determine if a number is uneven is to check if it's rightmost bit is set IMO.

6. what does it mean "it's rightmost bit is set IMO"? I don't think we learned that........

7. If you have not used bitwise operators then you haven't. But actually you could leave that bit since the first requirement (checking the range of the number) is commented out. So, solve that first. I would look for numbers < 0 and > 10 instead, if 'number' pass that test, print an error message and return 0; which will end the program.

8. I think I get what you mean... but I'm not sure how to do it correctly.... this is my attempt.....
Code:
```int main()
{
int number, i, sumx, productx, cubed, sqrt, factoral;
printf(" enter a postive number between 1 and 10:");
scanf("%d", &number);
if (number > 0);
printf (" number is not between 1 and 10\n");
else
if (number < 11);
printf (" number is not between 1 and 10\n");

/* if ((number > 0 ) && (number < 11)).....*/```
thanks
-Cess

9. Originally Posted by Cess
what does it mean "it's rightmost bit is set IMO"? I don't think we learned that........
In an integer value the least significant (right most) bit is always 1 for odd numbers and 0 for even numbers... it's a side effect of computers working in binary.

Code:
```if ((number & 1) > 0)
printf("%d is odd",number);
else
printf(%d is even", number);```
You can also use
Code:
```if ((number % 2) > 0)
printf("%d is odd",number);
else
printf(%d is even", number);```
Subsonics gave you excellent advice about working through it one small bit at a time. That's how we "real" programmers do things... write a given function or section of code and test... work on the next, test again... until it's all working.

The computer does things in little tiny steps, one at a time... and so should you.

10. Originally Posted by Cess
I think I get what you mean... but I'm not sure how to do it correctly.... this is my attempt.....
Code:
```int main()
{
int number, i, sumx, productx, cubed, sqrt, factoral;
printf(" enter a postive number between 1 and 10:");
scanf("%d", &number);
if (number > 0);
printf (" number is not between 1 and 10\n");
else
if (number < 11);
printf (" number is not between 1 and 10\n");

/* if ((number > 0 ) && (number < 11)).....*/```
thanks
-Cess
Code:
```if ((number < 1) || (number > 10))
{
printf("I specifically said from 1 to 10\n");
exit(1);
}

// rest of program here```

If the number is less than 1 OR the number is greater than 10 the program exits.

11. > printf ("the number %d is even\n");
Same mistake as in another thread - you're not supplying a parameter to match the %d

Mind you, you're using a gcc compiler, so perhaps you can find where in dev-c++ you set the compiler flags.

Add -W -Wall -ansi -pedantic and then see more error messages to fix.

12. Originally Posted by Cess
I think I get what you mean... but I'm not sure how to do it correctly.... this is my attempt.....
Code:
```int main()
{
int number, i, sumx, productx, cubed, sqrt, factoral;
printf(" enter a postive number between 1 and 10:");
scanf("%d", &number);
if (number > 0);
printf (" number is not between 1 and 10\n");
else
if (number < 11);
printf (" number is not between 1 and 10\n");

/* if ((number > 0 ) && (number < 11)).....*/```
thanks
-Cess
You have the answer right at the end there, but you need to change it a bit. 'number' can not be both less than 1 and larger than 10, so use OR instead of AND, that is || so:

Code:
```if( (number < 1) || (number > 10) ) {
printf("The number is out of range. (1-10)\n");
return 0;
}```

13. Ok tried fixing it and still getting error.... We haven't used || before so I don't really know how to use it but ya this is what I did and got an error on the return line it says....

Code:
```#include <iostream>
using namespace std;
int main()
{
int number;
printf(" enter a positive number between 1 and 10:");
if( (number < 1) || (number > 10) ) {
printf("The number is out of range. (1-10)\n");

system("PAUSE");
return 0; }```

14. You're going to get an error alright... you're using C++ headers and C doesn't have the first clue what "namespace" is.

Plus your brackets are mismatched ...

15. You need to adopt a good indentation style. Watch:
Code:
```#include <iostream> // this is C++
using namespace std; // so is this
int main() // this should really be (void) instead of just ()
{
int number;
printf(" enter a positive number between 1 and 10:");
if( (number < 1) || (number > 10) ) {
printf("The number is out of range. (1-10)\n");

system("PAUSE");
return 0; }
// where is the matching }```
Count the pairs of braces.

Quzah.

Page 1 of 4 1234 Last
Popular pages Recent additions