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. - #include <stdio.h>
-
#include<ctype.h>
-
#include<stdlib.h>
-
main()
-
{
-
char n[5];
-
int sum,csum;
-
printf("Enter 5 digit number\n");
-
gets(n);
-
int sumd(char *);
-
int sumr(char *);
-
sum=sumd(&n[0]);
-
printf("\nThe sum of the digits without recursion is %d\n",sum);
-
csum=sumr(&n[0]);
-
printf("\nThe sum of digits with recursion is %d\n",csum);
-
return 0;
-
}
-
int sumd(char *p)
-
{
-
int j,sum=0;
-
for(j=0;j<5;j++)
-
{
-
sum=sum+(isdigit(*p));
-
p++;
-
}
-
return sum;
-
}
-
int sumr(char *q)
-
{
-
int gsum;
-
gsum=(isdigit(*q))+sumr(q++);
-
return gsum;
-
}
7 6893
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: - int sumr(int sum, etc...)
-
{
-
Is this the 6th call?
-
If so return sum.
-
Otherwise, keep going
-
-
}
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?
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.
@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......
@donbock
I was putting 5 digit number as a string.Isn't gets() a standard method to get the string in the program......
True, gets is in the Standard C Library, but it is deprecated; for example, look here.
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.
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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...
|
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...
|
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
|
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';...
|
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...
|
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...
|
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: " ...
|
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...
|
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...
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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...
|
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,...
|
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...
|
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...
|
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...
|
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,...
| |