Posts

Program to check leap year

Image
LEAP YEAR: A leap year is a year that is divisible by 4, but not by 100, unless it is also divisible by 400. This means that years like 2024, 2020, and 2016 are leap years, but 2100 is not. PROGRAM: include <stdio.h> int  main() {     int year;     printf("Enter the year: ");     scanf("%d",&year);     if((year%4==0 && year%100!=0) || year%400==0)     {     printf("%d is a leap year", year);     }     else     {     printf("%d is not a leap year", year);     }     return 0; } TIME COMPLEXITY: The time complexity of this program is O(1) because the program only performs a fixed number of operations regardless of the input size. The if- else statement and the printf statements are executed once each, making the time complexity O(1). SPACE COMPLEXITY: The space complexity is O(1) because the program only uses a constant amount of memory to stor...

Swap two numbers without using third variable

Image
SWAPPING TWO NUMBERS WITHOUT USING A THIRD VARIABLE There are two common ways to swap two numbers without using a third variable: PROGRAM 1 (Using addition and Subtraction): #include <stdio.h> int main() {     int a,b;     printf("enter the value of a: ");     scanf("%d",&a);     printf("enter the value of b: ");     scanf("%d",&b);     printf("Before swapping, a=%d b=%d.",a,b);     a=a+b;     b=a-b;     a=a-b;     printf("\nAfter swapping, a=%d b=%d.",a,b);     return 0; } PROGRAM 2 (Using Bitwise XOR): #include <stdio.h> int main() {     int a,b;     printf("enter the value of a: ");     scanf("%d",&a);     printf("enter the value of b: ");     scanf("%d",&b);     printf("Before swapping, a=%d b=%d.",a,b);     a=a^b;     b=a^b;     a=a^b;     printf("...