0

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.

2
Contributors
4
Replies
5
Views
8 Years
Discussion Span
Last Post by shopnobhumi
Featured Replies
  • 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 [code=cplusplus] #include … Read More

0

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.

0
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

Edited by happygeek: fixed formatting

1

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;
}
Comments
Useful advice, and helpful! =)
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.