8
Answers

Can't understand the result of one simple code

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:
 ----------------------------------
  1. // Use byte.
  2. using System;
  3. class Use_byte {
  4. static void Main() {
  5. byte x;
  6. int sum;
  7. sum = 0;
  8. for(x = 1; x <= 100; x++)
  9. sum = sum + x;
  10. Console.WriteLine("Summation of 100 is " + sum);
  11. }
  12. }
------------------------- 
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. 

Answers (8)

3
Photo of Vulpes
NA 98.3k 1.5m 11y
Mohamed,

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:

// 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 " + x + " is " + sum);
}
}
}

The output is now:

Summation of 1 is 1
Summation of 2 is 3
Summation of 3 is 6
Summation of 4 is 10
Summation of 5 is 15
Summation of 6 is 21
Summation of 7 is 28
Summation of 8 is 36
Summation of 9 is 45
Summation of 10 is 55
Summation of 11 is 66
Summation of 12 is 78
Summation of 13 is 91
Summation of 14 is 105
Summation of 15 is 120
Summation of 16 is 136
Summation of 17 is 153
Summation of 18 is 171
Summation of 19 is 190
Summation of 20 is 210
Summation of 21 is 231
Summation of 22 is 253
Summation of 23 is 276
Summation of 24 is 300
Summation of 25 is 325
Summation of 26 is 351
Summation of 27 is 378
Summation of 28 is 406
Summation of 29 is 435
Summation of 30 is 465
Summation of 31 is 496
Summation of 32 is 528
Summation of 33 is 561
Summation of 34 is 595
Summation of 35 is 630
Summation of 36 is 666
Summation of 37 is 703
Summation of 38 is 741
Summation of 39 is 780
Summation of 40 is 820
Summation of 41 is 861
Summation of 42 is 903
Summation of 43 is 946
Summation of 44 is 990
Summation of 45 is 1035
Summation of 46 is 1081
Summation of 47 is 1128
Summation of 48 is 1176
Summation of 49 is 1225
Summation of 50 is 1275
Summation of 51 is 1326
Summation of 52 is 1378
Summation of 53 is 1431
Summation of 54 is 1485
Summation of 55 is 1540
Summation of 56 is 1596
Summation of 57 is 1653
Summation of 58 is 1711
Summation of 59 is 1770
Summation of 60 is 1830
Summation of 61 is 1891
Summation of 62 is 1953
Summation of 63 is 2016
Summation of 64 is 2080
Summation of 65 is 2145
Summation of 66 is 2211
Summation of 67 is 2278
Summation of 68 is 2346
Summation of 69 is 2415
Summation of 70 is 2485
Summation of 71 is 2556
Summation of 72 is 2628
Summation of 73 is 2701
Summation of 74 is 2775
Summation of 75 is 2850
Summation of 76 is 2926
Summation of 77 is 3003
Summation of 78 is 3081
Summation of 79 is 3160
Summation of 80 is 3240
Summation of 81 is 3321
Summation of 82 is 3403
Summation of 83 is 3486
Summation of 84 is 3570
Summation of 85 is 3655
Summation of 86 is 3741
Summation of 87 is 3828
Summation of 88 is 3916
Summation of 89 is 4005
Summation of 90 is 4095
Summation of 91 is 4186
Summation of 92 is 4278
Summation of 93 is 4371
Summation of 94 is 4465
Summation of 95 is 4560
Summation of 96 is 4656
Summation of 97 is 4753
Summation of 98 is 4851
Summation of 99 is 4950
Summation of 100 is 5050






Accepted
13
Photo of Vulpes
NA 98.3k 1.5m 11y
Salman,

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:

// Use byte.
using System;
class Use_byte {
static void Main() {
int x;
byte sum;
sum = 0;
for(x = 1; x <= 20; x++)
sum = (byte)(sum + x); // cast needed here
Console.WriteLine("Summation of 20 is " + sum);
}
}


 
12
Photo of Vulpes
NA 98.3k 1.5m 11y
If you're wondering how a byte (x) can be added to an int (sum) when they're different types, the answer is that the byte is automatically converted to an int before the addition takes place.

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.



2
Photo of Biswa Pujarini Mohapatra
NA 8.4k 922.9k 11y
here is the description of your logic

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
1
Photo of Mohamed Moubasher
NA 5 1.3k 11y
Thanks a lot all for your answers but I need to know why is the result is 5050 ??
 
Why for example it is not printed 1,3,5.. etc !  why 5050 and where it came from..

Thanks again.
1
Photo of Salman  Mushtaq
NA 57 22.6k 11y
Dear Vulpes , byte is automatically add in int , can int is automatically add into byte or we need parsing , if yes then please give syntax
1
Photo of Sachin Kalia
NA 22.9k 4.8m 11y
Hi Bro,
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

0
Photo of Mohamed Moubasher
NA 5 1.3k 11y
 thanks a lot for your help all of you I appreciate and I guess I must learn some math too :-)