C Programs List
C Programs List
1. Ramesh’s basic salary is input through the keyboard. His dearness allowance is 40% of basic salary, and
house rent allowance is 20% of basic salary. Write a program to calculate his gross salary.
Hint: Gross Salary = Basic Salary + DA + HRA
2. The distance in kilometers between two cities is input through the keyboard. Write a program to convert
and print distance in meters, feet, inches and centimeters.
Hint: 1 Km = 3280.84 ft
3. If the marks obtained by a student in three different subjects are input through the keyboard, find out the
aggregate marks and percentage marks obtained by the student. Assume that, maximum marks that can
be obtained by a student in each subject is 100.
Hint: Use subjects as Hindi, English and Mathematics
4. Temperature of a city in Fahrenheit degrees is input through the keyboard. Write a program to convert
this temperature into Centigrade degrees.
Hint: Centigrade = (Fahrenheit – 32) *5/9
5. The length & breadth of a rectangle and radius of a circle are input through the keyboard. Write a program
to calculate the area & perimeter of the rectangle, and the area & circumference of the circle.
6. Two numbers are input through the keyboard into two locations num1 and num2. Write a program to
interchange the contents of num1 and num2.
7. If a five-digit number is input through the keyboard, write a program to calculate the sum of its digits.
Test Case: Num = 12341 Sum = 11
8. If a five-digit number is input through the keyboard, write a program to reverse the number.
Test Case: Num = 12341 Reverse = 14321
9. If a four-digit number is input through the keyboard, write a program to obtain the sum of the first and
last digit of this number.
10. A cashier has currency notes of denominations 10, 50 and 100. If the amount to be withdrawn is input
through the keyboard, find the total number of currency notes of each denomination the cashier will have
to give to the withdrawer.
Test Case: Amount = Rs 570 Hundred_Note = 5 Fifity_Note = 1 Ten_Note = 2
CHAPTER 2 (The Decision Control Structure)
11. Find the absolute value of a number entered through the keyboard.
Hint: Absolute value of any negative number id is positive value for example absolute(-5) = 5 and
absolute(5) =5.
12. If cost price and selling price of an item is input through the keyboard, write a program to determine
whether the seller has made profit or incurred loss. Also determine how much profit he made or loss he
incurred.
13. Any integer is input through the keyboard. Write a program to find out whether it is an odd number or
even number.
14. Three angles of the triangle are entered through the keyboard, write a program to check whether a
triangle is valid or not. A triangle is valid if the sum of all the three angles is equal to 180 degrees.
15. A four-digit number is entered through the keyboard. Write a program to obtain the reversed number
and to determine whether the original and reversed numbers are equal or not. Such a number is also
called palindrome number for example Reverse of 1221 is 1221.
16. If the ages of Ram, Shyam and Ajay are input through the keyboard, write a program to determine the
youngest of the three.
17. Any year is input through the keyboard. Write a program to determine whether the year is a leap year or
not.
18. Any character is entered through the keyboard, write a program to determine whether the character
entered is a capital letter, a small case letter, a digit or a special symbol.
The following table shows the range of ASCII values for various characters.
Characters ASCII Values
A–Z 65 – 90
a –z 97 – 122
0–9 48 – 57
Special symbols 0 – 47, 58 – 64, 91 – 96, 123 - 127
19. Any year is entered through the keyboard, write a program to determine whether the year is leap or not.
Use the logical operators && and || operator.
20. Write a program which reads a character from keyboard and reports whether it is a vowel, consonant or
any other non-alphabet. Use the logical operators && and || operator.
21. A library charges per day fine for every book returned late. For first 5 days the fine is 50 paise per day, for
6-10 days fine is one rupee per day and above 10 days and till 30 days fine is 5 rupees per day. If you
return the book after 30 days your membership will be cancelled. Write a program to accept the number
of days the member is late to return the book and display the fine or appropriate message.
Hint : late_days = 4 fine = Rs 2
late_days = 7 fine = Rs 4.50
late_days = 12 fine = Rs 17.50
22. If the three sides of a triangle are entered through the keyboard, write a program to check whether the
triangle is isosceles, equilateral, scalene or right angled triangle.
Isosceles : Has two sides equal and third is different.
Equilateral : Has all three sides equal.
Scalene : None of the tree sides are equal.
Right Angled : Pythagoras theorem (H2 = B2 + P2) is true.
23. In a company, worker efficiency is determined on the basis of the time required for a worker to complete
a particular job. If the time taken by the worker is between 2-10 hours, then the worker is said to be highly
efficient. If the time required by the worker is between 10-20 hours, then the worker ordered to improve
speed. If the time taken is between 20-30 hours, the worker is given training to improve his speed, and if
the time taken by the worker is more than 30 hours, then the worker has to leave the company. If the
time taken by the worker is input through the keyboard, write a program to print the efficiency of the
worker.
24. The policy followed by a company to process customer orders is given by the following rules:
a) If a customer order quantity is less than or equal to that in stock and his credit rating is OK. Supply
his requirement.
b) If his credit rating is not OK do not supply. Send him an intimation that your credit rating in not
good.
c) If his credit is OK but the item in stock is less than his order, supply what is in stock. Intimate to
him the date on which the balance will be shipped.
Hint: Credit Rating is how honest the customer is while paying his credit. Use 1 for OK credit and
0 for Not OK credit
26. Write a program to fill the entire screen with a smiling face. The smiling face has an ASCII value 1.
27. Write a program to print all the ASCII values and their equivalent characters using a while loop. The
ASCII values vary from 0 to 255.
28. Write a program to find the factorial value of any number entered through the keyboard.
31. Two numbers are entered through the keyboard. Write a program to find the value of one number
raised to the power of another.
32. Write a program to find if the entered 3 digit number is Armstrong or not. Armstrong number is a
number in which sum of cube of digits is equal to number, Ex: 153 = (1 * 1 * 1) + (5 * 5 * 5) + (3 * 3 * 3)
33. Write a program to print out all Armstrong numbers between 1 and entered number. For example if
entered number is 500 then the program should find all Armstrong numbers between 1 and 500.
35. Write a program to print all prime numbers from 2 the number entered by user. For example if entered
number is 100 then the program should find all prime numbers between 1 and 100.
36. Write a program to print first N prime numbers. For example if entered number is 5 then the program
should print first 5 prime numbers as 2, 3, 5, 7, 11.
39. Write a program to enter the numbers till the user wants and at the end it should display the count of
positive, negative and zero’s entered.
40. Write a program to find the range of a set of numbers entered by user. Range is the difference between
the smallest and largest number in the list. The user should be able to enter multiple numbers as per
choice.
41. Write a c program to find out prime factor of given number. For example prime factors of 12 = 2 x 2 x 3
42. Write a program to generate all combinations of 1, 2 and 3 using for loop.
43. Write a program to print Fibonacci series till any entered number. For example the number entered id
50 then the series should be 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, if the entered number is 200 then the series
should be 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144.
44. Write a program to print first N terms of Fibonacci series. For example the number entered id 7 then the
series should be 0, 1, 1, 2, 3, 5, 8, if the entered number is 12 then the series should be 0, 1, 1, 2, 3, 5, 8,
13, 21, 34, 55, 89.
45. Write a program to print the multiplication table of the number entered by the user. For example if the
entered number is 29 The table should get displayed in the following from:
29 * 1 = 29
29 * 2 = 58
.
.
29 * 10 = 290
46. Write a program to enter the size of pattern and produce the following pattern for example if entered
size is 5.
*
* *
* * *
* * * *
* * * * *
47. Write a program to enter the size of pattern and produce the following pattern for example if entered
size is 5.
*
* *
* * *
* * * *
* * * * *
48. Write a program to enter the size of pattern and produce the following pattern for example if entered
size is 5.
*
* * *
* * * * *
* * * * * * *
* * * * * * * * *
49. Write a program to enter the size of pattern and produce the following pattern for example if entered
size is 5.
* * * * * * * * *
* * * * * * *
* * * * *
* * *
*
50. Write a program to enter the size of pattern and produce the following pattern for example if entered
size is 5.
1
1 2 1
1 2 3 2 1
1 2 3 4 3 2 1
51. Write a program to enter the size of pattern and produce the following pattern for example if entered
size is 5.
1
2 1
1 2 3
4 3 2 1
1 2 3 4 5
52. Write a program to enter the size of pattern and produce the following pattern for example if entered
size is 5.
1
2 3 2
3 4 5 4 3
4 5 6 7 6 5 4
5 6 7 8 9 8 7 6 5
53. Write a program to enter the size of pattern and produce the following pattern for example if entered
size is 5.
1
0 1
1 0 1
0 1 0 1
1 0 1 0 1
54. Write a program to enter the size of pattern and produce the following pattern using only two for loops
for example if entered size is 5.
* * * * 1
* * * 1 2
* * 1 2 3
* 1 2 3 4
1 2 3 4 5
55. Write a program to draw a Fibonacci triangle as below for example if entered size is 7 it should produce
following output.
01
011
0112
01123
011235
0112358
0 1 1 2 3 5 8 13
56. Write a C program to display the following rhombus symbol structure, for example if entered size is 4 it
should produce following output.
*
***
*****
*******
*****
***
*
57. Write a C program to display the following number rhombus structure, for example if entered size is 4 it
should produce following output.
1
212
32123
4321234
32123
212
1
58. Write a C program to display the following number rhombus structure, for example if entered size is 4 it
should produce following output.
1
121
12321
1234321
12321
121
1
59. Write a C program to display the following character rhombus structure, for example if entered size is 4
it should produce following output.
A
ABA
ABCBA
ABCDCBA
ABCBA
ABA
A
63. Write a program to find the grace marks for a student using switch. The user should enter the class
obtained by the student and the number of subjects he has failed in. Use the following logic:
- If the student gets first class and the number of subjects he failed in is greater than 3, then he does not
get any grace. If the number of subjects he failed in is less than or equal to 3, then the grace is of 5
marks per subject.
- If the student gets second class and the number of subjects he failed in is greater than 2, then he does
not get any grace. If the number of subjects he failed in is less than or equal to 2, then the grace is of 4
marks per subject.
- If the student gets third class and the number of subjects he failed in is greater than 1, then he does
not get any grace. If the number of subjects he failed in is equal to 1, then the grace is of 5 marks per
subject.
66. Any year is entered through the keyboard. Write a function to determine whether the year is a leap year
or not. Call this function from main() and print the results in main().
67. A positive integer is entered through the keyboard, write a function to calculate sum of digits of the
number.
68. Write a function that receives 5 integers and returns the sum, average and standard deviation of these
numbers. Call this function from main() and print the results in main().
Hint: Use call by reference to return multiple values
69. Write a function that receives marks obtained by a student in 3 subjects and returns the average and
percentage of these marks. Call this function from main( ) and print the results in main( ).
Hint: Use call by reference to return multiple values
70. A positive integer is entered through the keyboard, write a function to calculate sum of digits of the
number using recursion.
71. A positive integer is entered though the keyboard, write a program to obtain factors of the number.
Modify the function to obtain the prime factors recursively.
72. Write a recursive function to obtain the first 25 numbers of a Fibonacci sequence. In a Fibonacci
sequence the sum of two successive terms gives the third term. Following are the first few terms of the
Fibonacci sequence:
1 1 2 3 5 8 13 21 23 55 89…..
73. A positive integer is entered through the keyboard, write a function to find the binary equivalent of this
number using recursion.
74. Write a recursive function to obtain the running sum of first 25 natural numbers.
75. Given three variables x, y, z, write a function to circularly shift their values to right. In other words if x =
5, y = 8, z = 10 after circular shift y = 5, z = 8, x = 10 after circular shift y = 5, z = 8, and x = 10. Call the
function with variables a, b, c to circularly shift values.
76. Write a function to compute the greatest common divisor given by Euclid’s algorithm, exemplified for j =
1980, K = 1617 as follows:-
1980 / 1617 = 1 1980 – 1 * 1617 = 363
1617 / 363 = 4 1617 – 4 * 363 = 165
363 / 165 = 2 363 – 2 * 165 = 33
5 / 33 = 5 165 – 5 * 33 = 0
Thus, the greatest common divisor is 33.
CHAPTER 8 (Arrays)
81. Write a program to find out largest element of an array.
82. Write a program to find out second largest element of an unsorted array.
90. Twenty-five number are entered from the keyboard into an array. Write a program to find out how
many of them are positive, how many are negative, how many are odd and how many are even.
91. Implement the following procedure to generate prime numbers from 1 to 100 into a program. This
procedure is called sieve of Eratosthenes.
Step 1: Fill an array num[100] with numbers from 1 to 100
Step 2: Starting with the second entry in the array, set all its multiples to zero.
Step 3: Proceed to the next non-zero element and set all its multiples to zero.
Step 4: Repeat step 3 till you have set up the multiples of all the non-zero elements to zero
Step 5: At the conclusion of step 4, all the non-zero entries left in the array would be prime numbers, so
print out these numbers.
92. Write a program to copy the contents of one array into another in the reverse order using pointers.
94. How will you initialize a three-dimensional array threed[3][2][3]? How will you refer the first and last
element in this array?
95. Write a program to pick up the largest number from any 3 row by 3 column matrix.
96. Write a program to obtain transpose of a 3 x 3 matrix. The transpose of a matrix is obtained by
exchanging the elements of each row with the elements of the corresponding column.
101. Write a program that extracts part of the given string from the specified position. For example, if the
sting is "Working with strings is fun", then if from position 4, 4 characters are to be extracted then the
program should return string as "king". Moreover, if the position from where the string is to be
extracted is given and the number of characters to be extracted is 0 then the program should extract
entire string from the specified position.
102. Write a program that converts all lowercase characters in a given string to its equivalent uppercase
character.
103. Write a program that converts a string like "124" to an integer 124.
104. Write a program that replaces two or more consecutive blanks in a string by a single blank. For
example, if the input is
Grim return to the planet of apes!!
The output should be
Grim return to the planet of apes!!
105. Write a program that uses an array of pointers to strings str[ ]. Receive two strings str1 and str2 and
check if str1 is embedded in any of the strings in str[ ]. If str1 is found, then replace it with str2.
char *str[ ] = {
“We will teach you how to…”,
“Move a mountain”,
“Level a building”,
“Erase the past”,
“Make a million”,
“…..all through C!”
};
For example if str1 contains “mountain” and str2 contains “car”, then the second string in str should
get changed to “Move a car”,
106. Write a program to sort a set of names stored in an array in alphabetical order.
107. Write a program to reverse the strings stored in the following array of pointers to strings :
char *s[ ] = {
“To err is human….”,
“But to really mess things up…..”,
“One needs to know c!!”
};
108. Develop a program that receives the month and year from the keyboard as integers and prints the
calendar in the following format.
March 2006
Mon Tue Wed Thu Fri Sat Sun
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 31
Note that according to the Gregorian calendar 01/01/1900 was Monday. With this as the base the
calendar should be generated.
109. Modify the above program suitably so that once the calendar for a particular month and year has been
displayed on the screen, then using arrow keys the user must be able to change the calendar in the
following manner:
110. Write a program to delete all vowels from a sentence. Assume that the sentence is not more than 80
characters long.
111. Write a program that will read a line and delete from it all occurrences of the word ‘the’.
112. Write a program that takes a set of names of individuals and abbreviates the first, middle and other
names except the last name by their first letter.
113. Write a program to count the number of occurrences of any two vowels in succession in a line of text.
For example, in the sentence
“Please read this application and give me gratuity”
such occurrences are ea, ea, ui.
CHAPTER 10 (Structures)
114. Create a structure to specify data on student given below:
Roll number, Name, Department, Course, Year of joining
Assume that there are not more than 450 students in the college.
(a) Write a function to print names of all students who joined in a particular year.
(b) Write a function to print the data of a student whose roll number is given.
115. Create a structure to specify data of customers in a bank. The data to be stored is: Account number,
Name, Balance in account. Assume maximum of 200 customers in the bank.
(a) Write a function to print the Account number and name of each customer with balance below Rs.
100.
(b) If a customer request for withdrawal or deposit, it is given in the form:
Acct. no, amount, code (1 for deposit, 0 for withdrawal)
Write a function to complete the transaction and give a message, “The balance is insufficient for the
specified withdrawal” in case amount in account is less than withdrawal amount.
116. Write a menu driven program that depicts the working of a library. The menu options should be:
1. Add book information
2. Display book information
3. List all books of given author
4. List the title of specified book
5. List the count of books in the library
6. List the books in the order of accession number
7. Exit
Create a structure called library to hold accession number, title of the book, author name, price of the
book, and flag indicating whether book is issued or not.
117. Write a program that compares two given dates. To store date use structure say date that contains
three members namely date, month and year. If the dates are equal then display message as "Equal"
otherwise "Unequal".
118. Linked list is a very common data structure often used to store similar data in memory. While the
elements of an array occupy contiguous memory locations, those of a linked list are not constrained to
be stored in adjacent location. The individual elements are stored “somewhere” in memory, rather
like a family dispersed, but still bound together. The order of the elements is maintained by explicit
links between them. Thus, a linked list is a collection of elements called nodes, each of which stores
two item of information—an element of the list, and a link, i.e., a pointer or an address that indicates
explicitly the location of the node containing the successor of this list element. Write a program to
build a linked list by adding new nodes at the beginning, at the end or in the middle of the linked list.
Also write a function display( ) which display all the nodes present in the linked list.
119. A stack is a data structure in which addition of new element or deletion of existing element always
takes place at the same end. This end is often known as ‘top’ of stack. This situation can be compared
to a stack of plates in a cafeteria where every new plate taken off the stack is also from the ‘top’ of the
stack. There are several application where stack can be put to use. For example, recursion, keeping
track of function calls, evaluation of expressions, etc. Write a program to implement a stack using a
linked list.
120. Unlike a stack, in a queue the addition of new element takes place at the end (called ‘rear’ of queue)
whereas deletion takes place at the other end (called ‘front’ of queue). Write a program to implement
a queue using a linked list.
124. Write a program to find the size of a text file without traversing it character by character.
125. Write a program to add the contents of one file at the end of another.
126. Suppose a file contains student’s records with each record containing name and age of a student.
Write a program to read these records and display them in sorted order by name.
127. Write a program to copy one file to another. While doing so replace all lowercase characters to their
equivalent uppercase characters.
128. Write a program that merges lines alternately from two files and writes the results to new file. If one
file has less number of lines than the other, the remaining lines from the larger file should be simply
copied into the target file.
129. In the file ‘CUSTOMER.DAT’ there are 100 records with the following structure:
struct customer
{
int accno ;
char name[30] ;
float balance ;
};
In another file ‘TRANSACTIONS.DAT’ there are several records with the following structure:
struct trans
{
int accno ,
char trans_type ;
float amount ;
};
The parameter trans_type contains D/W indicating deposit or withdrawal of amount. Write a program
to update ‘CUSTOMER.DAT’ file, i.e. if the trans_type is ‘D’ then update the balance of
‘CUSTOMER.DAT’ by adding amount to balance for the corresponding accno. Similarly, if trans_type is
‘W’ then subtract the amount from balance. However, while subtracting the amount make sure that
the amount should not get overdrawn, i.e. at least 100 Rs. Should remain in the account.
130. There are 100 records present in a file with the following structure:
struct date
{
int d, m, y ;
};
struct employee
{
int empcode[6] ;
char empname[20] ;
struct date join_date ;
float salary ;
};
Write a program to read these records, arrange them in ascending order of join_date and write them in
to a target file.
131. Given a list of names of students in a class, write a program to store the names in a file on disk. Make
a provision to display the nth name in the list (n is data to be read) and to display all names starting
with S.
132. In a small firm employee numbers are given in serial numerical order, that is 1, 2, 3, etc.
− Create a file of employee data with following information: employee number, name, sex, gross
salary.
− If more employees join, append their data to the file.
− If an employee with serial number 25 (say) leaves, delete the record by making gross salary 0.
− If some employee’s gross salary increases, retrieve the record and update the salary.
Write a program to implement the above operations.
133. Given a text file, write a program to create another text file deleting the words “a”, “the”, “an” and
replacing each one of them with a blank space.
135. Write a program that can be used at command prompt as a calculating utility. The usage of the
program is shown below.
C:\> calc <switch> <n> <m>
Miscellaneous Programs
136. C program to print hello world without using semicolon.
137. Write a c program to add two numbers without using addition operator.
139. Write a c program to swap two numbers without using third variable.