w3resource

C programming Math : Exercises, Practice and Solution

C programming Mathematics [38 exercises with solution]

[An editor is available at the bottom of the page to write and execute the scripts. Go to the editor]

1. Write a C program to reverse the digits of a given integer.
Example:
Input:
i = 123
i = 208478933
i = -73634
Output:
Reverse integer: 321
Reverse integer: 339874802
Reverse integer: -43637
Click me to see the solution

2. Write a C program to check whether an integer is a palindrome or not. An integer is a palindrome when it reads the same forward as backward.
Example:
Input:
i = 1221
i = -121
i = 100
Output:
Is Palindrome: 1
Is Palindrome: 0
Is Palindrome: 0
Click me to see the solution

3. Write a C program to divide two integers (dividend and divisor) without using the multiplication, division and mod operator.
Example:
Input:
dividend_num = 7
divisor_num = 2

dividend_num = -17
divisor_num = 5

dividend_num = 35
divisor_num = 7
Output:
Result: 3
Result: -3
Result: 5
Click me to see the solution

4. Write a C program to calculate x raised to the power n (xn).
Example:
Input:
x = 7.0
n = 2

x = 6.2
n = 3
Output:
Result:(x^n) : 49.000000
Result:(x^n) : 238.328000
Click me to see the solution

5. The following set contains a total of n! unique permutations
Set: [1, 2, 3, ..., n]
If n =3 we will get the following sequence:
1. "123"
2. "132"
3. "213"
4. "231"
5. "312"
6. "321"
Input: n = 3, k = 4
Output: "231"
Write a C program to get the kth permutation sequence from two given integers n and k. In these integers, n is between 1 and 9 inclusive. In addition, k is between 1 and n! Inclusive.
Example:
Input:
n = 3
int k = 2

n = 4
k = 7
Output:
Kth sequence: 132
Kth sequence: 2134
Click me to see the solution

6. Write a C program to check if a given string can be interpreted as a decimal number.
Example:
Input:
str_num1[ ] ="1234"
str_num2[ ]=" 0.1 "
str_num3[ ]=" -90e3 "
str_num4[ ]=" 99e2.5 "
Output:
Is the above string is a number? 1
Is the above string is a number? 1
Is the above string is a number? 1
Is the above string is a number? 0
Click me to see the solution

7. Write a C program to get the fraction part from two given integers representing the numerator and denominator in string format.
Example:
Input:
n = 3
d = 2

n = 4
d = 7
Output:
Fractional part: 1.5
Fractional part: 0.(571428)
Click me to see the solution

8. Write a C program to get the Excel column title that corresponds to a given column number (integer value).
For example:
1 -> A
2 -> B
3 -> C
...
26 -> Z
27 -> AA
28 -> AB
...
Example:
Input:
n = 3
n = 27
n = 151
Output:
Excel column title: C
Excel column title: AA
Excel column title: EU
Click me to see the solution

9. Write a C program to get the column number (integer value) that corresponds to a column title as it appears in an Excel sheet.
For example:
A -> 1
B -> 2
C -> 3
...
Z -> 26
AA -> 27
AB -> 28
...
Example:
Input:
col_title1[ ] ="C"
col_title2[ ] ="AC"
col_title3[ ] ="ZY"
Output:
Corresponding number: 3
Corresponding number: 29
Corresponding number: 701
Click me to see the solution

10. Write a C program to find the number of trailing zeroes in a given factorial.
Example 1:
Input: 4
Output: 0
Explanation: 4! = 24, no trailing zero.
Example 2:
Input: 6
Output: 1
Explanation: 6! = 720, one trailing zero.

Example:
Input:
n = 4
n = 5
Output:
Number of trailing zeroes of factorial 4 is 0
Number of trailing zeroes of factorial 5 is 1
Click me to see the solution

11. Write a C program to count the total number of digits 1 appearing in all positive integers less than or equal to a given integer n.
Example:
Input n = 12,
Return 5, because digit 1 occurred 5 times in the following numbers: 1, 10, 11, 12.
Example:
Input:
n = 12
n = 30
Output:
Total number of digit 1 appearing in 12 (less than or equal) is 5.
Total number of digit 1 appearing in 30 (less than or equal) is 13.
Click me to see the solution

12. Write a C program to add repeatedly all digits of a given non-negative number until the result has only one digit.
Example:
Input: 48
Output: 2
Explanation: The formula is like: 4 + 8 = 12, 1 + 2 = 3.
Click me to see the solution

13. Write a C program to check if a given integer is a power of three.
Example:
Input: 9
Output: true
Input: 81
Output: true
Input: 45
Output: false
Click me to see the solution

14. For a non negative integer in the range 0 ≤ i ≤ n write a C program to calculate the number of 1's in their binary representation and return them as an array.
Example:
Input:
Number: 7
Number of 1's in the binary representation:
0: 0
1: 1
2: 1
3: 2
4: 1
5: 2
Click me to see the solution

15. Write a C program to get the maximum product of a given integer after breaking the integer into the sum of at least two positive integers.
Example:
Input: 12
Output: 81
Explanation: 12 = 3 + 3 + 3 + 3, 3 x 3 × 3 × 3 = 81.
Input: 7
Output: 12
Explanation: 7 = 3 + 2 + 2, 3 x 2 x 2 = 12.
Click me to see the solution

16. Lexicographical order:
From Wikipedia,
In mathematics, the lexicographic or lexicographical order (also known as lexical order, dictionary order, alphabetical order or lexicographic(al) product) is a generalization of the way words are alphabetically ordered based on the alphabetical order of their component letters. This generalization consists primarily in defining a total order on the sequences (often called strings in computer science) of elements of a finite totally ordered set, often called an alphabet.
Write a C program to print numbers from 1 to an integer(N) in lexicographic order.
Example:
Input: 10
Output:
Print numbers from 1 to 10 in lexicographic order-
1 10 2 3 4 5 6 7 8 9
Input: 25
Output:
Print numbers from 1 to 25 in lexicographic order-
1 10 11 12 13 14 15 16 17 18 19 2 20 21 22 23 24 25 3 4 5 6 7 8 9
Click me to see the solution

17. Write a C program to find the nth digit of the number 1 to n?
Infinite integer sequence: 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 .. where n is a positive integer.
Example:
Input:
7
Output:
7
Input:
12
Output:
1
The 12th digit of the sequence 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, ... is 1, which is part of the number 11.
Click me to see the solution

18. Write a C program to find the total number of full staircase rows that can be formed from a given number of dice.
Example 1:
n = 5
The dices can form the following rows:
C Exercises: staircase format when n = 5.
As the 3rd row is incomplete the program will return 2 (full staircase rows).
Example 1:
n = 8 The dices can form the following rows:
C Exercises: staircase format when n = 8.
As the 4th row is incomplete the program will return 3 (full staircase rows).
Click me to see the solution

19. Write a C program to find the square root of a number using the Babylonian method.
Example 1:
Input: n = 50
Output: 7.071068
Example 2:
Input: n = 17
Output: 4.123106
Click me to see the solution

20. Write a C program to multiply two integers without using multiplication, division, bitwise operators, and loops.
Example 1:
Input: n1 = 50
Input: n2 = 12
Output: 600
Example 2:
Input: n1 = 0
Input: n2 = 12
Output: 0
Click me to see the solution

21. Write a C program to calculate and print the average (or mean) of a stream of given numbers.
Example 1:
Input:
arr[] = {10, 20, 30, 40, 50, 60, 70, 80, 90, 100}
Output:
Average of 1 numbers is 10.000000
Average of 2 numbers is 15.000000
Average of 3 numbers is 20.000000
Average of 4 numbers is 25.000000
Average of 5 numbers is 30.000000
Average of 6 numbers is 35.000000
Average of 7 numbers is 40.000000
Average of 8 numbers is 45.000000
Average of 9 numbers is 50.000000
Average of 10 numbers is 55.000000
Click me to see the solution

22. Write a C program to count the numbers without the digit 7, from 1 to a given number.
Example 1:
Input: n = 10
Output: 9
Example 2:
Input: n = 687
Output: 555
Click me to see the solution

23. Write a C program to find next smallest palindrome of a given number.
From Wikipedia,
A palindrome is a word, number, phrase, or other sequence of characters which reads the same backward as forward, such as madam, racecar. There are also numeric palindromes, including date/time stamps using short digits 11/11/11 11:11 and long digits 02/02/2020. Sentence-length palindromes may be written when allowances are made for adjustments to capital letters, punctuation, and word dividers, such as "A man, a plan, a canal, Panama!".
Example 1:
Input: n = 121
Output: Next smallest palindrome of 121 is 131
Click me to see the solution

24. Write a C program to calculate e raised to the power of x using the sum of the first n terms of the Taylor Series.
From Wikipedia,
In mathematics, a Taylor series is a representation of a function as an infinite sum of terms that are calculated from the values of the function's derivatives at a single point.
Example:
The Taylor series for any polynomial is the polynomial itself.
C programming Exercises: Math - Taylor Series
The above expansion holds because the derivative of ex with respect to x is also ex, and e0 equals 1.
This leaves the terms (x − 0)n in the numerator and n! in the denominator for each term in the infinite sum.
Example 1:
Input: n = 25
float x= 5.0
Output: e^x = 148.413162
Click me to see the solution

25. Write a C program to print all prime factors of a given number.
Example 1:
Input: n = 75
Output: All prime factors of 75 are: 3 5 5
Click me to see the solution

26. Write a C program to check if a given number is a Fibonacci number or not.
In mathematics, the Fibonacci numbers, commonly denoted Fn form a sequence, called the Fibonacci sequence, such that each number is the sum of the two preceding ones, starting from 0 and 1. That is, and for n > 1. By starting with 1 and 2, the first 10 terms will be: 1, 2, 3, 5, 8, 13, 21, 34, 55, 89.
Example 1:
Input: n = 8
Output: 1
Click me to see the solution

27. Write a C program to multiply two numbers using bitwise operators.

Example 1:
Input: int x = 8
int y = 9
Output: Product of 8 and 9 using bitwise operators is: 72
Click me to see the solution

28. Write a C program to find the angle between the hour and minute hands.

Example 1:
Input: int ha = 11
int ma = 30 Output: Angle between hour and minute hands 165
Click me to see the solution

29. Write a C programming to get the smallest number of square numbers that add up to an integer n.
In mathematics, a perfect square is a number that can be expressed as either the product of an integer by itself or as the second exponent of an integer..
Sample Data:
14 = 32 + 22 + 12
Output – 3
15 = 32 + 22 + 12 + 12
Output - 4
16 = 42
Output – 1
17 = 42 + 12
Output – 2
Click me to see the solution

30. Write a C program that accepts a number (n) and counts all numbers with unique digits of length x within a specified range.
Range: 0 <= x < 10n
Test Data:
(1) -> 10
(2) -> 91
Click me to see the solution

31. Write a C programming to calculate the largest number that can be generated by swapping just two digits at most once.
Test Data:
(89) -> 98 [Swapping 8 and 9]
(568) -> 865 [Swapping 8 and 5]
(4499) -> 9494 [Swapping 9 and 4]
(12345) -> 52341 [Swapping 1 and 5]
(7743) -> 52341 [No Swap]
Click me to see the solution

32. Write a C programming to check whether a given integer can be expressed as the sum of any non-negative integer and its reverse. Return true otherwise false.
Test Data:
(554) -> 1
(51) -> 0
(55) -> 1
(181) -> 1
Click me to see the solution

33. Write a C program to count the digits in a given number that divide it.
Test Data:
(9) -> 1
(27) -> 0
(145) -> 2
(2245) -> 1
(2222) -> 4
Click me to see the solution

34. Write a C program that creates a multiplication table of size m x n using integers where 1 <= k <= m * n. Return the kth smallest element in the said multiplication table.
In mathematics, a multiplication table is a mathematical table used to define a multiplication operation for an algebraic system. The decimal multiplication table was traditionally taught as an essential part of elementary arithmetic around the world, as it lays the foundation for arithmetic operations with base-ten numbers
Test Data:
(3,3,8) -> 6
(2,3,4) -> 3
Click me to see the solution

35. Write a C program that accepts an integer and find all prime factors of the integer.
Prime factors of a number are those factors that are prime numbers. 2 and 4 are factors of 4, where 2 is considered the prime factor.
Test Data:
(77) -> 7, 11
(12) -> 2, 2, 3
(45) -> 3, 3, 5
Click me to see the solution

36. Write a C program to count common factors of the two given integers.
Factor - A number or algebraic expression that divides another evenly, that is, without leaving a remainder.
Test Data:
(18, 6) -> 4
(45, 105) -> 4
Click me to see the solution

37. Write a C program that counts the number of integers whose digits are unique from 1 and a given integer value.
Test Data:
(30) -> 28
(135) -> 110
Click me to see the solution

38. Accept a positive integer (n) from the user. Write a C program that counts the number of positive integers from 1 to n whose digit sums are odd.
Test Data:
(5) -> 3
(10) -> 6
(11) -> 6
Click me to see the solution

C Programming Code Editor:

More to Come !

Do not submit any solution of the above exercises at here, if you want to contribute go to the appropriate exercise page.



Follow us on Facebook and Twitter for latest update.