Dear Members,
I am a fresh beginner in programming just started few days ago, I am totally new!
While studying one of the examples about the integers (byte and int) I can't understand why it gives this result:
here is the code:
----------------------------------
- // Use byte.
- using System;
- class Use_byte {
- static void Main() {
- byte x;
- int sum;
- sum = 0;
- for(x = 1; x <= 100; x++)
- sum = sum + x;
- Console.WriteLine("Summation of 100 is " + sum);
- }
- }
-------------------------
the result is:
Summition of 100 is 5050
-----------------
Can anyone please explainstep by step why it shows 5050 and what is the logic of this code..
Thanks a lot.
VulpesPosted Jan 31, 2014, 9:09 AM
1, 3, 6 etc isn't printed because this line:
Console.WriteLine("Summation of 20 is " + sum);
is not within the body of the 'for' statement.
If you change the program to the following, then you'll see all the intermediate steps:
The output is now:
Mohamed MoubasherPosted Feb 1, 2014, 12:18 PM
VulpesPosted Jan 31, 2014, 9:02 AM
If you add int to byte, then the byte will still be converted to an int and the result will be an int. However, if you're assigning the result back to a byte variable, then a cast will be needed because - as far as the compiler is concerned - the result may not be within the range of a byte.
Here's the same program again but this time sum is a byte and x is an int. I've restricted the summation to the first 20 integers to ensure that the result (210) is within the range of a byte:
Mohamed MoubasherPosted Jan 31, 2014, 8:57 AM
Why for example it is not printed 1,3,5.. etc ! why 5050 and where it came from..
Thanks again.
Salman MushtaqPosted Jan 31, 2014, 7:39 AM
VulpesPosted Jan 31, 2014, 6:29 AM
It is possible to do this without any loss of accuracy because the range of a byte (+ or - 255) is well within the range of an int (+ or - 2 billion in round figures).
So, if you were to change the type of x from byte to int, you'd find that the result of this particular program is the same as before.
Incidentally, you can check that the program is giving the right answer using the well-known formula for summing the first 'n' integers:
n * (n + 1) / 2
This gives:
100 * 101 / 2 = 5050
as expected.
Biswa Pujarini MohapatraPosted Jan 31, 2014, 2:50 AM
Int is value type
first time Sum=0
and as per your loop if X<=100 means loop will itereate till x=100 means 100 times and it will increment by 1 with foreach X++ statement
so firsttime value of sum = 0+1, then sum =1 now again x will increment with 1 means now x=2 and sum is 1 so Sum=1+2 it will be 3 ... next sum = 3+3 = 6 will iterate till x=100.
If you want to see the sum value
Run you application --> keep debug pointer on Sum=Sum+X statement (keep the mouse pointer and press F9) --> rightclick on the sentence and add watch --> now you can see your step by step execution
Hope it helps
Sachin KaliaPosted Jan 31, 2014, 2:45 AM
Welcome to the world of Programming.
It add value one by one.
A short Example is
sum=0;
As loop increments one by one.
1st time : sum=0+1; // Result : sum=1;
2nd time : sum=1+2; //Result : sum =3;
3rd time : sum=3+3; //Result : Sum=5;
As it moves on and add values
Thanks