Question

*Has to be coded in c using Dev c++. Must use printf and scanf statements* 1....

*Has to be coded in c using Dev c++. Must use printf and scanf statements*

1. A 5 digit positive integer is entered through the keyboard. Write a recursive function to
calculate the sum of the digits of the number entered. (50 points - 10pts for commenting
and 40pts for successful execution)

Homework Answers

Answer #1

#include <stdio.h>

/* Function declaration */
int digitsSum(int value);

/* Main Body */
int main()
{
int number, sum;
  
printf("Enter any number to find its sum of digits: ");
scanf("%d", &number);
  
sum = digitsSum(number); /*Function Calling*/
  
printf("Sum of digits of %d = %d", number, sum); /*Displaying output*/
  
return 0;
}


/**
* Recursive function to find sum of digits of a number */
int digitsSum(int num)
{
// Condition to check finite number value
if(num == 0)
return 0;
  
   return ((num % 10) + digitsSum(num / 10)); /*returns integer value to the function return parameter sum*/
}

Output:

Know the answer?
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
Similar Questions
In this example you are allowed to use from the C standard library only functions for...
In this example you are allowed to use from the C standard library only functions for input and output (e.g. printf(), scanf()) Complete the following functions using C programming language: a) Complete the int Q7a(intQ7_input) function takes a seven-digit positive integer as input and returns it reversed. For example, if the integer is 9806593, the program should print 3956089. You are not permitted to use any function of C standard library other than scanf()and printf().You are not permitted to use...
In this example you are allowed to use from the C standard library only functions for...
In this example you are allowed to use from the C standard library only functions for input and output (e.g. printf(), scanf()) For this exercise you should be able to write a logical expression (i.e., with logical operators) which checks if some integer x consists of exactly 5 digits. Ex: 30498 and -14004 are 5-digit numbers, while 1018, -2 and 46 are not. Complete the intQ2(intQ2_input) function that takes an input integer parameter and returns 1 if the number is...
Write a C program that creates a security access code from a social security number entered...
Write a C program that creates a security access code from a social security number entered by a user as follows: Using a recursive function it adds all of the digits of the social security number and then using a regular function gets the square number of the sum and displays it. The access code displayed should be initialized as an integer number but using type casting is displayed as a floating number.
In this example you are allowed to use from the C standard library only functions for...
In this example you are allowed to use from the C standard library only functions for input and output (e.g. printf(), scanf()) Complete the following functions using C programming language: A positive integer number is said to be a perfect number if its positive factors, including 1 (but not the number itself), sum to the number. For example, 6 is a perfect number because 6=1+2+3. Complete the int Q6(intQ6_input, int perfect[])function that determines all perfect numbers smaller than or equal...
Using the following code perform ALL of the tasks below in C++: ------------------------------------------------------------------------------------------------------------------------------------------- Implementation: Overload input...
Using the following code perform ALL of the tasks below in C++: ------------------------------------------------------------------------------------------------------------------------------------------- Implementation: Overload input operator>> a bigint in the following manner: Read in any number of digits [0-9] until a semi colon ";" is encountered. The number may span over multiple lines. You can assume the input is valid. Overload the operator+ so that it adds two bigint together. Overload the subscript operator[]. It should return the i-th digit, where i is the 10^i position. So the first...
You will write a program that loops until the user selects 0 to exit. In the...
You will write a program that loops until the user selects 0 to exit. In the loop the user interactively selects a menu choice to compress or decompress a file. There are three menu options: Option 0: allows the user to exit the program. Option 1: allows the user to compress the specified input file and store the result in an output file. Option 2: allows the user to decompress the specified input file and store the result in an...
Need in C language also need full documentation/explanation of each line A student has established the...
Need in C language also need full documentation/explanation of each line A student has established the following monthly budget: Budget Categories Budgeted amount ----------------------------------------------------- Housing $ 580.00 Utilities $ 150.00 Household Expenses     $ 65.00 Transportation $ 50.00 Food $ 250.00 Medical $ 30.00 Insurance $ 100.00 Entertainment $ 150.00 Clothing $ 75.00 Miscellaneous $ 50.00 ---------------------------------------------------- Write a program that declares a MonthlyBudget structure designed with member variables to hold each of these expense categories. The program should define...
I've posted this question like 3 times now and I can't seem to find someone that...
I've posted this question like 3 times now and I can't seem to find someone that is able to answer it. Please can someone help me code this? Thank you!! Programming Project #4 – Programmer Jones and the Temple of Gloom Part 1 The stack data structure plays a pivotal role in the design of computer games. Any algorithm that requires the user to retrace their steps is a perfect candidate for using a stack. In this simple game you...