title: Program to read a 4-digit integer and output the sum of the digits
body: <p>I'm stuck with an assignment in an introduction course to programming. We are using C. The assignment is:</p>
  
  <blockquote>
    <p>write a program which asks the user for a four digit INTEGER and outputs the sum of the four digits in the integer.</p>
  </blockquote>
  
  <p>I have tried different thing but when I test the program it doesn't work. When I compile it, I get no error so when I go ahead and test it, it won't give me the right answer. I've tried different things and it just gives me a different answer but it's still the wrong one. </p>
  
  <p>I was given this to test the program: enter 1234 when asked to enter four digit integer and answer has to be 10.</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  
  int
  main(void)
  {
      int sum,digit1,digit2,digit3,digit4;
      printf("enter four digit integer:\n");
      scanf("%d", &amp;digit1,&amp;digit2,&amp;digit3,&amp;digit4);
  
      digit1=(digit1%1000)/10;
      digit2=(digit2%1000)/10;
      digit3=(digit3%1000)/10;
      digit4=(digit4%1000)/10;
  
      /*calculation*/
      sum=digit1+digit2+digit3+digit4;
  
      printf ("answer is: %d\n",sum);
      return (0);
  }
  </code></pre>