C Programming Lab
C Programming Lab
Part -A
1. Write a C program to exchange the values of two variables.
2. Write a C program to check whether the given integer is odd or even.
3. Write a C program to find the largest of three numbers.
4. Write a C program to find the area of a circle.
5. Write a C program to simulate a simple calculator using switch case statement.
6. Write a C program to compute the factorial of a number.
7. Write a C program to find the sum of 'N' natural numbers.
8. Write a C program to generate and display the first 'N' Fibonacci numbers.
Part-B
1. Write a C program to solve the roots of quadratic equation.
2. Write a C program to reverse a given integer.
3. Write a C program to sort 'N' numbers.
4. Write a C program to search a given number from an array.
5. Write a C program to add two numbers using function.
6. Write a C program to define a structure 'STUDENT'. Also read and display 'N' student details.
7. Write a C Program to read and write character using file.
Part A
1. Exchange the values of two variables:
#include <stdio.h>
int main() {
int a, b, temp;
printf("Enter two numbers: ");
scanf("%d %d", &a, &b);
temp = a;
a = b;
b = temp;
printf("After swapping: a = %d, b = %d\n", a, b);
return 0;
}
• Purpose: Swap the values of two variables without altering their data.
• Key Steps:
o Use a temporary variable (temp) to store the value of one variable.
o Assign the second variable to the first, and then temp to the second.
• Code Breakdown:
int temp = a; // Store value of 'a' in 'temp'.
a = b; // Assign value of 'b' to 'a'.
b = temp; // Assign value of 'temp' (original 'a') to 'b'.
• Input/Output:
o Input: a = 5, b = 10
o Output: a = 10, b = 5
o
2. Check whether a number is odd or even:
#include <stdio.h>
int main() {
int num;
printf("Enter an integer: ");
scanf("%d", &num);
if (num % 2 == 0)
printf("%d is even.\n", num);
else
printf("%d is odd.\n", num);
return 0; }
}
• Purpose: Calculate the area of a circle using the formula πr².
• Key Steps:
o Multiply the radius squared (radius * radius) by π (constant 3.14159).
• Code Breakdown:
area = PI * radius * radius;
• Input/Output:
o Input: radius = 5
o Output: 78.54
if (discriminant > 0) {
root1 = (-b + sqrt(discriminant)) / (2 * a);
root2 = (-b - sqrt(discriminant)) / (2 * a);
printf("Roots are real and distinct: %.2f, %.2f\n", root1, root2);
} else if (discriminant == 0) {
root1 = -b / (2 * a);
printf("Roots are real and equal: %.2f\n", root1);
} else {
realPart = -b / (2 * a);
imagPart = sqrt(-discriminant) / (2 * a);
printf("Roots are complex: %.2f + %.2fi, %.2f - %.2fi\n", realPart, imagPart, realPart,
imagPart);
}
return 0;
}
int main() {
int num1, num2, result;
printf("Enter two numbers: ");
scanf("%d %d", &num1, &num2);
result = add(num1, num2);
printf("Sum: %d\n", result);
return 0;
}
• Purpose: Use functions to modularize addition logic.
• Key Steps:
o Define a function int add(int a, int b) and call it in main.
• Code Breakdown:
struct STUDENT {
int id;
char name[50];
float marks;
};
int main() {
int n;
printf("Enter the number of students: ");
scanf("%d", &n);
struct STUDENT students[n];
for (int i = 0; i < n; i++) {
printf("Enter details for student %d:\n", i + 1);
printf("ID: ");
scanf("%d", &students[i].id);
printf("Name: ");
scanf("%s", students[i].name);
printf("Marks: ");
scanf("%f", &students[i].marks);
}
printf("\nStudent Details:\n");
for (int i = 0; i < n; i++) {
printf("ID: %d, Name: %s, Marks: %.2f\n", students[i].id, students[i].name,
students[i].marks);
}
return 0;
}
• Purpose: Use structures to store multiple details (e.g., ID, name, marks).
• Key Steps:
o Define struct STUDENT and use arrays to store multiple students.
• Code Breakdown:
struct STUDENT students[n];
// Writing to a file
fptr = fopen("output.txt", "w");
if (fptr == NULL) {
printf("Error opening file!\n");
return 1;
}
printf("Enter characters to write to the file (Press CTRL+D to stop):\n");
while ((ch = getchar()) != EOF) {
fputc(ch, fptr);
}
fclose(fptr);
return 0;
}
• Purpose: Demonstrate file operations (fopen, fputc, fgetc).
• Key Steps:
o Write user input to a file using fputc.
o Read the file contents using fgetc.
• Code Breakdown:
fputc(ch, fptr);
while ((ch = fgetc(fptr)) != EOF) { ... }