R
reepakmajhi.com
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)
positive,print its square root
otherwise print pow(n,5)
Sam said:Nobody here is going to help you with your homework assignment. Figure
it out by yourself.
Sam said:Nobody here is going to help you with your homework assignment. Figure
it out by yourself.
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)
osmium said: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)ow(number,5);
cout<<odd;
system("pause");
}
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)ow(number,5);
cout<<odd;
system("pause");
}
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)ow(number,5);
cout<<odd;
system("pause");
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)ow(number,5);
cout<<odd;
system("pause");
}
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.
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!
Ok, that doesn't compile on my system.
#include <iostream>
iostream.h possibly doesn't (and probably shouldn't) exist.
std::cout<<"enter number";
The iostream stuff is all in "namespace std" (read up about
namespaces if you're not familiar with them). Same for cin.
Hmmm... think of a simple test for oddness (or for evenness).
Hint: what do you get when you divide an odd "int" value by 2
?
However you do it, "odd" is surely not the right name for what
you want to print out.
Not sure why you want this... but if you do you probably have
to "#include <cstdlib>" or "#include stdlib.h" before main()
It definitely should exist, for reasons of backward compatibility. Many
implementations, however, provide a version of it which isn't backward
compatible. And you may need special compiler flags to get at it.
What is certain is that someone learning C++ today shouldn't be using
it.
Sure.
[...]
Hmmm... think of a simple test for oddness (or for evenness). Hint:
what do you get when you divide an odd "int" value by 2 ?
Better yet, what is the definition of an odd number? And is there an
operator which could give the required information. (I wouldn't use
division.)
It probably does exist, for backward compatibility.
Hm. My compiler (gcc 4.3.2) won't pick it up (with -std=c++98
-pedantic). In fact it doesn't appear to exist in the source
or installation tree for gcc 4.3.2. What does the standard
say?
Sure.[...]Better yet, what is the definition of an odd number? And iscin>>number;
odd=(n=number/2)? sqrt(number)ow(number,5);
Hmmm... think of a simple test for oddness (or for
evenness). Hint: what do you get when you divide an odd
"int" value by 2 ?
there an operator which could give the required information.
(I wouldn't use division.)
I wouldn't either; but I tend to think of division as a more
"elementary" operation than modulus (and perhaps more likely
to be familiar to the OP).
It's not a question of the standard. It's a question of supporting
existing code. A compiler would be quite irresponsible not to provide
it, although as I said, it may require special options to get it. (And
I wouldn't expect to get it if I specifically request 100% standard
compatibility.)
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.