Question 3: a) Write a C function count_digits() which takes an integer n as parameter and retums the number of digits in that integer. For example if n=12345 then your function should retum 5. Write appropriate main function to check your code. b) Write the above function count_digits() by using recursion.

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question

Using C language

Question 3:
a) Write a C function count_digits() which takes an integer n as parameter and returns the number
of digits in that integer. For example if n=12345 then your function should return 5. Write
appropriate main function to check your code.
b) Write the above function count_digits() by using recursion.
Transcribed Image Text:Question 3: a) Write a C function count_digits() which takes an integer n as parameter and returns the number of digits in that integer. For example if n=12345 then your function should return 5. Write appropriate main function to check your code. b) Write the above function count_digits() by using recursion.
Expert Solution
Step 1

Recursion

  • Recursion is the process of repeating items in a self-similar way. In programming languages, if a program allows you to call a function inside the same function, then it is called a recursive call of the function.
  • The C programming language supports recursion, i.e., a function to call itself. But while using recursion, programmers need to be careful to define an exit condition from the function, otherwise it will go into an infinite loop.
  • Recursive functions are very useful to solve many mathematical problems, such as calculating the factorial of a number, generating Fibonacci series, etc.
  • A function in C can be called either with arguments or without arguments.
  • These function may or may not return values to the calling functions.
  • All C functions can be called either with arguments or without arguments in a C program.

 

Step 2

Function with no argument and no return value : 

When a function has no arguments, it does not receive any data from the calling function.

Similarly when it does not return a value, the calling function does not receive any data from the called function.

Function declaration : void function();
Function call : function();
Function definition :
                      void function()
                      {
                        statements;
                      }
Function with arguments but no return value : When a function has arguments, it receive any data from the calling function but it returns no values.

Syntax :

Function declaration : void function ( int );
Function call : function( x );
Function definition:
             void function( int x )
             {
               statements;
             }
Function with no arguments but returns a value : There could be occasions where we may need to design functions that may not take any arguments but returns a value to the calling function. A example for this is getchar function it has no parameters but it returns an integer an integer type data that represents a character.

Function declaration : int function();
Function call : function();
Function definition :
                 int function()
                 {
                     statements;
                      return x;
                  }
steps

Step by step

Solved in 3 steps with 2 images

Blurred answer
Knowledge Booster
Introduction to computer system
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.
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education