简体   繁体   中英

digits sum of a number (c)

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.

***Generalized program for any n digit integer!***
#include<stdio.h>
int main()
{
  int num,digit=0,i,temp,sum=0,rem;
  printf("Enter the number:\n");
  scanf("%d",&num);
  temp=num;
  while(num!=0)
  {
    num=num/10;
    digit++;
    rem=temp%10;
    temp=temp/10;
    sum=sum+rem;
  }
  printf("It is a %d digit number.\n",digit);
  sum=abs(sum);
  printf("The sum of the digits is %d.\n",sum);
return 0;  
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM