To count World Population in 2015

/*
Description: This program is to calculate world population
Author: Liutong Xu
Date: 2016/10/24
*/

#include <stdio.h>
 
int main()
{
    int a[]={1369202232,1166079217,307212123,240271522,198739269,176242949,156050883,149229090,140041247,127078679,111211789,97976603,86967524,85237338,83082869,82329758,76805524,68692542,66429284,65905410,64420073,61113205,58126212,49052489,48508972,48137741,45700395,45644023,41087825,41048532,40913584,40525002,39002772,38482919,34859364,34178188,33609937,33487208,32369558,29546963,28945657,28686633,28563377,27606007,26814843,25715819,23832495,23822783,22974347,22665345,22215421,21669278,21324791,21262641,20653556,20617068,20178485,18879301,16715999,16601707,15746232,15399437,15306252,14573101,14494293,14268711,13711597,13276517,12799293,12666987,11862740,11451652,11392629,10737428,10707924,10486339,10473282,10414336,10329208,10211904,10057975,9905596,9832017,9775246,9650054,9648533,9059651,9035536,8988091,8791832,8238672,8210281,7792854,7604467,7379339,7349145,7233701,7204687,7185218,7055071,6995655,6834942,6440053,6342948,6310434,6057263,6019877,5891199,5647168,5500510,5463046,5431747,5250275,4884887,4798491,4660539,4657542,4615807,4613414,4511488,4489409,4320748,4253877,4213418,4203200,4017095,4012809,3971020,3639453,3555179,3494382,3441790,3418085,3360474,3129486,3041142,2967004,2825928,2691158,2461267,2231503,2130819,2108665,2066718,2005692,1990876,1804838,1782893,1551859,1533964,1514993,1299371,1284264,1229953,1131612,1123913,944720,833285,796740,772298,752438,727785,709470,691141,672180,633441,595613,559846,516055,491775,481267,431170,429474,405210,405165,396334,388190,385000,309156,307899,306694,287032,284589,227436,227049,223765,219998,218519,212679,209000,178430,160267,120898,112850,109825,107434,104574,103065,91626,90739,88662,87476,85632,83888,76512,72660,67837,65870,65628,64522,57600,49035,48856,40131,34761,32965,30324,29820,28034,24491,22942,20796,15289,14436,14019,12373,11870,7637,7448,7051,5097};
    int sum;
    int i;
 
    sum = 0;
    for (i = 0;i < 227;i++)
        sum = sum + a[i];
    printf("The World Population is %d.",sum);
 
    return 0;
}

stdout


 
The World Population is -1767575705.

But the real population is: 6822358887!

int in C has 4 bytes of memory, it can't represent such a big number!

Max integer with 4 bytes is 4294967295. Therefore, any bigger number will overflow, that is 4294967295+1=0! And in fact, 6822358887 - 2 * 4294967295 = -1767575705. Or we can wirte

$$6822358887 \equiv -1767575705 \pmod{4294967295}$$

In order to count such a big number, we'd use long long int, which occupies 8 bytes. 

/*
Description: This program is to calculate world population
Author: Liutong Xu
Date: 2016/10/24
*/

#include <stdio.h>
 
int main()
{
    long long int a[]={1369202232,1166079217,307212123,240271522,198739269,176242949,156050883,149229090,140041247,127078679,111211789,97976603,86967524,85237338,83082869,82329758,76805524,68692542,66429284,65905410,64420073,61113205,58126212,49052489,48508972,48137741,45700395,45644023,41087825,41048532,40913584,40525002,39002772,38482919,34859364,34178188,33609937,33487208,32369558,29546963,28945657,28686633,28563377,27606007,26814843,25715819,23832495,23822783,22974347,22665345,22215421,21669278,21324791,21262641,20653556,20617068,20178485,18879301,16715999,16601707,15746232,15399437,15306252,14573101,14494293,14268711,13711597,13276517,12799293,12666987,11862740,11451652,11392629,10737428,10707924,10486339,10473282,10414336,10329208,10211904,10057975,9905596,9832017,9775246,9650054,9648533,9059651,9035536,8988091,8791832,8238672,8210281,7792854,7604467,7379339,7349145,7233701,7204687,7185218,7055071,6995655,6834942,6440053,6342948,6310434,6057263,6019877,5891199,5647168,5500510,5463046,5431747,5250275,4884887,4798491,4660539,4657542,4615807,4613414,4511488,4489409,4320748,4253877,4213418,4203200,4017095,4012809,3971020,3639453,3555179,3494382,3441790,3418085,3360474,3129486,3041142,2967004,2825928,2691158,2461267,2231503,2130819,2108665,2066718,2005692,1990876,1804838,1782893,1551859,1533964,1514993,1299371,1284264,1229953,1131612,1123913,944720,833285,796740,772298,752438,727785,709470,691141,672180,633441,595613,559846,516055,491775,481267,431170,429474,405210,405165,396334,388190,385000,309156,307899,306694,287032,284589,227436,227049,223765,219998,218519,212679,209000,178430,160267,120898,112850,109825,107434,104574,103065,91626,90739,88662,87476,85632,83888,76512,72660,67837,65870,65628,64522,57600,49035,48856,40131,34761,32965,30324,29820,28034,24491,22942,20796,15289,14436,14019,12373,11870,7637,7448,7051,5097};
    long long int sum;
    int i;
 
    sum = 0;
    for (i = 0;i < 227;i++)
        sum = sum + a[i];
    printf("The World Population is %lld.",sum);
 
    return 0;
}

stdout


 
The World Population is 6822358887.

You have no rights to post comments