Follow Me

Iterative function exercises in C Programming in C

  • Sum of the first n terms of the harmonic series - iterative: Write an iterative function $\texttt{float harm_it(int n)}$ which computes the sum of the first n terms of the harmonic series (n is passed in parameter): \(1 + \frac{1}{2} + \frac{1}{3} + \cdots + \frac{1}{n}\)...
  • Sum of the terms of the harmonic series and stops when the value of the term added is lower than a positive threshold - iterative: Write an iterative function float harm_it_threshold(float s) which calculates the sum of the terms of the harmonic series and stops when the value of the term added is lower than a positive threshold s passed in parameter: \(1 + \frac{1}{2} + \frac{1}{3} + \cdots + \frac{1}{n}\)...
  • Iterative function to convert a decimal integer to binary: Write an iterative function that converts a decimal integer to binary....
  • Iterative function to calculate the binomial coefficient: Write an iterative function that calculates the binomial coefficient. \(C_n^p=\frac{n!}{(n-p)!\times p!}\)...
  • Twin integers: We say that two integers p and q are twins, if they are both primes and if q=p+2 (or p=q+2). F or example, 5 and 7 are twins. Write a twin function that determines whether two integers are twins....
  • Alexandrine multiplication: To multiply two integers X and Y, repeat the following double operation on the pair (X, Y), as long as Y is different from 0: multiply X by 2 and divide Y by 2 (integer quotient). The result is equal to the sum of the multiples of X corresponding to ...
  • Square string: A string of characters w is a square if there is a string u such that w=uu (for example "bonbon" is square). Write a function that returns 1 if the string passed as a parameter is a square, otherwise 0 (remember that the strlen function returns the n...
  • Harshad numbers: The number of Harshad is an integer divisible by the sum of its digits. For example 12 (= 3 * 4), 18, 20, 21 ... are such numbers. Write a function that, given an integer, determines whether it is a Harshad number or not....
  • Function sorting an array of strings: Write a program that reads 10 words and stores them in an array of strings. write a function that sorts the 10 words lexicographically using the strcmp and strcpy functions. Display the sorted array....
  • Hash code function: Define the function hash_code, which returns the hash code of a string.    (hash_code ("Abc") = ASCII(A) + ASCII(b) + ASCII(c) = 65 + 98+ 99 = 262 )...
  • Displaying numbers in blue and vowels in red: Define the function IsNumeric which returns 1 if the character is numeric, 0 otherwise Define the function IsVowel which returns 1 if the character is a vowel, 0 otherwise (Vowels = {‘a’, ‘e’, ‘i’, ‘o’, ‘u’}). Then, write a progr...
  • Function that calculates the cube of any number: Write a function that calculates the cube of any number passed as paramater....
  • Diameter circumference and area of circle: Write a function that calculates the diameter, circumference and area of circle. Diameter = 2 * radius Circumference = 2 * \(\pi\) * radius Area = \(\pi\) * radius\(^2\) ...
  • Maximum of 2 numbers: Write a function that calculates the maximum of 2 numbers....
  • Minimum of 2 numbers: Write a function that calculates the minimum of 2 numbers....
  • Check if a number is even: Write a function that check if a number is even....
  • Check if a number is odd: Write a function that check if a number is odd....
  • Check if a number is prime: Write a function that check if a number is prime....
  • Check if a number is an Armstrong number: Write a function that check if a number is an Armstrong number. Determine how many digits are in the number. Call that n. Then take every digit in the number and raise it to the n power. Add all those together, and if your answer is the original nu...
  • Check if a number is a Perfect number: Write a function that check if a number is a Perfect number. In number theory, a perfect number is a positive integer that is equal to the sum of its proper positive divisors, that is, the sum of its positive divisors excluding the number itself. F...
  • Transpose of a matrix using an iterative function: Write the function TRANSPO_MATRIX with 3 parameters MAT, R and C, CMAX transposes the MAT matrix. TRANSPO_MATRICE returns a logical value which indicates if the dimensions of the matrix are such that the transposition could be carried out. Write a sm...
  • Prime factors: Write functions that : reads and return a positive integer checks whether an integer is prime or not generates the first N primes numbers generates the prime factors of an integer ...
  • Prime and coprime numbers: Write a function isPrime that takes an integer a, returns 1 if a is prime and returns 0 if a is not prime. Two integers a and b are said to be coPrime if the only positive integer that divides both is 1. Write a function areCoPrime that takes two int...
  • File names: In this exercise we consider strings denoting complete file names. A complete file name consists of 2 parts: a name and an extension (example: prog.c is a file name, where “prog” is the name, and “c” is the extension). Therefore, a file name ...
  • Perfect numbers: Write the function perfect that checks if a number is perfect or not. The function returns 1 if the number is perfect and 0 otherwise. Note that an integer N is considered perfect if the sum of the divisors of N, excluding the number N itself, is equ...
  • Maximum of first half and the minimum of second half of an array: Write a C function DisplayArray that takes as parameter an array and two index m and n. This function should display all arrays’ elements that are between these indexes. Write a C function MaxMin that takes as parameter an array of integers and its...
  • Check if a string S1 is a substring of a string S2 using arrays: Write the function OnlyCapitals that takes a string S and checks if it contains only capital letters. The function returns 1 if the S contains only capital letters, 0 otherwise. Write the function CountCapitalLetters that takes a string S and an arra...
  • Largest digit in a number: Write a function that takes as parameter an integer n and returns the largest digit in n.Example 1: If n = 3091, your function must return 9Example 2: If n = 1385201, your function must return 8 Write a main program that asks the user to enter a posi...
  • Sequence number: Consider the following sequence: \(\Big\{\begin{array}{c} U_0=1 \\ U_n=U_{n-1}+\frac{U_{n-1}}{n!} \texttt{ if } n>0\end{array}\) Write the function int Factorial (int n) that returns the factorial of an integer n. Write the function float Sequence...
  • Brother numbers: Two numbers N1 and N2 are said to be brothers, if each digit of N1 appears at least one time in N2, AND each digit of N2 appears at least one time in N1. Examples: N1 = 1162 and N2 = 612, are brothers. N1 = 905 and N2 = 9059, are bothers. N1 = 405 an...
  • Regrouping alphabets in strings: Write the function isAlphabet that checks if a character passed as parameter is an alphabet or not (the function returns 1 if the character is an alphabet or 0 otherwise). Write the function EliminationOfNoAlphabet that takes a string S as parameter ...
  • Display elements of a sequence: Consider the following sequence: \(\Big\{\begin{array}{c} U_0=U_1=1 \\ U_n=2 \times U_{n-1}+3 \times U_{n-2} \end{array}\) Write the function int Seq (int n) that returns the value of the element. Write a main function that print the first 20 element...
  • Fact-inverse: Write a program that asks the user to enter a positive integer  then prints the factorial inverse  (fact-inverse) of  if . Example: the fact-inverse of 24 is 4 (as 24 = 4!), the fact-inverse of 120 is 5 (as 120 = 5!). If the number is not a factor...
  • Zeroing duplicated elements in an array: Write a main program that declares and reads an array of  integers, and then sets to 0 the duplicated appearance of all its elements. (elements are different than 0) Example: If the entered array is:1, 7, 1, 3, 5, 1, 7The array should become 1, 7, ...
  • Replace the most frequent capital letter by the least frequent capital letter in a string: Write the function isCapital that takes a string and returns 1 if the string contains only (exclusively) capital alphabet, and 0 otherwise. Write the function Replace that takes a string and replaces the most frequent capital letter by the least freq...
  • Kth largest element in an array: Write the function \(\texttt{int READDIM()}\) that reads and returns a strictly positive integer less than 50; Write the function \(\texttt{void READARRAY(int Arr[], int N)}\) that reads \(\texttt{N}\) positive integers of the array \(\texttt{Arr}\);...
  • Calculate the average of 2 numbers: Write a program using a float type AVERAGE function to display the arithmetic mean of two real numbers entered on the keyboard....
  • Calculate X power N: Write a function EXP that calculates the value \(X^N\) for a real value X (double type) and a positive integer value N (type int): EXP returns the value \(X^N\) as result. Write a program that tests the function using values read from the keyboard....
  • Minimum of 4 real numbers: Write a MIN function that determines the minimum of two real numbers. Write a program using the MIN function to determine the minimum of four real numbers entered on the keyboard....
  • Maximum of 4 real numbers: Write a MAX function that determines the maximum of two real numbers. Write a program using the MAX function to determine the maximum of four real numbers entered on the keyboard....
  • Display the table of values of a function: Write a program using an F function to display the table of values of the function defined by \(f(x)=\sin{x}+\ln{x}-\sqrt{x}\) where x is an integer between 1 and 10....
  • Number of digits in a long integer number: Write the NUM function of the type int which obtains an integer value N (positive or negative) of the long type as a parameter and which gives the number of digits of N as a result. Write a small program that tests the function NUM. Example:     ...
  • Iterative function to calculate the factorial of a positive integer: Write an iterative function that calculates the factorial of a positive integer....
  • Pascal triangle using binomial coefficients: Write a program that builds and displays Pascal's triangle by calculating the binomial coefficients: Example: Enter the degree of the triangle (max.13) : 6 Pascal Triangle of degree 7 : N= 1      1 N= 2      1   1 N= 3      1   2   1 ...
  • Maximum value of n integers using Max function: Write a function int MAX (int a, int b) which returns the maximum of 2 integers; Test the MAX function using a simple instruction. Write a main function that enters N integers, asks the user to enter N integers and calculates their maximum using the ...
  • Read the dimension and the elements on an array using an iterative function: Write the function READARRAY with 2 parameters TAB and NMAX reads and returns the dimension N and the components of a table TAB of the type int. The dimension N must be less than NMAX. Implement the function READARRAY by choosing the type of the para...
  • Display the elements on an array using an iterative function: Write the DISPLAYARRAY function with two parameters TAB and N which displays N components of the array TAB of the type int. Example: The table T read in the previous exercise will be displayed by the call:       DISPLAYARRAY(T, N); and will be p...
  • Sum of the elements of an array using an iterative function: Write the function SUM_TAB which calculates the sum of the N elements of an array TAB of the type int. N and TAB are provided as parameters; the sum is returned as the result of the long type....
  • Calling the sum function on an array: Using the functions of the previous exercises, write a program that reads an array A with a dimension less than or equal to 100 and displays the array and the sum of the elements of the array....
  • Average of an array: Write a function that returns the average of the grades given in an array....
  • Checking whether two arrays contain the same number of prime numbers: Write the function prime that checks if a number is prime or not. The function returns 1 if the number is prime and 0 otherwise. Write a function ReadArray that allows to read the value of an array of size M. All the elements of the array should be b...
  • Symmetric array: Write a function ArrayEqual that takes two arrays A1 and A2 of the same size and checks if the two arrays are equal. Two arrays are equal when their same position elements are equal. Example:A1 = {2,4,5,1} and A2 = {2,4,5,1} are equalA1 = {2,4,5,1} a...
  • Maximum element of an array using an iterative function: Determine the maximum of N elements of a TAB array of integers....
  • Index of the maximum element of an array using an iterative function: Determine the index of the maximum of N elements of a TAB array of integers....
  • Insert an element into a sorted array using an iterative function: Write the INSERT function which places an X element inside an array that contains N elements sorted in ascending order, so as to obtain an array with N + 1 elements sorted in ascending order. The dimension of the array is incremented in the INSERT fu...
  • Merge 2 sorted arrays using an iterative function: Write the FUSION function that constructs a FUS array sorted in ascending order with the elements of two arrays A and B sorted in ascending order. For two arrays of dimensions N and M, the table FUS will have the dimension N + M. Write a program that...
  • Length of a string using an iterative function: Write the function LENGTH which returns the length of a string of characters CH as result....
  • Converts all letters in a string to uppercase using an iterative function: Write the UPPER function that converts all letters in a string to uppercase....
  • Concatenate two strings using an iterative function: Write the function ADD having as parameters STR1 and STR2 which copies the string STR2 to the end of the string STR1....
  • Reverse a string using an iterative function: Write the REVERSE function which reverses the character order of a string....
  • Number of words in a string using an iterative function: Write the function NWORDS which returns as result the number of words contained in a string of characters. Use a logical variable, the function isspace and a helper variable N....
  • Check whether the first N characters of two strings are equal using an iterative function: Write the function EQUAL_N which returns the value 1 if the first N characters of STR1 and STR2 are equal, otherwise the value 0. (If N is greater than the length of STR1 or STR2, the result can be 1 or 0)....
  • Encryption of a character: Write a function \(\texttt{char encrypt (char c, int k)}\) that encrypts a character as follows: if c is an alphabetic character, shift c by k to the right (+ k) otherwise shift c by k to the left (-k). Write a main function that tests the function....
  • Displaying digits in words: Write a function digitname that takes a digit and displays its name in uppercase letters; for example if the digit is 4, the function displays FOUR. Write a C program that reads a positive integer n, and calls the above function to display the digits...
  • Number of words separated by a space: Write a function called nbrofWords that has an array of characters as parameter (string). This function must return the number of words in this string without considering spaces. We suppose that the string is composed of words separated by single bla...
  • Reading the elements of a matrix using an iterative function: Write the function READ_MATRIX taking 3 parameters MAT, R, and C which reads the components of a matrix MAT of the type int and dimensions R and C....
  • Display the elements of a matrix using an iterative function: Write the function PRINT_MATRIX with 3 parameters MAT, R, and C which displays the components of the matrix of dimensions R and C....
  • Sum of the elements of a matrix using an iterative function: Write the function SUM_MATRIX of the long type which calculates the sum of the elements of a MAT matrix of the type int. Choose the necessary parameters. Write a small program that tests the function SUM_MATRIX....
  • Sum of 2 matrices using an iterative function: Write the function ADDITION_MATRIX which carries out the addition of the matrices following: MAT1 = MAT1 + MAT2 Choose the necessary parameters and write a small program that tests the function ADDITION_MATRIX....
  • Multiplication of a matrix by a number using an iterative function: Write the MULTI_MATRIX function which multiplies the matrix MAT1 by an integer X: MAT1 = X * MAT1 Choose the necessary parameters and write a small program that tests the MULTI_MATRIX function....
  • Multiplication of two matrices using an iterative function: Write the function MULTI_2_MATRICES which performs the multiplication of two matrices MAT1 (dimensions R and P) and MAT2 (dimensions P and C) in a third matrix MAT3 (dimensions R and C): MAT3 = MAT1 * MAT2 Suppose that the maximum dimensions of the t...
  • Encryption of digits: The purpose of this application is to encrypt an integer number into a text message (string).  The following replacements of digits should be made in the encryption process: $$0\rightarrow ’a’, 1 \rightarrow ’b’, 2 \rightarrow ’c’, 3 \ri...
  • Twin prime numbers: Write the function « int Prime(int a) » that verifies if an integer « a » is a prime number or not. Write the function « int NextPrime (int a) » that returns the smallest prime number greater than « a ».Example: NextPrime(9) should return 11....
  • Checking palindrome words using functions: The objective of this exercise is to propose a new algorithm to check if a word (string without spaces) is palindrome. Write the function "firstlast" that takes as parameter a word (a string without spaces) and construct a new string by regrouping th...
  • Derivative and second derivative: Let the mathematical function f defined by $$f(x)=\frac{(2x^3+3)(x^2-1)}{\sqrt{3x^2+1}}$$ Write a C function that returns the value of f (x) for a point x passed as a parameter. An approximation of the derivative f' of the function f is given in each...
  • Maximum distance between points: A point is defined by its real coordinates (X, Y). The purpose of this program is to allow the user to enter N distinct points, calculates and prints the smallest distance between any two points of the sequence. Two arrays should be used to store the...
  • Read the dimension and the elements on a matrix using an iterative function: Write the function READ_NB_ROWS which reads the number of rows R of a matrix. Write the function READ_NB_COLUMNS which reads the number of columns C of a matrix. Write the function READ_MATRIX taking 3 parameters MAT, R, and C which reads the compone...
  • Program output: Give the output on the screen of the following program (indicate a space using this symbol _): #include<stdio.h>double mystery(int A[], int i){     i = A[i] * 2;     A[0] = 0;     return i+1;} int main(){     int i = 8, j = 5, k, A[...
  • Dot product of two unequal-length vectors: We are interested to perform the dot product for unequal-length vectors. Algebraically, the dot product is the sum of the products of the corresponding entries of the two sequences of numbers. The dot product of two unequal-length vectors $A = [a_0, ...
  • Program output 13: Give the output on the screen of the following program (indicate a space using this symbol _): #include<stdio.h>double mystery(int T[], int i){     int k;      for(k=0 ; k<i ; k++)          T[k] -= T[i] - k;     return T[k] + ...
  • Array manipulation using functions: Write the function $\texttt{int READDIM()}$ that reads and returns a strictly positive integer $N$ less than 50; Write the function $\texttt{void READARRAY(int T[], int N)}$ that fills $N$ positive integers in the array $\texttt{T}$; Write the functi...
  • The missing value: We have an array $T[N]$ containing all integers in the interval $0 \cdots N$, except one integer. We want to determine which integer is missing from $T$ using multiple approaches.  Example: for the following array $T$ of size $N=6$, the missing valu...
  • Swap two numbers using pointers: Write a program that declares 2 integers and swap it using pointers....
  • Removing a sequence of 3 characters from a string: Write a function Reduction that takes a string S and eliminates from S every sequence of 3 or more consecutive occurrences of the same character. The function returns 1 if it finds at least one sequence to eliminate from S and 0 if no sequence to eli...
  • Maximum value in a Binary tree of integers: Write each time a function that returns the maximum value of all the nodes in a binary tree of integers. Assume all values are nonnegative; return -1 if the tree is empty. recursive function iterartive using a queue iterative using a stack ...
  • Checking whether an element is in a Binary search tree: Write an iterative function that tests whether an element belongs to a \(BST\)....
  • Checking whether elements of 2 BSTs are the same using an iterative function: Knowing that the inorder traversal of BSTs produces sorted lists, write an iterative function function that given 2 BSTs, the function checks whether the elements of both BSTs are the same or not. The time complexity of your algorithm should be \(O(max(m, n))\)...
  • Write an iterative function that checks whether an element belongs to a BST: Write a iterative function that checks whether an element belongs to a BST....

Back to the list of exercises