Programming in C
Programming in C
4th Edition
ISBN: 9780321776419
Author: Stephen G. Kochan
Publisher: Addison-Wesley
bartleby

Concept explainers

bartleby

Videos

Textbook Question
Book Icon
Chapter 4, Problem 11E

Write a program that calculates the sum of the digits of an integer. For example, the sum of the digits of the number 2155 is 2 + 1 + 5 + 5 or 13. The program should accept any arbitrary integer typed in by the user.

Expert Solution & Answer
Check Mark

Explanation of Solution

Program:

The following program is used to sum the input digits using “do-while” condition:

//include the header file

#include <stdio.h>

//definition of main method

int main (void)

{

//declare the variables

int number, right_digit, s = 0;

//get the input from the user

printf("Enter your number.\n");

scanf("%i", &number);

//check the condition

do

{

//calculate the "right_digit"

right_digit = number % 10;

//calculate the "number"

number = number / 10;

//calculate the sum

s += right_digit;

}

while (number > 0);

//display newline

printf("The sum of the digit is: %i\n", s);

//return statement

return 0;

}

Explanation:

In the above program, declare the required header file. Inside the main method, declare the necessary variables. Get the number from the user and the “do-while” condition is used to add the input digits and finally display the result on the output screen.

Sample Output

Enter your number.

 2155

The sum of the digit is: 13

Want to see more full solutions like this?

Subscribe now to access step-by-step solutions to millions of textbook problems written by subject matter experts!
Students have asked these similar questions
Write a program that reads non-negative integer number and determines whether it is odd or even or prime?
Python Write a program that receives a number from a user and checks if it's float or integer.In case of being float, print for the user that his/her number is float with integer part andfractional part. In case of being integer, print to the user that his/her number is aninteger and odd/even numbeR
Write a program that allows a user to enter any number of student quiz scores, as integers, until the user enters 99. If the score entered is less than 0 or more than 10, display Score must be between 10 and 0 and do not use the score. After all the scores have been entered, display the number of valid scores entered, the highest score, the lowest score, and the arithmetic average
Knowledge Booster
Background pattern image
Computer Science
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
SEE MORE QUESTIONS
Recommended textbooks for you
  • Text book image
    Microsoft Visual C#
    Computer Science
    ISBN:9781337102100
    Author:Joyce, Farrell.
    Publisher:Cengage Learning,
Text book image
Microsoft Visual C#
Computer Science
ISBN:9781337102100
Author:Joyce, Farrell.
Publisher:Cengage Learning,
Constants, Variables, Data types, Keywords in C Programming Language Tutorial; Author: LearningLad;https://www.youtube.com/watch?v=d7tdL-ZEWdE;License: Standard YouTube License, CC-BY