Question

Using a while loop and if-else statements if required, write a function file that will add (sum) the DIGITS of any number entered. Your function should get the number (scalar) as an input 2. argument and return the sum as an output argument. You may use any of the following built-in functions to assist you: ceil, floor, round, fix, rem, or mod. HINT: What number do you need to divide by to get a single digit out of a long number? How do you keep track of what is left after the division? Here is an example of what the input and output should look like: Input number: 123456789 Output: 45 Test your code using: 98765 and 100001

0 0
Add a comment Improve this question Transcribed image text
Answer #1

#include <stdio.h>

int getSum(long long int num)

{

int sum = 0;

while(num)

{

sum += num%10;

num /= 10;

}

return sum;

}

int main()

{

long long int num;

printf("Enter a positive number : ");

scanf("%lld",&num);

printf("The sum of digits is : %d\n",getSum(num));

}

Enter a positive number 98765 The sum of digits is : 35

Enter a positive number 100001 The sum of digits is 2

Add a comment
Know the answer?
Add Answer to:
Using a while loop and if-else statements if required, write a function file that will add...
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
  • Using MatLab Write a function called PrimeFinder that receives an integer n as an input argument,...

    Using MatLab Write a function called PrimeFinder that receives an integer n as an input argument, and provides an output argument called Primes that is a vector containing all prime numbers between 2 and n, inclusive. Verify the correctness of your code with inserting 13 as the input argument, and check that your output is Primes = [2,3,5,7,11,13]. You are NOT allowed to use any directly relevant MATLAB built-in function such as divisors, etc. NOTE: A prime number is a...

  • Function Name: sepDigits Inputs: 1. (double) A three-digit integer Outputs: 1. (double) The hundreds-place digit of...

    Function Name: sepDigits Inputs: 1. (double) A three-digit integer Outputs: 1. (double) The hundreds-place digit of the input 2. (double) The tens-place digit of the input 3. (double) The ones-place digit of the input Function Description: This function will take in a three-digit integer (integer between 100 and 999 , inclusive) and should return each digit as a separate number. For example, if the input is 472 , the first output would be 4 , the second would be 7...

  • 3. Write a function called sort3 that takes a 3-element vector as its sole arguments. It uses if-statements, possibly nested, to return the three elements of the vector as three scalar output-argumen...

    3. Write a function called sort3 that takes a 3-element vector as its sole arguments. It uses if-statements, possibly nested, to return the three elements of the vector as three scalar output-arguments in nondecreasino-roer , i.e., the first output argument equals the smallest element of the input vector and the last output argument equals the largest element. NOTE: Your function may NOT use any built-in functions, e.g., sort, min, max, median, etc. (5 points) (bonus question). Write a function called...

  • c++ #include <iostream> #include <string> using namespace std; /* Write a function checkPrime such that input:...

    c++ #include <iostream> #include <string> using namespace std; /* Write a function checkPrime such that input: an int output: boolean function: Return true if the number is a prime number and return false otherwise */ //TO DO ************************************** /* Write a function checkPrime such that input: an array of int and size of array output: nothing function: Display the prime numbers of the array */ //TO DO ************************************** /* Write a function SumofPrimes such that input: an int output: nothing...

  • In C++ Programming Write a program in a file named SeedFinder.cpp that finds and displays all...

    In C++ Programming Write a program in a file named SeedFinder.cpp that finds and displays all the seeds of a integer given by the user. Let’s define a seed of an integer, n, to be a positive value that equals n when multiplied by its individual digits. For example, 23 is a seed of 138 because 23 × 2 × 3 = 138. 111 is an example of a seed of itself because 111 × 1 × 1 × 1...

  • C linux please write it by only using “if and else” conditions, thanks so much this...

    C linux please write it by only using “if and else” conditions, thanks so much this is my task 3 Requirements 1. Copy your solution for Task 3 t3.c to a new file t4.c. 2. The input is guaranteed to contain at least one non-whitespace character. 3. If the input is well-formed, i.e., can be parsed to a number, the program should behave identically to Task 3. All the requirements of Task 3 still apply except the file name. 4....

  • Using basic c++ write a separate code for each question. 1. Palindrome if else • Try...

    Using basic c++ write a separate code for each question. 1. Palindrome if else • Try to find if a 5 digit input number is a palindrome • Get one 5 digit number from user • Output: Enter a five-digit integer: 12345 12345 is not a palindrome Enter a five-digit integer: 34543 34543 is a palindrome 2. WiFi Diagonostics using multiple ifs PRINT: This program will help you diagnose a bad WiFi connection. 1 st level Check the below (Use...

  • (java) Write a While loop that prompts the user for only even numbers. Keep prompting the...

    (java) Write a While loop that prompts the user for only even numbers. Keep prompting the user to enter even numbers until the user enters an odd number. After the loop ends, print the sum of the numbers. Do not include that last odd number. You should not store the numbers in an array, just keep track of the sum. Make sure to use a break statement in your code. You may assume that a java.util.Scanner object named input has...

  • Open the Validate Number Solution.sln file contained in the VB2017\Chap07\Validate Number Solution folder. The interface provides...

    Open the Validate Number Solution.sln file contained in the VB2017\Chap07\Validate Number Solution folder. The interface provides a text box for entering a 9-digit number. The btnValidate_Click procedure should use the algorithm and example shown in Figure 7-56 to validate the user’s entry. The procedure should display a message indicating whether the entry is or is not valid. Code the procedure. Include any other code that will professionalize the interface. Save the solution and then start and test the application. Create...

  • C++ Problem 1 Write a function to calculate the greatest common divisor (GCD) of two integers...

    C++ Problem 1 Write a function to calculate the greatest common divisor (GCD) of two integers using Euclid’s algorithm (also known as the Euclidean algorithm). Write a main () function that requests two integers from the user, calls your function to compute the GCD, and outputs the return value of the function (all user input and output should be done in main ()). In particular, you will find this pseudocode for calculating the GCD, which should be useful to you:...

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
ADVERTISEMENT