Question

Write a C program program to calculate the sum of digits of a number: Use modulus...

Write a C program program to calculate the sum of digits of a number: Use modulus
operator (%) to extract individual digits of a number and keep on adding them.

0 0
Add a comment Improve this question Transcribed image text
Answer #1
#include <stdio.h>

int main() {
    int n;
    printf("Enter a positive integer: ");
    scanf("%d", &n);
    int sum = 0;
    while(n > 0) {
        sum += n%10;
        n /= 10;
    }
    printf("Sum of all digits = %d\n", sum);
    return 0;
}
Add a comment
Know the answer?
Add Answer to:
Write a C program program to calculate the sum of digits of a number: Use modulus...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • 1. (Sum the digits in an integer) Write a method that computes the sum of the...

    1. (Sum the digits in an integer) Write a method that computes the sum of the digits in an integer. Use the following method header: public static int sumDigits(long n) For example, sumDigits (234) returns 9 (2 + 3 + 4). (Hint: Use the % operator to extract digits, and the / operator to remove the extracted digit. For instance, to extract 4 from 234, use 234 % 10(= 4). To remove 4 from 234, use 234 / 10(= 23)....

  • (Java Please) Sum of Digits Write a program that will sum the digits of a number...

    (Java Please) Sum of Digits Write a program that will sum the digits of a number input by the user. For example, if the user enters the number 1234, the sum of the digits will be 10 (1 + 2 + 3 + 4 = 10). The user will enter a number with at least 4 digits (greater than 1000). That value will be sent to a method which will return the sum. Sample Output 1: SUM OF DIGITS -------------...

  • Java Please Source Only Program 2: All Hail Modulus Agustus! The modulus operator is used all...

    Java Please Source Only Program 2: All Hail Modulus Agustus! The modulus operator is used all the time. Realize that if you “mod” any number by a number “n”, you’ll get back a number between 0 and n-1. For example, “modding” any number by 20 will always give you a number between 0-19. Your job is to design (pseudocode) and implement (source code) a program to sum the total of all digits in an input integer number between 0 and...

  • Write a C program convert.c that converts each number in an array by the sum of...

    Write a C program convert.c that converts each number in an array by the sum of that number plus 6 modulus 10. A sample input/output: Enter the length of the array: 5 Enter the elements of the array: 3 928 4 14 77 Output: 9 4 0 0 3 The program should include the following function: void convert(int *a1, int n, int *a2) The function converts every element in array a1 of length n to an output array a2. The...

  • Q#1: Write a C++ program to enter a number, compute and print the following: - Sum...

    Q#1: Write a C++ program to enter a number, compute and print the following: - Sum of even digits in the number - Count of even digits in the number - Count of digits below 5 For example, if the input is 3429, then Sum of even digits in the number = 6 Count of even digits in the number = 2 Count of digits below 5 = 3

  • Write a C program that determines the sum of the decimal digits that appear in the...

    Write a C program that determines the sum of the decimal digits that appear in the command line arguments (excluding argv[0]) of the main function. The sum should be zero if there are no command line arguments following argv[0], or if the command line arguments following argv[0] do not include any decimal digits. Example If the command line arguments (following argv[0]) were 7M3 3DoorsDown Sum41 U2 The4Seasons Maroon5 TheB52s then the sum would be 7 + 3 + 3 +...

  • Write C++ program to prompt user to enter a 3-digits number, say 371, to a variable...

    Write C++ program to prompt user to enter a 3-digits number, say 371, to a variable called num. Then store each digit of this inputted number to three different variables called digit1, digit2 and digit3. Hence, when number entered is 371, digit1 is 3, digit2 is 7 and digit3 is 1. Display the value of inputted number followed by digit1, digit2 and digit3. Hint: You have to use / and % operator in this question.

  • To use the digits function, enter 1 To use the average function, enter 2 To use the perfect sum f...

    use matlab To use the digits function, enter 1 To use the average function, enter 2 To use the perfect sum function, enter3 To exit the program, enter 4 Please select a number = 6 Please re-select again: 2 please enter the first number 3 please enter the second number: 6 please enter the third number: 3 The average equals to: 4 Write a function, called digits function that is able to calculate the number of digits and the summation...

  • Program#3(17 points): write a java program (SunDigits) as follows The main method prompts the user to enter an integer number. The method then calls Method Sum (defined blew) to add the digits and re...

    Program#3(17 points): write a java program (SunDigits) as follows The main method prompts the user to enter an integer number. The method then calls Method Sum (defined blew) to add the digits and return their total. The main method then prints the digits total with proper label as shown below Method Sum )is of type integer and takes an integer value. The method recursively adds up the digits and returns their total. Document your code and use proper prompts for...

  • Write a program in C that calculates how many digits a number contains: Enter a number:...

    Write a program in C that calculates how many digits a number contains: Enter a number: 374 The number 374 has 3 digits You may assume that the number has no more than four digits. Hint: Use if statements to test the number. For example, if the number is between 0 and 9, it has one digit. If the number is between 10 and 99, it has two digits.

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT