0% found this document useful (0 votes)
10 views8 pages

C Programs for Basic Operations and File Handling

The document contains multiple C programming examples demonstrating various concepts such as finding the greatest of three numbers using an if-else ladder, calculating areas of shapes using macros, evaluating ratings with if-else statements, creating a menu-driven calculator with function pointers, managing employee salary details using structures, and performing file operations including copying, counting characters, and appending text to files. Each example includes code snippets and expected output. The programs illustrate fundamental programming techniques and file handling in C.

Uploaded by

vigneshdm2007
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views8 pages

C Programs for Basic Operations and File Handling

The document contains multiple C programming examples demonstrating various concepts such as finding the greatest of three numbers using an if-else ladder, calculating areas of shapes using macros, evaluating ratings with if-else statements, creating a menu-driven calculator with function pointers, managing employee salary details using structures, and performing file operations including copying, counting characters, and appending text to files. Each example includes code snippets and expected output. The programs illustrate fundamental programming techniques and file handling in C.

Uploaded by

vigneshdm2007
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

[Link] find the greatest of given 3 numbers using else – if ladder.

#include <stdio.h>
int main()
{
int n1, n2, n3;
printf("Enter three different numbers: ");
scanf("%d %d %d", &n1, &n2, &n3);
// if n1 is greater than both n2 and n3, n1 is the largest
if (n1 >= n2 && n1 >= n3)
printf("%d is the largest number.", n1);
// if n2 is greater than both n1 and n3, n2 is the largest
else if (n2 >= n1 && n2 >= n3)
printf("%d is the largest number.", n2);
// if n3 is greater than both n1 and n2, n3 is the largest
else
printf("%d is the largest number.", n3);
return 0;
}
Output:
Enter three different numbers: 5 9 3
9 is the largest number.

[Link] a macro to compute the area of a circle and rectangle, and use it in the main program.
#include <stdio.h>
// Define a macro for PI and the formula for the area of a circle
#define PI 3.14159
#define AreaCircle(radius) (PI * (radius) * (radius))
#define AreaRectangle(length,breadth) (length*breadth)

int main()
{
float radius,length,breadth,area;
// Input radius from the user
printf("Enter the radius of the circle: ");
scanf("%f", &radius);
area = AreaCircle(radius);
printf("Area of the circle is: %.2f\n", area);

printf("Enter the Length and breadth of rectangle: ");


scanf("%f %f", &length,&breadth);
// Calculate area using the macro
area=AreaRectangle(length,breadth);
printf("Area of the Rectangle is: %.2f\n", area);
return 0;
}
Output:
Enter the radius of the circle: 5.6
Area of the circle is: 98.52
Enter the Length and breadth of rectangle: 10 20
Area of the Rectangle is: 200.00

3. Develop a program using control structure if… else that examines the value of an integer variable
called rating and print one of the following messages, “Not recommended” – if the value of rating is
less than 2. “Recommended” – if the value of rating lies between 2 and 4 “Highly recommended” – if
the value of rating is above 4.
#include <stdio.h>
int main()
{
int rating;
printf("Enter a rating (integer value):");
scanf("%d", &rating);
if (rating < 2)
printf("Not recommended\n");
else if (rating >= 2 && rating <= 4)
printf("Recommended\n");
else
printf("Highly recommended\n");
return 0;
}
Output
Enter a rating (integer value):5
Highly recommended

4. Build a menu-driven calculator using an array of function pointers.


#include <stdio.h>
#include <stdlib.h> // For exit()
// Function prototypes for arithmetic operations
double add(double a, double b) { return a + b; }
double subtract(double a, double b) { return a - b; }
double multiply(double a, double b) { return a * b; }
double divide(double a, double b) {
if (b == 0)
{
printf("Error: Division by zero!\n");
return 0; // Or handle error appropriately
}
return a / b;
}
void display_menu()
{
printf("\n--- Calculator Menu ---\n");
printf("1. Addition\n");
printf("2. Subtraction\n");
printf("3. Multiplication\n");
printf("4. Division\n");
printf("5. Exit\n");
printf("Enter your choice: ");
}

int main() {
double (*operations[])(double, double) = {add, subtract, multiply, divide};
int choice;
double num1, num2, result;
do {
display_menu();
scanf("%d",&choice);
if (choice >= 1 && choice <= 4)
{
printf("Enter first number: ");
scanf("%lf",&num1);
printf("Enter second number: ");
scanf("%lf",&num2);
result = operations[choice - 1](num1, num2);
printf("Result: %lf\n", result);
}
else if (choice == 5)
printf("Exiting calculator. Goodbye!\n");
else
printf("Invalid choice. Please try again.\n");
} while (choice != 5);
return 0;
}

Output:
--- Calculator Menu ---
1. Addition
2. Subtraction
3. Multiplication
4. Division
5. Exit
Enter your choice: 1
Enter first number: 3
Enter second number: 6
Result: 9.000000

--- Calculator Menu ---


1. Addition
2. Subtraction
3. Multiplication
4. Division
5. Exit
Enter your choice: 5
Exiting calculator. Goodbye!

[Link] a structure named Employee with members (Emp_ID, Name, Basic, HRA, DA, Gross). Write
a program to read details for n employees, compute Gross Salary = Basic + HRA + DA, and print a
salary report.
#include <stdio.h>
#include <stdlib.h>
struct Employee
{
int id;
char name[50];
float basic_salary;
float hra;
float da;
float gross_salary;
};

int main()
{
int n, i;
printf("Enter the number of employees: ");
scanf("%d", &n);
struct Employee *employees = (struct Employee *)malloc(n * sizeof(struct Employee));
for (i = 0; i < n; i++)
{
printf("\nEnter details for Employee %d:\n", i + 1);
printf("Enter Employee ID: ");
scanf("%d", &employees[i].id);
printf("Enter Employee Name: ");
scanf("%s", employees[i].name); // Assuming names do not contain spaces
printf("Enter Basic Salary: ");
scanf("%f", &employees[i].basic_salary);
printf("Enter HRA: ");
scanf("%f", &employees[i].hra);
printf("Enter DA: ");
scanf("%f", &employees[i].da);
employees[i].gross_salary = employees[i].basic_salary + employees[i].hra + employees[i].da;
}
printf("\n--- Employee Details and Gross Salaries ---\n");
for (i = 0; i < n; i++)
{
printf("\nEmployee %d:\n", i + 1);
printf("ID: %d\n", employees[i].id);
printf("Name: %s\n", employees[i].name);
printf("Basic Salary: %.2f\n", employees[i].basic_salary);
printf("HRA: %.2f\n", employees[i].hra);
printf("DA: %.2f\n", employees[i].da);
printf("Gross Salary: %.2f\n", employees[i].gross_salary);
}
free(employees);
return 0;
}
Output:
Enter the number of employees: 2

Enter details for Employee 1:


Enter Employee ID: 101
Enter Employee Name: [Link]
Enter Basic Salary: 20000
Enter HRA: 2000
Enter DA: 3000

Enter details for Employee 2:


Enter Employee ID: 102
Enter Employee Name: [Link]
Enter Basic Salary: 15000
Enter HRA: 1500
Enter DA: 2500
--- Employee Details and Gross Salaries ---

Employee 1:
ID: 101
Name: [Link]
Basic Salary: 20000.00
HRA: 2000.00
DA: 3000.00
Gross Salary: 25000.00

Employee 2:
ID: 102
Name: [Link]
Basic Salary: 15000.00
HRA: 1500.00
DA: 2500.00
Gross Salary: 19000.00
#include <stdio.h>
#include <stdlib.h>

6.i) Develop a program to copy contents from one text file to another using file handling functions
int main()
{
FILE *sourceFile;
FILE *destFile;
char sourcePath[100];
char destPath[100];
char ch;
printf("Enter source file path: ");
scanf("%s", sourcePath);
printf("Enter destination file path: ");
scanf("%s", destPath);
sourceFile = fopen(sourcePath, "r");
destFile = fopen(destPath, "w");
ch = fgetc(sourceFile);
while (ch != EOF)
{
fputc(ch, destFile);
ch = fgetc(sourceFile);
}
printf("\nFiles copied successfully.\n");
fclose(sourceFile);
fclose(destFile);
return 0;
}

ii) Write a program to count the number of characters, words, and lines in a file.
#include <stdio.h>
int main()
{
FILE* fp;
int count = 0;
char filename[100];
char c;
printf("Enter file name: ");
scanf("%s", filename);
fp = fopen(filename, "r");
if (fp == NULL) {
printf("Could not open file %s",
filename);
return 0;
}
for (c = getc(fp); c != EOF; c = getc(fp))
count = count + 1;
fclose(fp);
printf("The file %s has %d characters\n ",filename, count);
return 0;
}

iii) Write a program that appends text at the end of an existing file and then displays the updated
content.
#include <stdio.h>
int main()
{
FILE *filePointer;
char filename[100], text[100], ch;
printf("Enter the filename to append to: ");
scanf("%s", filename);
filePointer = fopen(filename, "a");
printf("Enter text to append (press Enter after each line):\n");
getchar();
while ((ch = getchar()) != '\n')
fputc(ch, filePointer);
fprintf(filePointer, "\n"); // Add a newline at the end of the appended text
fclose(filePointer);
filePointer = fopen(filename, "r");
printf("\n--- Updated file content ---\n");
ch = fgetc(filePointer);
while (ch != EOF)
{
printf("%c", ch);
ch = fgetc(filePointer);
}
printf("\n--------------------------\n");
fclose(filePointer);
return 0;
}

You might also like