445,797 Members | 1,794 Online Need help? Post your question and get tips & solutions from a community of 445,797 IT Pros & Developers. It's quick & easy.

# How to do sum of the digits?

 P: 3 How do you calculate the sum of the Integers is between 0 to 1000? Examples integer is 945: the sum of all digits is 18. This is my assignment but I don't know where to start and I need your help... I must use the C++ language do it... hope u all can help me... Thanks all Jul 12 '06 #1
10 Replies

 P: 2 Here's a hint: "modulo" in c++ the % operator ...google that. Jul 12 '06 #2

 P: 3 Here's a hint: "modulo" in c++ the % operator ...google that. sorry i duno wat u meaning.. if can do the sample teach me... i reali duno how 2 do the programming.. i jus learn from tis years... thanks for your post Jul 12 '06 #3

 Expert Mod 5K+ P: 8,916 Modulo arithmatic is arithmatic that wraps at a given value, for instance in modulo 4 0 + 1 = 1 1 + 1 = 2 2 + 1 = 3 3 + 1 = 0 18 modulo 4 is 2. This is the remainder when 18 is divided by 4 using only whole numbers 18 / 4 = 4 remainder 2 The C/C++ % operator encompases this as it provides the integer remainder when 1 integer is divided by another 18 % 4 = 2; DaLeach is (correctly in my opinion) suggesting that you can use the % operator (and interger divide /) to perform your calculation. In integer arithmatic 18 % 10 = 8; 18 / 10 = 1; We are not going to write a program for you as you will not learn anything but if you have an attempt at writing a program we will help you with any errors in it. Write something, make sure it compiles without warnings or ask if there are errors or warnings you don't understand. if it doesn't work post your code and we'll help. Jul 12 '06 #4

 P: 35 Hi, Here is the program you wanted but it will work only for unsigned int values that two bytes data bytes. Its an 3 lines program & you can easily understand. Expand|Select|Wrap|Line Numbers #include  #include    void main(void) {     unsigned int Val = 65234, i=0, Res=0;       for(i=1;i<1000000;i=(i*10))         Res += (Val%i*10)/i;     printf("%d\n\n",Res); } Jul 12 '06 #5

 P: 5 this is a program to a input a natural number and output the sum of its digits but doesn't work for numbers greater than 10.Could someone pls help me. Expand|Select|Wrap|Line Numbers #include #include void main() {   clrscr();   int x,a,r,sum=0,sum2=0;   cout<<"Enter any natural number: ";   cin>>x;   a=x;   while(x!=0)   {     r=x/10;     if(r==0) sum2=sum2+x;     else sum=sum+r;     x=x/10;     }   cout<<"the sum of digits of "<

 Expert Mod 5K+ P: 8,916 Replace Expand|Select|Wrap|Line Numbers  r=x/10; if(r==0) sum2=sum2+x; else sum=sum+r;   which I can not see what you expected to do, with Expand|Select|Wrap|Line Numbers  r=x%10;  sum=sum+r;   Remove variable sum2 Jul 30 '06 #7

 P: 8 Hi I think the code will be like this : If the Input is an Integer then this code will be works fine. Expand|Select|Wrap|Line Numbers int sun_dig(int num) {    int ctr,rem,sum=0;      while(num!=0)          {             rem=num%10;             sum=sum+rem;             num=num/10;          }       return sum; } Jul 31 '06 #8

 P: 35 the integer is between 0 to 1000. how 2 do sum the digits? examples integer is 945. the sum of all digits is 18. tis is my assigment but i duno how 2 do... i need u all help... my letchurer say must use the C++ language do it... hope u all can help me... tis friday i need pass up. thanks all Hi, Now that u have got so many answers, so there is no point in giving any hint. just try this code without using modulus. Expand|Select|Wrap|Line Numbers unsigned int Val = 501;   unsigned char array;   itoa(Val, array, 10);   Val = 0;   for(int i=0; i< (strlen(array)); i++) {      Val += (array[i]-48); }   printf("%d\n\n", Val); Jul 31 '06 #9

 P: 1 Hey i think you can use this following logic. As you want to add 0-to-1000 where each bit addintion also there. Expand|Select|Wrap|Line Numbers total_sum=0; for (input=0;input<=1000;input++)  {         sum=0;  //Every time get reset.           for (i=0;i<4;i++)  {                    sum=sum+ (input >> (i*4))%(10) ;                   }          total_sum=total_sum+sum;     } Note: total_sum= Sum of all 0-to-1000 with each bit addition. sum=sum of each number with bit addition [ex.945 will reperesent 18] >>( 4*i) = will do the bit shift and with modulo it will give the that integer value. Ex. for 945 the for loop will executed 3 times First i=0, No shift so remainder =5 Second i=1, Shift by 4 bit so the Number is now 0094,Remainder gives 4 Third i=2, (094) Shifted by 4 bit so the Number is now 0009,Remainder gives 9. Just run the code and see whether its working. :) :D :p Aug 1 '06 #10

 Expert Mod 5K+ P: 8,916 Note: total_sum= Sum of all 0-to-1000 with each bit addition. sum=sum of each number with bit addition [ex.945 will reperesent 18] >>( 4*i) = will do the bit shift and with modulo it will give the that integer value. Ex. for 945 the for loop will executed 3 times First i=0, No shift so remainder =5 Second i=1, Shift by 4 bit so the Number is now 0094,Remainder gives 4 Third i=2, (094) Shifted by 4 bit so the Number is now 0009,Remainder gives 9. Unfortunately I do not need to run the code to know it is not working, you have confused decimal and hexidecimal digits For any hexidecimal number H = 0xWXYZ then H >> 4 = 0x0WXY for example 0x6b37 >> 4 = 0x06b3 and 0x06b3 % 0x10 = 0x0003 but not 0x10 == 16 But the problem and your code are not dealing with hexidecimal digits it is and should be dealing with decimal digits and >> 4 does not shift by a complete digit Taking your example of 945 i = 0 945 % 10 = 5 i = 1 945 = 0x031B >> i*4 = 0x0031 = 59 % 10 = 9 i = 2 945 = 0x031B >> i*4 = 0x0003 = 3 % 10 = 3 Therefore sum = 5 + 9 + 3 = 17 but the sum of the digits of 945 = 18 Your code line sum=sum+ (input >> (i*4))%(10) ; needs to be sum=sum+ (input / (i*10))%(10) ; to work as you intended Aug 2 '06 #11 