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.

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.