w3resource
C Programming Exercises

C Basic: Exercises, Practice, Solution

C Basic Declarations and Expressions [85 exercises with solution]

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

1. Write a C program to print your name, date of birth. and mobile number. Go to the editor
Expected Output:

Name   : Alexandra Abramov  
DOB    : July 14, 1975  
Mobile : 99-9999999999

Click me to see the solution

2. Write a C program to print a block F using hash (#), where the F has a height of six characters and width of five and four characters. Go to the editor
Expected Output:

######
#
#
#####
#
#
#

Click me to see the solution

3. Write a C program to print a big 'C'. Go to the editor
Expected Output:

    ######
  ##      ##
 #
 #
 #
 #
 #
  ##      ##
    ######

Click me to see the solution

4. Write a C program to print the following characters in a reverse way. Go to the editor
Test Characters: 'X', 'M', 'L'
Expected Output:
The reverse of XML is LMX
Click me to see the solution

5. Write a C program to compute the perimeter and area of a rectangle with a height of 7 inches. and width of 5 inches. Go to the editor
Expected Output:
Perimeter of the rectangle = 24 inches
Area of the rectangle = 35 square inches
Click me to see the solution

6. Write a C program to compute the perimeter and area of a circle with a radius of 6 inches. Go to the editor
Expected Output:
Perimeter of the Circle = 37.680000 inches
Area of the Circle = 113.040001 square inches
Click me to see the solution

7. Write a C program to display multiple variables. Go to the editor
Sample Variables :
a+ c, x + c,dx + x, ((int) dx) + ax, a + x, s + b, ax + b, s + c, ax + c, ax + ux
Declaration :
int a = 125, b = 12345;
long ax = 1234567890;
short s = 4043;
float x = 2.13459;
double dx = 1.1415927;
char c = 'W';
unsigned long ux = 2541567890;
Click me to see the solution

8. Write a C program to convert specified days into years, weeks and days. Go to the editor
Note: Ignore leap year.

Test Data :
Number of days : 1329
Expected Output :
Years: 3
Weeks: 33
Days: 3
Click me to see the solution

9. Write a C program that accepts two integers from the user and calculate the sum of the two integers. Go to the editor
Test Data :
Input the first integer: 25
Input the second integer: 38
Expected Output:
Sum of the above two integers = 63
Click me to see the solution

10. Write a C program that accepts two integers from the user and calculate the product of the two integers. Go to the editor
Test Data :
Input the first integer: 25
Input the second integer: 15
Expected Output:
Product of the above two integers = 375
Click me to see the solution

11. Write a C program that accepts two item’s weight (floating points' values ) and number of purchase (floating points' values) and calculate the average value of the items. Go to the editor
Test Data :
Weight - Item1: 15
No. of item1: 5
Weight - Item2: 25
No. of item2: 4
Expected Output:
Average Value = 19.444444
Click me to see the solution

12. Write a C program that accepts an employee's ID, total worked hours of a month and the amount he received per hour. Print the employee's ID and salary (with two decimal places) of a particular month. Go to the editor
Test Data :
Input the Employees ID(Max. 10 chars): 0342
Input the working hrs: 8
Salary amount/hr: 15000
Expected Output:
Employees ID = 0342
Salary = U$ 120000.00
Click me to see the solution

13. Write a C program that accepts three integers and find the maximum of three. Go to the editor
Test Data :
Input the first integer: 25
Input the second integer: 35
Input the third integer: 15
Expected Output:
Maximum value of three integers: 35
Click me to see the solution

14. Write a C program to calculate a bike’s average consumption from the given total distance (integer value) traveled (in km) and spent fuel (in liters, float number – 2 decimal point). Go to the editor
Test Data :
Input total distance in km: 350
Input total fuel spent in liters: 5
Expected Output:
Average consumption (km/lt) 70.000
Click me to see the solution

15. Write a C program to calculate the distance between the two points. Go to the editor
Test Data :
Input x1: 25
Input y1: 15
Input x2: 35
Input y2: 10
Expected Output:
Distance between the said points: 11.1803
Click me to see the solution

16. Write a C program to read an amount (integer value) and break the amount into smallest possible number of bank notes. Go to the editor
Test Data :
Input the amount: 375
Expected Output:
There are:
3 Note(s) of 100.00
1 Note(s) of 50.00
1 Note(s) of 20.00
0 Note(s) of 10.00
1 Note(s) of 5.00
0 Note(s) of 2.00
0 Note(s) of 1.00
Click me to see the solution

17. Write a C program to convert a given integer (in seconds) to hours, minutes and seconds. Go to the editor
Test Data :
Input seconds: 25300
Expected Output:
There are:
H:M:S - 7:1:40
Click me to see the solution

18. Write a C program to convert a given integer (in days) to years, months and days, assumes that all months have 30 days and all years have 365 days. Go to the editor
Test Data :
Input no. of days: 2535
Expected Output:
6 Year(s)
11 Month(s)
15 Day(s)
Click me to see the solution

19. Write a C program that accepts 4 integers p, q, r, s from the user where r and s are positive and p is even. If q is greater than r and s is greater than p and if the sum of r and s is greater than the sum of p and q print "Correct values", otherwise print "Wrong values". Go to the editor
Test Data :
Input the second integer: 35
Input the third integer: 15
Input the fourth integer: 46
Expected Output:
Wrong values
Click me to see the solution

20. Write a C program to print the roots of Bhaskara’s formula from the given three floating numbers. Display a message if it is not possible to find the roots. Go to the editor
Test Data :
Input the first number(a): 25
Input the second number(b): 35
Input the third number(c): 12
Expected Output:
Root1 = -0.60000
Root2 = -0.80000
Click me to see the solution

21. Write a C program that reads an integer and check the specified range where it belongs. Print an error message if the number is negative and greater than 80. Go to the editor
Test Data :
Input an integer: 15
Expected Output:
Range [0, 20]
Click me to see the solution

22. Write a C program that read 5 numbers and sum of all odd values between them. Go to the editor
Test Data :
Input the first number: 11
Input the second number: 17
Input the third number: 13
Input the fourth number: 12
Input the fifth number: 5
Expected Output:
Sum of all odd values: 46
Click me to see the solution

22. Write a C program that reads three floating values and check if it is possible to make a triangle with them. Also calculate the perimeter of the triangle if the said values are valid. Go to the editor
Test Data :
Input the first integer: 5
Input the second integer: 15
Input the third integer: 10

Expected Output:
Numbers in sorted order: 5 10 15
Click me to see the solution

23. Write a C program that reads three floating values and check if it is possible to make a triangle with them. Also calculate the perimeter of the triangle if the said values are valid. Go to the editor
Test Data :
Input the first number: 25
Input the second number: 15
Input the third number: 35
Expected Output:
Perimeter = 75.0
Click me to see the solution

24. Write a C program that reads two integers and checks if they are multiplied or not. Go to the editor
Test Data :
Input the first number: 5
Input the second number: 15
Expected Output:
Multiplied!
Click me to see the solution

25. Write a C program that reads an integer between 1 and 12 and print the month of the year in English. Go to the editor
Test Data :
Input a number between 1 to 12 to get the month name: 8
Expected Output:
August
Click me to see the solution

26. Write a C program that prints all even numbers between 1 and 50 (inclusive). Go to the editor
Test Data :
Even numbers between 1 to 50 (inclusive):
Expected Output:
2 4 6 8 10 12 14 16 18 20 22 24 26 28 30 32 34 36 38 40 42 44 46 48 50
Click me to see the solution

27. Write a C program that read 5 numbers and counts the number of positive numbers and negative numbers. Go to the editor
Test Data :
Input the first number: 5
Input the second number: -4
Input the third number: 10
Input the fourth number: 15
Input the fifth number: -1
Expected Output:
Number of positive numbers: 3
Number of negative numbers: 2
Click me to see the solution

28. Write a C program that read 5 numbers and counts the number of positive numbers and print the average of all positive values. Go to the editor
Test Data :
Input the first number: 5
Input the second number: 8
Input the third number: 10
Input the fourth number: -5
Input the fifth number: 25
Expected Output:
Number of positive numbers: 4
Average value of the said positive numbers: 12.00
Click me to see the solution

29. Write a C program that read 5 numbers and sum of all odd values between them. Go to the editor
Test Data :
Input the first number: 5
Input the second number: 7
Input the third number: 9
Input the fourth number: 10
Input the fifth number: 13
Expected Output:
Sum of all odd values: 34
Click me to see the solution

30. Write a C program to find and print the square of each one of the even values from 1 to a specified value. Go to the editor
Test Data :
List of square of each one of the even values from 1 to a 4 :
Expected Output:
2^2 = 4
4^2 = 16
Click me to see the solution

31. Write a C program to check a given integer is positive even, negative even, positive odd or negative odd. Print even if the number is 0. Go to the editor
Test Data :
Input an integer: 13
Expected Output:
Positive Odd
Click me to see the solution

32. Write a C program to print all numbers between 1 to 100 which divided by a specified number and the remainder will be 3. Go to the editor
Test Data :
Input an integer: 25
Expected Output:
3
28
53
78
Click me to see the solution

33. Write a C program that accepts some integers from the user and find the highest value and the input position. Go to the editor
Test Data :
Input 5 integers:
5
7
15
23
45
Expected Output:
Highest value: 45
Position: 5
Click me to see the solution

34. Write a C program to print the numbers from the lowest to the highest (inclusive) and the sum of consecutive integers from a given pair of numbers. Go to the editor
Test Data :
Input a pair of numbers (for example 10,2):
Input first number of the pair: 10
Input second number of the pair: 2
Expected Output:
List of odd numbers: 3
5
7
9
Sum=24
Click me to see the solution

35. Write a C program to check if two numbers in a pair is in ascending order or descending order. Go to the editor
Test Data :
Input a pair of numbers (for example 10,2 : 2,10):
Input first number of the pair: 10
Expected Output:
Input second number of the pair: 2
The pair is in descending order!
Click me to see the solution

36. Write a C program to read a password until it is correct. For wrong password print "Incorrect password" and for correct password print "Correct password" and quit the program. The correct password is 1234. Go to the editor
Test Data :
Input the password: 1234
Expected Output:
Correct password
Click me to see the solution

37. Write a C program to read the coordinates(x, y) (in Cartesian system) and find the quadrant to which it belongs (Quadrant -I, Quadrant -II, Quadrant -III, Quadrant -IV). Go to the editor
Note: A Cartesian coordinate system is a coordinate system that specifies each point uniquely in a plane by a pair of numerical coordinates.
These are often numbered from 1st to 4th and denoted by Roman numerals: I (where the signs of the (x,y) coordinates are I(+,+), II (−,+), III (−,−), and IV (+,−).
Test Data :
Input the Coordinate(x,y):
x: 25
y: 15
Expected Output:
Quadrant-I(+,+)
Click me to see the solution

38. Write a program that reads two numbers and divide the first number by second number. If the division not possible print "Division not possible". Go to the editor
Test Data :
Input two numbers:
x: 25
y: 5
Expected Output: 5.0
Click me to see the solution

39. Write a C program to calculate the sum of all number not divisible by 17 between two given integer numbers. Go to the editor
Test Data :
Input the first integer: 50 Input the second integer: 99
Expected Output:
Sum: 3521
Click me to see the solution

40. Write a C program to find all numbers which dividing it by 7 and the remainder is equal to 2 or 3 between two given integer numbers. Go to the editor
Test Data :
Input the first integer: 25
Input the second integer: 45
Expected Output:
30
31
37
38
44
Click me to see the solution

41. Write a C program to print 3 numbers in a line, starting from 1 and print n lines. Accept number of lines (n, integer) from the user. Go to the editor
Test Data :
Input number of lines: 5
Expected Output:
1 2 3
4 5 6
7 8 9
10 11 12
13 14 15
Click me to see the solution

42. Write a C program to print a number, it’s square and cube in a line, starting from 1 and print n lines. Accept number of lines (n, integer) from the user. Go to the editor
Test Data :
Input number of lines: 5
Expected Output:
1 1 1
2 4 8
3 9 27
4 16 64
5 25 125
Click me to see the solution

43. Write a C program that reads two integers p and q, print p number of lines in a sequence of 1 to b in a line. Go to the editor
Test Data :
Input number of lines: 5
Number of characters in a line: 6
Expected Output:
1 2 3 4 5 6
7 8 9 10 11 12
13 14 15 16 17 18
19 20 21 22 23 24
25 26 27 28 29 30
Click me to see the solution

44. Write a C program to calculate the average marks of mathematics of some students. Input 0 (excluding to calculate the average) or negative value to terminate the input process. Go to the editor
Test Data :
Input Mathematics marks (0 to terminate): 10
15
20
25
0
Expected Output:
Average marks in Mathematics: 17.50
Click me to see the solution

45. Write a C program to calculate the value of S where S = 1 + 1/2 + 1/3 + … + 1/50. Go to the editor
Expected Output:
Value of S: 4.50
Click me to see the solution

46. Write a C program to calculate the value of S where S = 1 + 3/2 + 5/4 + 7/8. Go to the editor
Expected Output:
Value of series: 4.62
Click me to see the solution

47. Write a C program that reads an integer and find all its divisor. Go to the editor
Test Data:
Input an integer: 45
Expected Output:
All the divisor of 45 are:
1
3
5
9
15
45
Click me to see the solution

48. Write a C program to read and print the elements of an array of length 7, before print replace every negative number, zero with 100. Go to the editor
Test Data:
Input the 5 members of the array:
25
45
35
65
15

Expected Output:
Array values are:
n[0] = 25
n[1] = 45
n[2] = 35
n[3] = 65
n[4] = 15
Click me to see the solution

49. Write a C program to read and print the elements of an array of length 7, before print, put the triple of the previous position starting from the second position of the array. Go to the editor
For example, if the first number is 2, the array numbers must be 2, 6, 18, 54 and 162
Test Data:
Input the first number of the array: 5
Expected Output:
n[0] = 5
n[1] = 15
n[2] = 45
n[3] = 135
n[4] = 405
Click me to see the solution

50. Write a C program to read an array of length 5 and print the position and value of the array elements of value less than 5. Go to the editor
Test Data:
Input the 5 members of the array:
15
25
4
35
40
Expected Output:
A[2] = 4.0
Click me to see the solution

51. Write a C program to read an array of length 6, change the first element by the last, the second element by the fifth and the third element by the fourth. Print the elements of the modified array. Go to the editor
Test Data:
Input the 5 members of the array:
15
20
25
30
35

Expected Output:
array_n[0] = 35
array_n[1] = 30
array_n[2] = 25
array_n[3] = 20
array_n[4] = 15
Click me to see the solution

52. Write a C program to read an array of length 6 and find the smallest element and its position. Go to the editor
Test Data:
Input the length of the array: 5 Input the array elements:
25
35
20
14
45
Expected Output:
Smallest Value: 14
Position of the element: 3
Click me to see the solution

53. Write a C program that accepts principle, rate of interest, time and compute the simple interest. Go to the editor
Test Data:
Input Data: p = 10000, r = 10% , t = 12 year
Expected Output:
Input principle, Rate of interest & time to find simple interest:
Simple interest = 12000
Click me to see the solution

54. Write a C program that accepts a distance in centimeters and prints the corresponding value in inches. Go to the editor
Test Data:
Input Data: 500cms
Input the distance in cm:
Distance of 500.00 cms is = 196.85 inches
Click me to see the solution

55. Write a C program that swaps two numbers without using third variable. Go to the editor
Input value for x & y:
Before swapping the value of x & y: 5 7
After swapping the value of x & y: 7 5
Click me to see the solution

56. Write a C program to shift given data by two bits to the left. Go to the editor
Input value : 2
Read the integer from keyboard-
Integer value = 2
The left shifted data is = 16
Click me to see the solution

57. Write a C program to reverse and print a given number. Go to the editor
Input a number:
The original number = 234
The reverse of the said number = 432
Click me to see the solution

58. Write a C program that accepts 4 real numbers from the keyboard and print out the difference of the maximum and minimum values of these four numbers. Go to the editor
Input four numbers: 1.54 1.236 1.3625 1.002
Difference is 0.5380
Click me to see the solution

59. Write a C program to display sum of series 1 + 1/2 + 1/3 + ………. + 1/n. Go to the editor
Input any number: 1 + 1/0
Sum = 1/0
Click me to see the solution

60. Write a C program to create enumerated data type for 7 days and display their values in integer constants. Go to the editor
Sun = 0
Mon = 1
Tue = 2
Wed = 3
Thu = 4
Fri = 5
Sat = 6
Click me to see the solution

61. Write a C program that accepts a real number x and prints out the corresponding value of sin(1/x) using 4-decimal places. Go to the editor
Input value of x: .6235
Value of sin(1/x) is 0.9995
Click me to see the solution

62. Write a C program that accepts a positive integer less than 500 and prints out the sum of the digits of this number. Go to the editor
Input a positive number less than 500:
Sum of the digits of 347 is 14
Click me to see the solution

63. Write a C program that accepts a positive integer n less than 100 from the user and prints out the sum 14 + 24 + 44 + 74 + 114 + • • • + m4 , where m is less than or equal to n. Print appropriate message. Go to the editor
Input a positive number less than 100: 68
Sum of the series is 37361622
Click me to see the solution

64. Write a C program that accepts integers from the user until a zero or a negative number, display the number of positive values, the minimum value, the maximum value and the average of all numbers. Go to the editor
Input a positive integer:
Input next positive integer: 15
Input next positive integer: 25
Input next positive integer: 37
Input next positive integer: 43
Number of positive values entered is 4
Maximum value entered is 43
Minimum value entered is 15
Average value is 30.0000
Click me to see the solution

65. Write a C program that prints out the prime numbers between 1 and 200. The output should be such that each row contains a maximum of 20 prime numbers. Go to the editor
Expected output:
The prime numbers between 1 and 199 are:
2 3 5 7 11 13 17 19 23 29
31 37 41 43 47 53 59 61 67 71
73 79 83 89 97 101 103 107 109 113
127 131 137 139 149 151 157 163 167 173
179 181 191 193 197
Click me to see the solution

66. Write a C program that generates 50 random numbers between -0.5 and 0.5 and writes them in a file rand.dat. The first line of ran.dat contains the number of data and the next 50 lines contains the 50 random numbers. Go to the editor
50
-0.4215
0.2620
0.3065
-0.0485
.... 0.3980
0.1750
0.4780
-0.2915
0.0715
0.3565
Click me to see the solution

67. Write a C program to evaluate the equation y=xn when n is a non-negative integer. Go to the editor
Input the values of x and n: 256
x=256.000000; n=0;
x to power n=1.000000
Click me to see the solution

68. Write a C program to print the powers of 2 table for the power 0 to 10, both positive and negative. Go to the editor
=======================================
n 2 to power n 2 to power -n
=======================================
0 1 1.000000000000
1 2 0.500000000000
2 4 0.250000000000
3 8 0.125000000000
4 16 0.062500000000
5 32 0.031250000000
6 64 0.015625000000
7 128 0.007812500000
8 256 0.003906250000
9 512 0.001953125000
10 1024 0.000976562500
======================================
Click me to see the solution

69. Write a C program to print a binomial coefficient table. Go to the editor
Mx 0 1 2 3 4 5 6 7 8 9 10
----------------------------------------------------------
0 1
1 1 1
2 1 2 1
3 1 3 3 1
4 1 4 6 4 1
5 1 5 10 10 5 1
6 1 6 15 20 15 6 1
7 1 7 21 35 35 21 7 1
8 1 8 28 56 70 56 28 8 1
9 1 9 36 84 126 126 84 36 9 1
10 1 10 45 120 210 252 210 120 45 10 1
----------------------------------------------------------
Click me to see the solution

70. Write a C program to print the alphabet set in decimal and character form. Go to the editor
[65-A] [66-B] [67-C] [68-D] [69-E] [70-F] [71-G] [72-H] [73-I] [74-J] [75-K] [76-L] [77-M] [78-N] [79-O] [80-P] [81-Q] [82-R] [83-S] [84-T] [85-U] [86-V] [87-W] [88-X] [89-Y]
[90-Z] [97-a] [98-b] [99-c] [100-d] [101-e] [102-f] [103-g] [104-h] [105-i] [106-j] [107-k] [108-l] [109-m] [110-n] [111-o] [112-p] [113-q] [114-r] [115-s] [116-t] [117-u] [118-v]
[119-w] [120-x] [121-y] [122-z]
Click me to see the solution

71. Write a C program to copy a given string into another and count the number of characters copied. Go to the editor
Input a string
Original string: w3resource
Number of characters = 10
Click me to see the solution

72. Write a C program to remove any negative sign in front of a number. Go to the editor
Input a value (negative):
Original value = -253
Absolute value = 253
Click me to see the solution

73. Write a C programming that reads in two integers and check whether the first integer is a multiple of the second integer. Go to the editor
Sample Input: 9 3
Sample Output:
Input the first integer : Input the second integer:
9 is a multiple of 3.
Click me to see the solution

74. Write a C programming to display the integer equivalents of letters (a-z, A-Z). Go to the editor
Sample Output:
List of integer equivalents of letters (a-z, A-Z).
==================================================
97 98 99 100 101 102
103 104 105 106 107 108
109 110 111 112 113 114
115 116 117 118 119 120
121 122 32 65 66 67
68 69 70 71 72 73
74 75 76 77 78 79
80 81 82 83 84 85
86 87 88 89 90
Click me to see the solution

75. Write a C programming that accepts one seven-digit number and separates the number into its individual digits, and prints the digits separated from one another by two spaces each. Go to the editor
Sample Input: 2345678
Input a seven digit number:
Output: 2 3 4 5 6 7 8
Click me to see the solution

76. Write a C programming to calculate and prints the squares and cubes of the numbers from 0 to 20 and uses tabs to display them in a table of values. Go to the editor
Sample Output:
Number Square Cube
=========================
0 0 0
1 1 1
2 4 8
3 9 27
.....
18 324 5832
19 361 6859
20 400 8000
Click me to see the solution

77. Write a C programming that accepts principal amount, rate of interest and days for a loan and calculate the simple interest for the loan, using the following formula. Go to the editor
interest = principal * rate * days / 365.
Sample Input:
10000
.1
365
0
Sample Output:
Input loan amount (0 to quit): Input interest rate: Input term of the loan in days: The interest amount is $1000.00
Input loan principal_amt (0 to quit):
Click me to see the solution

78. Write a C programming to demonstrates the difference between predecrementing and postdecrementing using the decrement operator --. Go to the editor
Sample Output:
Predecrementing:
x = 10
x-- = 10
x = 9
Click me to see the solution

79. Write a C programming using looping to produce the following table of values. Go to the editor
Sample Output:

x       x+2     x+4     x+6
--------------------------------
1       3       5       7
4       6       8       10
7       9       11      13
10      12      14      16
13      15      17      19
Click me to see the solution

80. Write a C programming that reads the side (side sizes between 1 and 10 ) of a square and prints square using hash (#) character. Go to the editor
Sample Input: 10
Sample Output:

Input the size of the square: 
 # # # # # # # # # #
 # # # # # # # # # #
 # # # # # # # # # #
 # # # # # # # # # #
 # # # # # # # # # #
 # # # # # # # # # #
 # # # # # # # # # #
 # # # # # # # # # #
 # # # # # # # # # #
 # # # # # # # # # #
Click me to see the solution

81. Write a C programming that reads the side (side sizes between 1 and 10 ) of a square and prints a hollow square using hash (#) character. Go to the editor
Sample Input: 10
Sample Output:

Input the size of the square: 
##########
#        #
#        #
#        #
#        #
#        #
#        #
#        #
#        #
##########
Click me to see the solution

82. Write a C programming Write a program that reads in a five-digit integer and determines whether or not it’s a palindrome. Go to the editor
Sample Input: 33333
Sample Output:

Input a five-digit number: 33333 is a palindrome.
Click me to see the solution

83. Write a C programming which reads an integer (7 digits or fewer) and count number of 3s in the given number. Go to the editor
Sample Input: 538453
Sample Output:

Input a number: The number of threes in the said number is 2
Click me to see the solution

84. Write a C programming to calculate and print the average of some integers. Accept all the values preceding 888. Go to the editor
Sample Input:12
15
24
888
Sample Output:

Input each number on a separate line (888 to exit):

The average value of the said numbers is 17.000000
Click me to see the solution

85. Write a C programming to print a table of all the Roman numeral equivalents of the decimal numbers in the range 1 to 50. Go to the editor
Sample Output:

Decimal  Roman
number   numeral
-------------------
1        I
2        II
3        III
4        IV
.....
98        LXXXXVIII
99        LXXXXIX
100        C
Click me to see the solution

Practice online

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.