Showing posts with label C Example. Show all posts
Showing posts with label C Example. Show all posts

Zeller's Congruence Algorithm in C.

Zeller's Congruence is an algorithm devised by Christian Zeller to calculate the day of the week for any date. The algorithm provides a simple and efficient way to determine the weekday for any date in the Gregorian calendar. 

The formula for Zeller's Congruence is given by:
Zeller's Congruence Algorithm
Where:
  • h is the day of the week (0 = Saturday, 1 = Sunday, 2 = Monday, ..., 6 = Friday)
  • q is the day of the month
  • m is the month (3 = March, 4 = April, ..., 14 = February)
  • K is the year of the century (i.e., year mod 100)
  • J is the zero-based century (actually ⌊ year/100 ⌋)
The result h corresponds to the days of the week, where 0 is Saturday, 1 is Sunday, and so on.

Here's a breakdown of the formula:
  • The term 13(+1)5 adjusts for the months (January and February are counted as months 13 and 14 of the previous year).
  • +4 takes care of the year part.
  • 42 deals with the century adjustment.

Example: Let's calculate the day of the week for March 1, 2023.
Calculation of the day of the week for March 1, 2023
After calculation, we got h value equal to 4 which means March 1, 2023, is Thursday.

C program Implementation of Zeller's Congruence.

// C program to find day of the week for a Date
// Zeller's Congruence
#include <stdio.h>

int zellersCongruence(int day, int month, int year) {
    if (month < 3) {
        month += 12;
        year--;
    }

    int K = year % 100;
    int J = year / 100;

    int h = (day + (13 * (month + 1)) / 5 + K + K / 4 + J / 4 - 2 * J) % 7;

    // Convert h to a more conventional form 
    // (0 = Saturday, 1 = Sunday, ..., 6 = Friday)
    return h % 7;
}

int main() {
    int day = 1, month = 3, year = 2023;

    int dayOfWeek = zellersCongruence(day, month, year);
    char arr[7][10] = {"Saturday", "Sunday", "Monday", 
                       "Tuesday", "Wednesday", "Thursday", "Friday"};
    
    printf("March 1, 2023 is a %s", arr[dayOfWeek]);
    
    return 0;
}
Output:
March 1, 2023 is a Wednesday

In this C program, the zellersCongruence function calculates the day of the week using Zeller's Congruence, and the result is then displayed in a user-friendly format. 

C Program to Reverse Given String.

Reversing a string is a process of arranging the character of the string in opposite directions. In this C programming tutorial, we are going to learn multiple ways to reverse the given String.

Example:
Input: str = Algolesson
Output: nosseloglA

Input: str = GoodCode
Output: edoCdooG

Reverse String Using Loop.

In this approach, we will reverse a string by using a loop that will iterate through the characters of the original string and build the reversed string character by character.

Below is the C implementation to reverse the string using for loop:
// C program to reverse the given string 
#include <stdio.h>
#include <string.h>

// function to reverse string
void reverseString(char str[]) {
    int length = strlen(str);
    for (int i = 0; i < length / 2; i++) {
        char temp = str[i];
        str[i] = str[length - i - 1];
        str[length - i - 1] = temp;
    }
}

int main() {
    char str[100];
    printf("Enter a string: ");
    scanf("%s", str);

    reverseString(str);

    printf("Reversed string: %s\n", str);

    return 0;
}
Output:
Enter a string: Algolesson
Reversed string: nosseloglA

Time Complexity: O(n)
Space Complexity: O(1)

Reverse String Using Recursion.

In this approach, we are going to use Recursion to reverse the given string. 

Below C code implementation:
// C program to reverse the given string using recursion
#include <stdio.h>

void reverseString(char str[], int start, int end) {
    if (start >= end) {
        return;
    }
    char temp = str[start];
    str[start] = str[end];
    str[end] = temp;
    reverseString(str, start + 1, end - 1);
}

int main() {
    char str[100];
    printf("Enter a string: ");
    scanf("%s", str);

    int length = strlen(str);
    reverseString(str, 0, length - 1);

    printf("Reversed string: %s\n", str);

    return 0;
}
Output:
Enter a string: Welcome
Reversed string: emocleW

In the recursive function, start and end represent the indices of the characters that need to be swapped. The function keeps swapping characters and recursively calling itself with the updated indices until the base case is reached.

Time Complexity: O(n)
Space Complexity: O(1)

DON'T MISS

Tech News
© all rights reserved
made with by AlgoLesson