Sunteți pe pagina 1din 5


Kovaipudur, Coimbatore

C Programming Lab

Sample Questions

(Questions –1 to 6 – use only printf(), scanf() and escape sequences)

1. Write a program to display a word “MOON” overlap the word with a letter ‘N’ in such a way that it displays
2.Write a program to find out ASCII equivalent of a character ‘y’, ‘d’, ‘9’, ‘$’.
3. Write a program that contains a printf statement containing the string “Have a nice day”, the output
displayed must be “Always have a good day”.
4. Write a program to print the character \n\t\b\m\y\A\R.
5. Write a program to display a word “Good morning”. Overlap the word with the word “evening” in such a
way that it displays “Good evening”.
6. Write a program to print the line “C language is simple. It has many features. Pointers increase the speed of
execution of a program”. In consecutive lines one below the other. Each and every word should have a tap
space between them.

(Questions –7 to 12 – use the above and operators only)

7. Write a program to print 0 if the 2 numbers entered by the user are unequal.
8. Write a program to print 1 if the 2 alphabets entered by the user are equal.
9. Write a program to mask lower 4 bits of an integer value and right shift the resultant 4 times.
10. Write a program to encrypt the data given by the user. The data entered by the user must be a character.
Complement the data and display the encrypted data in character format.
11.Write a program to display the bits that are high in a variable.
12.Write a program to find out whether the 5th bit in a variable is high or low.

(Questions – 13 to 17 – use the above and control structures, pre-processor directives only)

13. Write a symbolic constant or a macro definition for each of the following situations. Do not include
arguments unless the problem asks you to do so.
a) PI – 3.1415927
b) AREA – which will calculate the area of a circle in terms of its radius. Use the value of
constant PI defined earlier.
14. Write a multiline macro called INTEREST, which will evaluate the compound interest given by
F=P(1+i)^n, where F is the future amount of money that will accumulate after n years, P-Principal
amount, i=0.01r, and r is the annual interest rate, expressed as percentage. Evaluate i on one line of the
macro, and evaluate F on a separate line. Assume all of the symbols represent floating point quantities.
15. Write one or more pre-processor directives. If the symbolic constant SIZE has the same value as the
symbolic constant WIDE, define the symbolic constant WIDTH to have a value of 132 otherwise
16. Write one or more pre-processor directives. Use the “stringizing” operator to define a macro called
error(text) that will display text as a string.
17. Write one or more pre-processor directives. Use the “token-pasting” operator to define a macro called
error(i) that will print the value of the string variable error(e.g., error3).

(Questions – 18 to 19 – use the above and arrays, storage classes only)

18.Write a program to read 2D positive integer array. Display the negative representations of that array.
19. Write a program to sort n number of student record based on the total marks.
20. Write a program to read 2D character array which is declared as global. Convert all of the
elements to lower case in a separate function and then display the converted array.
21. Write a program to show the difference between global and auto variables.
22. Write a program to show the difference between extern and macro variables.
23. Write a program to show the difference between macro and global variables.
24. Write a program to generate a warning “unreachable code”.
Assignments (Sample Questions)

1. Home mortgage costs are determined in such a manner that the borrower pays the same amount of
money to the lending institution each month throughout the life of the mortgage. The fraction of the
total monthly payment that is required as an interest payment on the outstanding balance of the loan
varies, however, from month to month. Early in the life of the mortgage most of the monthly payment
is required to pay interest, and only a small fraction of the total monthly payment is applied toward
reducing the amount of the loan. Gradually, the outstanding balance becomes smaller, which causes
the monthly interest payment to decrease, and the amount available to reduce the outstanding balance
therefore increases. Hence the balance of the loan is reduced at an accelerated rate.
Typically, prospective home buyers know how much money they must borrow and the time required
for repayment. They then ask a lending institution how much their monthly payment will be at the
prevailing interest rate. They should also be concerned with how much of each monthly payment is
charged to interest, how much money they still owe the lending institution at the end of each month.
Write a c program that can be used by a lending institution to provide a potential customer with this
information. Assume that the amount of the monthly payment is calculated as
A= iP (1+i) n/ [(1+i) n-1]
Where A=monthly payment, dollars
P=total amount of the loan, dollars
i=monthly interest rate, expressed as a decimal (e.g., ½ per cent would be written 0.005)
n =total number of monthly payments
The monthly interest payment can then be calculated from the formula
Where I=monthly interest payment, dollars
B=current outstanding balance, dollars
The current outstanding balance is simply equal to the original amount of the loan, less the sum of the
previous payments toward principal. The monthly payment toward principal (i.e., the amount which is
used to reduce the outstanding balance) is simply
Where T=monthly payment toward principal.
Use the program to calculate the cost of a 25-year, $50,000 mortgage at an annual interest rate of 8 per
cent. Then repeat the calculation for an annual interest rate of 8.5 per cent. How significant is the
additional 0.5 per cent in the interest rate over the entire life of the mortgage.

2. Consider the following foreign currencies and their equivalents to one U.S. dollar.
British pound: 0.65 pound per U.S. dollar
Canadian dollar: 1.4 dollars per U.S. dollar
Dutch guilder: 1.7 guilders per U.S. dollar
French franc: 5.3 francs per U.S. dollar
German mark: 1.5 marks per U.S. dollar
Italian lira: 1570 lira per U.S. dollar
Japanese yen: 98 yen per U.S. dollar
Mexican peso: 3.4 pesos per U.S. dollar
Swiss franc: 1.3 francs per U.S. dollar
Write an interactive, menu-driven program that will accept two different currencies and return the
value of the second currency per one unit of the first currency. (For example, if the two currencies are
Japanese yen and Mexican pesos, the program will return the number of Mexican pesos equivalent to
one Japanese yen.) Use the data given above to carry out the conversions. Design the program so that
it executes repeatedly, until a stopping condition is selected from the menu.
3. A Piglatin Generator: Piglatin is an encoded form of English that is often used by children as a
game. A piglatin word is formed from an English word by transposing the first sound (usually the first
letter) to the end of the word, and then adding the letter “a”. Thus, the word “dog” becomes “ogda”,
“computer” becomes “omputerca”, “piglatin” becomes “iglatinpa” and so on. Let us write a C
program that will accept a line of English text and then print out the corresponding text in piglatin. We
will assume that each textual message can be typed on one 80-column line, with a single blank space
between successive words. (Actually, we will require that the piglatin message not exceed 80
characters. Therefore the original message must be somewhat less than 80 characters, since the
corresponding piglatin message will be lengthened by the addition of the letter “a” after each word.)
For simplicity, we will transpose only the first letter (not the first sound) of each word. Also, we will
ignore any special consideration that might be given to capital letters and to punctuation marks. We
will use two characters arrays in this program. One array will contain the original line of English text,
and the other will contain the translated piglatin.

4. The following problems are concerned with games of chance (gambling games). Each problem
requires the use of random numbers. The program written for each problem will require the use of an
array. The program should be interactive and they should be modularized.

(a) Write a C program that will simulate a game of blackjack between two players. The computer will not be a
participant in this game, but will specify deal the cards to each player and then provide each player with one or
more “hits” (additional cards) when requested.

The cards are dealt in order, first one card to each player, and then a second card to each player.
Additional hits may then be requested.

The object of the game is to obtain 21 points, or as many points as possible without exceeding 21 points,
on each hand. A player is automatically disqualified if his or her hand exceeds 21 points. Picture cards count
10 points, and an ace can count either 1 point or 11 points. Thus a player can obtain 21 points (blackjack) if he
or she is dealt an ace and either a picture card or a 10. If the player has a low score with his (her) first two
cards, he (she) may request one or more hits, as long as his (her) total score does not exceed 21.

Use random numbers to simulate dealing the cards. Be sure to include a provision that the same card is
not dealt more than once.

(b) Roulette is played with a wheel containing 38 different squares along its circumference. Two of these
squares, numbered 0 and 00, are given; 18 squares are red, and 18 are black. The red and black squares
alternate in colour, and are numbered 1 through 36 in a random order.

A small marble is spun within the wheel, which eventually comes to rest within a groove beneath one of
the squares. The game is played by betting on the outcome of each spin, in any one of the following ways.

(i) By selecting a single red or black square, at 35-to-1 odds. Thus, if a player were to bet $1.00 and
win, he or she would receive a total of $36.00: the original $1.00, plus an additional $35.00.
(ii) By selecting a colour, either red or black, at 1-to-1 odds. Thus if a player chose red on a $1.00 bet,
he or she would receive $2.00 if the marble came to rest beneath any red square.
(iii) By selecting either the odd or the even numbers (excluding 0 and 00), at 1-to-1 odds.
(iv) By selecting either the low 18 or the high 18 numbers at 1-to-1 odds.
The player will automatically lose if the marble comes to rest beneath one of the green squares (0 or 00).
Write an interactive C program that will simulate a roulette game. Allow the players to select
whatever type of bets they wish by choosing from a menu. Then print the outcome of each game followed by
an appropriate message indicating whether each player has won or lost.

5. Write an interactive C program that will simulate a game of BINGO, Print each letter-number
combinations as it is drawn (randomly generated). Be sure that no combination is drawn more than once.
Remember that each of the letters B-I-N-G-O corresponds to a certain range of numbers, as indicated below.
B: 1-15
I: 16-30
N: 31-45
G: 46-60
O: 61-75
Each player will have a card with five columns, labelled B-I-N-G-O. Each column will contain five numbers,
within the ranges indicated above. No two players will have the same card. The first player to have one entire
line of numbers drawn (vertically, horizontally or diagonally) wins.
Note: the centre position of each card is sometimes covered before the game begins (a “free” call). Also,
the game is sometimes played such that a player must have all of the numbers on his or her card drawn before
he (she) can win.

6. Write an interactive C program that will encode or decode a line of text. To encode a line of text,
proceed as follows.
1. Convert each character, including blank spaces, to its ASCII equivalent.
2. Generate a positive random integer. Add this integer to the ASCII equivalent of each character. The same
random integer will be used for the entire line of text.
3. Suppose that N1 represents the lowest permissible value in the ASCII code, and N2 represents the highest
permissible value. If the number obtained in step 2 above (i.e., the original ASCII equivalent plus the random
integer) exceeds N2, then subtract the largest possible multiple of N2 from this number, and add the remainder
to N1. Hence the encoded number will always fall between N1 and N2, and will therefore always represent
some ASCII character.
4. Display the characters that correspond to the encoded ASCII values.
The procedure is reversed when decoding a line of text. Be certain, however, that the same random number is
used in decoding as was used in encoding.

7. There are 3 executive each of whom should be invited for a lunch. The convenient dates for the
executives are given below:
Executive - 1: 12 22 28
Executive - 2: 14 22 31
Executive - 3: 6 15 22
Write a program to print the date on which all the executives are available.

Write a C program that will accept the following information for each team in a

baseball or a football league.

1. Team name, including the city (e.g., Pittsburgh Steelers)

2. Number of wins

3. Number of losses

For a baseball team, add the following information:

4. Number of hits

5. Number of runs

6. Number of errors

7. Number of extra-inning games

Similarly, add the following information for a football team:

4. Number of ties

5. Number of touchdowns

6. Number of field goals

7. Number of turnovers

8. Total yards gained (season total)

9. Total yards given up to opponents

Enter this information for all of the teams in the league. Then reorder and print the list of teams according to
their-win-lose records. Store the information in an array of structures, where each array element (i.e., each
structure) contains the information for a single team. Make use of a union to represent the variable information
(either football or baseball) that is included as a part of the structure. This union should itself contain two
structures, one for baseball-related statistics and the other for football-related statistics.

Test the program using a current set of league statistics. (Ideally, the program should be tested using both
baseball and football statistics.)

8. Write a C program that will allow you to enter and maintain a computerized version of your family tree.
Begin by specifying the number of generations (i.e., the number of levels within the tree). Then enter the
names and nationalities in a hierarchical fashion, beginning with your own name and nationality. Include
capabilities for modifying the tree and for adding new names (new nodes) to the tree. Also, include a provision
for displaying the entire tree automatically after each update. Test the program, including at least three
generations if possible (you, your parents and your grandparents). Obviously, the tree becomes more
interesting as the number of generations increases.