Program 1 - Write a function which adds three number and display output on the
screen
#include <stdio.h>
// Function to add three numbers
int addThreeNumbers(int num1, int num2, int num3)
{
return num1 + num2 + num3;
}
int main()
{
int num1, num2, num3, sum;
// Ask the user for three numbers
printf("Enter the first number: ");
scanf("%d", &num1);
printf("Enter the second number: ");
scanf("%d", &num2);
printf("Enter the third number: ");
scanf("%d", &num3);
// Call the function to add the three numbers
sum = addThreeNumbers(num1, num2, num3);
// Display the output on the screen
printf("The sum of %d, %d, and %d is: %d\n", num1, num2, num3, sum);
return 0;
}
Program 2 - Write a function to find factorial of a given number.
#include <stdio.h>
// Function to find factorial of a given number
long long factorial(int num)
{
if (num < 0)
{
printf("Error: Factorial of a negative number is not defined.\n");
return -1;
}
else if (num == 0 || num == 1)
{
return 1;
}
else
{
long long fact = 1;
for (int i = 2; i <= num; i++)
{
fact *= i;
}
return fact;
}
}
int main()
{
int num;
printf("Enter a number: ");
scanf("%d", &num);
long long fact = factorial(num);
if (fact != -1)
{
printf("Factorial of %d is: %lld\n", num, fact);
}
return 0;
}
Program 3 - Write a function to reverse a given number
#include <stdio.h>
// Function to reverse a given number
int reverseNumber(int num)
{
int reversedNum = 0;
while (num != 0)
{
int digit = num % 10;
reversedNum = reversedNum * 10 + digit;
num /= 10;
}
return reversedNum;
}
int main()
{
int num;
printf("Enter a number: ");
scanf("%d", &num);
int reversedNum = reverseNumber(num);
printf("Reversed number is: %d\n", reversedNum);
return 0;
}
Program 4 -Write a program to calculate factorial of given number using
recursive function.
#include <stdio.h>
// Recursive function to calculate factorial
long long factorial(int num)
{
if (num < 0)
{
printf("Error: Factorial of a negative number is not defined.\n");
return -1;
}
else if (num == 0 || num == 1)
{
return 1;
} else
{
return num * factorial(num - 1);
}
}
int main()
{
int num;
printf("Enter a number: ");
scanf("%d", &num);
long long fact = factorial(num);
if (fact != -1)
{
printf("Factorial of %d is: %lld\n", num, fact);
}
return 0;
}
Program 5 - Write a program using function which swap two number using a) call
by value and b) call by reference.
#include <stdio.h>
// Function to swap two numbers using call by value
void swapByValue(int num1, int num2)
{
int temp = num1;
num1 = num2;
num2 = temp;
printf("Swap by value: num1 = %d, num2 = %d\n", num1, num2);
}
// Function to swap two numbers using call by reference
void swapByReference(int* num1, int* num2)
{
int temp = *num1;
*num1 = *num2;
*num2 = temp;
printf("Swap by reference: num1 = %d, num2 = %d\n", *num1, *num2);
}
int main()
{
int num1 = 10;
int num2 = 20;
printf("Original values: num1 = %d, num2 = %d\n", num1, num2);
// Call the function with call by value
swapByValue(num1, num2);
printf("After swap by value: num1 = %d, num2 = %d\n", num1, num2);
// Call the function with call by reference
swapByReference(&num1, &num2);
printf("After swap by reference: num1 = %d, num2 = %d\n", num1, num2);
return 0;
}
Program 6 - Write a program to dynamically allocate memory of n items to an
integer pointer, display their sum and average.
#include <stdio.h>
#include <stdlib.h>
int main()
{
int n;
printf("Enter the number of items: ");
scanf("%d", &n);
// Dynamically allocate memory for the array
int* arr = (int*)malloc(n * sizeof(int));
if (arr == NULL)
{
printf("Memory allocation failed.\n");
return -1;
}
// Prompt the user to input values
printf("Enter %d integer values:\n", n);
for (int i = 0; i < n; i++)
{
scanf("%d", &arr[i]);
}
// Calculate the sum of the values
int sum = 0;
for (int i = 0; i < n; i++)
{
sum += arr[i];
}
// Calculate the average of the values
double average = (double)sum / n;
// Display the sum and average
printf("Sum: %d\n", sum);
printf("Average: %.2f\n", average);
// Deallocate the memory
free(arr);
return 0;
}
Program 7 - Write a program to dynamically allocate memory of n items to a
character array, end it with ‘\0’ and count number of vowels, consonants and
spaces in it
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
int main()
{
int n;
printf("Enter the length of the string: ");
scanf("%d", &n);
char* str = (char*)malloc((n + 1) * sizeof(char));
if (str == NULL)
{
printf("Memory allocation failed.\n");
return -1;
}
// Prompt the user to input a string
printf("Enter a string of length %d: ", n);
scanf(" %[^\n]", str);
// Count the number of vowels, consonants, and spaces
int vowels = 0, consonants = 0, spaces = 0;
for (int i = 0; str[i] != '\0'; i++)
{
if (isspace(str[i]))
{
spaces++;
}
else if (isalpha(str[i]))
{
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++;
}
}
}
// Display the counts
printf("Number of vowels: %d\n", vowels);
printf("Number of consonants: %d\n", consonants);
printf("Number of spaces: %d\n", spaces);
// Deallocate the memory
free(str);
return 0;
}
Program 8 - Write a program which creates student structure which accept-
students rollno, student name, address, subject marks, percentage and display
same on screen.
#include <stdio.h>
#include <string.h>
// Define the student structure
struct Student
{
int rollNo;
char name[50];
char address[100];
float subjectMarks[5];
float percentage;
};
// Function to calculate the percentage
float calculatePercentage(float marks[5])
{
float sum = 0;
for (int i = 0; i < 5; i++)
{
sum += marks[i];
}
return (sum / 500) * 100;
}
// Function to display the student details
void displayStudentDetails(struct Student student)
{
printf("Roll No: %d\n", student.rollNo);
printf("Name: %s\n", student.name);
printf("Address: %s\n", student.address);
printf("Subject Marks: ");
for (int i = 0; i < 5; i++)
{
printf("%.2f ", student.subjectMarks[i]);
}
printf("\nPercentage: %.2f%%\n", student.percentage);
}
int main()
{
struct Student student;
// Accept the student details
printf("Enter Roll No: ");
scanf("%d", &student.rollNo);
printf("Enter Name: ");
scanf(" %[^\n]", student.name);
printf("Enter Address: ");
scanf(" %[^\n]", student.address);
printf("Enter Subject Marks (5 subjects): ");
for (int i = 0; i < 5; i++) {
scanf("%f", &student.subjectMarks[i]);
}
// Calculate the percentage
student.percentage = calculatePercentage(student.subjectMarks);
// Display the student details
printf("\nStudent Details:\n");
displayStudentDetails(student);
return 0;
}
Program 9 - Write a file handling program which accept student information store
it into disk file.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// Define the student structure
struct Student
{
int rollNo;
char name[50];
char address[100];
float subjectMarks[5];
};
// Function to accept student information
void acceptStudentInfo(struct Student* student)
{
printf("Enter Roll No: ");
scanf("%d", &student->rollNo);
printf("Enter Name: ");
scanf(" %[^\n]", student->name);
printf("Enter Address: ");
scanf(" %[^\n]", student->address);
printf("Enter Subject Marks (5 subjects): ");
for (int i = 0; i < 5; i++) {
scanf("%f", &student->subjectMarks[i]);
}
}
// Function to display student information
void displayStudentInfo(struct Student student)
{
printf("Roll No: %d\n", student.rollNo);
printf("Name: %s\n", student.name);
printf("Address: %s\n", student.address);
printf("Subject Marks: ");
for (int i = 0; i < 5; i++) {
printf("%.2f ", student.subjectMarks[i]);
}
printf("\n");
}
int main()
{
struct Student student;
FILE* file;
// Open the file in write mode
file = fopen("student_info.txt", "w");
if (file == NULL)
{
printf("Error opening file!\n");
exit(1);
}
// Accept student information
acceptStudentInfo(&student);
// Write student information to the file
fprintf(file, "Roll No: %d\n", student.rollNo);
fprintf(file, "Name: %s\n", student.name);
fprintf(file, "Address: %s\n", student.address);
fprintf(file, "Subject Marks: ");
for (int i = 0; i < 5; i++)
{
fprintf(file, "%.2f ", student.subjectMarks[i]);
}
fprintf(file, "\n");
// Close the file
fclose(file);
// Display student information
printf("\nStudent Information:\n");
displayStudentInfo(student);
return 0;
}
Program 10 - Write a program to separate even and odd numbers available in file
#include <stdio.h>
#include <stdlib.h>
// Function to separate even and odd numbers
void separateEvenOddNumbers(const char* filename) {
FILE* file = fopen(filename, "r");
if (file == NULL) {
printf("Error opening file!\n");
exit(1);
}
FILE* evenFile = fopen("even_numbers.txt", "w");
if (evenFile == NULL)
{
printf("Error opening even numbers file!\n");
exit(1);
}
FILE* oddFile = fopen("odd_numbers.txt", "w");
if (oddFile == NULL)
{
printf("Error opening odd numbers file!\n");
exit(1);
}
int number;
while (fscanf(file, "%d", &number) == 1)
{
if (number % 2 == 0) {
fprintf(evenFile, "%d\n", number);
}
else
{
fprintf(oddFile, "%d\n", number);
}
}
fclose(file);
fclose(evenFile);
fclose(oddFile);
}
int main()
{
const char* filename = "numbers.txt";
separateEvenOddNumbers(filename);
printf("Even and odd numbers have been separated into even_numbers.txt and
odd_numbers.txt files.\n");
return 0;
}
Program 11 - Write a program to copy contain of text file into another text file
#include <stdio.h>
#include <stdlib.h>
// Function to copy the contents of a file
void copyFile(const char* sourceFilename, const char* destinationFilename)
{
FILE* sourceFile = fopen(sourceFilename, "r");
if (sourceFile == NULL)
{
printf("Error opening source file!\n");
exit(1);
}
FILE* destinationFile = fopen(destinationFilename, "w");
if (destinationFile == NULL)
{
printf("Error opening destination file!\n");
exit(1);
}
char character;
while ((character = fgetc(sourceFile)) != EOF)
{
fputc(character, destinationFile);
}
fclose(sourceFile);
fclose(destinationFile);
}
int main()
{
const char* sourceFilename = "source.txt";
const char* destinationFilename = "destination.txt";
printf("Copying contents of %s to %s...\n", sourceFilename, destinationFilename);
copyFile(sourceFilename, destinationFilename);
printf("Copy operation successful!\n");
return 0;
}
Program 12 - Write a program to count number of lines and characters of given
text file
#include <stdio.h>
#include <stdlib.h>
// Function to count the number of lines and characters
void countLinesAndCharacters(const char* filename)
{
FILE* file = fopen(filename, "r");
if (file == NULL)
{
printf("Error opening file!\n");
exit(1);
}
int lines = 0;
int characters = 0;
char character;
while ((character = fgetc(file)) != EOF)
{
characters++;
if (character == '\n')
{
lines++;
}
}
lines++; // Count the last line
fclose(file);
printf("Number of lines: %d\n", lines);
printf("Number of characters: %d\n", characters);
}
int main()
{
const char* filename = "example.txt";
printf("Counting lines and characters in %s...\n", filename);
countLinesAndCharacters(filename);
return 0;
}
Program 13 - Write a program to remove blank lines from a file
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// Function to remove blank lines from a file
void removeBlankLines(const char* inputFilename, const char* outputFilename)
{
FILE* inputFile = fopen(inputFilename, "r");
if (inputFile == NULL) {
printf("Error opening input file!\n");
exit(1);
}
FILE* outputFile = fopen(outputFilename, "w");
if (outputFile == NULL) {
printf("Error opening output file!\n");
exit(1);
}
char line[1024];
while (fgets(line, sizeof(line), inputFile) != NULL)
{
// Remove trailing newline character
line[strcspn(line, "\n")] = 0;
// Check if the line is not blank
if (strlen(line) > 0) {
fprintf(outputFile, "%s\n", line);
}
}
fclose(inputFile);
fclose(outputFile);
}
int main()
{
const char* inputFilename = "input.txt";
const char* outputFilename = "output.txt";
printf("Removing blank lines from %s...\n", inputFilename);
removeBlankLines(inputFilename, outputFilename);
printf("Blank lines removed and written to %s.\n", outputFilename);
return 0;
}
Program 14 - Write a program to count the no. of words in a given text file
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
// Function to count the number of words in a file
int countWordsInFile(const char* filename)
{
FILE* file = fopen(filename, "r");
if (file == NULL) {
printf("Error opening file!\n");
exit(1);
}
int wordCount = 0;
int inWord = 0;
char character;
while ((character = fgetc(file)) != EOF)
{
// Check if the character is a whitespace
if (isspace(character)) {
inWord = 0;
} else if (!inWord) {
wordCount++;
inWord = 1;
}
}
fclose(file);
return wordCount;
}
int main()
{
const char* filename = "example.txt";
printf("Counting words in %s...\n", filename);
int wordCount = countWordsInFile(filename);
printf("Number of words: %d\n", wordCount);
return 0;
}