In: Computer Science
C PROGRAM ONLY! You should make two functions.
Prototypes are included in loops.h. :
#ifndef FUNCTIONS_H
#define FUNCTIONS_H
void mult_table(int n);
int num_digits(int n);
#endif
mult_table should accept an integer. It should PRINT the
multiplication table for that number up to 10.
Example: if N was 3, your function should PRINT
3 * 1 = 3
3 * 2 = 6
3 * 3 = 9
...
3 * 10 = 30
num_digits should accept an integer. It should RETURN the number of
digits in that number.
Example: if N was 124, your function should RETURN 3, since there
are 3 digits in the number.
Here is the C code to the given question.
The two functions mult_table and num_digits are completed and tested in main().
Sample output is added at the end.
Code:
#include <stdio.h>
void mult_table(int n){
for(int i=1;i<=10;i++){ /*runs the loop from i=1 to i=10*/
printf("%d * %d = %d\n",n,i,n*i); /*prints the present value of table*/
}
}
int num_digits(int n){
int numOfDigits=0; /*initializes numOfDigits to 0*/
if(n<0){ /*check if the number is negative*/
n=0-n; /*change n to positive*/
}
while(n>0){ /*runs the loop as long as n is greater than 0*/
n=n/10; /*divides n by 10*/
numOfDigits+=1; /*increments numOfDigits by 1*/
}
if(numOfDigits==0){ /*special case where the number passed to function is 0*/
return 1; /*returns 1*/
}
else{
return numOfDigits; /*returns numOfDigits*/
}
}
int main()
{
printf("Testing mult_table:\n");
mult_table(3);
printf("\nTesting num_digits\n%d",num_digits(124));
return 0;
}
Output: