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)
Python
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.

Additional Engineering Textbook Solutions

Find more solutions based on key concepts
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
EBK JAVA PROGRAMMING
Computer Science
ISBN:9781337671385
Author:FARRELL
Publisher:CENGAGE LEARNING - CONSIGNMENT
Text book image
C++ Programming: From Problem Analysis to Program...
Computer Science
ISBN:9781337102087
Author:D. S. Malik
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