//Prime Number Checker

#include <stdio.h>
// Function to check if a number is prime
int isPrime(int num) {
    if (num <= 1) return 0;
    for (int i = 2; i <= num / 2; i++) {
        if (num % i == 0) return 0;
    }
    return 1;
}


int main() {
    int num;
    printf("Enter a number: ");
    scanf("%d", &num);
    if (isPrime(num)) {
        printf("%d is a prime number.\n", num);
    } else {
        printf("%d is not a prime number.\n", num);
         }
    return 0;
}



//Largest Number Out Of Three

#include <stdio.h>
int main() {
    int a, b, c, largest;
    printf("Enter first number: ");
    scanf("%d", &a);
    printf("Enter second number: ");
    scanf("%d", &b);
    printf("Enter third number: ");
    scanf("%d", &c);
    if (a >= b && a >= c) {
        largest = a;
    } else if (b >= a && b >= c) {
        largest = b;
    } else {
        largest = c;
    }
    printf("The largest number is: %d\n", largest);
    return 0;
}



//Fibonacci Sequence

#include <stdio.h>
// Function to generate Fibonacci sequence
void generateFibonacci(int n) {
    int fib[n];
    fib[0] = 0;
    fib[1] = 1;
    // Generate Fibonacci sequence
    for (int i = 2; i < n; i++) {
        fib[i] = fib[i - 1] + fib[i - 2];
    }
    // Print Fibonacci sequence
    printf("Fibonacci sequence up to %d terms:\n", n);
    for (int i = 0; i < n; i++) {
        printf("%d ", fib[i]);
    }
    printf("\n");
}
int main() {
int n;
    printf("Enter the number of terms: ");
    scanf("%d", &n);
    // Check if n is a positive integer
    if (n <= 0) {
        printf("Please enter a positive integer.\n");
    } else {
        generateFibonacci(n);
    }
     return 0;
}



//Factorial Recursion

#include <stdio.h>
// Function to calculate factorial using recursion
long long factorial(int n) {
    if (n == 0 || n == 1) {
        return 1;
    } else {
        return n * factorial(n - 1);
    }
}


int main() {
    int num;
    printf("Enter a number: ");
    scanf("%d", &num);
    // Check if the number is negative
    if (num < 0) {
        printf("Factorial is not defined for negative numbers.\n");
    } else {
        printf("Factorial of %d = %11d\n", num, factorial(num));
    }
    return 0;
}



//Palindrome

#include <stdio.h>
#include <string.h>


int main() {
    char str[100];
    int i, len, isPalindrome = 1;


    printf("Enter a string: ");
    fgets(str, sizeof(str), stdin);
    len = strlen(str);
   
    if (str[len - 1] == '\n') str[len - 1] = '\0'; // remove newline
    len = strlen(str);


    for (i = 0; i < len / 2; i++) {
        if (str[i] != str[len - i - 1]) {
            isPalindrome = 0;
            break;
        }
    }


    if (isPalindrome)
        printf("The string is a palindrome.\n");
    else
        printf("The string is not a palindrome.\n");


    return 0;
}



//Swap Two Numbers

#include <stdio.h>


void swap(int *a, int *b) {
    int temp = *a;
    *a = *b;
    *b = temp;
}


int main() {
    int x, y;


    printf("Enter two numbers: ");
    scanf("%d %d", &x, &y);


    printf("Before swapping: x = %d, y = %d\n", x, y);
    swap(&x, &y);
    printf("After swapping: x = %d, y = %d\n", x, y);


    return 0;
}




//Pointer to pointer

#include <stdio.h>


int main() {
    int val = 42;
    int *ptr = &val;
    int **pptr = &ptr;


    printf("Value: %d\n", val);
    printf("Value via ptr: %d\n", *ptr);
    printf("Value via pptr: %d\n", **pptr);


    return 0;
}



//append file

#include <stdio.h>>
#include <stdlib.h>
// Function to append the contents of one file to another
void append_file(const char *source, const char *destination) {
    FILE *src, *dest;
    char buffer[1024];
    // Open the source file in read mode
    src = fopen(source, "r");
    if (src == NULL) {
        printf("Could not open source file\n");
         exit(1);
    }
    // Open the destination file in append mode
    dest = fopen(destination, "a");
    if (dest == NULL) {
        printf("Could not open destination file\n");
        exit(1);
    }
    // Read from the source file and write to the destination file
    while (fread(buffer, 1, 1024, src) > 0) {
        fwrite(buffer, 1, 1024, dest);
    }
    // Close the files
    fclose(src);
    fclose(dest);
}

// Function to get the size of a file
long get_file_size(const char *filename) {
    FILE *file;
    long size;
    // Open the file in read mode
    file = fopen(filename, "r");
    if (file == NULL) {
        printf("Could not open file\n");
        exit(1);
    }
    // Get the current position
    fseek(file, 0, SEEK_END);
    size = ftell(file);
    // Close the file
    fclose(file);

     return size;
}
int main() {
    const char *source = "source.txt";
    const char *destination = "destination.txt";

    // Append the contents of the source file to the destination file
    append_file(source, destination);
    // Get the size of the resultant file
    long size = get_file_size(destination);
    // Display the size of the resultant file
    printf("The size of the resultant file is: %ld bytes\n", size);

    return 0;
}



//GCD and LCM

#include <stdio.h>

int gcd(int a, int b) {
    while (b != 0) {
        int temp = b;
        b = a % b;
        a = temp;
    }
    return a;
}

int lcm(int a, int b) {
    return (a * b) / gcd(a, b);
}

int main() {
    int num1, num2;

    printf("Enter two numbers: ");
    scanf("%d %d", &num1, &num2);

    printf("GCD of %d and %d is %d\n", num1, num2, gcd(num1, num2));
    printf("LCM of %d and %d is %d\n", num1, num2, lcm(num1, num2));

    return 0;
}




//consonents and strings

#include 
#include 
int main() {
    char str[100];
    int vowels = 0, consonants = 0;
    printf("Enter a string: ");
    fgets(str, sizeof(str), stdin);

    for (int i = 0; str[i]; i++) {
        char ch = tolower(str[i]);
        if (isalpha(ch)) {
            (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u') ? vowels++ : consonants++;
        }
    }

    printf("Vowels: %d\nConsonants: %d\n", vowels, consonants);
    return 0;
}




//largest and smallest array

#include <stdio.h>
// Function to find the largest and smallest elements in an array
void findLargestSmallest(int arr[], int n, int *largest, int *smallest) {
    *largest = *smallest = arr[0];
    for (int i = 1; i < n; i++) {
        if (arr[i] > *largest) {
            *largest = arr[i];
        } else if (arr[i] < *smallest) {
            *smallest = arr[i];
        }
    }
    }
int main() {
    int n;
    printf("Enter the number of elements in the array: ");
    scanf("%d", &n);


    int arr[n];
    printf("Enter the elements of the array: ");
    for (int i = 0; i < n; i++) {
        scanf("%d", &arr[i]);
    }
    int largest, smallest;
    findLargestSmallest(arr, n, &largest, &smallest);

    printf("Largest element: %d\n", largest);
    printf("Smallest element: %d\n", smallest);
    return 0;
} 




//matrix Multiplication

#include <stdio.h>

int main() {
    int a[10][10], b[10][10], sum[10][10], mul[10][10];
    int i, j, k, r1, c1, r2, c2;

    printf("Enter rows and columns for first matrix: ");
    scanf("%d%d", &r1, &c1);
    printf("Enter rows and columns for second matrix: ");
    scanf("%d%d", &r2, &c2);

    if (r1 != r2 || c1 != c2) {
        printf("Addition not possible. Matrix dimensions must match.\n");
    } else {
        printf("Enter elements of first matrix:\n");
        for (i = 0; i < r1; i++)
            for (j = 0; j < c1; j++)
                scanf("%d", &a[i][j]);

        printf("Enter elements of second matrix:\n");
        for (i = 0; i < r2; i++)
            for (j = 0; j < c2; j++)
                scanf("%d", &b[i][j]);

        printf("Sum of matrices:\n");
        for (i = 0; i < r1; i++) {
            for (j = 0; j < c1; j++) {
                sum[i][j] = a[i][j] + b[i][j];
                printf("%d ", sum[i][j]);
            }
            printf("\n");
        }
    }

    if (c1 != r2) {
        printf("Multiplication not possible. Columns of first must equal rows of second.\n");
    } else {
        for (i = 0; i < r1; i++) {
            for (j = 0; j < c2; j++) {
                mul[i][j] = 0;
                for (k = 0; k < c1; k++) {
                    mul[i][j] += a[i][k] * b[k][j];
                }
            }
        }

        printf("Product of matrices:\n");
        for (i = 0; i < r1; i++) {
            for (j = 0; j < c2; j++) {
                printf("%d ", mul[i][j]);
            }
            printf("\n");
        }
    }

    return 0;
}





//number words and character file

#include <stdio.h>
#include <ctype.h>

int main() {
    FILE *fp;
    char ch;
    int words = 0, chars = 0;
    int inWord = 0;

    fp = fopen("sample.txt", "w");
    if (fp == NULL) {
        printf("File cannot be opened.\n");
        return 1;
    }

    printf("Enter text (end input with # on a new line):\n");
    while ((ch = getchar()) != '#') {
        fputc(ch, fp);
    }
    fclose(fp);

    fp = fopen("sample.txt", "r");
    if (fp == NULL) {
        printf("File cannot be opened.\n");
        return 1;
    }

    while ((ch = fgetc(fp)) != EOF) {
        chars++;
        if (isspace(ch))
            inWord = 0;
        else if (!inWord) {
            inWord = 1;
            words++;
        }
    }

    fclose(fp);

    printf("Total characters: %d\n", chars);
    printf("Total words: %d\n", words);

    return 0;
}





//strev function

#include <stdio.h>
#include <string.h>

int main() {
    char str[100], temp;
    int i, len;

    printf("Enter a string: ");
    fgets(str, sizeof(str), stdin);

    len = strlen(str);
    if (str[len - 1] == '\n') str[--len] = '\0';

    for (i = 0; i < len / 2; i++) {
        temp = str[i];
        str[i] = str[len - i - 1];
        str[len - i - 1] = temp;
    }

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

    return 0;
}






//bubblr sort algpo

#include
//Function to check if a number is prime
int isPrime(int num){
    if(num<=0)return 0;
    for (int i = 2; i <= num/2;i++){
        if(num%i==0)return 0;
    }
    return 1;
}

int main (){
    int num;
    printf("Enter a number: ");
    scanf("%d",&num);
    if(isPrime(num)){
        printf("%d is a prime number.\n",num);
    }else{
        printf("%d is not a prime number.\n",num);
    }
    return 0;
}