Click here to Skip to main content
15,905,563 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
static void Main(string[] args)
{
  long    virus ;
           
  virus = Convert.ToInt32(Console.ReadLine ());
  string c="";
  for (int x = 0; x<=120; x++)
  {
    //   Console.WriteLine(b.ToString ()); 
    virus = virus * 2;
    //c = b.ToString();
  }
  Console.WriteLine((virus).ToString ());
  Console.Read();
}



why give me zero?
Posted
Updated 29-Oct-10 20:49pm
v2

you can use double, there is no other data type that is integer & gather than long. I think you multiply the value by 2^120. It will be out of range in long. You also can use string for integer multiplication.
static void Main(string[] args)
{
  double    virus ;
           
  virus = Convert.double(Console.ReadLine ());
  string c="";
  for (int x = 0; x<=120; x++)
  {
    //   Console.WriteLine(b.ToString ()); 
    virus = virus * 2;
    //c = b.ToString();
  }
  Console.WriteLine((virus).ToString ());
  Console.Read();
}


for string multiplication, you can see this link

Click
 
Share this answer
 
That is because long is 64-bit signed Integer
and range of long datatype is -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807, It can't count more then this

My advise would be to use double instead will give you value in Exponential format and will not be overflowed.

Please vote and Accept Answer if it Helped.
 
Share this answer
 
Comments
Mostafa Elsadany 30-Oct-10 3:30am    
ok thnx for help
but i try double giving my 1.000000 so this fraction
what i do
shakil0304003 30-Oct-10 3:32am    
For removing fraction you can use, big integer multiplication, which link i gave in my answer. link -> http://www.learnxpress.com/addition-multiplication-of-very-long-integers.html
static void Main(string[] args)
{
double virus;
virus = Convert.ToInt32(Console.ReadLine());
for (int x = 0; x <= 120; x++)
{
// Console.WriteLine(b.ToString ());
virus = virus * 2;
//c = b.ToString();
}
Console.WriteLine(virus);
Console.Read();
}

It is giving solution in exponential form.
 
Share this answer
 
What exactly do you want with it?
Creating a 128bit (or bigger) may do the trick if you want integers. It'll be faster than a BigNum, and it won't suffer from the accuracy problem of a double.
 
Share this answer
 
Comments
Mostafa Elsadany 2-Nov-10 20:27pm    
static void Main(string[] args)
{
long virus ;

virus = Convert.ToInt32(Console.ReadLine ());
string c="";
for (int x = 0; x<=120; x++)
{
// Console.WriteLine(b.ToString ());
virus = virus * 2;
//c = b.ToString();
}
Console.WriteLine((virus).ToString ());
Console.Read();
}

when try this code give me error because the result is very large
thnx for help
[no name] 3-Nov-10 4:12am    
yes.. that's because you actually Didn't do what I suggested, you just used a 64bit data type again.
static void Main(string[] args)
{
long virus;
virus = Convert.ToInt32(Console.ReadLine());
string c = "";
for (int x = 0; x <= 120; x++)
{
// Console.WriteLine(b.ToString ());
virus = virus * 2;

//c = b.ToString();
}
Console.WriteLine(virus);
Console.Read();
}
in this case giving zero
plz try
thnx for help
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900