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
Python Write a program that asks the user for two integers that will correspond to the numbering of the first and last lines, then a third integer that will correspond to the number of z before "zigzag" (not counting the first "z" of zigzag), then finally that displays a zig-zag following the following pattern: Exemple 1:   Start Number: * user type 3 * End number:   * user type 10 * Number of z:   * user type 5 * zzzzzzigzag 3 4 zzzzzzigzag zzzzzzigzag 5 6 zzzzzzigzag zzzzzzigzag 7 8 zzzzzzigzag zzzzzzigzag 9 10 zzzzzzigzag (Even integers must be left-zigzag and odd integers must be right.)   Example 2:   Start number:* user type -2 * End Number:* user type 4 * Number of z: * user type 3 * -2 zzzzigzag zzzzigzag -1 0 zzzzigzag zzzzigzag 1 2 zzzzigzag zzzzigzag 3 4 zzzzigzag   Example 3:   Start Number:* user type 5 * End number:* user type 0 * Number of z: * user type 10 * (Nothing is displayed because 5>0)
Write a program that computes the molecular weight of a carbohydrate (ingrams per mole) based on the number of hydrogen, carbon, and oxygenatoms in the molecule. The program should prompt the user to enter thenumber of hydrogen atoms, the number of carbon atoms, and the numberof oxygen atoms. The program then prints the total combined molecularweight of all the atoms based on these individual atom weights:Atom Weight(grams I mole)H 1.00794c 12.01070 15.9994For example, the molecular weight of water (H20) is: 2(1.00794) +15.9994 = 18.01528.
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
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
Database System Concepts
Computer Science
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:McGraw-Hill Education
Text book image
Starting Out with Python (4th Edition)
Computer Science
ISBN:9780134444321
Author:Tony Gaddis
Publisher:PEARSON
Text book image
Digital Fundamentals (11th Edition)
Computer Science
ISBN:9780132737968
Author:Thomas L. Floyd
Publisher:PEARSON
Text book image
C How to Program (8th Edition)
Computer Science
ISBN:9780133976892
Author:Paul J. Deitel, Harvey Deitel
Publisher:PEARSON
Text book image
Database Systems: Design, Implementation, & Manag...
Computer Science
ISBN:9781337627900
Author:Carlos Coronel, Steven Morris
Publisher:Cengage Learning
Text book image
Programmable Logic Controllers
Computer Science
ISBN:9780073373843
Author:Frank D. Petruzella
Publisher:McGraw-Hill Education
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