Você está na página 1de 12

C Programming Assignments

Day 5

Assignments Day #1:


Concepts: Formatted input/output, basic data types, type conversions Estimated time: 1 Hour Objective: At the end of the assignments, participants will be able to design the output using escape sequences, to take input using proper format specifier as per the data type. Also participants will be able to use type casting wherever necessary. Problems: 1. Write a C program with appropriate comments at appropriate places to display the following lines of text on screen: Emp No 22000 22100 22300 Name Rajesh Magesh Amol Role\Designation Project Manager Team Leader Sr. Software Engineer, System Analyst

2. Write a C program which accepts input for integer, character, float & double data type identifiers from the user and prints the same on the screen. For each data type consider all possible forms such as signed, unsigned, long etc wherever applicable. 3. Write a C program to perform the following : i. Multiplication ii. Division. Note .: Accept two inputs from the user during runtime, While doing the division operation the program should print the quotient and the remainder in a formatted way. 4. Write a C program to accept an input and print its corresponding ASCII value. Concept: Operators Estimated time: 1.5 Hours Objective: At the end of the assignments, participants will able to use different operators with considering their precedence and associtivity. Problems: 1/12

C Programming Assignments 1. Write a C program to calculate the simple interest with following formula Simple interest = (Principle Amount * No_Of_Years * Rate_Of_Interest ----------------------------------------------------------------100 Note : Accept Input for Principle_Amount, No_of_Years and Rate_Of_Interest.

Day 5

2. Write a C program which accepts radius as an input and find the area and perimeter of the circle. 3. Write a C program to solve the quadratic equation. Note: Accept necessary inputs to solve the equation. 4. Write a C program to accept two numbers during runtime and find the maximum and minimum of the given two numbers using conditional or ternary operator. 5. Write a C program which accepts a character as an input and check whether the entered input is an lower case, upper case or special symbol.

Concept: Conditional flow Estimated time: 1.5 Hours Objective: At the end of assignments, participants will able to implement conditional flow using proper use of if-else or switch statements Problems: 1. Write a C program which accepts four integer numbers and find the largest among the four without using logical operator. 2. Write a C program which accepts selling price and cost price of an item during runtime and determine whether the seller has made the profit or incurred loss and display the output for the same. 3. Write a C program to accept a number during the runtime and find whether it is even or odd. 4. Write a program that accepts a number between 1 and 100 from the user. If there is a coin of that value in cents, it should display its name. Otherwise, it should report that there is no such coin 1 = cent, 5 = nickel, 10 = dime, 25 = quarter, 100 = dollar 5. Write a menu driven C program which accepts two numbers during runtime and perform the following, 2/12

C Programming Assignments Menu 1. Addition 2. Subtraction 3. Multiplication 4. Division 5. Mod and display the result according to selected menu option.

Day 5

6. Write a C program which will accept an integer during runtime between 1 and 12 and display the corresponding month for the accepted input and perform necessary validations for the given input. 7. Write a program which will input the seconds and will convert it into minutes. For example: If the input is 125seconds then the output should be 2 minutes and 5 seconds 8. Write a program which will accept the length in feet and display it in inches. (1 = 12)

Assignments Day #2:


Concept: iterative statements: loops (while, do while, for), flow of control statements: break & continue statement Estimated time: 3.5 Hours Objective: at the end of this assignment participant will be able to implement iterative flow using loops.
1) Accept a number in N & print the first N numbers of Fibonacci series. 2) Store a number in a variable & ask the user to guess it, if guess is correct display message U GOT IT otherwise, give him 2 more chances; if after 2 chances, he is unable to guess it, then display HARD WORK. 3) Write a program which will accept a number (1 -> 32767) & print sum of the digits to one digit number. eg:-3+2+7+6+7=26 further 2+6=8

4) Accept a number & print the following pattern n=5 (a)

3/12

C Programming Assignments
* *** ***** ******* *********

Day 5

(b)

1 11 121 1331 14641

5) Input a number (1 to 30000) and print it in words. For example, if the number is 1234, display the
output as One Thousand Two Hundred Thirty Four. If the user inputs a number out of range, then display suitable error message and exit the program 6) Write a program which will accept a number (1-> 30000) & print following pattern N=4738 **** ******* *** ******** N=4708 **** ******* ********

Concept: arrays, type of arrays Objective: At the end of assignment, participants will able to efficiently use arrays
1) Write a program which will accept 7 numbers. & array (array is sorted & it may contain duplicates); Write a program which creates an array of unique elements. 2) Write a program to read the integers 24, 16, 86, 65, 42, 38, 82 into one array and the integers 86, 55, 42, 18, 99, 32, 64, 11 into another array and print one array that contain only those integers that are common to both the array. 3) Write a program which will accept a string and number. N; display N characters from left of the string. Example: String is: computer Number is: 3 O / P: com 4) Write a program which will accept a string & print number of vowels in the string. 5) Write a program which will accept a string, and reverse it in another string. 6) Write a program which will accept a string reverse it in same string & print it

4/12

C Programming Assignments 7) Write a program which will accept 2 strings join 2nd string to the 1st. & print 1st string.
Example : S = Tech, T= Mahindra Output : TechMahindra 8) 9) 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 Write a program which will add the string to the original string. Accept the original string. Ask the user to accept the string to be added and the position where string to be added.

Day 5

10) I/P a string & 1 sentence; search the string in the sentence, if it is present, print the location at which the string begins in the sentence else display the appropriate message. 11) Write a program which will accept 6 elements in an array in a sorted order. Ask the user to accept the element to be deleted and delete that element from an array. 12) Write a program to accept a matrix of size m * n and print the matrix in matrix form and the sum of all the elements in the array. 13) Write a program which will accept the two matrices of order r1, c1 and r2, c2 respectively. Store the multiplication of it in the third matrix and display the same. 14) Write a program to accept a matrix of size m* n. Print the transpose of the matrix.

Assignments Day #3:


Concept: User Defined data types(Structure, Union, Enum, Introduction to Pointers and Dynamic Memory Allocation) Estimated time: 2 Hour Objective: At the end of the assignments, participants will able to use appropriate user defined data type, pointers. Problems: 1. Write a program which takes Roll Number, Name & Marks of Physics, Chemistry and Mathematics of 3 Students. Calculate total marks & percentage for all students & display the merit list on screen. (Make proper use of typedef) 2. Define a structure person with the following attributes: a. Person_ID b. Name c. Address integer string string

5/12

C Programming Assignments d. Age float

Day 5

Write a menu driven program which will do the following: MENU ===== 1. Add Record 2. Search by Person ID 3. Display All 4. Exit 3. Write a program which stores information of person as follows, name string gender char (m or f) age int salary float It is required that, for all males only salary and for all females only age to be stored by the program. Display information of all persons on the screen. 4. Write a program which should store data about windows (suppose in certain graphical application) in following format: window_id title forecolor backcolor height width Where different possible colors are Black, White, Violet, Orange, Red, Pink, Green. Accept data of 3 windows from user & display all windows which have background white & foreground pink. 5. Write a program which read name, gross_pay, designation for 8 Employees. The field gross_pay consists of subfields like basic_pay, da, hra, conveyance. Calculate total salary for all the employees and display it on the screen. Also display the details of an employee with highest and lowest salary. (Make proper use of typedef, nested structure).

6. Write a program which stores information of resident as follows: Name string Address string Status string (unemployed, employed, retired) Income float 6/12

C Programming Assignments Age int

Day 5

It is required that, for all the retired and unemployed resident only age to be stored and for employed residents income is to be stored. Display information of Senior Residents and employed resident. (Make proper use of typedef and user defined data types) 7. Write a program to read n elements in an array. Allocate memory for n-element array dynamically. Interchange odd and even position elements of an array. Display the elements of an array using pointer. Do not use separate pointer. 8. Write a program to accept 10 elements in an array. Using pointer notation, access each element of an array and increment each element by 5. Display all the modified values of an array without using separate pointer. 9. Write a program to read 8 elements each for two arrays. Using pointer notation, add corresponding elements of both the arrays and store it in third array. Display all the elements of the third array. For example: a. a[]={ 1,2,3,4,} b. b [] = {7,8,9,4,.} c. Resulting array: - c [] = {8, 10, 12, 8..}

Assignments Day #4:


Concept: Functions (built-in & user defined), pointer to structure, pointer to array , array of pointers and dynamic memory allocation Estimated time: 4 hours Objective: at the end of the assignments, participants will able to work with pointer to structure, pointer to array and array of pointers and functions. Problems: 1. Write a program which performs the following tasks: a. b. c. d. Declare and initialize an integer array of 10 elements in main() Pass the entire array to a function modify(). In modify() multiply each element of the array by 2 Return the control to main() and print the modified array elements in main().

2. Write a function to find the maximum and minimum number in an array using pointers. 7/12

C Programming Assignments

Day 5

3. Write a program to reverse the strings stored in the following array of pointers to strings: char *array_str_ptr = { Brinda Roy Rakesh Baid Neha Saxena Ankit Jain } 4. 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 and display those numbers. 5. Write functions to insert and delete a string from an array of strings. Write a program that displays the following menu to the user: a) Insert String b) Delete Strings c) Exit Depending on the user choice the program will call functions that will insert / delete a string in/from an array of strings. ( Allocate the memory dynamically for array of strings) 6. Write a program that can maintain the name, roll number and marks of students of one batch. The size of the batch is variable. Include a function to calculate the average marks of the class. (Hint: Use structure array and dynamic memory allocation). 7. Write a C function MyStrcat( ) to simulate the functionality of strcat() library function. Note: strcat() is a libraray function which concatenates the second string to first string) 8. Write a function which will accept an array of integers as an argument. It should find and return the smallest element in the array after sorting it. 9. Write a function to find the length of the greatest common substring between two strings passed as arguments to it. 10. Write a function which accepts a line of text in the form of a character pointer and will display the number of vowels, consonants, digits and the white space characters contained in the line. 11. Write a function that compares two dates. To store a date use a structure that contains three members namely day, month and year each of type int. The two dates should be passed as structure pointers through the function. If the two dates are equal then return 1 else return 0. In the main function call the function and display appropriate message saying dates are equal or unequal. 8/12

C Programming Assignments

Day 5

12. Create a structure to specify the following data of customers in a bank: Account number, name, balance in account. Create an array of 5 customers in main and define the following functions: a. Write a function to print the account number and name of each customer with balance below Rs. 500. b. Write a function to withdraw or deposit money corresponding to an account. Pass account no, amount and code(1 for deposit and 0 for withdrawal) thru the function , the function should check the balance amount before withdrawal and should display Insufficient amount for withdrawal, if the balance is not sufficient. If the transaction succeeds it should display the success message and the balance amount after the transaction Note: Store these function definitions in a header file called as Myhead.h and include this header file in source program in which main is defined. Call these functions in main and test the functionality. 13. Write a program to solve the expression xn .Accept values of n & x from the user. [Use appropriate library function from math.h] 14. Write a program to accept a string from the user and perform the following operations on it(use C library functions): a. Display the length of the string. b. Display the string in reverse way. c. Copy the string to some other string and display the new string. 15. Write a function which accepts an array of integers as an argument and places all negative elements of the array to the end without altering the original sequence. for example, if array contains 5 -4 3 -2 6 -11 12 -8 9 Then the return array will be 5 3 6 12 9 -4 -2 -11 -8

Assignments Day #5:


Concept: Preprocessors Objective: At the end of the assignments, participants will be able to implement macro & file inclusion using preprocessor. Estimated time: 50 Minutes Problems: 1. A 5 digit positive integer is entered through the keyboard. Write a function to calculate 9/12

C Programming Assignments

Day 5

the sum of digits of this number in your own header file mylib.h. Call this header file in mymain.c & display the result. 2 Write a program using a simple macro that sets pi as 3.14 & calculates pi*r*r*h . 3 Write a program using macro large(a,b) to find largest of two nos. Concept: Command line arguments Estimated time: 1 Hour 20 Minutes Objective: At the end of the assignments, participants will able to identify the need & use of command line arguments Problems: 1. Write a program validate.c which will take phone number as a command line argument and will validate whether it is of 8 digits or not. Display the appropriate message. 2. Write a program Palimdrome.c, with command line argument as a string. Check whether the passed string is palindrome or not. Also, Validate for zero or more arguments. 3 Write a program that takes three arguments from command line such that first & second argument will be float & third will be operator(+,-,*, /). Perform the operation & display the result. 4 Write a program that reads character M or F as an command line argument & displays gender of a person. Also validate for improper gender. Concept: File Handling Estimated time: 2 Hours 30 Minutes Objective: At the end of the assignments, participants will able to know about various operations on files. Problems: 1. Write a program which will copy one file into another. 2. Write a program which will count the number of lines, words and characters present in the file. 3. Write a program which takes following employee data from user, write it into file, 10/12

C Programming Assignments retrieve it back & display on screen .(Use fread & fwrite functions only) Emp ID Name Salary 1 Sanjay Patil 12000.00 2 Sanjeev Kumar 13000.50 3 Sunil Dev 25000.50 ------Accept an employee id, to be modified & modify the information in the file. Note: Do not allow employee no for modification

Day 5

4. Write a command in C called GREP in which the functionality of the grep command in unix is simulated, use file handling and command line arguments. Ex: GREP <pattern> <filename> Prints all the lines of the file containing the pattern. 5. Two files sort1 & sort2 contain sorted list of integers. Write a program to produce a third file sort which holds a single sorted , merged list of these two lists. ( Hint: Use fscanf and fprintf). 6 Write a program that reads following cdr.txt file. ANumber 9822211111 9822233333 9822244444 BNumber 9998712345 9998712346 9998712348 Date 10/10/2007 02/02/2002 20/03/2008 Duration(Min) 40 10 17

If record consist of all entries , write into cdr_valid.txt else into cdr_invalid.txt

7 Write a program calcharg.c that reads following cdr.txt & which takes mobileno & duration as command line arguments from user . Mobile No 9822310000 9822320000 9822330000 9822340000 9822350000 Rate(Rs)/Min 1 1.5 2 1.75 2.5 11/12

C Programming Assignments

Day 5

Calculate total charge for respective customers. Use fseek function to go to particular record.

12/12