0% found this document useful (0 votes)
7 views

C ob

The document outlines a School Management System and a Library Management System, both implemented in C. The School Management System allows for adding, displaying, and searching student and teacher records, while the Library Management System enables user registration, book searching by name or author, and borrowing books. Each system includes file handling for saving records and user data.

Uploaded by

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

C ob

The document outlines a School Management System and a Library Management System, both implemented in C. The School Management System allows for adding, displaying, and searching student and teacher records, while the Library Management System enables user registration, book searching by name or author, and borrowing books. Each system includes file handling for saving records and user data.

Uploaded by

sudipbaniya0000
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 43

1.

School management system


#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define size 100

// Define a structure to store student and teacher information


struct student
{
int ID, Age, atten, TID, TAge, Tsalary;
char name[50], Add[50], gen[50], Tname[50], TAdd[50], Tgen[10], rank[50];
};

// Global variables for student and teacher records


struct student stud[size];
int studentcount = 0; // Counter for students
int teachercount = 0; // Counter for teachers

// Function to add a new student record


void student_rec()
{
FILE *fptr = fopen("Student_rec.txt", "w");
if (fptr == NULL)
{
printf("Error opening the file!");
}
if (studentcount < size)
{
printf("\nEnter the student ID: ");
scanf("%d", &stud[studentcount].ID);
printf("Enter the student name: ");
scanf("%s", &stud[studentcount].name);
printf("Enter the student gender: ");
scanf("%s", &stud[studentcount].gen);
printf("Enter the student Age: ");
scanf("%d", &stud[studentcount].Age);
printf("Enter the student address: ");
scanf("%s", &stud[studentcount].Add);
printf("Enter the student attendance (X out of 365):
"); scanf("%d", &stud[studentcount].atten);
printf("Record of %s with ID: %d was added successfully!\n", stud[studentcount].name,
stud[studentcount].ID);
fprintf(fptr, "ID: %d,\tName: %s\tGender: %s\tAge: %d\tAddress: %s\tAttendance: %d\n",
stud[studentcount].ID, stud[studentcount].name, stud[studentcount].gen, stud[studentcount].Age,
stud[studentcount].Add, stud[studentcount].atten);
studentcount++;
}
else
{
printf("Maximum amount of student record has been reached. No more records can be added.");
}
fclose(fptr);
}

// Function to add a new teacher or staff record


void teacher_rec()
{
FILE *fptr = fopen("Teacher_rec.txt", "w");
if (fptr == NULL)
{
printf("Error opening the file!");
}
if (teachercount < size)
{
printf("\nEnter the teacher/staff ID: ");
scanf("%d", &stud[teachercount].TID);
printf("Enter the name: ");
scanf("%s", &stud[teachercount].Tname);
printf("Enter the rank (teacher or staff) of %s: ", stud[teachercount].Tname);
scanf("%s", &stud[teachercount].rank);
printf("Enter the gender: ");
scanf("%s", &stud[teachercount].Tgen);
printf("Enter the Age: ");
scanf("%d", &stud[teachercount].TAge);
printf("Enter the address: ");
scanf("%s", &stud[teachercount].TAdd);
printf("Enter the salary: ");
scanf("%d", &stud[teachercount].Tsalary);
printf("Record of %s with ID: %d was added successfully!\n", stud[teachercount].Tname,
stud[teachercount].TID);
fprintf(fptr, "ID: %d\t\tName: %s\t\tRank: %s\t\tGender: %s\t\tAge: %d\t\tAddress: %s\t\tSalary: %d\n",
stud[teachercount].TID, stud[teachercount].Tname, stud[teachercount].rank, stud[teachercount].Tgen,
stud[teachercount].TAge, stud[teachercount].TAdd, stud[teachercount].Tsalary);
teachercount++;
}
fclose(fptr);
}

// Function to display the total number of student records


void student_count()
{
if (studentcount == 0)
{
printf("No record of any student was found.\nTo enter the record of student please enter the respective
number 1 for it.");
}
else
{
printf("\nOut of 100 records, you have entered the record of %d students.\n", studentcount);
int vacant = 100 - studentcount;
printf("You have space for %d more records", vacant);
}
}

// Function to display the total number of teacher or staff records


void teacher_count()
{
if (teachercount == 0)
{
printf("No record of any teacher was found.\nTo enter the record of teacher/staff please enter the respective
number 2 for it.");
}
else
{
printf("\nOut of 100 records, you have entered the record of %d teachers.\n", teachercount);
int vacant = 100 - teachercount;
printf("You have space for %d more records", vacant);
}
}

// Function to display all student records


void display_stud()
{
if (studentcount == 0)
{
printf("\nNo record of student was found");
}
else
{
printf("\nThe list of student info recorded so far is as follows:\n");

printf("*********************************************************************************
***************************\n");
for (int i = 0; i < studentcount; i++)
{
printf("SN: %d\tID: %d\tName: %s\tGender: %s\tAge: %d\tAddress: %s\tAttendance: %d\n", i + 1,
stud[i].ID, stud[i].name, stud[i].gen, stud[i].Age, stud[i].Add, stud[i].atten);
}

printf("*********************************************************************************
***************************\n");
}
}
// Function to display all teacher or staff records
void display_teach()
{
if (teachercount == 0)
{
printf("\nNo record of teacher/staff was found");
}
else
{
printf("\nThe list of teacher/staff info recorded so far is as follows:\n");

printf("*********************************************************************************
***************************\n");
for (int i = 0; i < teachercount; i++)
{
printf("SN: %d\tID: %d\tName: %s\tRank: %s\tGender: %s\tAge: %d\tAddress: %s\tSalary: %d\n", i + 1,
stud[i].TID, stud[i].Tname, stud[i].rank, stud[i].Tgen, stud[i].TAge, stud[i].TAdd, stud[i].Tsalary);
}

printf("*********************************************************************************
***************************\n");
}
}

// Function to find and display student information by ID


void find_student_by_ID()
{
int searchID, found = 0;
printf("Enter the ID of the student: ");
scanf("%d", &searchID);
for (int i = 0; i < studentcount; i++)
{
if (stud[i].ID == searchID)
{
printf("\nStudent ID: %d\n", stud[i].ID);
printf("Student Name: %s\n", stud[i].name);
printf("Student Gender: %s\n", stud[i].gen);
printf("Student Age: %d\n", stud[i].Age);
printf("Student Address: %s\n", stud[i].Add);
printf("Student Attendance: %d\n", stud[i].atten);
found = 1;
}
}
if (!found)
{
printf("\nThere was no record of a student with ID: %d", searchID);
}
}
// Function to find and display teacher information by ID
void find_teacher_by_ID()
{
int searchID, found = 0;
printf("Enter the ID: ");
scanf("%d",
&searchID);
for (int i = 0; i < teachercount; i++)
{
if (stud[i].TID == searchID)
{
printf("\nID: %d\n", stud[i].TID);
printf("Name: %s\n", stud[i].Tname);
printf("Rank: %s\n", stud[i].rank);
printf("Gender: %s\n", stud[i].Tgen);
printf("Age: %d\n", stud[i].TAge);
printf("Student Address: %s\n", stud[i].TAdd);
printf("Salary %d\n", stud[i].Tsalary);
found = 1;
break;
}
}
if (!found)
{
printf("\nNo records were found with ID: %d", searchID);
}
}

// Main function to handle user interaction


int main()
{
int choice;
char c;
printf("--------------------------------------WELCOME TO THE ABC SCHOOL MANAGEMENT SYSTEM-------
");
top:
printf("\n\nPlease select what you want to do: ");
printf("\n\na) For adding new student record, press:
1\n"); printf("b) For adding
new staff or teacher record, press: 2\n");
printf("c) To display all students, press: 3\n");
printf("d) To display all teachers, press: 4\n");
printf("e) To find the student by ID, press: 5\n");
printf("f) To find the teacher/staff by ID, press: 6\n");
printf("g) To show the student count, press: 7\n");
printf("h) To show the teacher/staff count, press: 8\n");
printf("i) To exit, press: 9\n\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice)
{
case 1:
{
student_rec();
break;
}
case 2:
{
teacher_rec();
break;
}
case 3:
{
display_stud();
break;
}
case 4:
{
display_teach();
break;
}
case 5:
{
find_student_by_ID();
break;
}
case 6:
{
find_teacher_by_ID();
break;
}
case 7:
{
student_count();
break;
}
case 8:
{
teacher_count();
break;
}
}

printf("\n\nDo you wish to continue? (press Y to continue, N to exit): ");


scanf("%s", &c);
if (c == 'y' || c == 'Y')
{
goto top;
}
else
{
printf("Exiting the program.\nExit Successful!");
}
return 0;
}
Output:
Flow chart
Block diagram
2. Library Management system
#include <stdio.h>
#include <string.h>
#define storage 100

// Global variables for storing books and user information


int total_books = 5;
char books[5][100] = {"Atomic Habits", "Psychology of Money", "Ikigai", "Deep Work", "The Art of Laziness"};
char authors[5][100] = {"James Clear", "Morgan Housel", "Hector Garcia", "Cal Newport", "Kyrie Petrakis"};

// Structure to store registered user information


struct {
char username[20];
int password;
} registered_users[storage];

int registered_count = 0;

// Function to save books to a file


void save_books_to_file() {
FILE *fptr = fopen("books.txt", "w");
if (fptr == NULL) { // Check if file opens successfully
printf("Error opening the file!\n");
return;
}

// Write all books and their authors to the file


for (int i = 0; i < total_books; i++) {
fprintf(fptr, "No. %d\nName: %s\nAuthor: %s\n\n", i + 1, books[i], authors[i]);
}

fclose(fptr); // Close the file


}

// Function to save user data and borrowed book details to a file


void save_user_data_to_file(const char *username, int password, const char *borrowed_book, const char
*borrowed_author) {
FILE *fptr = fopen("users.txt", "w");
if (fptr == NULL) { // Check if file opens successfully
printf("Error opening the file!\n");
return;
}

// Write user details and borrowed book details to the file


fprintf(fptr, "Username: %s\nPassword: %d\nBorrowed Book: %s\nAuthor: %s\n\n", username, password,
borrowed_book, borrowed_author);

fclose(fptr); // Close the file


}

// Function to search for books by their name


void search_books_by_name() {
char search_name[100];
int found = 0; // Flag to check if the book is found

printf("Enter the book name to search: ");


getchar(); // Clear the input buffer
scanf("%[^\n]%*c", search_name);

printf("\nSearch results for book name '%s':\n", search_name);

// Loop through all books to find matches


for (int i = 0; i < total_books; i++) {
if (strcmp(books[i], search_name) == 0) {
printf("Book No. %d\nName: %s\nAuthor: %s\n\n", i + 1, books[i], authors[i]);
found = 1; // Mark as found
}
}

// Print message if no books are found


if (!found) {
printf("No books found with the name '%s'.\n", search_name);
}
}

// Function to search for books by their author


void search_books_by_author() {
char search_author[100];
int found = 0; // Flag to check if the author is found

printf("Enter the author name to search: ");


getchar(); // Clear the input buffer
scanf("%[^\n]%*c", search_author);

printf("\nSearch results for author '%s':\n", search_author);

// Loop through all authors to find matches


for (int i = 0; i < total_books; i++) {
if (strcmp(authors[i], search_author) == 0) {
printf("Book No. %d\nName: %s\nAuthor: %s\n\n", i + 1, books[i], authors[i]);
found = 1; // Mark as found
}
}

// Print message if no authors are found


if (!found) {
printf("No books found by the author '%s'.\n", search_author);
}
}

// Function to validate user login credentials


int login_user(char user[], int pass) {
for (int i = 0; i < registered_count; i++) {
if (strcmp(registered_users[i].username, user) == 0 && registered_users[i].password == pass) {
return 1; // Login successful
}
}
return 0; // Login failed
}

int main() {
char user[20]; // Stores username
int pass, choice, choice2, choice3; // Variables for user choices
top:

// Main menu
printf("-------------------------------------------WELCOME TO ABC LIBRARY MANAGEMENT--------------------
\n");
printf(" Here you can add, search and borrow the needed books!\n");
printf(" Please select what you want to do:\n\n");
printf("->Sign up to our ABC library to gain access, press: 1\n");
printf(" \n Already have an account? \n\n->Login to check your books, press: 2\n");
printf("\nEnter your choice: ");
scanf("%d", &choice);
if (choice == 1) {
// Sign-up process
printf(" Enter the following information
----\n\n");
printf("Enter your username: ");
scanf("%s", user);
printf("\nEnter your password (only numbers): ");
scanf("%d", &pass);

// Save the new user details


strcpy(registered_users[registered_count].username, user);
registered_users[registered_count].password = pass;
registered_count++;

printf("\n Saving your information


- \n\n");
printf("Dear %s, This is our current list of books that we have:\n\n", user);

// Display the list of books


for (int i = 0; i < total_books; i++) {
printf("\nBook No. %d\nName: %s\nAuthor: %s\n", i + 1, books[i], authors[i]);
}

top2:
// Options after signing up or logging in
printf("\n
\n");
printf("\nTo add new books, press: 10\n");
printf("To borrow a book, press: 11\n");
printf("To search for a book by name, press: 12\n");
printf("To search for a book by author, press: 13\n");
printf("To exit, press: 14\n");
printf("To return a book, press: 15\n");
printf("\nWhat would you like to do? ");
printf("\nEnter the choice: ");
scanf("%d", &choice2);

if (choice2 == 10) {
// Adding a new book
if (total_books < 100) {
char new_book[100], new_author[100];
printf("\n Enter the following information
\n\n");
printf("Enter the name of the new book: ");
getchar(); // Clear the input buffer
scanf("%[^\n]%*c", new_book);
printf("Enter the author of the new book: ");
scanf("%[^\n]%*c", new_author);

// Add the new book and author to the library


strcpy(books[total_books], new_book);
strcpy(authors[total_books], new_author);
total_books++;

printf("The book '%s' by %s has been added to the collection!\n", new_book, new_author);

save_books_to_file(); // Save the updated list of books to the file

printf("\nHere is the updated list of books in our library:\n");


for (int i = 0; i < total_books; i++) {
printf("\nNo. %d\nName: %s\nAuthor: %s\n", i + 1, books[i], authors[i]);
}
} else {
printf("Our inventory is currently full. Please try again later\n");
}
printf("\nTo go back, press: 1\n");
printf("To logout and go to main menu, press: 2\n");
printf("To exit the program, press: 3\n");
scanf("%d", &choice3);
printf("Enter your choice: ");
if (choice3 == 1) {
goto top2;
} else if (choice3 == 2) {
goto top;
} else {
printf("\nExiting the program. Goodbye!");
}
} else if (choice2 == 11) {
// Borrowing a book
int book_number;
printf("\nEnter the number of the book you want to borrow: ");
scanf("%d", &book_number);

if (book_number > 0 && book_number <= total_books) {


printf("\nYou have borrowed the book:\n\nBook No:%d \n%s by \n%s\n", book_number,
books[book_number - 1], authors[book_number - 1]);
save_user_data_to_file(user, pass, books[book_number - 1], authors[book_number - 1]); // Save user and
book data to the file
} else {
printf("Invalid book number. Please try again.\n");
}
printf("\nTo go back, press: 1\n");
printf("To logout and go to main menu, press: 2\n");
printf("To exit the program, press: 3\n");
scanf("%d", &choice3);
printf("Enter your choice: ");
if (choice3 == 1) {
goto top2;
} else if (choice3 == 2) {
goto top;
} else {
printf("\nExiting the program. Goodbye!");
}
}

if (choice2 == 12) {
// Search for a book by name
search_books_by_name();
printf("\nTo go back, press: 1\n");
printf("To logout and go to main menu, press: 2\n");
printf("To exit the program, press: 3\n");
scanf("%d", &choice3);
printf("Enter your choice: ");
if (choice3 == 1) {
goto top2;
} else if (choice3 == 2) {
goto top;
} else {
printf("\nExiting the program. Goodbye!");
}
} else if (choice2 == 13) {
// Search for a book by author
search_books_by_author();
printf("\nTo go back, press: 1\n");
printf("To logout and go to main menu, press: 2\n");
printf("To exit the program, press: 3\n");
scanf("%d", &choice3);
printf("Enter your choice: ");
if (choice3 == 1) {
goto top2;
} else if (choice3 == 2) {
goto top;
} else {
printf("\nExiting the program. Goodbye!");
}
} else if (choice2 == 14) {
// Exit the library system
printf("Exiting the library system. Have a great day!\n");
}
else if (choice2 == 15) {
// Return a borrowed book
int book_number;
printf("\nEnter the number of the book you want to return: ");
scanf("%d", &book_number);

if (book_number > 0 && book_number <= total_books) {


printf("\nYou have returned the book:\n\nBook No:%d \n%s by \n%s\n", book_number,
books[book_number - 1], authors[book_number - 1]);
save_user_data_to_file(user, pass, books[book_number - 1], authors[book_number - 1]); // Save user and
book data to the file
} else {
printf("Invalid book number. Please try again.\n");
}
printf("\nTo go back, press: 1\n");
printf("To logout and go to main menu, press: 2\n");
printf("To exit the program, press: 3\n");
scanf("%d", &choice3);
printf("Enter your choice: ");
if (choice3 == 1) {
goto top2;
} else if (choice3 == 2) {
goto top;
} else {
printf("\nExiting the program. Goodbye!");
}
}

else if (choice == 2) {
// Login process
printf("\n Enter the following information
\n\n");
printf("Enter your username: ");
scanf("%s", user);
printf("Enter your password: ");
scanf("%d", &pass);
if (login_user(user, pass)) {
printf("Login successful! Welcome, %s.\n", user);
goto top2;
}

else {
printf("Login failed. Username or password incorrect.\n");
}
}

return 0;
}
Output:
Flow Chart

Block diagram
3. Bus reservation system
#include <stdio.h>
#include <string.h>
#define MAX_USERS 100
#define MAX_BUSES 5

// Structure to store user details


struct {
char username[20];
int password;
} registered_users[MAX_USERS];

int registered_count = 0; // Counter for registered users

// Structure to store bus details


typedef struct {
char route[100];
char number_plate[10];
int price;
char status[20];
int tickets_available;
} Bus;

// List of predefined buses


Bus buses[MAX_BUSES] = {
{"Kathmandu to Pokhara", "9082", 2000, "Comfortable", 40},
{"Kathmandu to Chitwan", "7856", 2500, "Comfortable", 30},
{"Kathmandu to Surkhet", "8345", 3000, "Comparisalbe", 35},
{"Kathmandu to Jhapa", "5498", 4500, "Comfortable", 20},
{"Kathmandu to Sarlahi", "2968", 5000, "Not Comfortable", 50}
};

// Function to save user activity to a file


void save_user_data_to_file(const char *username, int password, const char *action, const char *bus_details) {
FILE *fptr = fopen("users.txt", "w");
if (fptr == NULL) {
printf("Error opening the file!\n");
return;
}
fprintf(fptr, "Username: %s\nPassword: %d\nAction: %s\nBus: %s\n\n", username, password, action,
bus_details);
fclose(fptr);
}

// Function to display all available buses


void display_buses() {
printf("\nAvailable\n");
for (int i = 0; i < MAX_BUSES; i++) {
printf("Bus No: %d\nRoute: %s\nNumber Plate: %s\nPrice: Rs %d\nStatus: %s\nTickets Available: %d\n\n",
i + 1, buses[i].route, buses[i].number_plate, buses[i].price, buses[i].status, buses[i].tickets_available);
}
}

// Function to book tickets for a bus


void book_ticket(char username[], int password) {
int bus_number, tickets;
display_buses();
printf("Enter the bus number you want to book tickets for: ");
scanf("%d", &bus_number);

if (bus_number < 1 || bus_number > MAX_BUSES) {


printf("Invalid bus number. Please try again.\n");
return;
}

printf("Enter the number of tickets you want to book: ");


scanf("%d", &tickets);

if (tickets > buses[bus_number - 1].tickets_available) {


printf("Not enough tickets available. Please try again.\n");
return;
}

buses[bus_number - 1].tickets_available -= tickets;


printf("You have successfully booked %d tickets for the bus: %s\n", tickets, buses[bus_number - 1].route);

// Save booking information to file


save_user_data_to_file(username, password, "Booked Tickets", buses[bus_number - 1].route);
}

// Function to cancel booked tickets


void cancel_ticket(char username[], int password) {
int bus_number, tickets;
printf("Enter the bus number for which you want to cancel tickets: ");
scanf("%d", &bus_number);

if (bus_number < 1 || bus_number > MAX_BUSES) {


printf("Invalid bus number. Please try again.\n");
return;
}

printf("Enter the number of tickets you want to cancel: ");


scanf("%d", &tickets);

buses[bus_number - 1].tickets_available += tickets;


printf("You have successfully canceled %d tickets for the bus: %s\n", tickets, buses[bus_number - 1].route);

// Save cancellation information to file


save_user_data_to_file(username, password, "Cancelled Tickets", buses[bus_number - 1].route);
}

// Function to display bus status


void check_bus_status() {
display_buses();
}

// Function to verify login credentials


int login_user(char user[], int pass) {
for (int i = 0; i < registered_count; i++) {
if (strcmp(registered_users[i].username, user) == 0 && registered_users[i].password == pass) {
return 1; // Login successful
}
}
return 0; // Login failed
}

// Main function to handle program logic


int main() {
char user[20];
int pass, choice, choice2, choice3;
top3:
// Welcome screen and main menu
printf("-----------------------------------------WELCOME TO ABC BUS RESERVATION SYSTEM------------------
\n");
printf(" Book tickets, check bus status, and more!\n");
printf(" Please select what you want to do:\n\n");
printf("->Register to our ABC bus reservation system, press: 1\n");
printf(" \n Already registered? \n\n->Login to check or bus tickets, press: 2\n");
printf("\nEnter your choice: ");
scanf("%d", &choice);

if (choice == 1) { // User registration


printf(" Enter the following information
----\n\n");
printf("Enter your username: ");
scanf("%s", user);
printf("Enter your password (only numbers): ");
scanf("%d", &pass);

strcpy(registered_users[registered_count].username, user);
registered_users[registered_count].password = pass;
registered_count++;
printf("Registration successful! You can now log in.\n");
goto top;
} else if (choice == 2) { // User login
top:
printf(" Login
\n\n");
printf("Enter your username: ");
scanf("%s", user);
printf("Enter your password: ");
scanf("%d", &pass);

if (login_user(user, pass)) { // If login is successful


printf("Login successful! Welcome, %s.\n", user);
top2:
// Menu after successful login
printf("\nTo view available buses, press: 10\n");
printf("To book tickets, press: 11\n");
printf("To cancel tickets, press: 12\n");
printf("To check bus status, press: 13\n");
printf("To logout, press: 14\n");
printf("To exit the system, press: 15\n");
printf("\nEnter your choice: ");
scanf("%d", &choice2);

if (choice2 == 10) { // Display available buses


display_buses();
goto top2;
} else if (choice2 == 11) { // Book tickets
book_ticket(user, pass);
goto top2;
} else if (choice2 == 12) { // Cancel tickets
cancel_ticket(user, pass);
goto top2;
} else if (choice2 == 13) { // Check bus status
check_bus_status();
goto top2;
} else if (choice2 == 14) { // Logout
goto top3;
} else if (choice2 == 15) { // Exit the system
printf("Exiting the system. Goodbye!\n");
}
} else { // If login fails
printf("Login failed. Username or password incorrect.\n");
goto top;
}
}

return 0;
}
Output:
Flow Chart
Block diagram
4. Tic Tac Toe
#include <stdio.h>
#define SIZE 3 // Constant for the size of the board
char board[SIZE][SIZE]; // Tic Tac Toe board
char player = 'X'; // Starting player

// Function to initialize the board


void initializeBoard() {
for (int i = 0; i < SIZE; i++) {
for (int j = 0; j < SIZE; j++) {
board[i][j] = ' ';
}
}
}

// Function to print the board


void printBoard() {
printf("\n");
for (int i = 0; i < SIZE; i++) {
printf(" %c | %c | %c \n", board[i][0], board[i][1], board[i][2]);
if (i < SIZE - 1) {
printf("---|---|---\n");
}
}
printf("\n");
}

// Function to check for a win


int checkWin() {
for (int i = 0; i < SIZE; i++) {
// Check rows and columns
if ((board[i][0] == player && board[i][1] == player && board[i][2] == player) ||
(board[0][i] == player && board[1][i] == player && board[2][i] == player)) {
return 1; // Win found
}
}
// Check diagonals
if ((board[0][0] == player && board[1][1] == player && board[2][2] == player) ||
(board[0][2] == player && board[1][1] == player && board[2][0] == player)) {
return 1; // Win found
}
return 0; // No win
}

// Function to check for a draw


int isDraw() {
// Use only one loop to check for an empty space
for (int i = 0; i < SIZE * SIZE; i++) {
if (board[i / SIZE][i % SIZE] == ' ') {
return 0; // There is still an empty space
}
}
return 1; // No empty spaces left
}

// Function to switch players


void switchPlayer() {
player = (player == 'X') ? 'O' : 'X';
}

// Function to make a move


void makeMove() {
int row, col;
while (1) {
printf("Player %c, enter your move (row and column: 1-3): ", player);
if (scanf("%d %d", &row, &col) != 2 || row < 1 || row > 3 || col < 1 || col > 3) {
printf("Invalid input. Please enter two numbers between 1 and 3.\n");
while(getchar() != '\n'); // Clear invalid input
continue;
}
row--; // Convert to 0-based index
col--;
if (board[row][col] == ' ') {
board[row][col] = player; // Make move
break;
} else {
printf("Invalid move. Try again.\n");
}
}
}

// Main function of the game


int main() {
initializeBoard();
while (1) {
printBoard();
makeMove();
if (checkWin()) {
printBoard();
printf("Player %c wins!\n", player);
break; // Exit on win
}
if (isDraw()) {
printBoard();
printf("It's a draw!\n");
break; // Exit on draw
}
switchPlayer();
}
return 0;
}
Output:
Flow Chart
Block Diagram
5. Telecom Billing System
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define size 100 // Maximum number of customers that can be stored
struct customer
{
int ID; //Customer ID
char name[50]; //Customer name
float payment; // Payment made by the customer
};
struct customer cust[size]; // Array to store customer records
int customercount = 0; // Keeps track of the current number of customers

// Function to add a new customer record


void customer_rec()
{
FILE *fptr = fopen("customer_rec.txt","w");
if(fptr==NULL)
{
printf("Error opening the file!");
}
if(customercount<size)
{
printf("\nEnter the customer ID: ");
scanf("%d",&cust[customercount].ID);
printf("Enter the customer name: ");
scanf("%s",&cust[customercount].name);
printf("Enter the payment amount: ");
scanf("%f",&cust[customercount].payment);
printf("Record of %s with ID: %d was added
successfully!\n",cust[customercount].name,cust[customercount].ID);
fprintf(fptr,"ID: %d,\tName:
%f\tPayment:\n",cust[customercount].ID,cust[customercount].name,cust[customercount].payment);
customercount++;
}
else
{
printf("Maximum amount of customer record has reached. No more records can be added");
}
fclose(fptr);
}
void customer_count()
{
if(customercount==0)
{
printf("No record of any customer was fount.\nTo enter the record of customer please enter the respective
number 1 for it.");
}
else
{
printf("\nOut of 100 record you have entered the record of %d customers.\n",customercount);
int vaccant = 100-customercount;
printf("You have space for %d more records",vaccant);
}
}

void viewRecords() {
if (customercount == 0) { // Check if there are no records to display
printf("No records found.\n");
return;
}

printf("\nCustomer Records:\n");
printf("ID\tName\t\tPayment\n");
printf(" \n");
for (int i = 0; i < customercount; i++) {
printf("%d\t%-15s\t%f\n",cust[i].ID, cust[i].name, cust[i].payment);
}
}

void modifyRecord() {
int ID;
printf("Enter customer ID to modify: ");
scanf("%d", &ID);
int found = 0;

// Search for the customer by ID


for (int i = 0; i < customercount; i++) {
if (cust[i].ID == ID) {
// Prompt user to modify the details
printf("Enter new name (current: %s): ", cust[i].name);
scanf("%49s", cust[i].name);
printf("Enter new payment amount (current: %f): ", cust[i].payment);
scanf("%f", &cust[i].payment);
printf("Record modified successfully.\n");
found = 1;
break;
}
}
if (!found) {
printf("Invalid ID.\n"); // If no record is found for the entered ID
}
}
// Function to view the payment details of a customer
void viewPayment() {
int ID;
printf("Enter customer ID to view payment: ");
scanf("%d", &ID);

// Search for the customer and display the payment details


for (int i = 0; i < customercount; i++) {
if (cust[i].ID == ID) {
printf("Payment for %s (ID: %d) is %f\n", cust[i].name, ID, cust[i].payment);
return;
}
}
printf("Invalid ID.\n"); // If the ID is not found
}

// Function to search a record by customer name


void searchRecord() {
char name[50];
printf("Enter customer name to search: ");
scanf("%49s", name);
int found = 0;

// Search for the customer by name


for (int i = 0; i < customercount; i++) {
if (strcmp(cust[i].name, name) == 0) {
printf("Record found: ID: %d, Name: %s, Payment: %f\n", cust[i].ID, cust[i].name, cust[i].payment);
found = 1;
break;
}
}
if (!found) {
printf("No record found for the name: %s\n", name); // If no customer matches the name
}
}

// Function to delete a customer record by ID


void deleteRecord() {
int ID;
printf("Enter customer ID to delete: ");
scanf("%d", &ID);
int found = 0;

// Search for the customer by ID and delete the record


for (int i = 0; i < customercount; i++) {
if (cust[i].ID == ID) {
// Shift the remaining records left to remove the entry
for (int j = i; j < customercount - 1; j++) {
cust[j] = cust[j + 1];
}
customercount--; // Decrease the customer count
printf("Record deleted successfully.\n");
found = 1;
break;
}
}
if (!found) {
printf("Invalid ID.\n"); // If the ID is not found
}
}

int main()
{
int choice;
char c;
printf("--------------------------------------WELCOME TO THE CBA TELECOM BILLING SYSTEM----------
");
top:
printf("\n\nPlease Select what you want to do: \n");
printf("a)For adding new customer record press: 1\n");
printf("b)To view List of records press: 2\n");
printf("c)To Modify Record press: 3\n");
printf("d)To View Payment press: 4\n");
printf("e)To Search Records press: 5\n");
printf("f)To Delete Record press: 6\n");
printf("g)To Exit press: 7\n");
printf("Enter your choice: ");
scanf("%d",&choice);
switch(choice)
{
case 1:

customer_rec();
break;

case 2:

viewRecords();
break;

case 3:

modifyRecord();
break;
case 4:

viewPayment();
break;

case 5:

searchRecord();
break;

case 6:

deleteRecord();
break;

}
printf("\n\nDo you wish continue?(press Y to continue N to exit): ");
scanf("%s",&c);
if(c=='y'||c=='Y')
{
goto top;
}
else
{
printf("Exiting the program.\nExit Successful!");
}

return 0;
}
OUTPUT:
Flow Chart
Block Diagram

You might also like