Reload your browser or click the button below to continue.
Disable AdBlock Plus:
Click on the icon in yor browser toolbar.
Click the blue button next to "Block Ads On:"
Click the red "Refresh" button or click the button below to continue.
Disable Adguard AdBlocker:
Click on the icon in yor browser toolbar.
Click on the toggle next to "Protection on this website".
Reload your browser or click the button below to continue.
Disable uBlock Origin:
Click on the icon in yor browser toolbar.
Click on the big blue "power" icon.
Reload your browser or click the button below to continue.
Repetitive structure exercises in C Programming in C
Find the missing element in n consecutive elements: Write a program that reads the number N of integers, then read N consecutive integers starting from 0 with one integer missing (integers could be entered in any random manner) and finds the missing value.
Example:
Enter the number of elements: 9
En...
Sum between 1 and 100: Write a program that displays the sum of numbers between 1 and 100...
Binary code of a character: Write a program that reads a character on the keyboard and display its numeric code in binary notation....
Sum product and average of N numbers using loops: Write a program that reads N whole numbers on the keyboard and displays their sum, product, and average. Choose an appropriate type for the values to display. The number N is to enter on the keyboard. Solve this problem,
using while,
using do - while...
Harmonic series: Calculate the sum of the first N terms of the harmonic series:\(1 + \frac{1}{2}+\frac{1}{3}+\cdots+\frac{1}{N}\)...
Sum of positive non zero digits: Calculate the sum, the product and the average of a sequence of non-zero digits entered on the keyboard, knowing that the sequence is ended by zero. Just count the numbers (0, 1 ... 9) when entering data and beep if the data comes out of this range....
Reverse a sequence of integers: Calculate the countdown count of a positive number entered on the keyboard assuming that the standard input contains a sequence of non-zero, zero-ended digits (check if they are really digits). Example: Entry: 1 9 7 4 0 Display: 4791...
Reverse an integer: Write a program that reads an integer and then calculates and displays its reverse.Example: Entry: 19740 Display: 4791...
Numerical value of a Polynomial: Calculate for a given value X of the type float the numerical value of a polynomial of degree n: \(P(X) = A_nX^n + A_{n-1}X^{n-1}+ \cdots + A_1X + A_0\)
The values of n, coefficients \(A_n, \cdots , A_0\) and X will be entered on the keyboard.
Use th...
GCD between 2 numbers: Calculate the gcd between two natural numbers using the EUCLIDE algorithm.
Example: gcd(1220,516)=?1220 mod 516 = 188 516 mod 188 = 140 188 mod 140 = 48 140 mod 48 = 44 48 mod 44 = 4 44 mod 4= 0gcd(1220,516)= 4...
Fibonacci sequence: Calculate the N-th term UN of the FIBONACCI sequence that is given by the recurrence relation:\(U_1=1 \ \ \ U_2=1 \ \ \ U_N=U_{N-1}+ U_{N-2}\) (for N>2)...
Number pattern 1: \(\begin{array}{cccccc}1&2&3&4&5&6\\2&3&4&5&6&7\\3&4&5&6&7&8\\4&5&6&7&8&9\\5&6&7&8&9&0\\ 6&7&8&9&0&1\end{array}\)
Write a progra...
Number pattern 2: \(\begin{array}{cccc}1&2&3&4\\5&6&7&8\\9&10&11&12\\13&14&15&16\end{array}\)
Write a program that reads a number (number of lines equal to the number of columns) and displays the above pattern....
Number pattern 3: \(\begin{array}{cccc}1&2&3&4\\2&3&4&1\\3&4&2&1\\4&3&2&1\end{array}\)
Write a program that reads a number (number of lines equal to the number of columns) and displays the above pattern....
Number pattern 4: \(\begin{array}{cccc}1&2&3&4\\2&1&2&3\\3&2&1&2\\4&3&2&1\end{array}\)
Write a program that reads a number (number of lines equal to the number of columns) and displays the above pattern....
Number pattern 5: \(\begin{array}{ccccc}3&3&3&3&3\\3&2&2&2&3\\3&2&1&2&3\\3&2&2&2&3\\3&3&3&3&3\end{array}\)
Write a program that reads a number and displays the above pattern.
In order...
Approximation of the value of e up to N terms: Write a program that reads a number N and approximates the value of e by the following partial sum: \(\sum_{n=0}^{N}\frac{1}{n!}=2+\frac{1}{2}+\frac{1}{3!}+ \cdots + \frac{1}{N!}\)...
Approximation of the value of e: Write a program that approximates the value of e by the following partial sum: \(\sum_{n=0}^{N}\frac{1}{n!}=2+\frac{1}{2}+\frac{1}{3!}+ \cdots + \frac{1}{N!}\)
You should continuously add only the terms greater than \(10^{-10}\)....
Solution of an equation using the method of Newton: We want to solve the following equation with Newton's method: \(f(x)=xe^x-1=0\).
The method consists of defining a sequence \(\{x_n\}_{n\in\mathbb{N}}\) by: \(\begin{array}{lll} x_0=1 & & \\ x_{n+1}=x_n - \frac{f(x_n)}{f'(x_n)} & for & n \in \mathbb{N}\end{array}\)...
Diffraction ratio of two slots: The illumination of the diffaction ratio of two slots is given, according to the position \(x\) in this figure, by the formula \(E=4E_0\cos^2{\frac{\pi ex}{\lambda f}}(\frac{\sin{\frac{\pi ax}{\lambda f}}}{\frac{\pi ax}{\lambda f}})^2\)
\(E_0\) illum...
Fresnel integral: Let the series (development of \(\int_0^x\cos{\frac{\pi t^2}{2}}dt\) - Fresnel integral) \(C(x)=\sum_{n=0}^{n=+\infty}\alpha_n(x)\)
the \(\alpha_n\) being defined by \(\alpha_0(x)=x\) and the recurrence formula \(\alpha_{n+1}(x)=-\frac{4n+1}{(2n+2)(2n+1)(4n+5)}(\frac{\pi}{2})^2x^4\alpha_n(x)\)...
Coordinates of a polygon at N vertices: We want to draw the star polygon at N vertices, of step p, inscribed in the trigonometric circle. The summits have for coordinates: \((x_k,y_k)=(\cos{\frac{2\pi kp}{N}},\sin{\frac{2\pi kp}{N}}), k=0 \cdots N\)
We fixe N = 25 and p = 7. Write a progra...
Probability density of the p-pulse for a particle: The probability density of the p-pulse for a particle is
\(\rho(p)=\frac{1}{4}\frac{a}{\pi\hbar}\Big[\frac{\sin{\frac{pa}{2\hbar}-\frac{n\pi}{2}}}{\frac{pa}{2\hbar}-\frac{n\pi}{2}}+(-1)^{n+1}\frac{\sin{\frac{pa}{2\hbar}+\frac{n\pi}{2}}}{\frac{pa}{2\hbar}+\frac{n\pi}{2}}\Big]^2\)...
Successive terms of a series: We consider the function \(f(x)=\frac{1}{1+x^2}\) and the series \(u_0=1\) and \(u_{n+1}=f(u_n)\)
Write a program that calculates and displays the successive terms of \(u_n\) until \(|u_{n+1}-u_n|\) is less than \(\epsilon=10^{-8}\)....
9th complement: The 9th complement of a positive integer A is a positive integer B obtained by subtracting each digit in A from 9.Write a program that asks the user to enter a positive integer (a negative number should be ignored and the user should be asked to ente...
Average value and second distinct maximum: Write a program that asks the user to enter positive integer values. The program terminates when the user enters a negative value by displaying on the screen the average value and the second distinct maximum of the list of the positive even integers ...
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...
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...
Output values of a variable: What is the value of the variables “i” and “counts” at the end of this program block?
int count = 0;int i = 0;while(count < 20){ i++; if(count % 5 == 0) break; if(count % 2 == 0) count += 2; if (c...
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, ...
Displaying nth term of 2 series: Write a program that reads a positive integer n and then displays the nth term of the following series :
\(\Big\{\begin{array}{c c c} U_0 & = & 1 \\ U_n & = & V_{n-1}+1 \end{array}\)
\(\Big\{\begin{array}{c c c} V_0 & = & 0 \\ V_n & = & 2 \times U_{n-1} \end{array}\)...
Calculus of an expression: Write a program that reads two positive integer values X and Y. The program must then calculate and display the result of the following expression :
\(\frac{(X^2+1)(\frac{X}{Y}-3)}{(X-Y^2)(Y^2-X)}\)...
Star pattern 1: \(\begin{array}{c } * \\ *\\ *\\ *\end{array}\)
Write a program that reads a number and displays a star on each line....
Star pattern 2: \(\begin{array}{c c c c } * &* & * &* \\ * &* & * &* \\ * &* & * &* \\* &* & * &* \end{array}\)
Write a program that reads a number and displa...
Star pattern 3: \(\begin{array}{c c c c } * &* & * &* \\ * & & &* \\ * & & &* \\* &* & * &* \end{array}\)
Write a program that reads a number and disp...
Star pattern 4: \(\begin{array}{c c c c c c c } & & & * & * & * & * \\ & & * & * & * & * & \\ & * & * & * & * & & \\ * & * & * & * & & & \end{array}\)
Write a ...
Star pattern 5: \(\begin{array}{c c c c c c c } & & & * & * & * & * \\ & & * & & & * & \\ & * & & & * & & \\ * & * & * & * & & & \end{array}\)
Wr...
Star pattern 6: \(\begin{array}{c c c c } * & & & \\ * & * & & \\ * & * & * & \\ * & * & * & * \end{array}\)
Write a program that reads a number and displays the above pattern....
Star pattern 7: \(\begin{array}{c c c c } * & & & \\ * & * & & \\ * & & * & \\ * & * & * & * \end{array}\)
Write a program that reads a number and displays the above pattern....
Star pattern 8: \(\begin{array}{c c c c }& & & *\\ & & * & * \\ & * & * & * \\ * & * & * & * \end{array}\)
Write a program that reads a number and displays the above pattern....
Star pattern 9: \(\begin{array}{c c c c }& & & *\\ & & * & * \\ & * & & * \\ * & * & * & * \end{array}\)
Write a program that reads a number and displays the above pattern....
Star pattern 10: \(\begin{array}{c c c c }* & * & * & * \\ * & * & * &\\* & * & & \\* & & & \end{array}\)
Write a program that reads a number and displays the above pattern....
Star pattern 11: \(\begin{array}{c c c c }* & * & * & * \\ * & & * &\\* & * & & \\* & & & \end{array}\)
Write a program that reads a number and displays the above pattern....
Star pattern 12: \(\begin{array}{c c c c }* & * & * & * \\ & *& * & *\\ & & *& *\\& & &* \end{array}\)
Write a program that reads a number and displays the above pattern....
Star pattern 13: \(\begin{array}{c c c c }* & * & * & * \\ & *& & *\\ & & *& *\\& & &* \end{array}\)
Write a program that reads a number and displays the above pattern....
Star pattern 14: \(\begin{array}{c c c c c} & & * & & \\ & * & * & *& \\ * &* & * &* & * \end{array}\)
Write a program that reads a number and displays the above pattern....
Star pattern 16: \(\begin{array}{c c c c c} *&* &*&*&* \\ &*&* &*& \\ & & * & & \end{array}\)
Write a program that reads a number and displays the above pattern....
Star pattern 17: \(\begin{array}{c c c c c} *&* &*&*&* \\ & * & & *& \\ & & * & & \end{array}\)
Write a program that reads a number and displays the above pattern....
Star pattern 18: \(\begin{array}{cccc}*&&&\\*&*&&\\*&*&*&\\*&*&*&*\\*&*&*&\\*&*&&\\*&&&\end{array}\)
Write a program that reads a number and displays the above pattern....
Star pattern 19: \(\begin{array}{cccc}*&&&\\*&*&&\\*&&*&\\*&&&*\\*&&*&\\*&*&&\\*&&&\end{array}\)
Write a program that reads a number and displays the above pattern....
Star pattern 20: \(\begin{array}{cccc}&&&*\\&&*&*\\&*&*&*\\*&*&*&*\\&*&*&*\\&&*&*\\&&&*\end{array}\)
Write a program that reads a number and displays the above pattern....
Star pattern 21: \(\begin{array}{cccc}&&&*\\&&*&*\\&*&&*\\*&&&*\\&*&&*\\&&*&*\\&&&*\end{array}\)
Write a program that reads a number and displays the above pattern....
Star pattern 22: \(\begin{array}{ccccc}&&*&&\\&*&*&*&\\*&*&*&*&*\\&*&*&*&\\&&*&&\end{array}\)
Write a program that reads a number and displays the above pattern....
Star pattern 23: \(\begin{array}{ccccc}&&*&&\\&*&&*&\\*&&&&*\\&*&&*&\\&&*&&\end{array}\)
Write a program that reads a number and displays the above pattern....
Star pattern 24: \(\begin{array}{ccccc}*&&&&*\\&*&&*&\\&&*&&\\&*&&*&\\*&&&&*\end{array}\)
Write a program that reads a number and displays the above pattern....
Star pattern 25: \(\begin{array}{ccccc}&&*&&\\&&*&&\\*&*&*&*&*\\&&*&&\\&&*&&\end{array}\)
Write a program that reads an odd number and displays the above pattern....
Star pattern 26: \(\begin{array}{ccccccc}&&*&*&*&&\\&&*&*&*&&\\*&*&*&*&*&*&*\\*&*&*&*&*&*&*\\*&*&*&*&*&*&*\\&&*&*&*&&\\&&*&*&*&&\end{array}\)...
Star pattern 27: \(\begin{array}{ccccccccc}&&&&*&&&&\\&&&I&*&I&&&\\&&*&*&*&*&*&&\\&I&*&*&*&*&*&I&\\*&*&*&*&*&*&*&*&*\end{array}\)...
Multiplication table: Displays the product table for N varying from 1 to 10:
X*Y I 0 1 2 3 4 5 6 7 8 9 10
--------------------------------------------------
0 I 0 0 0 0 0 0 0 0 0 0 0
1 I 0 1 2 3 4 5 6 7 8...
Stop reading values: Write a program which reads a sequence of real values filled by the user and stops by displaying "FINISHED" when the sum of these values exceeds 100....
Perfect number: Write a program which reads a positive integer value N and indicates if N is a perfect number or not (N is a perfect number if N = the sum of its divisors without the number itself)....
Result of an expression: Write a program which reads a positive integer value N, calculates and shows the result of the expression: 1 + 4 + 7 + 10 +... + N...
Value of a sum: Write a program which reads a positive integer value n, calculates and shows the result of the expression: $$\sum_{i=1}^{n}\frac{i+3}{i^2-5}$$...
Prime number: Write a program which indicates if a positive number N, filled by the user, is prime or not (N is a prime number if its only divisors are 1 and N)....
Maximum of a sequence of positive number: Write a program which reads a sequence of positive real numbers. The program stops when the user fills a negative value and show the maximum of these numbers....
Farthest point from the circle center: Write a C program that asks the user to enter a positive integer N, and a real value R representing the radius of a circle C of center O(0,0).
The program should then prompt the user to enter a sequence of points’ coordinates (x and y) until the nu...
Star pattern 15: \(\begin{array}{c c c c c} & & * & & \\ & * & & *& \\ * &* & * &* & * \end{array}\)
Write a program that reads an integer x and then prints on the screen a triangle o...
Number pattern 7: Write a program that reads a positive integer N and displays:
1223334444…NNNN….N...
Series values: Write a program that takes an integer n and displays the value of $$U_n$$ respecting: $$U_1=1$$ ;$$U_n=\sum_{i=1}^{n-1}\frac{U_i}{(i-1)!}$$ for n>2 ;
Write a program that asks the user to enter an integer a and then prints the series elements fro...
Divisibility by 9: An integer number is divisible by 9 if the sum of its digits is divisible by 9.
Write a program that reads a positive integer, finds the number of digits of this integer and using the criteria above, discovers if it is divisible by 9 and then prints ...
Length of the longest ascending sequence of characters: Write a program that reads an integer N, asks the user to enter N characters, calculate the length of the longest ascending sequence of characters and the position of its first character.
Example of execution:Enter a positive integer: 11Enter 11 char...
Divisor of a number: Write a program which reads a positive integer value and shows its divisors....
Sum of even numbers up to a number: Write a program which reads a positive integer value N, calculates and shows the sum of the even numbers < = N....
Sum of odd numbers up to a number: Write a program which reads a positive integer value N, calculates and shows the sum of the odd numbers < = N....
Fractional sum: Write a program which reads a positive integer number n, calculates and shows the following:
$$1 + \frac{1}{2} + \frac{1}{3} + \frac{1}{4} + ... + \frac{1}{n}$$ if $$n$$ is divisible by 7
$$n + \frac{n}{2} + \frac{n}{3} + \frac{n}{4} + ... + \frac{...
Maximum and minimum of ten real numbers: Write a program which reads 10 real numbers. The program must then show the maximum and the minimum of these numbers....
GCD and LCM: Write a program which reads two positive integer values and shows their GCD (Greatest Common Divisor) and their LCM (Least Common Multiple)....
Abundant number: Write a program that asks the user to enter an integer making sure it is a positive number (a negative integer should not be accepted), and checks if this number is ABUNDANT. A number n is said to be abundant when the sum of its divisors (including t...
Calculation of a Polynomial of degree N: Calculate for a given value X of the type float the numerical value of a polynomial of degree n: \(P(X) = A_nX^n + A_{n-1}X^{n-1}+ \cdots + A_1X + A_0\)
The values of n, coefficients \(A_n, \cdots , A_0\) and X will be entered on the keyboard and sto...
Sorting seven numbers: Write a program which reads an array of 7 integers then represents them in an increasing order....
Display some elements in an array: Write a program which fills an array of integers of size 15. This program must show the elements of the array which are multiple of 7....
Reading elements of an array with a special condition: Write a program which declares an array of integers of size maximum size 20, asks the user to enter its effective size (should be less than 20) and allows the user to fill his values. This program should not accept values greater than 150. In other w...
Compare 2 strings: Write a program that reads two strings of characters, and indicates their lexicographic precedence in the character code of the machine (here: ASCII code)....
Uppercase to lowercase and vice versa: Write a program that reads a string of characters STR and converts all uppercase letters to lowercase and vice versa.
The result will be stored in the same STR variable and displayed after the conversion....
Reverse and squeeze empty spaces in a string: Write a program that reads a string s and copy s into a string t (of the same size) in reverse order, except that a sequence of white space is squeezed to a single space....
Number of occurrence of a string into another one: Write a program that reads two strings s1 and s2, and then prints on the screen the number of times s1 appears in s2, and the position of each occurrence of s1 in s2....
Number of occurrence of a character in a string: Write an algorithm that reads a string s and a character c, prints on the screen the number of times c appears in s and the first position of c in s....
Abundant numbers: Write a function that tests if an integer is ABUNDANT (returns 1 if yes and 0 otherwise). The integer n is abundant when the sum of its divisors (including n itself) is greater than its double (2n). Example: 12 is abundant (1 + 2 + 3 + 4 + 6 + 12>...
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[...
Cost of a telephone call: Write a program that asks the user to enter a number of minutes and a number of seconds, then calculates and displays the cost of a telephone call according to the following rules:
a unit is equal to 20 seconds;
the first minute is indivisible and is...
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...
Sum of the first and the last digits of a number: Write a C program that asks the user to enter an integer number greater than 9, then calculates the sum of the first and the last digits of the number.
Running example:Input a number greater than 9: -12Input a number greater than 9: 925Sum of the fir...
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...
Program output 11: Give the output on the screen of the following program
#include <stdio.h>
#define PRINT(format,x) printf(#x " = %"#format"\t",x)#define NL putchar('\n');
#define PRINT1(f,x) PRINT(f,x);NL#define PRINT2(f,x1,x2) PRINT(f,x1);PRINT1(f,x2)#define...