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: Determine if a number is divisible by 2.
• Key Steps:
o Use the modulus operator (%) to check the remainder when dividing by 2.
o If remainder is 0, it’s even; otherwise, it’s odd.
• Code Breakdown:
c
CopyEdit
if (num % 2 == 0)
printf("Even");
else
printf("Odd");
• Input/Output:
o Input: 5
o Output: Odd
3. Find the largest of three numbers:
#include <stdio.h>
int main() {
int a, b, c;
printf("Enter three numbers: ");
scanf("%d %d %d", &a, &b, &c);
if (a > b && a > c)
printf("Largest number is: %d\n", a);
else if (b > c)
printf("Largest number is: %d\n", b);
else
printf("Largest number is: %d\n", c);
return 0;
}
• Purpose: Compare three numbers and identify the largest.
• Key Steps:
o Use nested if-else statements to compare a, b, and c.
• Code Breakdown:
if (a > b && a > c)
printf("%d is largest", a);
else if (b > c)
printf("%d is largest", b);
else
printf("%d is largest", c);
• Input/Output:
o Input: a = 5, b = 7, c = 3
o Output: 7
4. Find the area of a circle:
#include <stdio.h>
#define PI 3.14159
int main() {
float radius, area;
printf("Enter the radius of the circle: ");
scanf("%f", &radius);
area = PI * radius * radius;
printf("Area of the circle: %.2f\n", area);
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
5. Simple calculator using switch case:
#include <stdio.h>
int main() {
char operator;
float num1, num2, result;
printf("Enter operator (+, -, *, /): ");
scanf(" %c", &operator);
printf("Enter two numbers: ");
scanf("%f %f", &num1, &num2);
switch (operator) {
case '+': result = num1 + num2; break;
case '-': result = num1 - num2; break;
case '*': result = num1 * num2; break;
case '/': result = num2 != 0 ? num1 / num2 : 0; break;
default: printf("Invalid operator!\n"); return 1;
}
printf("Result: %.2f\n", result);
return 0;
}
• Purpose: Perform arithmetic operations based on user choice.
• Key Steps:
o Use a switch statement to perform addition, subtraction, multiplication, or
division based on operator input.
• Code Breakdown:
switch (operator) {
case '+': result = num1 + num2; break;
case '-': result = num1 - num2; break;
case '*': result = num1 * num2; break;
case '/': result = num2 != 0 ? num1 / num2 : 0; break;
}
• Input/Output:
o Input: num1 = 5, num2 = 2, operator = '+'
o Output: 7
6. Compute the factorial of a number:
#include <stdio.h>
int main() {
int num, fact = 1;
printf("Enter a number: ");
scanf("%d", &num);
for (int i = 1; i <= num; i++)
fact *= i;
printf("Factorial of %d is %d\n", num, fact);
return 0;
}
• Purpose: Calculate the product of all positive integers up to n (e.g., 5! = 5*4*3*2*1).
• Key Steps:
o Use a loop to multiply numbers from 1 to n.
• Code Breakdown:
for (int i = 1; i <= num; i++)
fact *= i;
• Input/Output:
o Input: n = 5
o Output: 120
7. Find the sum of 'N' natural numbers:
#include <stdio.h>
int main() {
int n, sum = 0;
printf("Enter the value of N: ");
scanf("%d", &n);
for (int i = 1; i <= n; i++)
sum += i;
printf("Sum of first %d natural numbers is %d\n", n, sum);
return 0;
}
• Purpose: Add numbers from 1 to n (e.g., 1 + 2 + 3 + ... + n).
• Key Steps:
o Use a loop to compute the sum iteratively.
• Code Breakdown:
for (int i = 1; i <= n; i++)
sum += i;
• Input/Output:
o Input: n = 5
o Output: 15
8. Generate the first 'N' Fibonacci numbers:
#include <stdio.h>
int main() {
int n, t1 = 0, t2 = 1, nextTerm;
printf("Enter the value of N: ");
scanf("%d", &n);
printf("Fibonacci Series: %d, %d", t1, t2);
for (int i = 3; i <= n; i++) {
nextTerm = t1 + t2;
printf(", %d", nextTerm);
t1 = t2;
t2 = nextTerm;
}
printf("\n");
return 0;
}
• Purpose: Generate the Fibonacci series up to N terms (0, 1, 1, 2, 3, 5, ...).
• Key Steps:
o Use two variables (t1 and t2) to store the previous terms and calculate the next
term.
• Code Breakdown:
nextTerm = t1 + t2;
t1 = t2;
t2 = nextTerm;
• Input/Output:
o Input: N = 5
o Output: 0, 1, 1, 2, 3
Part B
1. Solve the roots of a quadratic equation:
#include <stdio.h>
#include <math.h>
int main() {
float a, b, c, discriminant, root1, root2, realPart, imagPart;
printf("Enter coefficients a, b, and c: ");
scanf("%f %f %f", &a, &b, &c);
discriminant = b * b - 4 * a * c;
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;
}
• Purpose: Find the roots using the quadratic formula:
root1 = (-b ± sqrt(b² - 4ac)) / 2a
• Key Steps:
o Calculate the discriminant b² - 4ac.
o Use conditions to check if roots are real, equal, or complex.
• Code Breakdown:
discriminant = b * b - 4 * a * c;
if (discriminant > 0) { ... }
else if (discriminant == 0) { ... }
else { ... }
2. Reverse a given integer:
#include <stdio.h>
int main() {
int num, reversed = 0, remainder;
printf("Enter an integer: ");
scanf("%d", &num);
while (num != 0) {
remainder = num % 10;
reversed = reversed * 10 + remainder;
num /= 10;
}
printf("Reversed integer: %d\n", reversed);
return 0;
}
• Purpose: Reverse the digits of an integer.
• Key Steps:
o Extract digits using modulus (% 10) and construct the reverse by multiplying and
adding.
• Code Breakdown:
reversed = reversed * 10 + remainder;
num /= 10;
3. Sort 'N' numbers:
#include <stdio.h>
int main() {
int n, temp;
printf("Enter the number of elements: ");
scanf("%d", &n);
int arr[n];
printf("Enter the elements:\n");
for (int i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}
for (int i = 0; i < n - 1; i++) {
for (int j = 0; j < n - i - 1; j++) {
if (arr[j] > arr[j + 1]) {
temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
printf("Sorted elements:\n");
for (int i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
printf("\n");
return 0;
}
• Purpose: Rearrange an array in ascending order using the Bubble Sort algorithm.
• Key Steps:
o Compare adjacent elements and swap if needed.
• Code Breakdown:
if (arr[j] > arr[j + 1]) {
temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
4. Search a given number from an array:
#include <stdio.h>
int main() {
int n, search, found = 0;
printf("Enter the number of elements: ");
scanf("%d", &n);
int arr[n];
printf("Enter the elements:\n");
for (int i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}
printf("Enter the number to search: ");
scanf("%d", &search);
for (int i = 0; i < n; i++) {
if (arr[i] == search) {
printf("Number found at position %d\n", i + 1);
found = 1;
break;
}
}
if (!found) {
printf("Number not found.\n");
}
return 0;
}
• Purpose: Check if a number exists in an array.
• Key Steps:
o Use a loop to compare each element with the search value.
• Code Breakdown:
if (arr[i] == search) { ... }
5. Add two numbers using a function:
#include <stdio.h>
int add(int a, int b) {
return a + b;
}
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:
int add(int a, int b) { return a + b; }
6. Define a structure 'STUDENT' and read/display 'N' student details:
#include <stdio.h>
#include <string.h>
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];
7. Read and write character using a file:
#include <stdio.h>
int main() {
char ch;
FILE *fptr;
// 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);
// Reading from the file
fptr = fopen("output.txt", "r");
if (fptr == NULL) {
printf("Error opening file!\n");
return 1;
}
printf("\nContents of the file:\n");
while ((ch = fgetc(fptr)) != EOF) {
putchar(ch);
}
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) { ... }