Posts

Maze Solver in C

              Maze is a two-dimensional grid. It has the values of 0’s and 1’s. maze solving is the process of finding the path from two entries. This implementation uses Depth-First-Search to find the path. C implementation: Let us create a Maze as two dimensional array. Here, value ‘1’ represents path and ‘0’ represents wall(no path). The path starts from top-left and travels through bottom-right. ‘isitSafe()’ function checks the path is available or not. ‘solve_maze()’ finds the solution for this maze solver. ‘displaysolution()’ prints the solved maze. ‘main()’ calls the function ‘solve_maze()’ and prints the value according to it. C Program: #include <stdio.h> #define no 5 int maze[no][no] = {     {1, 1, 0, 0, 1},     {1, 1, 0, 1, 0},     {0, 1, 1, 0, 1},     {1, 1, 0, 0, 1},     {0, 1, 0, 0, 1} }; int m_soln[no][no]; int isitSafe(...

Calendar Generator in C

Image
                 It is a classic concept in mathematics. Calendar has a day, month and year. It also deals with leap year which comes 4 year once. Let us create a calendar generator based on year. C implementation: It uses 4 functions to implement this concept. LeapYear_check() : Used to check the given year is leap or not. getDays_Month() : This gives you number of days in a month. Get_firstDay() : It gives the first day of the month using Zeller’s Congruence. Display_month() : displays the month of the calendar. Finally, main() function reads the year from the user. C Program: #include <stdio.h> // Check the leap year int LeapYear_check(int year1) {     return (year1 % 4 == 0 && year1 % 100 != 0) || (year1 % 400 == 0); } // Get the number of days in a month int getDays_Month(int month1, int year1) {     int days1[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31...

Tower of Hanoi Problem implementation in C

              Tower of Hanoi is a classical problem in operating system. It has rods and disks. To arrange it in a particular order, this program is used. Steps to follow:               Create a function towerofHanoi()with four parameters. No- number of disks, source and auxiliary and destination. First, the control moves no-1 disks to the auxiliary rod. Next, it moves the largest disk to the destination. At last, it moves the no-1 disks from auxiliary to destination. C Program: #include <stdio.h>   // function towerofHanoi void towerofHanoi(int no, char src, char auxi, char dest) {     if (no == 1) {         printf("Move disk 1 from %c to %c\n", src, dest);         return;     }       // As a beginning,Move n-1 disks from source to auxi...

Prime factorization in C

 Prime factorization:     It is mathematical process which has a product of its prime factors. For example, let us consider 48. Eg: 48 =2 x 2 x 2 x 2 x 3 The algorithm is given below… Algorithm: ·        First, Start with the smallest prime number. Let it 2. ·        Next, divide the number by the prime until it is divisible. ·        Try next prime for dividing the number. ·        Repeat the process until the number becomes 1. Program implementation: ·        First, create a function prime_Factorization() with the input of no. ·        It finds the number is divisible by 2 or not. If yes, it repeats the process until the number becomes odd. ·        The odd number is checked for its prime division until, it becomes 1. ·    ...

Basic Matrix Operations in C

               Matrix is one of the important mathematical concepts. It has rows and columns. Th structure is given below. For eg, 2x2 means 2 rows and 2 columns   1   2                3   4 Here, each element is represented in a ij format. The operations in C is given below…. Addition, Subtraction, Multiplication and Transpose Program implementation: #include <stdio.h> #define M_SIZE 3   // Set the size as 3 void input_Matrix(int mat1[M_SIZE][M_SIZE], const char *name) {     printf("Enter the elements of %s (%dx%d):\n", name, M_SIZE, M_SIZE);     for (int i = 0; i < M_SIZE; i++)         for (int j = 0; j < M_SIZE; j++) {             printf("%s[%d][%d]: ", name, i, j);      ...

Employee record management in C

               Let us create a employee record management in C. A employee record has name, id and salary. The operations are adding the details, search an employee, delete a record and display the employee records. Program implementation: #include <stdio.h> #include <stdlib.h> #include <string.h> #define MAX 100 struct Employee {     int e_id;     char e_name[50];     float e_salary; }; struct Employee emp[MAX]; int count = 0; void addEmployee() {     printf("\nEnter the Employee id: ");     scanf("%d", &emp[count].e_id);     printf("Enter the Employee Name: ");     scanf(" %[^\n]", emp[count].e_name);     printf("Enter the salary: ");     scanf("%f", &emp[count].e_salary);     count++;     printf("Employee details ar...

Quick sort implementation in C

               Quick sort is one of the sorting methods in data structures. The algorithm follows the divide and conquer method. Logic: First, a pivot element is selected. Check the elements in the array with the pivot element. If the element is less than pivot, it goes to the left of the array. If the element is greater than the array, it goes to the right of the array. This process is repeated until the last element. Program: #include <stdio.h> void fn_swap(int *a, int *b) {     int temp = *a;     *a = *b;     *b = temp; } int fn_partition(int arr[], int v_low, int v_high) {     int pivot = arr[v_high]; // set last element as pivot     int i = v_low - 1;     for (int j = v_low; j < v_high; j++) {         if (arr[j] < pivot) {         ...