473,503 Members | 5,004 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to do sum of the digits?

3 New Member
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 51042
DaLeach
2 New Member
Here's a hint:

"modulo"
in c++ the % operator
...google that.
Jul 12 '06 #2
evolutionccf
3 New Member
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
Banfa
9,065 Recognized Expert Moderator Expert
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
Ashish_CPP
35 New Member
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
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. void main(void)
  5. {
  6.     unsigned int Val = 65234, i=0, Res=0;
  7.  
  8.     for(i=1;i<1000000;i=(i*10))
  9.         Res += (Val%i*10)/i;
  10.     printf("%d\n\n",Res);
  11. }
Jul 12 '06 #5
gowgun
5 New Member
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
  1. #include<iostream.h>
  2. #include<conio.h>
  3. void main()
  4. {
  5.   clrscr();
  6.   int x,a,r,sum=0,sum2=0;
  7.   cout<<"Enter any natural number: ";
  8.   cin>>x;
  9.   a=x;
  10.   while(x!=0)
  11.   {
  12.     r=x/10;
  13.     if(r==0) sum2=sum2+x;
  14.     else sum=sum+r;
  15.     x=x/10;
  16.  
  17.   }
  18.   cout<<"the sum of digits of "<<a<<"= "<<sum+sum2;
  19.   getch();
  20. }
Jul 30 '06 #6
Banfa
9,065 Recognized Expert Moderator Expert
Replace
Expand|Select|Wrap|Line Numbers
  1.  r=x/10;
  2. if(r==0) sum2=sum2+x;
  3. else sum=sum+r;
  4.  
which I can not see what you expected to do, with
Expand|Select|Wrap|Line Numbers
  1.  r=x%10;
  2.  sum=sum+r;
  3.  
Remove variable sum2
Jul 30 '06 #7
pkp_budha
8 New Member
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
  1. int sun_dig(int num)
  2. {
  3.    int ctr,rem,sum=0;
  4.      while(num!=0)
  5.          {
  6.             rem=num%10;
  7.             sum=sum+rem;
  8.             num=num/10;
  9.          }
  10.       return sum;
  11. }
Jul 31 '06 #8
Ashish_CPP
35 New Member
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
  1. unsigned int Val = 501;
  2.  
  3. unsigned char array[15];
  4.  
  5. itoa(Val, array, 10);
  6.  
  7. Val = 0;
  8.  
  9. for(int i=0; i< (strlen(array)); i++)
  10. {
  11.      Val += (array[i]-48);
  12. }
  13.  
  14. printf("%d\n\n", Val);
Jul 31 '06 #9
aloksinha_iisc
1 New Member
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
  1. total_sum=0;
  2. for (input=0;input<=1000;input++)  {
  3.         sum=0;  //Every time get reset.
  4.  
  5.         for (i=0;i<4;i++)  {
  6.                    sum=sum+ (input >> (i*4))%(10) ;
  7.                   }
  8.          total_sum=total_sum+sum;
  9.     }

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
Banfa
9,065 Recognized Expert Moderator Expert
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

Sign in to post your reply or Sign up for a free account.

Similar topics

14
7158
by: Westcoast Sheri | last post by:
What is the most efficient way of extracting the first two digits in a string? The following is wrong for me, because it only gives me the first instance of two digits together: $string =...
1
3859
by: Shreyas Kulkarni | last post by:
hi there, recently i have got a problem regarding calculation of sum of digits in a floating point or precision number. the weird behaviour of compiler/language is preventing me from calculating...
13
15937
by: Kosio | last post by:
Hello, I know of a way to extract digits from a number using the %10 and divide by 10. But I am wondering if there is an algorithm out there that does not use a divide by 10 feature. The...
27
31341
by: Luke Wu | last post by:
Is there a C function that returns the number of digits in an input int/long? example: numdigits(123) returns 3 numdigits(1232132) returns 7
18
12149
by: Kuljit | last post by:
I am doing Engineering(B.Tech) in Computer Science. I have a question for which i am struggling to write a C code(program). It struck me when we were being taught about a program which counts the...
4
12705
by: Anoop | last post by:
Hi All I am getting two different outputs when i do an operation using string.digits and test.isdigit(). Is there any difference between the two. I have given the sample program and the output ...
109
7512
by: jmcgill | last post by:
Hello. Is there a method for computing the number of digits, in a given numeric base, of N factorial, without actually computing the factorial? For example, 8! has 5 digits in base 10; 10! has...
2
6191
by: Ulrich Dorda | last post by:
I need a pytho nscript to read numbers(with loads of digits) from a file, do some basic math on it and write the result out to another file. My problem: I don't get python to use more digits: ...
6
9514
by: ocean | last post by:
Write a program that prompts the user to input an integer and then outputs both the individual digits of the number and the sum of the digits. For example, it should output the individual digits...
0
7063
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7258
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
6970
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7441
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
4987
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4663
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3156
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3146
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1489
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.