Hello all,
I want to multiply a number by itself,and the number must be Integer.A program, when i get the input number as a string it wont take it as the input n it will show an error "You are not entered Integer,try with some other number".(Without use of exceptions),Is there any possiblity?
i have written one code.But it shows an exception at run time.
int number=int.parse(console.ReadLine());
if (number <= int.MaxValue && number >= int.MinValue)
{
Console.Write("\n Enter the number :");
number *= number;
Console.WriteLine(number);
}
else
{
Console.WriteLine("You are not entered Integer,try with some other number");
}
Whats wrong with my code?How will i write the code only it will accept Integers not an decimal or string.
or Some Clues to do it.I will find out the way.
As am learner i want to know the things.
Thanks.
Loading
ManuelPosted Aug 10, 2007, 5:44 AM
Also you should check
Math.Abs( numer ) <= Math.Sqrt( int.MaxValue )
because you would get an negative number if you would get a larger number than int.MaxValue
AlanPosted Aug 10, 2007, 4:03 AM
If you have .NET 2.0, then using the int.TryParse() method will enable you to do this without throwing an exception:
int number;
bool isValid = false;
do
{
Console.Write("\n Enter the number : ");
isValid = int.TryParse(Console.ReadLine(), out number);
if (isValid) break;
Console.WriteLine("\n You have not entered an integer, try with some other number");
}
while(!isValid);
number *= number;
Console.WriteLine("\n The square of your number is {0}",number);
Notice that you never need to check whether an int lies between its minimum and maximum values. As long as it is an 'int', it must do :)