I need to find the sum of the digits of a number with five digits. For example, the sum of the digits of the number 3709
is 3 + 7 + 0 + 9 = 19
.
#include <stdio.h>
int main()
{
int sum;
char digit_1, digit_2, digit_3, digit_4, digit_5;
printf("Plase enter a five digit number\n");
scanf("%c,%c,%c,%c,%c", &digit_1, &digit_2, &digit_3, &digit_4, &digit_5);
sum = digit_1 + digit_2 + digit_3 + digit_4 + digit_5;
printf("the sum of the digits is: %d", sum);
return 0;
}
output:
plase enter a five digit number
3709
the sum of the digits is 51
For some reason it doesn't show to correct answer and i can't seem to find whats wrong.
The problem with your code is
your scanf
needs ,
separated inputs
scanf("%c,%c,%c,%c,%c", ...)
Hence when you enter 3709 only digit1
will be read and rest will be omitted by scanf
. You can check the return value of scanf
to verify.
and ASCII value of 3
is 51
thus you are getting 51
as output.
Try this
scanf("%c%c%c%c", &digit_1 ,&digit_2 ,&digit_3 ,&digit_4 );
int sum = (digit_1 -'0')+(digit_2 -'0')+(digit_3 -'0')+(digit_4 -'0');
This works for many digits;
#include<stdio.h>
int main()
{
int n,digit,sum=0;
printf("Please gine a positive integer");
scanf("%d",&n);
while (n>0)
{
digit=n%10;
sum=sum+digit;
n=n/10;
}
printf("%d",sum);
}
[By intention not a complete answer, but way to long for a comment]
A general rule applies: In case you feel a function fails to do what it should: Take a look at its documentation to learn if it would return any completion/error state. Here: Consult the value the scanf()
function returns.
Verbatim from scanf()
s docs:
The scanf function returns the value of the macro EOF if an input failure occurs before the first conversion (if any) has completed. Otherwise, the scanf function returns the number of input items assigned, which can be fewer than provided for, or even zero, in the event of an early matching failure.
Also during development it makes sense to add some debug output, to see what is really going on.
So your code could look like this:
/* main.c */
#include <stdlib.h> /* for EXIT_* macros */
#include <stdio.h>
int main(void)
{
int sum;
char digit_1, digit_2, digit_3, digit_4, digit_5;
printf("Please enter a five digit number\n");
int result = scanf("%c,%c,%c,%c,%c", &digit_1, &digit_2, &digit_3, &digit_4, &digit_5);
if (EOF == result)
{
if (ferror(stdin))
{
perror("scanf() failed");
exit(EXIT_FAILURE);
}
result = 0;
}
if (5 > result)
{
fprintf(stderr, "To few input: Should be %d, but is %d\n", 5, result);
exit(EXIT_FAILURE);
}
#ifdef DEBUG
printf("printing as char: %c, %c, %c, %c, %c\n", digit_1, digit_2, digit_3, digit_4, digit_5);
printf("printing as int: %d, %d, %d, %d, %d\n", digit_1, digit_2, digit_3, digit_4, digit_5);
#endif
sum = digit_1 + digit_2 + digit_3 + digit_4 + digit_5;
printf("the sum of the digits is: %d", sum);
return EXIT_SUCCESS;
}
Compile this with (most) all warnings on:
gcc -Wall -Wextra -pedantic -DDEBUG main.c -g -o main
To fix the final error I leave to you. If done then just remove -DDEBUG
from the compile options, which would exclude the (hopefully) enlightening printf
s. ;)
The lesson learned may be: Checking for errors and logging is debugging for free.