On Fri, 14 Nov 2008 11:12:58 -0600, osmium wrote:
> <reepakmajhi.com@gmail.com> wrote:
>
>> osmium wrote:
>>> <reepakmajhi.com@gmail.com> wrote:
>>>
>>> > Write a C++ program to input a number. If the number n is odd and
>>> > positive,print its square root
>>> > otherwise print pow(n,5)
>>>
>>> There are five or six sub-problems there. I think you should post
>>> some code
>>> to show that you are at least trying and also to show where your
>>> problems are. Unless you do post some code, I predict you will get no
>>> further help.
>>
>> The following i have tried
>>
>> #include<iostream.h>
>> #include<math.h>
>> int main()
>> {
>> int number,odd;
>> float n;
>> cout<<"enter number";
>> cin>>number;
>> odd=(n=number/2)? sqrt(number):pow(number,5);
>
> You're trying to do too much in one statement. Crawl, walk, and then
> run is a good idea. Note that odd is an integer, but that square roots
> are, in general, decimal numbers. So odd can't contain a meaningful
> square root, can it? But odd is all you print.
>
> The assignment says "... n is odd ...". But in the code you use n for a
> float. Oddness and evenness are qualities of integers, not decimals.
> Don't use n to mean two things in the same exercise.
>
>> cout<<odd;
>> system("pause");
>> }
>
> I would write it something like this. -----
> #include <iostream>
> #include <cmath>
> #include <cstdlib>
>
> int main()
> {
> using namespace std;
> while(1)
> {
> int n;
> cin >> n;
> if((n%2) > 0)
> cout << sqrt(n) << endl;
> else if(n > 0)
> cout << pow(n, 5.0) << endl;
> else
> cout << "n was zero or negative\n";
> }
> system("pause");
> }
> ------
> Some people who have way too much time on their hands will bitch about
> using the "using" statement and that the user might type in something
> like "apple" and the program will respond badly. To them I say: For
> God's sake man, get a life!
Others may say: For God's sake man, you've just given away the answer!
How's anyone going to learn anything without thinking for themself?
--
Lionel B