1) Design and test sample C programs to display a message on screen.

#include <stdio.h>

int main() {

    // Display a simple message

    printf("Hello, World!\n");

    return 0;

}


2) Design and test minimum 3 C programs using constants, variables and data-types.

Program 1: Simple Arithmetic Operations

#include <stdio.h>

int main() {

    // Constants

    const int number1 = 10;

    const int number2 = 5;

    // Variables

    int sum, difference, product;

    float quotient;

    // Arithmetic operations

    sum = number1 + number2;

    difference = number1 - number2;

    product = number1 * number2;

    quotient = (float)number1 / number2;

    // Display results

    printf("Sum: %d\n", sum);

    printf("Difference: %d\n", difference);

    printf("Product: %d\n", product);

    printf("Quotient: %.2f\n", quotient);

    return 0;

}

Program 2: Input and Output of Different Data Types

#include <stdio.h>

int main() {

    // Variables

    int integerNumber;

    float floatNumber;

    char character;

    // Input

    printf("Enter an integer: ");

    scanf("%d", &integerNumber);

    printf("Enter a floating-point number: ");

    scanf("%f", &floatNumber);

    printf("Enter a character: ");

    scanf(" %c", &character);

    // Output

    printf("Integer: %d\n", integerNumber);

    printf("Float: %.2f\n", floatNumber);

    printf("Character: %c\n", character);

    return 0;

}

Program 3: Using Constants for Mathematical Calculations

#include <stdio.h>

// Constants

#define PI 3.14159

int main() {

    // Variables

    float radius, area;

    // Input

    printf("Enter the radius of a circle: ");

    scanf("%f", &radius);

    // Area calculation using the constant PI

    area = PI * radius * radius;

    // Output

    printf("Area of the circle: %.2f\n", area);

    return 0;

}


3) Design and test a C program to swap 2 numbers using a third variable and without using a third variable

Program 1: Swap with a Third Variable

#include <stdio.h>

int main() {

    // Variables

    int num1, num2, temp;

    // Input

    printf("Enter the first number: ");

    scanf("%d", &num1);

    printf("Enter the second number: ");

    scanf("%d", &num2);

    // Swapping with a third variable

    temp = num1;

    num1 = num2;

    num2 = temp;

    // Output

    printf("After swapping (with third variable):\n");

    printf("First number: %d\n", num1);

    printf("Second number: %d\n", num2);

    return 0;

}

Program 2: Swap without a Third Variable (Using Arithmetic Operations)

#include <stdio.h>

int main() {

    // Variables

    int num1, num2;

    // Input

    printf("Enter the first number: ");

    scanf("%d", &num1);

    printf("Enter the second number: ");

    scanf("%d", &num2);

    // Swapping without a third variable using arithmetic operations

    num1 = num1 + num2;

    num2 = num1 - num2;

    num1 = num1 - num2;

    // Output

    printf("After swapping (without third variable):\n");

    printf("First number: %d\n", num1);

    printf("Second number: %d\n", num2);

    return 0;

}


4) Design and test a C program to compute volume and surface area of a sphere.

#include <stdio.h>

#include <math.h>

int main() {

    // Constants

    const double PI = 3.14159;

    // Variables

    double radius, volume, surfaceArea;

    // Input

    printf("Enter the radius of the sphere: ");

    scanf("%lf", &radius);

    // Calculations

    volume = (4.0 / 3.0) * PI * pow(radius, 3);

    surfaceArea = 4 * PI * pow(radius, 2);

    // Output

    printf("Volume of the sphere: %.2lf cubic units\n", volume);

    printf("Surface area of the sphere: %.2lf square units\n", surfaceArea);

    return 0;

}


5) Design and test a C program to convert temperature in Fahrenheit to Celsius and vice versa.

#include <stdio.h>
int main() {
    // Variables
    float temperature;
    char choice;
    // Input
    printf("Enter temperature: ");
    scanf("%f", &temperature);
    // Input choice (F for Fahrenheit, C for Celsius)
    printf("Enter the scale of temperature (F for Fahrenheit, C for Celsius): ");
    scanf(" %c", &choice);
    // Convert temperature based on the user's choice
    if (choice == 'F' || choice == 'f') {
        // Fahrenheit to Celsius conversion
        temperature = (temperature - 32) * 5 / 9;
        printf("Temperature in Celsius: %.2f\n", temperature);
    } else if (choice == 'C' || choice == 'c') {
        // Celsius to Fahrenheit conversion
        temperature = (temperature * 9 / 5) + 32;
        printf("Temperature in Fahrenheit: %.2f\n", temperature);
    } else {
        // Invalid choice
        printf("Invalid choice. Please enter 'F' or 'C'.\n");
    }
    return 0;
}

6) Design and test C programs to using enlisted operators: (1) Assignment (2) Arithmetic (3) Relational (4) Logical

Program 1: Assignment Operator
#include <stdio.h>
int main() {
    // Assignment Operator
    int number1, number2;
    // Assign values using the assignment operator
    number1 = 10;
    number2 = 5;
    // Display values
    printf("Number 1: %d\n", number1);
    printf("Number 2: %d\n", number2);

    return 0;
}

Program 2: Arithmetic Operators
#include <stdio.h>
int main() {
    // Arithmetic Operators
    int num1 = 10, num2 = 5;

    // Arithmetic operations
    printf("Sum: %d\n", num1 + num2);
    printf("Difference: %d\n", num1 - num2);
    printf("Product: %d\n", num1 * num2);
    printf("Quotient: %d\n", num1 / num2);
    printf("Remainder: %d\n", num1 % num2);
    return 0;
}

Program 3: Relational Operators
#include <stdio.h>
int main() {
    // Relational Operators
    int num1 = 10, num2 = 5;
    // Relational operations
    printf("%d is equal to %d: %d\n", num1, num2, num1 == num2);
    printf("%d is not equal to %d: %d\n", num1, num2, num1 != num2);
    printf("%d is greater than %d: %d\n", num1, num2, num1 > num2);
    printf("%d is less than %d: %d\n", num1, num2, num1 < num2);
    printf("%d is greater than or equal to %d: %d\n", num1, num2, num1 >= num2);
    printf("%d is less than or equal to %d: %d\n", num1, num2, num1 <= num2);
    return 0;
}

Program 4: Logical Operators
#include <stdio.h>
int main() {
    // Logical Operators
    int condition1 = 1, condition2 = 0;
    // Logical operations
    printf("AND: %d\n", condition1 && condition2);
    printf("OR: %d\n", condition1 || condition2);
    printf("NOT: %d\n", !condition1);
    return 0;
}


7) Design and test at least 5 C programs using the enlisted operators: (1) Bitwise (2) Increment and Decrement (3) Conditional (4) Comma (5) size of

Program 1: Bitwise Operators
#include <stdio.h>
int main() {
    // Bitwise Operators
    unsigned int a = 5; // 0000 0101
    unsigned int b = 3; // 0000 0011
    // Bitwise AND
    printf("Bitwise AND: %u\n", a & b); // 0000 0001
    // Bitwise OR
    printf("Bitwise OR: %u\n", a | b); // 0000 0111
    // Bitwise XOR
    printf("Bitwise XOR: %u\n", a ^ b); // 0000 0110
    // Bitwise NOT
    printf("Bitwise NOT (for 'a'): %u\n", ~a); // 1111 1010
    return 0;
}

Program 2: Increment and Decrement Operators
#include <stdio.h>
int main() {
    // Increment and Decrement Operators
    int num = 10;
    // Increment
    printf("After increment: %d\n", ++num);
    // Decrement
    printf("After decrement: %d\n", --num);
    return 0;
}

Program 3: Conditional Operator (Ternary Operator)
#include <stdio.h>
int main() {
    // Conditional Operator
    int a = 5, b = 3;
    int max = (a > b) ? a : b;
    // Display the maximum value
    printf("Maximum value: %d\n", max);
    return 0;
}

Program 4: Comma Operator
#include <stdio.h>
int main() {
    // Comma Operator
    int x = 5, y = 10, z;
    // Comma operator in action
    z = (x++, y++, x + y);
    // Display the result
    printf("Result after comma operator: %d\n", z);
    return 0;
}

Program 5: sizeof Operator
#include <stdio.h>
int main() {
    // sizeof Operator
    int integerVariable;
    double doubleVariable;
    // Display the size of different data types
    printf("Size of int: %zu bytes\n", sizeof(int));
    printf("Size of double: %zu bytes\n", sizeof(double));
    printf("Size of int variable: %zu bytes\n", sizeof(integerVariable));
    printf("Size of double variable: %zu bytes\n", sizeof(doubleVariable));
    return 0;
}


8) Design and test at least 2 C programs using decision making statements: (1) Simple if (2) if…else (3) Nested if (4) if…else ladder (5) switch (6) goto

Program 1: Simple if Statement
#include <stdio.h>
int main() {
    // Simple if Statement
    int num;
    // Input
    printf("Enter an integer: ");
    scanf("%d", &num);
    // Simple if statement to check if the number is positive
    if (num > 0) {
        printf("The number is positive.\n");
    }
    return 0;
}

Program 2: if...else Statement
#include <stdio.h>
int main() {
    // if...else Statement
    int num;
    // Input
    printf("Enter an integer: ");
    scanf("%d", &num);
    // if...else statement to check if the number is positive or not
    if (num > 0) {
        printf("The number is positive.\n");
    } else {
        printf("The number is either zero or negative.\n");
    }
    return 0;
}

Program 3: Nested if Statement
#include <stdio.h>
int main() {
    // Nested if Statement
    int num;
    // Input
    printf("Enter an integer: ");
    scanf("%d", &num);
    // Nested if statement to check if the number is positive, negative, or zero
    if (num > 0) {
        printf("The number is positive.\n");
    } else if (num < 0) {
        printf("The number is negative.\n");
    } else {
        printf("The number is zero.\n");
    }
    return 0;
}

Program 4: if...else Ladder
#include <stdio.h>
int main() {
    // if...else Ladder
    int num;
    // Input
    printf("Enter an integer: ");
    scanf("%d", &num);
    // if...else ladder to check the range of the number
    if (num > 0 && num <= 10) {
        printf("The number is between 1 and 10.\n");
    } else if (num > 10 && num <= 20) {
        printf("The number is between 11 and 20.\n");
    } else if (num > 20 && num <= 30) {
        printf("The number is between 21 and 30.\n");
    } else {
        printf("The number is either less than or equal to 0 or greater than 30.\n");
    }
    return 0;
}

Program 5: Switch Statement
#include <stdio.h>
int main() {
    // Switch Statement
    char operator;
    int num1, num2, result;
    // Input
    printf("Enter an operator (+, -, *, /): ");
    scanf(" %c", &operator);
    printf("Enter two numbers: ");
    scanf("%d %d", &num1, &num2);
    // Switch statement to perform different operations based on the operator
    switch (operator) {
        case '+':
            result = num1 + num2;
            printf("Sum: %d\n", result);
            break;
        case '-':
            result = num1 - num2;
            printf("Difference: %d\n", result);
            break;
        case '*':
            result = num1 * num2;
            printf("Product: %d\n", result);
            break;
        case '/':
            if (num2 != 0) {
                result = num1 / num2;
                printf("Quotient: %d\n", result);
            } else {
                printf("Cannot divide by zero.\n");
            }
            break;
        default:
            printf("Invalid operator.\n");
    }
    return 0;
}

Program 6: Goto Statement (Avoided where possible, not recommended)
#include <stdio.h>
int main() {
    // Goto Statement (Not recommended, used here for demonstration purposes)
    int num;
    // Input
    printf("Enter an integer: ");
    scanf("%d", &num);
    // Goto statement to jump to the specified label based on the value of num
    if (num > 0) {
        goto positive;
    } else if (num < 0) {
        goto negative;
    } else {
        goto zero;
    }

positive:
    printf("The number is positive.\n");
    goto end;

negative:
    printf("The number is negative.\n");
    goto end;

zero:
    printf("The number is zero.\n");

end:
    return 0;
}


9) Design and test at least 3 C programs using (1) for loop (2) while loop (3) do…while loop

Program 1: For Loop
#include <stdio.h>
int main() {
    // For Loop
    int n;
    // Input
    printf("Enter a number: ");
    scanf("%d", &n);
    // Using a for loop to print numbers from 1 to n
    printf("Using a for loop to print numbers from 1 to %d:\n", n);
    for (int i = 1; i <= n; i++) {
        printf("%d ", i);
    }
    printf("\n");

    return 0;
}

Program 2: While Loop
#include <stdio.h>
int main() {
    // While Loop
    int n;
    // Input
    printf("Enter a number: ");
    scanf("%d", &n);
    // Using a while loop to print numbers from 1 to n
    printf("Using a while loop to print numbers from 1 to %d:\n", n);
    int i = 1;
    while (i <= n) {
        printf("%d ", i);
        i++;
    }
    printf("\n");
    return 0;
}

Program 3: Do...While Loop
#include <stdio.h>
int main() {
    // Do...While Loop
    int n;
    // Input
    printf("Enter a number: ");
    scanf("%d", &n);
    // Using a do...while loop to print numbers from 1 to n
    printf("Using a do...while loop to print numbers from 1 to %d:\n", n);
    int i = 1;
    do {
        printf("%d ", i);
        i++;
    } while (i <= n);
    printf("\n");
    return 0;
}

10) Design and test a C program using break and continue statements

#include <stdio.h>
int main() {
    // Using break and continue statements
    int i;
    // Using break to exit the loop when i equals 5
    printf("Using break statement:\n");
    for (i = 1; i <= 10; i++) {
        if (i == 5) {
            break;
        }
        printf("%d ", i);
    }
    printf("\n");

    // Using continue to skip printing when i equals 5
    printf("\nUsing continue statement:\n");
    for (i = 1; i <= 10; i++) {
        if (i == 5) {
            continue;
        }
        printf("%d ", i);
    }
    printf("\n");

    return 0;
}


11) Design and test at least 5 pattern programs using loop structures

Program 1: Print a Half Pyramid
#include <stdio.h>
int main() {
    // Half Pyramid
    int rows;
    // Input
    printf("Enter the number of rows: ");
    scanf("%d", &rows);
    // Printing half pyramid
    for (int i = 1; i <= rows; i++) {
        for (int j = 1; j <= i; j++) {
            printf("* ");
        }
        printf("\n");
    }
    return 0;
}
Output:
* * 
* * * 
* * * * 
* * * * * 

Program 2: Print a Right-Angled Triangle
#include <stdio.h>
int main() {
    // Right-Angled Triangle
    int rows;
    // Input
    printf("Enter the number of rows: ");
    scanf("%d", &rows);
    // Printing right-angled triangle
    for (int i = 1; i <= rows; i++) {
        for (int j = 1; j <= i; j++) {
            printf("* ");
        }
        printf("\n");
    }
    return 0;
}
Output:
* * 
* * * 
* * * * 
* * * * * 

Program 3: Print an Inverted Half Pyramid
#include <stdio.h>
int main() {
    // Inverted Half Pyramid
    int rows;
    // Input
    printf("Enter the number of rows: ");
    scanf("%d", &rows);
    // Printing inverted half pyramid
    for (int i = rows; i >= 1; i--) {
        for (int j = 1; j <= i; j++) {
            printf("* ");
        }
        printf("\n");
    }
    return 0;
}
Output:
* * * * * 
* * * * 
* * * 
* * 

Program 4: Print a Pyramid
#include <stdio.h>
int main() {
    // Pyramid
    int rows, spaces;
    // Input
    printf("Enter the number of rows: ");
    scanf("%d", &rows);
    // Printing pyramid
    for (int i = 1, k = 0; i <= rows; ++i, k = 0) {
        for (spaces = 1; spaces <= rows - i; ++spaces) {
            printf("  ");
        }
        while (k != 2 * i - 1) {
            printf("* ");
            ++k;
        }
        printf("\n");
    }
    return 0;
}
Output:
        * 
      * * * 
    * * * * * 
  * * * * * * * 
* * * * * * * * * 

Program 5: Print a Rhombus Pattern (Diamond)
#include <stdio.h>
int main() {
    // Rhombus Pattern
    int rows;
    // Input
    printf("Enter the number of rows: ");
    scanf("%d", &rows);
    // Printing rhombus pattern
    for (int i = 1; i <= rows; ++i) {
        for (int j = 1; j <= rows - i; ++j) {
            printf("  ");
        }

        for (int k = 0; k != 2 * i - 1; ++k) {
            printf("* ");
        }
        printf("\n");
    }

    for (int i = rows - 1; i >= 1; --i) {
        for (int j = 1; j <= rows - i; ++j) {
            printf("  ");
        }

        for (int k = 0; k != 2 * i - 1; ++k) {
            printf("* ");
        }
        printf("\n");
    }
    return 0;
}
Output:
        * 
      * * * 
    * * * * * 
  * * * * * * * 
* * * * * * * * *
  * * * * * * * 
    * * * * * 
      * * * 
        * 



12) Design and test at least 5 C programs using (1) one dimensional array (2) two dimensional arrays.

Program 1: One-Dimensional Array - Sum of Elements
#include <stdio.h>
int main() {
    // One-Dimensional Array - Sum of Elements
    int size;
    // Input
    printf("Enter the size of the array: ");
    scanf("%d", &size);
    int array[size];
    // Input array elements
    printf("Enter %d elements:\n", size);
    for (int i = 0; i < size; ++i) {
        scanf("%d", &array[i]);
    }
    // Calculate sum of elements
    int sum = 0;
    for (int i = 0; i < size; ++i) {
        sum += array[i];
    }
    // Output
    printf("Sum of elements: %d\n", sum);
    return 0;
}

Program 2: One-Dimensional Array - Largest Element
#include <stdio.h>
int main() {
    // One-Dimensional Array - Largest Element
    int size;
    // Input
    printf("Enter the size of the array: ");
    scanf("%d", &size);
    int array[size];
    // Input array elements
    printf("Enter %d elements:\n", size);
    for (int i = 0; i < size; ++i) {
        scanf("%d", &array[i]);
    }
    // Find the largest element
    int max = array[0];
    for (int i = 1; i < size; ++i) {
        if (array[i] > max) {
            max = array[i];
        }
    }
    // Output
    printf("Largest element: %d\n", max);
    return 0;
}

Program 3: Two-Dimensional Array - Matrix Addition
#include <stdio.h>
int main() {
    // Two-Dimensional Array - Matrix Addition
    int rows, cols;
    // Input
    printf("Enter the number of rows: ");
    scanf("%d", &rows);
    printf("Enter the number of columns: ");
    scanf("%d", &cols);
    int matrix1[rows][cols], matrix2[rows][cols], result[rows][cols];
    // Input matrices
    printf("Enter elements of matrix1:\n");
    for (int i = 0; i < rows; ++i) {
        for (int j = 0; j < cols; ++j) {
            scanf("%d", &matrix1[i][j]);
        }
    }
    printf("Enter elements of matrix2:\n");
    for (int i = 0; i < rows; ++i) {
        for (int j = 0; j < cols; ++j) {
            scanf("%d", &matrix2[i][j]);
        }
    }
    // Matrix addition
    for (int i = 0; i < rows; ++i) {
        for (int j = 0; j < cols; ++j) {
            result[i][j] = matrix1[i][j] + matrix2[i][j];
        }
    }
    // Output
    printf("Result of matrix addition:\n");
    for (int i = 0; i < rows; ++i) {
        for (int j = 0; j < cols; ++j) {
            printf("%d ", result[i][j]);
        }
        printf("\n");
    }
    return 0;
}

Program 4: Two-Dimensional Array - Matrix Multiplication
#include <stdio.h>
int main() {
    // Two-Dimensional Array - Matrix Multiplication
    int rows1, cols1, rows2, cols2;
    // Input
    printf("Enter the number of rows for matrix1: ");
    scanf("%d", &rows1);
    printf("Enter the number of columns for matrix1: ");
    scanf("%d", &cols1);
    printf("Enter the number of rows for matrix2: ");
    scanf("%d", &rows2);
    printf("Enter the number of columns for matrix2: ");
    scanf("%d", &cols2);

    if (cols1 != rows2) {
        printf("Matrix multiplication is not possible.\n");
        return 1;
    }

    int matrix1[rows1][cols1], matrix2[rows2][cols2], result[rows1][cols2];

    // Input matrices
    printf("Enter elements of matrix1:\n");
    for (int i = 0; i < rows1; ++i) {
        for (int j = 0; j < cols1; ++j) {
            scanf("%d", &matrix1[i][j]);
        }
    }

    printf("Enter elements of matrix2:\n");
    for (int i = 0; i < rows2; ++i) {
        for (int j = 0; j < cols2; ++j) {
            scanf("%d", &matrix2[i][j]);
        }
    }

    // Matrix multiplication
    for (int i = 0; i < rows1; ++i) {
        for (int j = 0; j < cols2; ++j) {
            result[i][j] = 0;
            for (int k = 0; k < cols1; ++k) {
                result[i][j] += matrix1[i][k] * matrix2[k][j];
            }
        }
    }

    // Output
    printf("Result of matrix multiplication:\n");
    for (int i = 0; i < rows1; ++i) {
        for (int j = 0; j < cols2; ++j) {
            printf("%d ", result[i][j]);
        }
        printf("\n");
    }

    return 0;
}

Program 5: Two-Dimensional Array - Transpose of a Matrix
#include <stdio.h>
int main() {
    // Two-Dimensional Array - Transpose of a Matrix
    int rows, cols;

    // Input
    printf("Enter the number of rows: ");
    scanf("%d", &rows);

    printf("Enter the number of columns: ");
    scanf("%d", &cols);

    int matrix[rows][cols], transpose[cols][rows];

    // Input matrix
    printf("Enter elements of the matrix:\n");
    for (int i = 0; i < rows; ++i) {
        for (int j = 0; j < cols; ++j) {
            scanf("%d", &matrix[i][j]);
        }
    }

    // Transpose of the matrix
    for (int i = 0; i < rows; ++i) {
        for (int j = 0; j < cols; ++j) {
            transpose[j][i] = matrix[i][j];
        }
    }

    // Output
    printf("Original matrix:\n");
    for (int i = 0; i < rows; ++i) {
        for (int j = 0; j < cols; ++j) {
            printf("%d ", matrix[i][j]);
        }
        printf("\n");
    }

    printf("Transpose of the matrix:\n");
    for (int i = 0; i < cols; ++i) {
        for (int j = 0; j < rows; ++j) {
            printf("%d ", transpose[i][j]);
        }
        printf("\n");
    }
    return 0;
}


13) Design and test at least 3 C programs using strings.

Program 1: Length of a String
#include <stdio.h>
int main() {
    // Length of a String
    char str[100];

    // Input
    printf("Enter a string: ");
    gets(str);

    // Calculate and display the length of the string
    int length = 0;
    while (str[length] != '\0') {
        length++;
    }

    printf("Length of the string: %d\n", length);

    return 0;
}

Program 2: String Concatenation
#include <stdio.h>
#include <string.h>
int main() {
    // String Concatenation
    char str1[50], str2[50];

    // Input
    printf("Enter the first string: ");
    gets(str1);

    printf("Enter the second string: ");
    gets(str2);

    // Concatenate str2 to str1
    strcat(str1, str2);

    // Display the concatenated string
    printf("Concatenated string: %s\n", str1);

    return 0;
}

Program 3: Palindrome Check
#include <stdio.h>
#include <string.h>
int main() {
    // Palindrome Check
    char str[100];

    // Input
    printf("Enter a string: ");
    gets(str);

    // Check if the string is a palindrome
    int len = strlen(str);
    int isPalindrome = 1;

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

    // Display the result
    if (isPalindrome) {
        printf("The string is a palindrome.\n");
    } else {
        printf("The string is not a palindrome.\n");
    }

    return 0;
}

14) Design and test at least 3 C programs using pointers.

Program 1: Swap Two Numbers using Pointers
#include <stdio.h>
int main() {
    int num1 = 5, num2 = 10;
    int *ptr1 = &num1, *ptr2 = &num2;

    printf("Original: %d, %d\n", *ptr1, *ptr2);

    // Swap using pointers
    int temp = *ptr1;
    *ptr1 = *ptr2;
    *ptr2 = temp;

    printf("Swapped: %d, %d\n", *ptr1, *ptr2);

    return 0;
}

Program 2: Reverse a String using Pointers
#include <stdio.h>
#include <string.h>
int main() {
    char str[] = "Hello";
    char *start = str, *end = str + strlen(str) - 1;

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

    // Reverse the string using pointers
    while (start < end) {
        char temp = *start;
        *start = *end;
        *end = temp;

        start++;
        end--;
    }

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

    return 0;
}

Program 3: Find the Sum of Elements in an Array using Pointers
#include <stdio.h>
int main() {
    int array[] = {1, 2, 3, 4, 5};
    int size = sizeof(array) / sizeof(array[0]);
    int *ptr = array;
    int sum = 0;

    // Calculate the sum using pointers
    for (int i = 0; i < size; i++) {
        sum += *(ptr + i);
    }

    printf("Sum: %d\n", sum);

    return 0;
}


15) Design and test a C program using the concept of pointer to pointer.

Program: Swap Two Numbers using Pointer to Pointer
#include <stdio.h>
int main() {
    int num1 = 5, num2 = 10;
    int *ptr1 = &num1, *ptr2 = &num2;

    printf("Original: %d, %d\n", *ptr1, *ptr2);

    // Pointer to pointer
    int *temp = ptr1;
    ptr1 = ptr2;
    ptr2 = temp;

    printf("Swapped: %d, %d\n", *ptr1, *ptr2);

    return 0;
}

16) Design and test at least 5 C programs using user defined functions

1. Calculate the Area of a Circle:
#include <stdio.h>
#define PI 3.14159
float calculateArea(float radius) {
    return PI * radius * radius;
}
int main() {
    float radius;
    printf("Enter the radius of the circle: ");
    scanf("%f", &radius);
    float area = calculateArea(radius);
    printf("The area of the circle is: %.2f\n", area);
    return 0;
}

2. Sum of Two Numbers:
#include <stdio.h>
int add(int a, int b) {
    return a + b;
}
int main() {
    int num1, num2;
    printf("Enter two numbers: ");
    scanf("%d %d", &num1, &num2);
    int sum = add(num1, num2);
    printf("Sum: %d\n", sum);
    return 0;
}

3. Check Even or Odd:
#include <stdio.h>
void checkEvenOdd(int num) {
    if (num % 2 == 0)
        printf("%d is even.\n", num);
    else
        printf("%d is odd.\n", num);
}
int main() {
    int number;
    printf("Enter an integer: ");
    scanf("%d", &number);
    checkEvenOdd(number);
    return 0;
}

4. Fibonacci Series:
#include <stdio.h>
void generateFibonacci(int n) {
    int a = 0, b = 1, nextTerm;
    printf("Fibonacci Series: ");
    for (int i = 1; i <= n; ++i) {
        printf("%d, ", a);
        nextTerm = a + b;
        a = b;
        b = nextTerm;
    }
    printf("\n");
}
int main() {
    int terms;
    printf("Enter the number of terms for Fibonacci Series: ");
    scanf("%d", &terms);
    generateFibonacci(terms);
    return 0;
}

5. Factorial Calculation:
#include <stdio.h>
int calculateFactorial(int n) {
    if (n == 0 || n == 1)
        return 1;
    else
        return n * calculateFactorial(n - 1);
}
int main() {
    int num;
    printf("Enter a non-negative integer: ");
    scanf("%d", &num);
    if (num < 0)
        printf("Factorial is not defined for negative numbers.\n");
    else
        printf("Factorial of %d is: %d\n", num, calculateFactorial(num));
    return 0;
}

17) Design and test at least 3 C programs by applying the recursion concept.

1. Factorial Calculation Using Recursion:
#include <stdio.h>
int calculateFactorial(int n) {
    if (n == 0 || n == 1)
        return 1;
    else
        return n * calculateFactorial(n - 1);
}
int main() {
    int num;
    printf("Enter a non-negative integer: ");
    scanf("%d", &num);
    if (num < 0)
        printf("Factorial is not defined for negative numbers.\n");
    else
        printf("Factorial of %d is: %d\n", num, calculateFactorial(num));
    return 0;
}

2. Fibonacci Series Using Recursion:
#include <stdio.h>
int generateFibonacci(int n) {
    if (n <= 1)
        return n;
    else
        return generateFibonacci(n - 1) + generateFibonacci(n - 2);
}
void printFibonacciSeries(int terms) {
    printf("Fibonacci Series: ");
    for (int i = 0; i < terms; ++i) {
        printf("%d, ", generateFibonacci(i));
    }
    printf("\n");
}
int main() {
    int terms;
    printf("Enter the number of terms for Fibonacci Series: ");
    scanf("%d", &terms);
    printFibonacciSeries(terms);
    return 0;
}

3. Sum of Digits Using Recursion:
#include <stdio.h>
int sumOfDigits(int n) {
    if (n == 0)
        return 0;
    else
        return n % 10 + sumOfDigits(n / 10);
}
int main() {
    int num;
    printf("Enter an integer: ");
    scanf("%d", &num);
    printf("Sum of digits of %d is: %d\n", num, sumOfDigits(num));
    return 0;
}


18) Design and test a C program to test various inbuilt string functions.
#include <stdio.h>
#include <string.h>

int main() {
    char str1[50], str2[50];

    // Input
    printf("Enter the first string: ");
    fgets(str1, sizeof(str1), stdin);
    str1[strcspn(str1, "\n")] = '\0'; // Remove newline character from input

    printf("Enter the second string: ");
    fgets(str2, sizeof(str2), stdin);
    str2[strcspn(str2, "\n")] = '\0'; // Remove newline character from input

    // String Length
    printf("\nString Length:\n");
    printf("Length of the first string: %zu\n", strlen(str1));
    printf("Length of the second string: %zu\n", strlen(str2));

    // String Copy
    printf("\nString Copy:\n");
    strcpy(str1, str2);
    printf("Copied string (str1): %s\n", str1);

    // String Concatenation
    printf("\nString Concatenation:\n");
    strcat(str1, " ");
    strcat(str1, str2);
    printf("Concatenated string (str1): %s\n", str1);

    // String Comparison
    printf("\nString Comparison:\n");
    int result = strcmp(str1, str2);
    if (result == 0)
        printf("Both strings are equal.\n");
    else if (result < 0)
        printf("str1 is less than str2.\n");
    else
        printf("str1 is greater than str2.\n");

    // Substring Search
    printf("\nSubstring Search:\n");
    char *substring = strstr(str1, "world");
    if (substring != NULL)
        printf("Substring 'world' found at position: %ld\n", substring - str1);
    else
        printf("Substring 'world' not found.\n");

    return 0;
}

19) Design and test a C program to demonstrate various inbuilt math functions

#include <stdio.h>
#include <math.h>
int main() {
    double num;

    // Input
    printf("Enter a number: ");
    scanf("%lf", &num);

    // Square Root
    printf("\nSquare Root: %.2f\n", sqrt(num));

    // Power
    printf("Square of the number: %.2f\n", pow(num, 2));
    printf("Cube of the number: %.2f\n", pow(num, 3));

    // Absolute Value
    printf("\nAbsolute Value: %.2f\n", fabs(num));

    // Ceiling and Floor
    printf("\nCeiling and Floor:\n");
    printf("Ceiling of the number: %.2f\n", ceil(num));
    printf("Floor of the number: %.2f\n", floor(num));

    // Trigonometric Functions
    printf("\nTrigonometric Functions:\n");
    printf("Sine: %.2f\n", sin(num));
    printf("Cosine: %.2f\n", cos(num));
    printf("Tangent: %.2f\n", tan(num));

    return 0;
}

20) Design and test a C program to demonstrate storage classes

#include <stdio.h>

// Global variable with static storage class
static int globalStaticVariable = 10;

// Function prototype
void demoStorageClasses();

int main() {
    // Automatic variable
    auto int autoVariable = 5;

    // Register variable
    register int registerVariable = 20;

    printf("Automatic Variable: %d\n", autoVariable);
    printf("Register Variable: %d\n", registerVariable);
    printf("Global Static Variable: %d\n", globalStaticVariable);

    // Call the function to demonstrate static storage class
    demoStorageClasses();

    return 0;
}

// Function definition with static storage class
void demoStorageClasses() {
    // Local static variable
    static int localStaticVariable = 30;

    printf("Local Static Variable: %d\n", localStaticVariable);

    // Modify global static variable
    globalStaticVariable = 15;

    printf("Modified Global Static Variable: %d\n", globalStaticVariable);
}

21) Design and test a C program to demonstrate usage of enum and typdef

#include <stdio.h>
// Define an enumeration named 'Color'
enum Color {
    RED,
    GREEN,
    BLUE,
    YELLOW,
    WHITE
};

// Use typedef to create a new type name 'ColorType' for the 'enum Color'
typedef enum Color ColorType;

int main() {
    // Declare variables of the 'ColorType' enum type
    ColorType shirtColor, carColor;

    // Assign values to the variables
    shirtColor = BLUE;
    carColor = RED;

    // Use the enum values in a switch statement
    switch (shirtColor) {
        case RED:
            printf("The shirt color is Red.\n");
            break;
        case GREEN:
            printf("The shirt color is Green.\n");
            break;
        case BLUE:
            printf("The shirt color is Blue.\n");
            break;
        case YELLOW:
            printf("The shirt color is Yellow.\n");
            break;
        case WHITE:
            printf("The shirt color is White.\n");
            break;
        default:
            printf("Invalid color.\n");
    }

    // Print the car color
    printf("The car color is %d.\n", carColor);

    return 0;
}


22) Design and test at least 3 C programs on structures and unions.

1. Structure to Represent a Point in 2D Space:
#include <stdio.h>
// Define a structure named 'Point' to represent a point in 2D space
struct Point {
    float x;
    float y;
};

int main() {
    // Declare a variable of the 'Point' structure
    struct Point p1;

    // Input the coordinates
    printf("Enter x-coordinate: ");
    scanf("%f", &p1.x);
    printf("Enter y-coordinate: ");
    scanf("%f", &p1.y);

    // Display the coordinates
    printf("Coordinates of the point: (%.2f, %.2f)\n", p1.x, p1.y);

    return 0;
}

2. Union to Represent a Value of Either Integer or Float:
#include <stdio.h>
// Define a union named 'Number' to represent either an integer or a float
union Number {
    int intValue;
    float floatValue;
};

int main() {
    // Declare a variable of the 'Number' union
    union Number num;

    // Input an integer value
    printf("Enter an integer value: ");
    scanf("%d", &num.intValue);

    // Display the integer value
    printf("Integer Value: %d\n", num.intValue);

    // Input a float value
    printf("Enter a float value: ");
    scanf("%f", &num.floatValue);

    // Display the float value
    printf("Float Value: %.2f\n", num.floatValue);

    return 0;
}

3. Structure to Represent a Book:
#include <stdio.h>
// Define a structure named 'Book' to represent information about a book
struct Book {
    char title[100];
    char author[50];
    int pages;
    float price;
};

int main() {
    // Declare a variable of the 'Book' structure
    struct Book book1;

    // Input book details
    printf("Enter book title: ");
    scanf("%s", book1.title);
    printf("Enter author name: ");
    scanf("%s", book1.author);
    printf("Enter number of pages: ");
    scanf("%d", &book1.pages);
    printf("Enter price: ");
    scanf("%f", &book1.price);

    // Display book details
    printf("\nBook Details:\n");
    printf("Title: %s\n", book1.title);
    printf("Author: %s\n", book1.author);
    printf("Pages: %d\n", book1.pages);
    printf("Price: %.2f\n", book1.price);

    return 0;
}

23) Design and test at least 2 C programs using file operations

1. Writing to a File:
#include <stdio.h>
int main() {
    // Declare a FILE pointer
    FILE *file;

    // Open a file in write mode
    file = fopen("output.txt", "w");

    // Check if the file is opened successfully
    if (file == NULL) {
        printf("Unable to open the file.\n");
        return 1;
    }

    // Write data to the file
    fprintf(file, "Hello, this is a sample text.\n");

    // Close the file
    fclose(file);

    printf("Data written to the file successfully.\n");

    return 0;
}

2. Reading from a File:
#include <stdio.h>
int main() {
    // Declare a FILE pointer
    FILE *file;

    // Open a file in read mode
    file = fopen("output.txt", "r");

    // Check if the file is opened successfully
    if (file == NULL) {
        printf("Unable to open the file.\n");
        return 1;
    }

    // Read and display data from the file
    char buffer[100];
    while (fgets(buffer, sizeof(buffer), file) != NULL) {
        printf("%s", buffer);
    }

    // Close the file
    fclose(file);

    return 0;
}

If you have any question then comment below - We will answer your questions. And do not forget to follow us.