I need to write a program where it would ask the user to enter a positive even number,when entered if wrong then it would say value is illegal and will terminate the program. If it is even then program prints out the square of the number.
I have just started programming and i am finding this really hard.please help.

Dani AI

Generated

This thread shows the basic goal (accept a positive even integer, otherwise terminate) and the practical syntax fixes supplied by to the first attempt from . The quick fixes on parentheses and I/O get the program running, but a few robustness points were not covered: treat zero as not positive, detect non-numeric input, and avoid overflow when squaring large values.

A recommended validation flow for a small console program:

  • Read the whole input line and parse it to an integer type that can hold the expected range (use 64-bit for safety).
  • Reject the input if parsing fails or leaves trailing characters.
  • Reject values <= 0 (zero is not positive) or values that are odd.
  • Before computing the square, ensure the multiplication will not overflow the target type; if it would, handle that as an error rather than producing a wrapped result.
  • On any failure, print an appropriate error and exit with a non-zero status (or call exit(EXIT_FAILURE)).

A compact, robust example (parsing + positivity + parity + overflow check):

#include <iostream>
#include <string>
#include <cstdlib>
#include <cerrno>
#include <limits>

int main() {
    std::string line;
    if (!std::getline(std::cin, line)) return 1;
    char *end = nullptr;
    errno = 0;
    long long v = std::strtoll(line.c_str(), &end, 10);
    if (end == line.c_str() || *end != '\0' || errno == ERANGE) return 1;
    if (v <= 0 || (v % 2) != 0) return 1;
    if (v > std::numeric_limits<long long>::max() / v) return 1;
    std::cout << (v * v) << '\n';
    return 0;
}

Notes: for quick classroom exercises an int and cin >> are fine, but real code benefits from the parsing and overflow guard above. Also prefer explicit namespace qualification in shared code rather than a global using namespace std;.

Recommended Answers

All 4 Replies

In this forum you need to show effort for people to help you. Take a stab at it and post what you're having troubles with.

include <iostream>
using namespace std;
int main()
{
int x=0;
cout<<"please enter a positive even number"<<
cin>>x;
if ((x%2==0)
cout<<"the square is"<<x*x<<endl;
if ((x%2==1)
cout<<"value is illegal"<<endl;
exit (0);
return 0;
}

This is what i did

You're almost dead on, so i'll just fix up the syntax for you. You should note that code tags should be used when posting code. The syntax for code tags is
[ code = cplusplus ]...code goes here [ / code] without any spaces

Here's your fixed code

#include <iostream>
using namespace std;
int main() 
{
int x=0;
cout<<"Please enter a positive even number: ";
cin>>x;
if (!(x%2))
	cout << "\nThe square is " << x*x << endl;
else
	cout<<"\nValue is illegal"<<endl;

//flush the input and wait for the return key to exit
cin.ignore(cin.rdbuf()->in_avail());
cin.get();
return 0;
}
commented: Useful advice, and helpful! =) +4

got it.thnx a lot.

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.