C Programming Lab Programs
1. Input/Output Functions – Calculate Area of Circle
Aim:
To write a C program to calculate the area of a circle using input/output functions.
Procedure:
1. Start the program and include the necessary header files.
2. Define the constant PI with value 3.1416.
3. Declare variables for radius and area.
4. Read the radius from the user.
5. Calculate area using the formula: area = PI * radius * radius.
6. Display the result on the screen.
7. Stop the program.
Result:
The above program was successfully compiled and executed.
2. Conditional Structures – Check Even or Odd
Aim:
To write a C program to check whether a given number is even or odd using conditional
structures.
Procedure:
1. Start the program and include the necessary header files.
2. Declare an integer variable to store the number.
3. Read the number from the user.
4. Check if the number is divisible by 2 using the modulus operator.
5. If divisible, display that it is even, otherwise display odd.
6. Stop the program.
Result:
The above program was successfully compiled and executed.
3. Command Line Arguments – Greet User
Aim:
To write a C program to greet the user using command line arguments.
Procedure:
1. Start the program and include the necessary header files.
2. Use main function with arguments (argc, argv).
3. Check if the number of arguments passed is correct.
4. If not, display the usage instruction.
5. Otherwise, print a greeting message with the given name.
6. Stop the program.
Result:
The above program was successfully compiled and executed.
4. Arrays – Average of Numbers
Aim:
To write a C program to find the average of numbers using arrays.
Procedure:
1. Start the program and include the necessary header files.
2. Declare an array to store numbers and variables for sum and average.
3. Read the number of elements from the user.
4. Use a loop to read the numbers and add them to sum.
5. Calculate average by dividing sum by the number of elements.
6. Display the average.
7. Stop the program.
Result:
The above program was successfully compiled and executed.
5. Strings – Reverse a String and Find Length
Aim:
To write a C program to reverse a string and find its length.
Procedure:
1. Start the program and include the necessary header files.
2. Declare a character array to store the string.
3. Read the string from the user.
4. Find the length using strlen() function.
5. Reverse the string using strrev() function.
6. Display the length and reversed string.
7. Stop the program.
Result:
The above program was successfully compiled and executed.
6. Functions – Addition of Two Numbers
Aim:
To write a C program to add two numbers using functions.
Procedure:
1. Start the program and include the necessary header files.
2. Define a function add() that takes two integers and returns their sum.
3. In the main function, declare two numbers.
4. Call the add() function and store the result.
5. Display the sum of the numbers.
6. Stop the program.
Result:
The above program was successfully compiled and executed.
7. Recursive Functions – Factorial of a Number
Aim:
To write a C program to find the factorial of a number using recursion.
Procedure:
1. Start the program and include the necessary header files.
2. Define a recursive function factorial() which calls itself.
3. If number is 0, return 1 (base case).
4. Otherwise, return n * factorial(n-1).
5. Read a number from the user and call factorial().
6. Display the factorial result.
7. Stop the program.
Result:
The above program was successfully compiled and executed.
8. Pointers – Swap Two Numbers
Aim:
To write a C program to swap two numbers using pointers.
Procedure:
1. Start the program and include the necessary header files.
2. Define a function swap() that accepts two integer pointers.
3. Swap the values using dereferencing.
4. In the main function, read two numbers from the user.
5. Call swap() function by passing addresses of variables.
6. Display the swapped numbers.
7. Stop the program.
Result:
The above program was successfully compiled and executed.
9. Structures and Unions – Student Details
Aim:
To write a C program to demonstrate structures and unions.
Procedure:
1. Start the program and include the necessary header files.
2. Define a structure Student with id, name, and marks.
3. Define a union Data with int and float members.
4. Initialize a structure variable and display details.
5. Assign and display values in union to show memory sharing.
6. Stop the program.
Result:
The above program was successfully compiled and executed.
10. File Handling – Read and Write Data
Aim:
To write a C program to perform file operations such as writing and reading data.
Procedure:
1. Start the program and include the necessary header files.
2. Declare a file pointer.
3. Open a file in write mode.
4. Read user input (name and age) and write to the file.
5. Close the file.
6. Open the same file in read mode.
7. Read data from file and display it on the screen.
8. Close the file and stop the program.
Result:
The above program was successfully compiled and executed.