473,407 Members | 2,676 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,407 software developers and data experts.

Addition of digits in five digit number as an character array.

Been brushing up my C,C++ for my new venture in Graduate Studies for this fall.I am doing basic programs in which I am trying this one right now.

A 5-digit positive integer is entered through the keyboard, write a function to calculate sum of digits of the 5-digit number:
(1) Without using recursion
(2) Using recursion



I am entering five digit number as a character array and I am trying to add them.
(I found it clumsy to construct conventional method using %,/ operators to implement in functions.)

I am trying hands on various functions of string handling to add the digits but no success.Another thing my recursive function 'sumr' is not giving any output on terminal and 'sumd' is giving output as 20 however may be the digit entered.

I am using Netbeans.Here is the code snippet that I have created.



Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2. #include<ctype.h>
  3. #include<stdlib.h>
  4. main()
  5. {
  6.     char n[5];
  7.     int sum,csum;
  8.     printf("Enter 5 digit number\n");
  9.     gets(n);
  10.     int sumd(char *);
  11.     int sumr(char *);
  12.     sum=sumd(&n[0]);
  13.     printf("\nThe sum of the digits without recursion is %d\n",sum);
  14.     csum=sumr(&n[0]);
  15.     printf("\nThe sum of digits with recursion is %d\n",csum);
  16.     return 0;
  17. }
  18. int sumd(char *p)
  19. {
  20.     int j,sum=0;
  21.     for(j=0;j<5;j++)
  22.     {
  23.         sum=sum+(isdigit(*p));
  24.         p++;
  25.     }
  26.     return sum;
  27. }
  28. int sumr(char *q)
  29. {
  30.     int gsum;
  31.     gsum=(isdigit(*q))+sumr(q++);
  32.     return gsum;
  33. }
May 12 '10 #1
7 6893
weaknessforcats
9,208 Expert Mod 8TB
Two things.

One this is C code. There's no C++ here. Use cout rather than printf.

Two, for a recursive function to work it hass to stop calling itself at some point and return. sumr() has no such descision. You call call yourself forever and blow the stack. Something liker:

Expand|Select|Wrap|Line Numbers
  1. int sumr(int sum, etc...)
  2. {
  3.      Is this the 6th call?
  4.      If so return sum.
  5.      Otherwise, keep going
  6.  
  7. }
May 12 '10 #2
whodgson
542 512MB
1. I think printf() is perfectly acceptable in C++ so you can continue to use it.
2. I wonder why you don`t use % and / and sum+= to get the sum of the digits?
May 13 '10 #3
donbock
2,426 Expert 2GB
Your character array is too small to hold the entered 5-digit number. At the very least, you need to allow space for the terminating null. Furthermore, you should add some additional extra space to allow for somebody entering a few too many characters. Fundamentally, gets is a poor choice for data entry because there is no way to protect your program from overflow.
May 13 '10 #4
@whodgson


Thanks...I guess I ll go away with workable method of / and %.But still I wasnt able to work with pointers and char array......
May 13 '10 #5
@donbock
I was putting 5 digit number as a string.Isn't gets() a standard method to get the string in the program......
May 13 '10 #6
donbock
2,426 Expert 2GB
True, gets is in the Standard C Library, but it is deprecated; for example, look here.
May 13 '10 #7
weaknessforcats
9,208 Expert Mod 8TB
I think printf() is perfectly acceptable in C++ so you can continue to use it.
It only works for built-in types and only works for pre-defined display formats and only works for stdout. When these are in C++ code they have to be ripped out at the first requirement change. Like you suddenly want you use a file instead so you switch to fprintf(). Instead, there should have been a function written with a stream reference as an argument to avoid this. This function could acess a paraneters table to obtain variable display formats from one execution of the program to the next. Or may the whole thing is handled by a class hierarchy.

I say again, the printf() family of functions is a very poor choice for a C++ program.
May 13 '10 #8

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

Similar topics

13
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...
11
by: dipti | last post by:
Hi, Can u guys please tell me what's wrong with the following code as on giving a simple input value like: 10001, 10032, 10432, everything seems to be working fine but on giving a input value...
6
by: Kannan | last post by:
Hi, I have question about character array initialization. In section 6.7.8 paragraph number 21, it's given that "If there are fewer initializers in a brace-enclosed list than there are...
33
by: Prasad | last post by:
Hi, Can anyone please tell me how to store a 13 digit number in C language ? Also what is the format specifier to be used to access this variable ? Thanks in advance, Prasad P
3
by: Jim Langston | last post by:
Is it possible to initialize a std::string with a character array, not neccessarily null terminated? I.E. Given something like this: char buffer; buffer = 0x01; buffer = 0x00; buffer = 'A';...
2
by: dhutton | last post by:
How would one go about grabbing just the last 5 digits of a 16 digit number - then prefixing the last 5 numbers with 99800 so if the number is 8351101100000029 I need my SQL (Microsoft) to grab...
4
by: =?Utf-8?B?Vmlua2k=?= | last post by:
Hello Everyone, I am trying to convert the digits to even number so for example if I have 3 digit number, I want it to be 4 digit and If I have 5 digit number, I want it to be 6 digit. How can i...
12
by: bejiz | last post by:
Hi, Do you know a way to tell to the compiler that if there are numbers of 1 digit ( 2, 6, 8,...), I want spaces like this: " ". But if I have numbers of 3 digits, I want spaces like this: " ...
7
by: harijay | last post by:
Hi I am a few months new into python. I have used regexps before in perl and java but am a little confused with this problem. I want to parse a number of strings and extract only those that...
11
by: Jordan218 | last post by:
Hi. How do I write a program that sums the digits of a 4-digit number? I have a program that works, but you have to input the digits separately. Can anyone help or possibly explain what I'm doing...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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
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,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
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
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.