C Assignments PDF
C Assignments PDF
Control Sequence
1. Develop a C program having following logic. If i is 20 or j is 20, display as Atleast one variable is
having 20 otherwise display Both variables are not having 20. If i is less than or equal to 40
and j is less than or equal to 40, It should display Both are less than or equal to 40 otherwise, it
should display as Both are not less than or equal to 40. Implement this using if-else statement
as well as with conditional operator.
2. Develop a C program which accepts character type data item from user. In case if user typed
A or a, it should display A for Apple
B or b, it should display B for Bat
D or d, it should display D for Dog
F or f, it should display F for Fan
Instead of the above 4 characters, if user types any other character, it should display Character
is not in the range. Implement this using if-else statement and switch statement.
3. Develop a C program which adds all numbers from 1 to N, except those which are divisible by 5.
Implement this using for loop and continue statement.
4. Develop a C program to find factorial of a number N using for loop.
5. Develop a C program to find sum of all odd numbers upto N using while loop.
6. Write a program to print ASCII values of upper case and lower case alphabets and digits (A-Z, a-z
and 0-9).
7. Write a Program to find if a given number is Armstrong number.
Hint: (153 = 1^3 + 5^3 + 3^3)
8. Write a program to find whether given number is palindrome or not.
9. Write a menu based C program to perform operations (+, - and *) on matrices.
10. Generate the following pyramid of numbers using nested loops
1
212
32123
4321234
543212345
11. Write a program to search for an element in a given list of elements. Use break statement.
12. Write a program to print all the prime numbers in the first N numbers.
13. Write a program to find the sum of digits of a given number.
14. Write a C program to generate two Relatively Prime numbers.
15. Write a program to generate Random number
Any
Prime number
Two Relatively Prime Numbers
16. Read two integers, representing a rate of pay (pence per hour) and a number of hours. Print out
the total pay, with hours up to 40 being paid at basic rate, from 40 to 60 at rate-and-a-half,
above 60 at double-rate. Print the pay as rupees to two decimal places.
Terminate the loop when a zero rate is encountered.
At the end of the loop, printout the total pay.
The code for computing the pay from the rate and hours is to be written as a function.
The recommended output format is something like:
Pay at 200 pence/hr for 38 hours is 76.00 rupees
Pay at 220 pence/hr for 48 hours is 114.40 rupees
Pay at 240 pence/hr for 68 hours is 206.40 rupees
Pay at 260 pence/hr for 48 hours is 135.20 rupees
Pay at 280 pence/hr for 68 hours is 240.80 rupees
Pay at 300 pence/hr for 48 hours is 156.00 rupees
Total pay is 928.80 rupees
Arrays
1. Write a program to read your name into a character array. Print the name along with the length
of your name and sizeof the array in which name is stored.
2. Use scanf function to read a string of characters (into character type array called text) including
alphabets, digits, blanks, tabs etc except new line character. Write a loop that will examine each
character in a character-type array and determine how many of the characters are letters, how
many are digits, how many are blanks and how many are tabs. Assume that text contains 80
characters.
3. Write a program that reads a number that says how many integer numbers are to be stored in
an array, creates an array to fit the exact size of the data and then reads in that many numbers
into the array.
Functions
1. Write a program to calculate n!/(n-r)! using functions.
2. Write a recursive function to find factorial of a number.
3. Write a function to swap contents of two variables using functions and pointer variables.
4. Write a C program with a function rotoate_Write a program to add first seven terms of the
following series:
5.
1 / 1! + 2 / 2! 3 /3! + 4 / 4! . . . right (n, b). This function rotates integer n
towards right by b positions.
6. Write a C program with a function invertponwards (n, p, b). This function inverts b bits of
integer n, that begin at position p, leaving the others unchanged.
7. Write a C program with a function tolower, which converts upper case letters to lower case. Use
conditional expression.
8. Develop a program to calculate nPr and nCr given n and r.
9. Write a function to get the transpose of a matrix.
10. Write a C program with two functions itob (n, s) and itoh (n, s). itob converts integer into binary
character representation in s. Similarly itoh converts integer into hexadecimal character
representation in s.
11. Write a C program with a function indexr(s,t), which returns the index of right most occurrence
of t in s otherwise -1.
12. Write a C program with a recursive function itoa, which converts integer into a string.
13. Write a program to add first seven terms of the following series:
1 / 1! + 2 / 2! 3 /3! + 4 / 4! . . .
14. Write a program to compute factorial and GCD using recursion.
15. Write a menu driven application that performs the functions of a calculator. The inputs from the
user should be validated and error messages in case, inputs are not valid, should be displayed of
multiplication, division, factorials (use recursion) and squares. Modularize the code wherever
possible.
Pointers
1. A C program contains the following declaration
int x[8]= {10,20,30,40,50,60,70,80};
What is the meaning of x?
What is the meaning of (x+2)?
What is the value of *x?
What is the value of (*x+2)?
What is the value of *(x+2)?.
2. A C program contains the following declaration
float table[2][3] = { { 1.1,1.2,1.3},{2.1,2.2,2.3}};
a) What is the meaning of a table?
b) What is the meaning of (table+1)?
c) What is the meaning of *(table+1)?
d) What is the meaning of (*(table+1)+1)?
e) What is the meaning of (*(table)+1)?
f) What is the value of *(*(table+1) +1)?
g) What is the value of *(*(table)+1)?
h) What is the value of *(*(table+1)?
i) What is the value of *(*(table) + 1)+1?
3. A C program contains the following declaration
char *color[6] = {red, green, blue, white, black, yellow};
a. What is the meaning of color?
b. What is the meaning of (color+2);
c. What is the value of *color?
d. What is the value of *(color+2)?
e. How do color[5] and *(color + 5) differ?.
4. Write a program to count the number of e in the following array of pointer to
char * s [ ] = {
we will teach you how to ;
Move a mountain ;
the string.
Level a building ;
Erase the past ;
Make a million ;}.
5. Write a function ``replace'' which takes a pointer to a string as a parameter, which replaces all
spaces in that string by minus signs, and delivers the number of spaces it replaced.
Thus
char *cat = "The cat sat";
n = replace( cat );
should set
cat to "The-cat-sat"
and
n to 2.
Strings
1. Write a program to convert lower case string to upper case string and vice versa.
2. Write a program to reverse a string using recursive functions
3. Write a program to read n number of strings using two-dimensional character array, sort them
and display the sorted list of strings on the screen.
4. Write a program to read n number of strings and display them on the screen. Use array of
pointers and dynamic memory allocation techniques.
5. Write a C program with a function any (s1, s2). This function returns the first location (index of
location) in the string s1 which matches with any string in s2 otherwise.
6. Write a C program with a function delete (s1, c). This function deletes each character in s1 which
matches character c.
7. Write a Program to implement strtok library function.
8. Write a C program with a function deletes2 (s1, s2). This function deletes each character in
string s1 which matches any character in string s2.
9. Write a function expand (s, t) which converts characters like newline and tab into visible escape
sequences like \n and \t as it copies the string s to t. Use switch statement and also display both
s and t at the end.
10. Write a function expand (s1, s2) which expands shorthand notations of s1 like a-d into abcd and
0-9 to 0123456789 in s2. For example if the string in s1 is 0123a-e1-4 then s1 is expanded in s2
to 0123abcde1234.
11. Write a program to print out all rotations of a string typed in. For eg:if the input is Space, the
output should be: space paces acesp cespa espac.
12. Implement string library functions. strrev, strcpy, strcat, strcmp with same return values and all
error handling features using pointers.
Read the contents from the file and display as it is as well in ones complement form. Use
command line arguments to pass file name to your C program.
6. Write a program which reads a line of characters. Each character entered from the keyboard is
tested to determine its case, and is then written to the data file in opposite case. Display the
contents of the file. Also use ftell and fseek to determine the current file position and to change
the file position.
7. Write a program to embed assembly language code in C program
8. Write a program to read a positive integer at least equal to 3, and print out all possible
permutations of three positive integers less or equal to than this value.
9. Given as input a floating (real) number of centimeters, print out the equivalent number of feet
(integer) and inches (floating, 1 decimal), with the inches given to an accuracy of one decimal
place.
Assume 2.54 centimeters per inch, and 12 inches per foot.
If the input value is 333.3, the output format should be:
333.3 centimeters is 10 feet 11.2 inches.
10. Given as input an integer number of seconds, print as output the equivalent time in hours,
minutes and seconds. Recommended output format is something like 7322 seconds is
equivalent to 2 hours 2 minutes 2 seconds.
11. Read a positive integer value, and compute the following sequence: If the number is even, halve
it; if it's odd, multiply by 3 and add 1. Repeat this process until the value is 1, printing out each
value. Finally print out how many of these operations you performed.
Typical output might be:
Inital value is 9
Next value is 28
Next value is 14
Next value is 7
Next value is 22
Next value is 11
Next value is 34
Next value is 17
Next value is 52
Next value is 26
Next value is 13
Next value is 40
Next value is 20
Next value is 10
Next value is 5
Next value is 16
Next value is 8
Next value is 4
Next value is 2
Final value 1, number of steps 19
If the input value is less than 1, print a message containing the word
Error
and perform an exit( 0 );
12. Write a program to count the vowels and letters in free text given as standard input. Read text a
character at a time until you encounter end-of-data. Then print out the number of occurrences
of each of the vowels a, e, i, o and u in the text, the total number of letters, and each of the vowels
as an integer percentage of the letter total.
Suggested output format is:
Numbers of characters:
a 3 ; e 2 ; i 0 ; o 1 ; u 0 ; rest 17
Percentages of total:
a 13%; e 8%; i 0%; o 4%; u 0%; rest 73%
Read characters to end of data using a construct such as
char ch;
while(( ch = getchar() ) >= ) {
/* ch is the next character */ ....
}
to read characters one at a time using getchar() until a negative value is returned.
13. Write a program to read English text to end-of-data (type control-D to indicate end of data at a
terminal, see below for detecting it), and print a count of word lengths, i.e. the total number of
words of length 1 which occurred, the number of length 2, and so on.
Define a word to be a sequence of alphabetic characters. You should allow for word lengths up to
25 letters. Typical output should be like this:
length 1 : 10 occurrences
length 2 : 19 occurrences
length 3 : 127 occurrences
length 4 : 0 occurrences
length 5 : 18 occurrences
....
14. Write a program last that prints the last n lines of its text input. By default n should be 5, but
your program should allow an optional argument so that
last -n
prints out the last n lines, where n is any integer. Your program should make the best use of
available storage. (Input of text could be by reading a file specified from the command or reading
a file from standard input)
15. Write a program to list the files given as arguments, stopping every 20 lines until a key is hit.(a
simple version of more UNIX utility)
Bitwise Operations
1. Write a program to count number of bits as 1 in an 8 bit number.
2. Write a program in which define a as an unsigned integer whose value is (hexadecimal) 0xa2c3.
Write the corresponding bit pattern for this value. Then evaluate each of the following bitwise
expressions, first showing the resulting bit pattern and then the equivalent hexadecimal value.
Utilize the original value of a in each expression. Assume that a is stored in a 16-bit word
a) ~a
b) a ^ 0x3f06
c) a | 0x3f06
d) a | ~0x3f06
e) a >> 3
f) a << 5
g) a ^ ~a
h) a | ~a
i) (a & ~0x3f06) << 8
j) a & ~ (0x3f06 >> 8
3. Write a C program that will accept a hexadecimal number as input, and then display a menu
that will permit any of the following operations to be carried out
a) Display the hexadecimal equivalent of the ones complement
b) Carry out a masking operation and then display the hexadecimal equivalent of the result
c) Carry out a bit shifting operation and then display the hexadecimal equivalent of the result
d) Exit
If the masking operation is selected, prompt the user for the type of operation (bitwise and,
bitwise exclusive or, or bitwise or) and then a (hexadecimal) value for the mask. If the bit
shifting operation is selected, prompt the user for the type of shift (left to right), and then the
number of bits.
4. Write a C program that will illustrate the equivalence between
Shifting a binary number to the left n bits and multiplying the binary number by 2n
Shifting a binary number to the right n bits and dividing the binary number by 2n.
Choose the initial binary number carefully, so that bits will not be lost as a result of the shifting
operation.
Preprocessor
1. Write a C program to calculate factorial of a number. Factorial function has to be written as a
multiline macro.
2. Develop sample programs using preprocessor operators #, ## and also conditional compilation.