Question

In: Computer Science

C PROGRAM ONLY! You should make two functions. Prototypes are included in loops.h. : #ifndef FUNCTIONS_H...

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.

Solutions

Expert Solution

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:


Related Solutions

Complete the provided C++ program, by adding the following functions. Use prototypes and put your functions...
Complete the provided C++ program, by adding the following functions. Use prototypes and put your functions below main. 1. Write a function harmonicMeans that repeatedly asks the user for two int values until at least one of them is 0. For each pair, the function should calculate and display the harmonic mean of the numbers. The harmonic mean of the numbers is the inverse of the average of the inverses. The harmonic mean of x and y can be calculated...
C program with functions Make a dice game with the following characteristics: must be two players...
C program with functions Make a dice game with the following characteristics: must be two players with two dice. when player 1 rolls the marker is annotated when player 2 rolls the marker is annotated in other words, for each roll the added scores must be shown. Example: Round 1: player 1 score = 6 player 2 score = 8 Round 2: player 1 score = 14 player 2 score = 20 Whoever reaches 35 points first wins. if a...
In C# A Tic Tac Toe program that uses functions and arrays. The program should start...
In C# A Tic Tac Toe program that uses functions and arrays. The program should start by asking the player one for their name and then player two for their name. Then should print the board and ask the user what column and then what row they would like to put their x (or o, depending on the player) . Use the clear function to make it look as though the board is never repeating.. Thanks!
C++ Programming 19.2 Operator Overloading practice Write the prototypes and functions to overload the given operators...
C++ Programming 19.2 Operator Overloading practice Write the prototypes and functions to overload the given operators in the code main.cpp //This program shows how to use the class rectangleType. #include <iostream> #include "rectangleType.h" using namespace std; int main() { rectangleType rectangle1(23, 45); //Line 1 rectangleType rectangle2(12, 10); //Line 2 rectangleType rectangle3; //Line 3 rectangleType rectangle4; //Line 4 cout << "Line 5: rectangle1: "; //Line 5 rectangle1.print(); //Line 6 cout << endl; //Line 7 cout << "Line 8: rectangle2: "; //Line...
make a C / C++, Java, or Python program with two processes, a producer and a...
make a C / C++, Java, or Python program with two processes, a producer and a consumer. The producer and consumer will share an integer array with a length of 5 which is a circular buffer. The producer and consumer will also share one or two (your choice) variables to coordinate the placing and removal of items from the circular buffer. The producer process consists of a loop that writes the loop count (a value from 0 to 99) into...
C++ Please read the question carefully and make sure that the function prototypes given are used...
C++ Please read the question carefully and make sure that the function prototypes given are used correctly for both parts. This is one whole programming assignment so please make sure that it;s answered entirely not just one part. The output example is provided at the end of the question. First , write a program to create an array and fill it up with 20 randomly generated integers between 0 to 10 and output the array. Part 1: Write a function...
Write a C++ program (using pointers and dynamic memory allocation only) to implement the following functions...
Write a C++ program (using pointers and dynamic memory allocation only) to implement the following functions and call it from the main function. (1)Write a function whose signature looks like (char*, char) which returns true if the 1st parameter cstring contains the 2nd parameter char, or false otherwise. (2)Create an array of Planets. Populate the array and print the contents of the array using the pointer notation instead of the subscripts.
The code should be written in c++. It should be in only OpenGL // ***** ONLY...
The code should be written in c++. It should be in only OpenGL // ***** ONLY OpenGL PLEASE ******// write a program snippet that accepts the coordinates of the vertices of a rectangle centered around the origin, with edges that are parallel to the X and Y axes, and with aspect ratio W and converts it into a rectangle centered around the origin with aspect ratio of 1/W. The program has to figure W from the given points. You MUST...
C++ program using Eclipse IDE The C++ program should have concurrency. The C++ program should create...
C++ program using Eclipse IDE The C++ program should have concurrency. The C++ program should create 2 threads that will act as counters. One thread should count down from 5 to 0. Once that thread reaches 0, then a second thread should be used to count up to 20.
create a C++ program where you have 2 functions with two parameters. Limit the first function...
create a C++ program where you have 2 functions with two parameters. Limit the first function allowed input to values of numbers from 1-10 and from 5 to 20 for the second function. have each function add their two-parameter together then add the functions final values together. Show error message if wrong input is entered   Ask the user if they wish to continue the program (use loop or decision for this question).
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT