If-Else:
1. Write a C program to check whether a number is even or odd using if-else.
2. Write a C program to determine whether a character is an uppercase letter, a lowercase letter, or a digit.
3. Write a C program to check whether a triangle is valid or not based on the given angles using if-else.
4. Write a C program to calculate the grade based on given marks using if-else (A, B, C, etc.).
5. Write a C program to check whether a number is divisible by both 3 and 5 using if-else.
6. Write a C program to determine whether a given number is positive, negative, or zero using if-else.
7. Write a C program to check whether a character is a vowel, consonant, or a digit using if-else.
8. Write a C program to determine whether a person is eligible for a senior citizen discount based on their age using if-else.
9. Write a C program to determine the largest among three numbers using nested if-else.
10. Write a C program to determine whether a number is within a specific range using if-else.
Loops:
1. Write a C program to print the multiplication table of a given number using a loop.
2. Write a C program to find the factorial of a given number using a loop.
3. Write a C program to calculate the sum of all even numbers between 1 and 100 using a loop.
4. Write a C program to check whether a given number is prime using a loop.
5. Write a C program to reverse a number using a loop.
6. Write a C program to calculate the sum of the digits of a given number using a while loop.
7. Write a C program to print the Fibonacci series up to a given number using a loop.
8. Write a C program to print all numbers between two intervals divisible by a given number using a loop.
9. Write a C program to find the greatest common divisor (GCD) of two numbers using a loop.
10. Write a C program to print a pattern of stars in a pyramid shape using nested loops.
Arrays:
1. Write a C program to find the sum and average of all elements in an array.
2. Write a C program to find the largest and smallest element in an array.
3. Write a C program to sort an array in ascending order using the bubble sort algorithm.
4. Write a C program to find the frequency of each element in an array.
5. Write a C program to merge two sorted arrays into a single sorted array.
6. Write a C program to search for an element in an array using linear search.
7. Write a C program to insert an element at a specific position in an array.
8. Write a C program to delete an element from an array at a specific position.
9. Write a C program to find the second largest element in an array.
10. Write a C program to rotate the elements of an array by a given number of positions.
Strings:
1. Write a C program to find the length of a string without using the `strlen()` function.
2. Write a C program to reverse a string without using the `strrev()` function.
3. Write a C program to check whether a string is a palindrome.
4. Write a C program to concatenate two strings without using the `strcat()` function.
5. Write a C program to count the number of vowels and consonants in a string.
6. Write a C program to find the frequency of each character in a string.
7. Write a C program to convert all lowercase characters in a string to uppercase.
8. Write a C program to remove all occurrences of a character from a string.
9. Write a C program to find the first occurrence of a substring within a string.
10. Write a C program to replace a substring within a string with another substring.
Functions:
1. Write a C program to find the factorial of a number using recursion.
2. Write a C program to calculate the nth Fibonacci number using a function.
3. Write a C program to swap two numbers using a function and pass by reference.
4. Write a C program to find the GCD of two numbers using a recursive function.
5. Write a C program to find the power of a number using a function.
6. Write a C program to check if a number is prime using a function.
7. Write a C program to find the sum of all elements in an array using a function.
8. Write a C program to convert temperature from Celsius to Fahrenheit using a function.
9. Write a C program to check whether a number is even or odd using a function.
10. Write a C program to sort an array using a function that implements the selection sort algorithm.
Pointers:
1. Write a C program to swap two numbers using pointers.
2. Write a C program to find the sum of all elements in an array using pointers.
3. Write a C program to reverse a string using pointers.
4. Write a C program to find the length of a string using pointers.
5. Write a C program to find the largest element in an array using pointers.
6. Write a C program to sort an array using pointers.
7. Write a C program to copy the contents of one string to another using pointers.
8. Write a C program to add two numbers using pointers.
9. Write a C program to find the difference between two pointers.
10. Write a C program to access and modify array elements using pointer notation.
Structures:
1. Write a C program to store and display information of a student using a structure.
2. Write a C program to add two complex numbers using structures.
3. Write a C program to store and display information of multiple employees using structures.
4. Write a C program to find the sum of distances (in feet and inches) using structures.
5. Write a C program to store and display details of a book (title, author, price) using a structure.
6. Write a C program to sort an array of structures based on student marks.
7. Write a C program to calculate the total and average marks of students using structures.
8. Write a C program to merge two date structures and display the result.
9. Write a C program to perform addition and subtraction of two time structures (hours, minutes, seconds).
10. Write a C program to compare the age of two persons using structures.
Files:
1. Write a C program to create a text file and write user input into it.
2. Write a C program to read the contents of a text file and display them on the console.
3. Write a C program to copy the contents of one file to another.
4. Write a C program to count the number of words, characters, and lines in a text file.
5. Write a C program to append content to an existing file.
6. Write a C program to merge the contents of two files into a third file.
7. Write a C program to find the frequency of a word in a text file.
8. Write a C program to display only the even-numbered lines of a file.
9. Write a C program to encrypt the contents of a file.
10. Write a C program to delete a specific line from a text file.
If else
1. Write a C program to check whether a number is even or odd using if-else.
#include <stdio.h>
int main() {
int number;
printf(“Enter an integer: “);
scanf(“%d”, &number);
if (number % 2 == 0)
printf(“%d is even.\n”, number);
else
printf(“%d is odd.\n”, number);
return 0;
}
2. Write a C program to determine whether a character is an uppercase letter, a lowercase letter, or a digit.
#include <stdio.h>
int main() {
char ch;
printf(“Enter a character: “);
scanf(“%c”, &ch);
if (ch >= ‘A’ && ch <= ‘Z’)
printf(“‘%c’ is an uppercase letter.\n”, ch);
else if (ch >= ‘a’ && ch <= ‘z’)
printf(“‘%c’ is a lowercase letter.\n”, ch);
else if (ch >= ‘0’ && ch <= ‘9’)
printf(“‘%c’ is a digit.\n”, ch);
else
printf(“‘%c’ is a special character.\n”, ch);
return 0;
}
3. Write a C program to check whether a triangle is valid or not based on the given angles using if-else.
#include <stdio.h>
int main() {
int angle1, angle2, angle3;
printf(“Enter three angles of a triangle: “);
scanf(“%d %d %d”, &angle1, &angle2, &angle3);
if ((angle1 + angle2 + angle3) == 180)
printf(“The triangle is valid.\n”);
else
printf(“The triangle is not valid.\n”);
return 0;
}
4. Write a C program to calculate the grade based on given marks using if-else (A, B, C, etc.).
#include <stdio.h>
int main() {
int marks;
printf(“Enter your marks: “);
scanf(“%d”, &marks);
if (marks >= 90)
printf(“Grade: A\n”);
else if (marks >= 80)
printf(“Grade: B\n”);
else if (marks >= 70)
printf(“Grade: C\n”);
else if (marks >= 60)
printf(“Grade: D\n”);
else
printf(“Grade: F\n”);
return 0;
}
5. Write a C program to check whether a number is divisible by both 3 and 5 using if-else.
#include <stdio.h>
int main() {
int number;
printf(“Enter an integer: “);
scanf(“%d”, &number);
if (number % 3 == 0 && number % 5 == 0)
printf(“%d is divisible by both 3 and 5.\n”, number);
else
printf(“%d is not divisible by both 3 and 5.\n”, number);
return 0;
}
6. Write a C program to determine whether a given number is positive, negative, or zero using if-else.
#include <stdio.h>
int main() {
int number;
printf(“Enter an integer: “);
scanf(“%d”, &number);
if (number > 0)
printf(“%d is positive.\n”, number);
else if (number < 0)
printf(“%d is negative.\n”, number);
else
printf(“The number is zero.\n”);
return 0;
}
7. Write a C program to check whether a character is a vowel, consonant, or a digit using if-else.
#include <stdio.h>
int main() {
char ch;
printf(“Enter a character: “);
scanf(“%c”, &ch);
if ((ch >= ‘a’ && ch <= ‘z’) || (ch >= ‘A’ && ch <= ‘Z’)) {
if (ch == ‘a’ || ch == ‘e’ || ch == ‘i’ || ch == ‘o’ || ch == ‘u’ ||
ch == ‘A’ || ch == ‘E’ || ch == ‘I’ || ch == ‘O’ || ch == ‘U’)
printf(“‘%c’ is a vowel.\n”, ch);
else
printf(“‘%c’ is a consonant.\n”, ch);
} else if (ch >= ‘0’ && ch <= ‘9’) {
printf(“‘%c’ is a digit.\n”, ch);
} else {
printf(“‘%c’ is a special character.\n”, ch);
}
return 0;
}
8. Write a C program to determine whether a person is eligible for a senior citizen discount based on their age using if-else.
#include <stdio.h>
int main() {
int age;
printf(“Enter your age: “);
scanf(“%d”, &age);
if (age >= 60)
printf(“You are eligible for a senior citizen discount.\n”);
else
printf(“You are not eligible for a senior citizen discount.\n”);
return 0;
}
9. Write a C program to determine the largest among three numbers using nested if-else.
#include <stdio.h>
int main() {
int num1, num2, num3;
printf(“Enter three numbers: “);
scanf(“%d %d %d”, &num1, &num2, &num3);
if (num1 >= num2) {
if (num1 >= num3)
printf(“%d is the largest.\n”, num1);
else
printf(“%d is the largest.\n”, num3);
} else {
if (num2 >= num3)
printf(“%d is the largest.\n”, num2);
else
printf(“%d is the largest.\n”, num3);
}
return 0;
}
10. Write a C program to determine whether a number is within a specific range using if-else.
#include <stdio.h>
int main() {
int number, lower, upper;
printf(“Enter a number: “);
scanf(“%d”, &number);
printf(“Enter the lower and upper range: “);
scanf(“%d %d”, &lower, &upper);
if (number >= lower && number <= upper)
printf(“%d is within the range %d to %d.\n”, number, lower, upper);
else
printf(“%d is outside the range %d to %d.\n”, number, lower, upper);
return 0;
}
Loops
1. Write a C program to print the multiplication table of a given number using a loop.
#include <stdio.h>
int main() {
int num;
printf(“Enter a number: “);
scanf(“%d”, &num);
for (int i = 1; i <= 10; i++) {
printf(“%d x %d = %d\n”, num, i, num * i);
}
return 0;
}
2. Write a C program to find the factorial of a given number using a loop.
#include <stdio.h>
int main() {
int num;
unsigned long long factorial = 1;
printf(“Enter a number: “);
scanf(“%d”, &num);
if (num < 0)
printf(“Factorial of a negative number doesn’t exist.\n”);
else {
for (int i = 1; i <= num; i++) {
factorial *= i;
}
printf(“Factorial of %d = %llu\n”, num, factorial);
}
return 0;
}
3. Write a C program to calculate the sum of all even numbers between 1 and 100 using a loop.
#include <stdio.h>
int main() {
int sum = 0;
for (int i = 2; i <= 100; i += 2) {
sum += i;
}
printf(“Sum of all even numbers between 1 and 100 = %d\n”, sum);
return 0;
}
4. Write a C program to check whether a given number is prime using a loop.
#include <stdio.h>
int main() {
int num, isPrime = 1;
printf(“Enter a number: “);
scanf(“%d”, &num);
if (num <= 1) {
printf(“%d is not a prime number.\n”, num);
return 0;
}
for (int i = 2; i <= num / 2; i++) {
if (num % i == 0) {
isPrime = 0;
break;
}
}
if (isPrime)
printf(“%d is a prime number.\n”, num);
else
printf(“%d is not a prime number.\n”, num);
return 0;
}
5. Write a C program to reverse a number using a loop.
#include <stdio.h>
int main() {
int num, reversed = 0;
printf(“Enter a number: “);
scanf(“%d”, &num);
while (num != 0) {
int digit = num % 10;
reversed = reversed * 10 + digit;
num /= 10;
}
printf(“Reversed number = %d\n”, reversed);
return 0;
}
6. Write a C program to calculate the sum of the digits of a given number using a while loop.
#include <stdio.h>
int main() {
int num, sum = 0;
printf(“Enter a number: “);
scanf(“%d”, &num);
while (num != 0) {
sum += num % 10;
num /= 10;
}
printf(“Sum of the digits = %d\n”, sum);
return 0;
}
7. Write a C program to print the Fibonacci series up to a given number using a loop.
#include <stdio.h>
int main() {
int n, t1 = 0, t2 = 1, nextTerm;
printf(“Enter the number of terms: “);
scanf(“%d”, &n);
printf(“Fibonacci Series: “);
for (int i = 1; i <= n; i++) {
printf(“%d “, t1);
nextTerm = t1 + t2;
t1 = t2;
t2 = nextTerm;
}
printf(“\n”);
return 0;
}
8. Write a C program to print all numbers between two intervals divisible by a given number using a loop.
#include <stdio.h>
int main() {
int start, end, divisor;
printf(“Enter the start of the interval: “);
scanf(“%d”, &start);
printf(“Enter the end of the interval: “);
scanf(“%d”, &end);
printf(“Enter the divisor: “);
scanf(“%d”, &divisor);
printf(“Numbers divisible by %d between %d and %d:\n”, divisor, start, end);
for (int i = start; i <= end; i++) {
if (i % divisor == 0) {
printf(“%d “, i);
}
}
printf(“\n”);
return 0;
}
9. Write a C program to find the greatest common divisor (GCD) of two numbers using a loop.
#include <stdio.h>
int main() {
int num1, num2, gcd;
printf(“Enter two numbers: “);
scanf(“%d %d”, &num1, &num2);
for (int i = 1; i <= num1 && i <= num2; i++) {
if (num1 % i == 0 && num2 % i == 0) {
gcd = i;
}
}
printf(“GCD of %d and %d = %d\n”, num1, num2, gcd);
return 0;
}
10. Write a C program to print a pattern of stars in a pyramid shape using nested loops.
#include <stdio.h>
int main() {
int rows;
printf(“Enter the number of rows: “);
scanf(“%d”, &rows);
for (int i = 1; i <= rows; i++) {
// Print leading spaces
for (int j = 1; j <= rows – i; j++) {
printf(” “);
}
// Print stars
for (int k = 1; k <= 2 * i – 1; k++) {
printf(“*”);
}
printf(“\n”);
}
return 0;
}
Arrays
1. Write a C program to find the sum and average of all elements in an array.
c
#include <stdio.h>
int main() {
int n, i;
float sum = 0, average;
printf(“Enter the number of elements: “);
scanf(“%d”, &n);
int arr[n];
printf(“Enter the elements: \n”);
for (i = 0; i < n; i++) {
scanf(“%d”, &arr[i]);
sum += arr[i];
}
average = sum / n;
printf(“Sum = %.2f\n”, sum);
printf(“Average = %.2f\n”, average);
return 0;
}
2. Write a C program to find the largest and smallest element in an array.
#include <stdio.h>
int main() {
int n, i, largest, smallest;
printf(“Enter the number of elements: “);
scanf(“%d”, &n);
int arr[n];
printf(“Enter the elements: \n”);
for (i = 0; i < n; i++) {
scanf(“%d”, &arr[i]);
}
largest = smallest = arr[0];
for (i = 1; i < n; i++) {
if (arr[i] > largest)
largest = arr[i];
if (arr[i] < smallest)
smallest = arr[i];
}
printf(“Largest element = %d\n”, largest);
printf(“Smallest element = %d\n”, smallest);
return 0;
}
3. Write a C program to sort an array in ascending order using the bubble sort algorithm.
#include <stdio.h>
int main() {
int n, i, j, temp;
printf(“Enter the number of elements: “);
scanf(“%d”, &n);
int arr[n];
printf(“Enter the elements: \n”);
for (i = 0; i < n; i++) {
scanf(“%d”, &arr[i]);
}
// Bubble Sort Algorithm
for (i = 0; i < n-1; i++) {
for (j = 0; j < n-i-1; j++) {
if (arr[j] > arr[j+1]) {
// Swap the elements
temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
}
}
}
printf(“Array in ascending order: \n”);
for (i = 0; i < n; i++) {
printf(“%d “, arr[i]);
}
printf(“\n”);
return 0;
}
4. Write a C program to find the frequency of each element in an array.
#include <stdio.h>
int main() {
int n, i, j, count;
printf(“Enter the number of elements: “);
scanf(“%d”, &n);
int arr[n], freq[n];
printf(“Enter the elements: \n”);
for (i = 0; i < n; i++) {
scanf(“%d”, &arr[i]);
freq[i] = -1;
}
for (i = 0; i < n; i++) {
count = 1;
for (j = i + 1; j < n; j++) {
if (arr[i] == arr[j]) {
count++;
freq[j] = 0; // Mark as visited
}
}
if (freq[i] != 0) {
freq[i] = count;
}
}
printf(“Element – Frequency\n”);
for (i = 0; i < n; i++) {
if (freq[i] != 0) {
printf(“%d – %d\n”, arr[i], freq[i]);
}
}
return 0;
}
5. Write a C program to merge two sorted arrays into a single sorted array.
#include <stdio.h>
int main() {
int n1, n2, i, j, k;
printf(“Enter the number of elements in the first array: “);
scanf(“%d”, &n1);
int arr1[n1];
printf(“Enter elements of the first sorted array: \n”);
for (i = 0; i < n1; i++) {
scanf(“%d”, &arr1[i]);
}
printf(“Enter the number of elements in the second array: “);
scanf(“%d”, &n2);
int arr2[n2];
printf(“Enter elements of the second sorted array: \n”);
for (i = 0; i < n2; i++) {
scanf(“%d”, &arr2[i]);
}
int merged[n1 + n2];
i = j = k = 0;
// Merging the arrays
while (i < n1 && j < n2) {
if (arr1[i] < arr2[j]) {
merged[k++] = arr1[i++];
} else {
merged[k++] = arr2[j++];
}
}
// Copy remaining elements
while (i < n1) {
merged[k++] = arr1[i++];
}
while (j < n2) {
merged[k++] = arr2[j++];
}
printf(“Merged sorted array: \n”);
for (i = 0; i < n1 + n2; i++) {
printf(“%d “, merged[i]);
}
printf(“\n”);
return 0;
}
6. Write a C program to search for an element in an array using linear search.
#include <stdio.h>
int main() {
int n, i, searchElement, found = 0;
printf(“Enter the number of elements: “);
scanf(“%d”, &n);
int arr[n];
printf(“Enter the elements: \n”);
for (i = 0; i < n; i++) {
scanf(“%d”, &arr[i]);
}
printf(“Enter the element to search: “);
scanf(“%d”, &searchElement);
for (i = 0; i < n; i++) {
if (arr[i] == searchElement) {
printf(“Element %d found at position %d.\n”, searchElement, i + 1);
found = 1;
break;
}
}
if (!found) {
printf(“Element %d not found in the array.\n”, searchElement);
}
return 0;
}
7. Write a C program to insert an element at a specific position in an array.
#include <stdio.h>
int main() {
int n, i, position, newElement;
printf(“Enter the number of elements: “);
scanf(“%d”, &n);
int arr[n+1];
printf(“Enter the elements: \n”);
for (i = 0; i < n; i++) {
scanf(“%d”, &arr[i]);
}
printf(“Enter the position where you want to insert the new element: “);
scanf(“%d”, &position);
printf(“Enter the new element: “);
scanf(“%d”, &newElement);
for (i = n; i >= position; i–) {
arr[i] = arr[i-1];
}
arr[position – 1] = newElement;
printf(“Array after insertion: \n”);
for (i = 0; i <= n; i++) {
printf(“%d “, arr[i]);
}
printf(“\n”);
return 0;
}
8. Write a C program to delete an element from an array at a specific position.
#include <stdio.h>
int main() {
int n, i, position;
printf(“Enter the number of elements: “);
scanf(“%d”, &n);
int arr[n];
printf(“Enter the elements: \n”);
for (i = 0; i < n; i++) {
scanf(“%d”, &arr[i]);
}
printf(“Enter the position of the element to delete: “);
scanf(“%d”, &position);
if (position >= n + 1 || position < 1) {
printf(“Invalid position!\n”);
} else {
for (i = position – 1; i < n – 1; i++) {
arr[i] = arr[i + 1];
}
printf(“Array after deletion: \n”);
for (i = 0; i < n – 1; i++) {
printf(“%d “, arr[i]);
}
printf(“\n”);
}
return 0;
}
9. Write a C program to find the second largest element in an array.
#include <stdio.h>
int main() {
int n, i, largest, secondLargest;
printf(“Enter the number of elements: “);
scanf(“%d”, &n);
int arr[n];
printf(“Enter the elements: \n”);
for (i = 0; i < n; i++) {
scanf(“%d”, &arr[i]);
}
largest = secondLargest = arr[0];
for (i = 1; i < n; i++) {
if (arr[i] > largest) {
secondLargest = largest;
largest = arr[i];
} else if (arr[i] > secondLargest && arr[i] != largest) {
secondLargest = arr[i];
}
}
printf(“Second largest element = %d\n”, secondLargest);
return 0;
}
10. Write a C program to rotate the elements of an array by a given number of positions.
#include <stdio.h>
void rotateArray(int arr[], int n, int positions) {
int temp[positions];
// Store the first ‘positions’ elements in temp array
for (int i = 0; i < positions; i++) {
temp[i] = arr[i];
}
// Shift the elements of the array to the left
for (int i = 0; i < n – positions; i++) {
arr[i] = arr[i + positions];
}
// Move the elements from temp to the end of the array
for (int i = 0; i < positions; i++) {
arr[n – positions + i] = temp[i];
}
}
int main() {
int n, positions, i;
printf(“Enter the number of elements: “);
scanf(“%d”, &n);
int arr[n];
printf(“Enter the elements: \n”);
for (i = 0; i < n; i++) {
scanf(“%d”, &arr[i]);
}
printf(“Enter the number of positions to rotate: “);
scanf(“%d”, &positions);
rotateArray(arr, n, positions);
printf(“Array after rotation: \n”);
for (i = 0; i < n; i++) {
printf(“%d “, arr[i]);
}
printf(“\n”);
return 0;
}
Strings
1. Write a C program to find the length of a string without using the `strlen()` function.
c
#include <stdio.h>
int main() {
char str[100];
int length = 0;
printf(“Enter a string: “);
gets(str);
while (str[length] != ‘\0’) {
length++;
}
printf(“Length of the string = %d\n”, length);
return 0;
}
2. Write a C program to reverse a string without using the `strrev()` function.
#include <stdio.h>
int main() {
char str[100];
int length = 0, i;
printf(“Enter a string: “);
gets(str);
// Calculate the length of the string
while (str[length] != ‘\0’) {
length++;
}
// Print the string in reverse order
printf(“Reversed string: “);
for (i = length – 1; i >= 0; i–) {
printf(“%c”, str[i]);
}
printf(“\n”);
return 0;
}
3. Write a C program to check whether a string is a palindrome.
#include <stdio.h>
#include <string.h>
int main() {
char str[100];
int length, i, isPalindrome = 1;
printf(“Enter a string: “);
gets(str);
length = strlen(str);
for (i = 0; i < length / 2; i++) {
if (str[i] != str[length – i – 1]) {
isPalindrome = 0;
break;
}
}
if (isPalindrome)
printf(“The string is a palindrome.\n”);
else
printf(“The string is not a palindrome.\n”);
return 0;
}
4. Write a C program to concatenate two strings without using the `strcat()` function.
#include <stdio.h>
int main() {
char str1[100], str2[100];
int i = 0, j = 0;
printf(“Enter the first string: “);
gets(str1);
printf(“Enter the second string: “);
gets(str2);
// Move to the end of the first string
while (str1[i] != ‘\0’) {
i++;
}
// Copy the second string to the end of the first string
while (str2[j] != ‘\0’) {
str1[i] = str2[j];
i++;
j++;
}
str1[i] = ‘\0’; // Null-terminate the concatenated string
printf(“Concatenated string: %s\n”, str1);
return 0;
}
5. Write a C program to count the number of vowels and consonants in a string.
#include <stdio.h>
int main() {
char str[100];
int i = 0, vowels = 0, consonants = 0;
printf(“Enter a string: “);
gets(str);
while (str[i] != ‘\0’) {
if ((str[i] >= ‘a’ && str[i] <= ‘z’) || (str[i] >= ‘A’ && str[i] <= ‘Z’)) {
if (str[i] == ‘a’ || str[i] == ‘e’ || str[i] == ‘i’ || str[i] == ‘o’ || str[i] == ‘u’ ||
str[i] == ‘A’ || str[i] == ‘E’ || str[i] == ‘I’ || str[i] == ‘O’ || str[i] == ‘U’) {
vowels++;
} else {
consonants++;
}
}
i++;
}
printf(“Vowels: %d\n”, vowels);
printf(“Consonants: %d\n”, consonants);
return 0;
}
6. Write a C program to find the frequency of each character in a string.
#include <stdio.h>
int main() {
char str[100];
int freq[256] = {0}, i;
printf(“Enter a string: “);
gets(str);
for (i = 0; str[i] != ‘\0’; i++) {
freq[(int)str[i]]++;
}
printf(“Character frequencies:\n”);
for (i = 0; i < 256; i++) {
if (freq[i] != 0) {
printf(“%c: %d\n”, i, freq[i]);
}
}
return 0;
}
7. Write a C program to convert all lowercase characters in a string to uppercase.
#include <stdio.h>
int main() {
char str[100];
int i = 0;
printf(“Enter a string: “);
gets(str);
while (str[i] != ‘\0’) {
if (str[i] >= ‘a’ && str[i] <= ‘z’) {
str[i] = str[i] – 32;
}
i++;
}
printf(“String in uppercase: %s\n”, str);
return 0;
}
8. Write a C program to remove all occurrences of a character from a string.
#include <stdio.h>
int main() {
char str[100], ch;
int i, j;
printf(“Enter a string: “);
gets(str);
printf(“Enter the character to remove: “);
scanf(“%c”, &ch);
for (i = 0; str[i] != ‘\0’; i++) {
if (str[i] == ch) {
for (j = i; str[j] != ‘\0’; j++) {
str[j] = str[j + 1];
}
i–; // Adjust index since characters shifted left
}
}
printf(“String after removal: %s\n”, str);
return 0;
}
9. Write a C program to find the first occurrence of a substring within a string.
#include <stdio.h>
#include <string.h>
int main() {
char str[100], subStr[100];
int i, j, found, strLen, subStrLen;
printf(“Enter the main string: “);
gets(str);
printf(“Enter the substring: “);
gets(subStr);
strLen = strlen(str);
subStrLen = strlen(subStr);
for (i = 0; i <= strLen – subStrLen; i++) {
found = 1;
for (j = 0; j < subStrLen; j++) {
if (str[i + j] != subStr[j]) {
found = 0;
break;
}
}
if (found) {
printf(“Substring found at position %d.\n”, i + 1);
return 0;
}
}
printf(“Substring not found.\n”);
return 0;
}
10. Write a C program to replace a substring within a string with another substring.
#include <stdio.h>
#include <string.h>
int main() {
char str[200], subStr[100], replaceStr[100];
char result[300];
int i, j, k, found, strLen, subStrLen, replaceStrLen;
printf(“Enter the main string: “);
gets(str);
printf(“Enter the substring to replace: “);
gets(subStr);
printf(“Enter the new substring: “);
gets(replaceStr);
strLen = strlen(str);
subStrLen = strlen(subStr);
replaceStrLen = strlen(replaceStr);
for (i = 0; i < strLen; i++) {
found = 1;
for (j = 0; j < subStrLen; j++) {
if (str[i + j] != subStr[j]) {
found = 0;
break;
}
}
if (found) {
for (k = 0; k < replaceStrLen; k++) {
result[i + k] = replaceStr[k];
}
i += subStrLen – 1;
} else {
result[i] = str[i];
}
}
result[i] = ‘\0’;
printf(“String after replacement: %s\n”, result);
return 0;
}
Functions
1. Write a C program to find the factorial of a number using recursion.
#include <stdio.h>
unsigned long long factorial(int n) {
if (n == 0 || n == 1)
return 1;
else
return n * factorial(n – 1);
}
int main() {
int num;
printf(“Enter a number: “);
scanf(“%d”, &num);
if (num < 0)
printf(“Factorial of a negative number doesn’t exist.\n”);
else
printf(“Factorial of %d = %llu\n”, num, factorial(num));
return 0;
}
2. Write a C program to calculate the nth Fibonacci number using a function.
#include <stdio.h>
int fibonacci(int n) {
if (n == 0)
return 0;
else if (n == 1)
return 1;
else
return fibonacci(n – 1) + fibonacci(n – 2);
}
int main() {
int n;
printf(“Enter the position of the Fibonacci sequence: “);
scanf(“%d”, &n);
printf(“Fibonacci number at position %d = %d\n”, n, fibonacci(n));
return 0;
}
3. Write a C program to swap two numbers using a function and pass by reference.
#include <stdio.h>
void swap(int *x, int *y) {
int temp;
temp = *x;
*x = *y;
*y = temp;
}
int main() {
int a, b;
printf(“Enter two numbers: “);
scanf(“%d %d”, &a, &b);
printf(“Before swapping: a = %d, b = %d\n”, a, b);
swap(&a, &b);
printf(“After swapping: a = %d, b = %d\n”, a, b);
return 0;
}
4. Write a C program to find the GCD of two numbers using a recursive function.
#include <stdio.h>
int gcd(int a, int b) {
if (b == 0)
return a;
else
return gcd(b, a % b);
}
int main() {
int num1, num2;
printf(“Enter two numbers: “);
scanf(“%d %d”, &num1, &num2);
printf(“GCD of %d and %d = %d\n”, num1, num2, gcd(num1, num2));
return 0;
}
5. Write a C program to find the power of a number using a function.
#include <stdio.h>
int power(int base, int exp) {
int result = 1;
for (int i = 0; i < exp; i++) {
result *= base;
}
return result;
}
int main() {
int base, exp;
printf(“Enter the base and exponent: “);
scanf(“%d %d”, &base, &exp);
printf(“%d raised to the power of %d = %d\n”, base, exp, power(base, exp));
return 0;
}
6. Write a C program to check if a number is prime using a function.
#include <stdio.h>
int isPrime(int n) {
if (n <= 1)
return 0;
for (int i = 2; i <= n / 2; i++) {
if (n % i == 0)
return 0;
}
return 1;
}
int main() {
int num;
printf(“Enter a number: “);
scanf(“%d”, &num);
if (isPrime(num))
printf(“%d is a prime number.\n”, num);
else
printf(“%d is not a prime number.\n”, num);
return 0;
}
7. Write a C program to find the sum of all elements in an array using a function.
#include <stdio.h>
int sumOfArray(int arr[], int n) {
int sum = 0;
for (int i = 0; i < n; i++) {
sum += arr[i];
}
return sum;
}
int main() {
int n;
printf(“Enter the number of elements in the array: “);
scanf(“%d”, &n);
int arr[n];
printf(“Enter the elements of the array: \n”);
for (int i = 0; i < n; i++) {
scanf(“%d”, &arr[i]);
}
printf(“Sum of the array elements = %d\n”, sumOfArray(arr, n));
return 0;
}
8. Write a C program to convert temperature from Celsius to Fahrenheit using a function.
#include <stdio.h>
float celsiusToFahrenheit(float celsius) {
return (celsius * 9 / 5) + 32;
}
int main() {
float celsius;
printf(“Enter temperature in Celsius: “);
scanf(“%f”, &celsius);
printf(“Temperature in Fahrenheit = %.2f\n”, celsiusToFahrenheit(celsius));
return 0;
}
9. Write a C program to check whether a number is even or odd using a function.
#include <stdio.h>
int isEven(int n) {
return (n % 2 == 0);
}
int main() {
int num;
printf(“Enter a number: “);
scanf(“%d”, &num);
if (isEven(num))
printf(“%d is even.\n”, num);
else
printf(“%d is odd.\n”, num);
return 0;
}
10. Write a C program to sort an array using a function that implements the selection sort algorithm.
#include <stdio.h>
void selectionSort(int arr[], int n) {
int i, j, minIndex, temp;
for (i = 0; i < n – 1; i++) {
minIndex = i;
for (j = i + 1; j < n; j++) {
if (arr[j] < arr[minIndex]) {
minIndex = j;
}
}
// Swap the found minimum element with the first element
temp = arr[minIndex];
arr[minIndex] = arr[i];
arr[i] = temp;
}
}
int main() {
int n;
printf(“Enter the number of elements in the array: “);
scanf(“%d”, &n);
int arr[n];
printf(“Enter the elements of the array: \n”);
for (int i = 0; i < n; i++) {
scanf(“%d”, &arr[i]);
}
selectionSort(arr, n);
printf(“Sorted array: \n”);
for (int i = 0; i < n; i++) {
printf(“%d “, arr[i]);
}
printf(“\n”);
return 0;
}
Pointers
1. Write a C program to swap two numbers using pointers.
#include <stdio.h>
void swap(int *x, int *y) {
int temp;
temp = *x;
*x = *y;
*y = temp;
}
int main() {
int a, b;
printf(“Enter two numbers: “);
scanf(“%d %d”, &a, &b);
printf(“Before swapping: a = %d, b = %d\n”, a, b);
swap(&a, &b);
printf(“After swapping: a = %d, b = %d\n”, a, b);
return 0;
}
2. Write a C program to find the sum of all elements in an array using pointers.
#include <stdio.h>
int sumOfArray(int *arr, int n) {
int sum = 0;
for (int i = 0; i < n; i++) {
sum += *(arr + i);
}
return sum;
}
int main() {
int n;
printf(“Enter the number of elements in the array: “);
scanf(“%d”, &n);
int arr[n];
printf(“Enter the elements: \n”);
for (int i = 0; i < n; i++) {
scanf(“%d”, &arr[i]);
}
printf(“Sum of array elements = %d\n”, sumOfArray(arr, n));
return 0;
}
3. Write a C program to reverse a string using pointers.
#include <stdio.h>
#include <string.h>
void reverseString(char *str) {
int length = strlen(str);
char *start = str;
char *end = str + length – 1;
char temp;
while (start < end) {
temp = *start;
*start = *end;
*end = temp;
start++;
end–;
}
}
int main() {
char str[100];
printf(“Enter a string: “);
gets(str);
reverseString(str);
printf(“Reversed string: %s\n”, str);
return 0;
}
4. Write a C program to find the length of a string using pointers.
#include <stdio.h>
int stringLength(char *str) {
int length = 0;
while (*str != ‘\0’) {
length++;
str++;
}
return length;
}
int main() {
char str[100];
printf(“Enter a string: “);
gets(str);
printf(“Length of the string = %d\n”, stringLength(str));
return 0;
}
5. Write a C program to find the largest element in an array using pointers.
#include <stdio.h>
int findLargest(int *arr, int n) {
int largest = *arr;
for (int i = 1; i < n; i++) {
if (*(arr + i) > largest) {
largest = *(arr + i);
}
}
return largest;
}
int main() {
int n;
printf(“Enter the number of elements in the array: “);
scanf(“%d”, &n);
int arr[n];
printf(“Enter the elements: \n”);
for (int i = 0; i < n; i++) {
scanf(“%d”, &arr[i]);
}
printf(“Largest element = %d\n”, findLargest(arr, n));
return 0;
}
6. Write a C program to sort an array using pointers.
#include <stdio.h>
void sortArray(int *arr, int n) {
int i, j, temp;
for (i = 0; i < n – 1; i++) {
for (j = i + 1; j < n; j++) {
if (*(arr + i) > *(arr + j)) {
temp = *(arr + i);
*(arr + i) = *(arr + j);
*(arr + j) = temp;
}
}
}
}
int main() {
int n;
printf(“Enter the number of elements in the array: “);
scanf(“%d”, &n);
int arr[n];
printf(“Enter the elements: \n”);
for (int i = 0; i < n; i++) {
scanf(“%d”, &arr[i]);
}
sortArray(arr, n);
printf(“Sorted array: \n”);
for (int i = 0; i < n; i++) {
printf(“%d “, arr[i]);
}
printf(“\n”);
return 0;
}
7. Write a C program to copy the contents of one string to another using pointers.
#include <stdio.h>
void copyString(char *source, char *destination) {
while (*source != ‘\0’) {
*destination = *source;
source++;
destination++;
}
*destination = ‘\0’; // Null-terminate the destination string
}
int main() {
char source[100], destination[100];
printf(“Enter a string: “);
gets(source);
copyString(source, destination);
printf(“Copied string: %s\n”, destination);
return 0;
}
8. Write a C program to add two numbers using pointers.
#include <stdio.h>
int add(int *x, int *y) {
return *x + *y;
}
int main() {
int a, b;
printf(“Enter two numbers: “);
scanf(“%d %d”, &a, &b);
printf(“Sum = %d\n”, add(&a, &b));
return 0;
}
9. Write a C program to find the difference between two pointers.
#include <stdio.h>
int main() {
int arr[5] = {1, 2, 3, 4, 5};
int *ptr1 = &arr[1]; // Points to the second element
int *ptr2 = &arr[4]; // Points to the fifth element
printf(“Difference between pointers: %ld\n”, ptr2 – ptr1);
return 0;
}
10. Write a C program to access and modify array elements using pointer notation.
#include <stdio.h>
int main() {
int n, i;
printf(“Enter the number of elements in the array: “);
scanf(“%d”, &n);
int arr[n];
printf(“Enter the elements: \n”);
for (i = 0; i < n; i++) {
scanf(“%d”, &arr[i]);
}
// Access and modify elements using pointer notation
for (i = 0; i < n; i++) {
*(arr + i) += 10; // Add 10 to each element
}
printf(“Modified array: \n”);
for (i = 0; i < n; i++) {
printf(“%d “, *(arr + i));
}
printf(“\n”);
return 0;
}
Structures
1. Write a C program to store and display information of a student using a structure.
#include <stdio.h>
struct Student {
char name[50];
int roll;
float marks;
};
int main() {
struct Student s;
printf(“Enter name: “);
gets(s.name);
printf(“Enter roll number: “);
scanf(“%d”, &s.roll);
printf(“Enter marks: “);
scanf(“%f”, &s.marks);
printf(“\nStudent Information:\n”);
printf(“Name: %s\n”, s.name);
printf(“Roll Number: %d\n”, s.roll);
printf(“Marks: %.2f\n”, s.marks);
return 0;
}
2. Write a C program to add two complex numbers using structures.
#include <stdio.h>
struct Complex {
float real;
float imag;
};
int main() {
struct Complex c1, c2, result;
printf(“Enter real and imaginary part of first complex number: “);
scanf(“%f %f”, &c1.real, &c1.imag);
printf(“Enter real and imaginary part of second complex number: “);
scanf(“%f %f”, &c2.real, &c2.imag);
result.real = c1.real + c2.real;
result.imag = c1.imag + c2.imag;
printf(“Sum of complex numbers = %.2f + %.2fi\n”, result.real, result.imag);
return 0;
}
3. Write a C program to store and display information of multiple employees using structures.
c
#include <stdio.h>
struct Employee {
char name[50];
int id;
float salary;
};
int main() {
int n;
printf(“Enter the number of employees: “);
scanf(“%d”, &n);
struct Employee emp[n];
for (int i = 0; i < n; i++) {
printf(“\nEnter information for employee %d:\n”, i + 1);
printf(“Name: “);
getchar(); // to consume the newline character left by previous input
gets(emp[i].name);
printf(“ID: “);
scanf(“%d”, &emp[i].id);
printf(“Salary: “);
scanf(“%f”, &emp[i].salary);
}
printf(“\nEmployee Information:\n”);
for (int i = 0; i < n; i++) {
printf(“\nEmployee %d:\n”, i + 1);
printf(“Name: %s\n”, emp[i].name);
printf(“ID: %d\n”, emp[i].id);
printf(“Salary: %.2f\n”, emp[i].salary);
}
return 0;
}
4. Write a C program to find the sum of distances (in feet and inches) using structures.
#include <stdio.h>
struct Distance {
int feet;
float inches;
};
int main() {
struct Distance d1, d2, result;
printf(“Enter feet and inches for first distance: “);
scanf(“%d %f”, &d1.feet, &d1.inches);
printf(“Enter feet and inches for second distance: “);
scanf(“%d %f”, &d2.feet, &d2.inches);
result.feet = d1.feet + d2.feet;
result.inches = d1.inches + d2.inches;
// Convert inches to feet if greater than 12
if (result.inches >= 12.0) {
result.feet += (int)result.inches / 12;
result.inches = (int)result.inches % 12;
}
printf(“Sum of distances = %d feet %.2f inches\n”, result.feet, result.inches);
return 0;
}
5. Write a C program to store and display details of a book (title, author, price) using a structure.
c
#include <stdio.h>
struct Book {
char title[100];
char author[100];
float price;
};
int main() {
struct Book b;
printf(“Enter the book title: “);
getchar(); // to consume the newline character left by previous input
gets(b.title);
printf(“Enter the author name: “);
gets(b.author);
printf(“Enter the book price: “);
scanf(“%f”, &b.price);
printf(“\nBook Details:\n”);
printf(“Title: %s\n”, b.title);
printf(“Author: %s\n”, b.author);
printf(“Price: %.2f\n”, b.price);
return 0;
}
6. Write a C program to sort an array of structures based on student marks.
#include <stdio.h>
struct Student {
char name[50];
int roll;
float marks;
};
void sortStudents(struct Student s[], int n) {
struct Student temp;
for (int i = 0; i < n – 1; i++) {
for (int j = 0; j < n – i – 1; j++) {
if (s[j].marks < s[j + 1].marks) {
temp = s[j];
s[j] = s[j + 1];
s[j + 1] = temp;
}
}
}
}
int main() {
int n;
printf(“Enter the number of students: “);
scanf(“%d”, &n);
struct Student s[n];
for (int i = 0; i < n; i++) {
printf(“\nEnter information for student %d:\n”, i + 1);
printf(“Name: “);
getchar(); // to consume the newline character left by previous input
gets(s[i].name);
printf(“Roll number: “);
scanf(“%d”, &s[i].roll);
printf(“Marks: “);
scanf(“%f”, &s[i].marks);
}
sortStudents(s, n);
printf(“\nStudents sorted by marks in descending order:\n”);
for (int i = 0; i < n; i++) {
printf(“%s (Roll: %d, Marks: %.2f)\n”, s[i].name, s[i].roll, s[i].marks);
}
return 0;
}
7. Write a C program to calculate the total and average marks of students using structures.
#include <stdio.h>
struct Student {
char name[50];
int roll;
float marks;
};
int main() {
int n;
float total = 0, average;
printf(“Enter the number of students: “);
scanf(“%d”, &n);
struct Student s[n];
for (int i = 0; i < n; i++) {
printf(“\nEnter information for student %d:\n”, i + 1);
printf(“Name: “);
getchar(); // to consume the newline character left by previous input
gets(s[i].name);
printf(“Roll number: “);
scanf(“%d”, &s[i].roll);
printf(“Marks: “);
scanf(“%f”, &s[i].marks);
total += s[i].marks;
}
average = total / n;
printf(“\nTotal Marks = %.2f\n”, total);
printf(“Average Marks = %.2f\n”, average);
return 0;
}
8. Write a C program to merge two date structures and display the result.
#include <stdio.h>
struct Date {
int day;
int month;
int year;
};
struct Date mergeDates(struct Date d1, struct Date d2) {
struct Date result;
result.day = d1.day + d2.day;
result.month = d1.month + d2.month;
result.year = d1.year + d2.year;
if (result.day > 30) { // Assuming each month has 30 days
result.day -= 30;
result.month++;
}
if (result.month > 12) {
result.month -= 12;
result.year++;
}
return result;
}
int main() {
struct Date d1, d2, merged;
printf(“Enter first date (day month year): “);
scanf(“%d %d %d”, &d1.day, &d1.month, &d1.year);
printf(“Enter second date (day month year): “);
scanf(“%d %d %d”, &d2.day, &d2.month, &d2.year);
merged = mergeDates(d1, d2);
printf(“Merged Date: %d/%d/%d\n”, merged.day, merged.month, merged.year);
return 0;
}
9. Write a C program to perform addition and subtraction of two time structures (hours, minutes, seconds).
#include <stdio.h>
struct Time {
int hours;
int minutes;
int seconds;
};
struct Time addTimes(struct Time t1, struct Time t2) {
struct Time result;
result.seconds = t1.seconds + t2.seconds;
result.minutes = t1.minutes + t2.minutes + result.seconds / 60;
result.hours = t1.hours + t2.hours + result.minutes / 60
;
result.seconds %= 60;
result.minutes %= 60;
return result;
}
struct Time subtractTimes(struct Time t1, struct Time t2) {
struct Time result;
result.seconds = t1.seconds – t2.seconds;
result.minutes = t1.minutes – t2.minutes;
result.hours = t1.hours – t2.hours;
if (result.seconds < 0) {
result.seconds += 60;
result.minutes–;
}
if (result.minutes < 0) {
result.minutes += 60;
result.hours–;
}
return result;
}
int main() {
struct Time t1, t2, added, subtracted;
printf(“Enter first time (hours minutes seconds): “);
scanf(“%d %d %d”, &t1.hours, &t1.minutes, &t1.seconds);
printf(“Enter second time (hours minutes seconds): “);
scanf(“%d %d %d”, &t2.hours, &t2.minutes, &t2.seconds);
added = addTimes(t1, t2);
subtracted = subtractTimes(t1, t2);
printf(“Added Time: %02d:%02d:%02d\n”, added.hours, added.minutes, added.seconds);
printf(“Subtracted Time: %02d:%02d:%02d\n”, subtracted.hours, subtracted.minutes, subtracted.seconds);
return 0;
}
10. Write a C program to compare the age of two persons using structures.
#include <stdio.h>
struct Person {
char name[50];
int age;
};
void compareAge(struct Person p1, struct Person p2) {
if (p1.age > p2.age)
printf(“%s is older than %s.\n”, p1.name, p2.name);
else if (p1.age < p2.age)
printf(“%s is older than %s.\n”, p2.name, p1.name);
else
printf(“%s and %s are of the same age.\n”, p1.name, p2.name);
}
int main() {
struct Person p1, p2;
printf(“Enter the name and age of the first person: “);
scanf(“%s %d”, p1.name, &p1.age);
printf(“Enter the name and age of the second person: “);
scanf(“%s %d”, p2.name, &p2.age);
compareAge(p1, p2);
return 0;
}
File Handling
1. Write a C program to create a text file and write user input into it.
#include <stdio.h>
int main() {
FILE *fptr;
char filename[50], data[1000];
printf(“Enter the filename to create: “);
gets(filename);
fptr = fopen(filename, “w”); // Open file in write mode
if (fptr == NULL) {
printf(“Error opening file!\n”);
return 1;
}
printf(“Enter text to write into the file:\n”);
gets(data);
fprintf(fptr, “%s”, data); // Write data to the file
fclose(fptr);
printf(“File created and data written successfully.\n”);
return 0;
}
2. Write a C program to read the contents of a text file and display them on the console.
#include <stdio.h>
int main() {
FILE *fptr;
char filename[50], ch;
printf(“Enter the filename to read: “);
gets(filename);
fptr = fopen(filename, “r”); // Open file in read mode
if (fptr == NULL) {
printf(“Error opening file!\n”);
return 1;
}
printf(“Contents of the file:\n”);
while ((ch = fgetc(fptr)) != EOF) { // Read character by character
putchar(ch);
}
fclose(fptr);
return 0;
}
3. Write a C program to copy the contents of one file to another.
#include <stdio.h>
int main() {
FILE *sourceFile, *targetFile;
char sourceFilename[50], targetFilename[50], ch;
printf(“Enter the source filename: “);
gets(sourceFilename);
sourceFile = fopen(sourceFilename, “r”); // Open source file in read mode
if (sourceFile == NULL) {
printf(“Error opening source file!\n”);
return 1;
}
printf(“Enter the target filename: “);
gets(targetFilename);
targetFile = fopen(targetFilename, “w”); // Open target file in write mode
if (targetFile == NULL) {
printf(“Error opening target file!\n”);
return 1;
}
while ((ch = fgetc(sourceFile)) != EOF) { // Read from source and write to target
fputc(ch, targetFile);
}
printf(“File copied successfully.\n”);
fclose(sourceFile);
fclose(targetFile);
return 0;
}
4. Write a C program to count the number of words, characters, and lines in a text file.
#include <stdio.h>
int main() {
FILE *fptr;
char filename[50], ch;
int characters = 0, words = 0, lines = 0;
printf(“Enter the filename to read: “);
gets(filename);
fptr = fopen(filename, “r”); // Open file in read mode
if (fptr == NULL) {
printf(“Error opening file!\n”);
return 1;
}
while ((ch = fgetc(fptr)) != EOF) {
characters++;
if (ch == ‘ ‘ || ch == ‘\n’) {
words++;
}
if (ch == ‘\n’) {
lines++;
}
}
// The last word and line might not be counted properly
if (characters > 0) {
words++;
lines++;
}
printf(“Characters: %d\n”, characters);
printf(“Words: %d\n”, words);
printf(“Lines: %d\n”, lines);
fclose(fptr);
return 0;
}
5. Write a C program to append content to an existing file.
#include <stdio.h>
int main() {
FILE *fptr;
char filename[50], data[1000];
printf(“Enter the filename to append content: “);
gets(filename);
fptr = fopen(filename, “a”); // Open file in append mode
if (fptr == NULL) {
printf(“Error opening file!\n”);
return 1;
}
printf(“Enter text to append to the file:\n”);
gets(data);
fprintf(fptr, “%s”, data); // Append data to the file
fclose(fptr);
printf(“Data appended successfully.\n”);
return 0;
}
6. Write a C program to merge the contents of two files into a third file.
#include <stdio.h>
int main() {
FILE *file1, *file2, *file3;
char file1name[50], file2name[50], file3name[50], ch;
printf(“Enter the first filename: “);
gets(file1name);
printf(“Enter the second filename: “);
gets(file2name);
printf(“Enter the third filename to store merged content: “);
gets(file3name);
file1 = fopen(file1name, “r”);
file2 = fopen(file2name, “r”);
file3 = fopen(file3name, “w”);
if (file1 == NULL || file2 == NULL || file3 == NULL) {
printf(“Error opening files!\n”);
return 1;
}
// Copy content of first file to the third file
while ((ch = fgetc(file1)) != EOF) {
fputc(ch, file3);
}
// Copy content of second file to the third file
while ((ch = fgetc(file2)) != EOF) {
fputc(ch, file3);
}
printf(“Files merged successfully into %s.\n”, file3name);
fclose(file1);
fclose(file2);
fclose(file3);
return 0;
}
7. Write a C program to find the frequency of a word in a text file.
#include <stdio.h>
#include <string.h>
#include <ctype.h>
int main() {
FILE *fptr;
char filename[50], word[50], fileWord[50];
int frequency = 0;
printf(“Enter the filename: “);
gets(filename);
printf(“Enter the word to find its frequency: “);
gets(word);
fptr = fopen(filename, “r”); // Open file in read mode
if (fptr == NULL) {
printf(“Error opening file!\n”);
return 1;
}
while (fscanf(fptr, “%s”, fileWord) != EOF) {
// Convert both words to lowercase for case-insensitive comparison
for (int i = 0; fileWord[i]; i++) {
fileWord[i] = tolower(fileWord[i]);
}
for (int i = 0; word[i]; i++) {
word[i] = tolower(word[i]);
}
if (strcmp(fileWord, word) == 0) {
frequency++;
}
}
printf(“The word ‘%s’ occurs %d times in the file.\n”, word, frequency);
fclose(fptr);
return 0;
}
8. Write a C program to display only the even-numbered lines of a file.
#include <stdio.h>
int main() {
FILE *fptr;
char filename[50], line[200];
int lineNumber = 1;
printf(“Enter the filename: “);
gets(filename);
fptr = fopen(filename, “r”); // Open file in read mode
if (fptr == NULL) {
printf(“Error opening file!\n”);
return 1;
}
while (fgets(line, sizeof(line), fptr)) {
if (lineNumber % 2 == 0) {
printf(“%s”, line);
}
lineNumber++;
}
fclose(fptr);
return 0;
}
9. Write a C program to encrypt the contents of a file.
#include <stdio.h>
int main() {
FILE *fptr;
char filename[50], ch;
int key;
printf(“Enter the filename: “);
gets(filename);
printf(“Enter the encryption key (a number): “);
scanf(“%d”, &key);
fptr = fopen(filename, “r+”); // Open file in read/write mode
if (fptr == NULL) {
printf(“Error opening file!\n”);
return 1;
}
while ((ch = fgetc(fptr)) != EOF) {
fseek(fptr, -1, SEEK_CUR); // Move the file pointer back
fputc(ch + key, fptr); // Encrypt by adding the key value
fseek(fptr, 0, SEEK_CUR); // Move file pointer forward
}
printf(“File encrypted successfully.\n”);
fclose(fptr);
return 0;
}
10. Write a C program to delete a specific line from a text file.
#include <stdio.h>
int main() {
FILE *fptr, *tempFile;
char filename[50], tempFilename[50] = “temp.txt”;
char line[200];
int lineToDelete, lineNumber = 1;
printf(“Enter the filename: “);
gets(filename);
printf(“Enter the line number to delete: “);
scanf(“%d”, &lineToDelete);
fptr = fopen(filename, “r”); // Open file in read mode
tempFile = fopen(tempFilename, “w”); // Open temporary file in write mode
if (fptr == NULL || tempFile == NULL) {
printf(“Error opening files!\n”);
return 1;
}
while (fgets(line, sizeof(line), fptr)) {
if (lineNumber != lineToDelete) {
fputs(line, tempFile); // Copy lines except the one to delete
}
lineNumber++;
}
fclose(fptr);
fclose(tempFile);sddsd
// Replace original file with the updated file
remove(filename);
rename(tempFilename, filename);
printf(“Line %d deleted successfully.\n”, lineToDelete);
return 0;
}