Download File
1) Cube of a number using if (terminate on negative input)
#include <stdio.h>
int main() {
    int n;
    while (1) {
        printf("Enter a number (negative to exit): ");
        scanf("%d", &n);
        if (n < 0)
            break;
        printf("Cube of %d is %d\n", n, n * n * n);
    }
    return 0;
}
2) Swap two numbers
#include <stdio.h>
int main() {
    int a, b, temp;
    printf("Enter two numbers: ");
    scanf("%d %d", &a, &b);
    temp = a;
    a = b;
    b = temp;
    printf("After swapping: a = %d, b = %d\n", a, b);
    return 0;
}
3) Area and perimeter of a square
#include <stdio.h>
int main() {
    int side;
    printf("Enter side of square: ");
    scanf("%d", &side);
    printf("Area = %d\n", side * side);
    printf("Perimeter = %d\n", 4 * side);
    return 0;
}
4) Operator precedence example
#include <stdio.h>
int main() {
    int a = 10, b = 5, c = 2;
    int result = a - b * c;
    printf("Result = %d\n", result);
    return 0;
}
5) Check if number divisible by 5
#include <stdio.h>
int main() {
    int n;
    printf("Enter a number: ");
    scanf("%d", &n);
    if (n % 5 == 0)
        printf("%d is divisible by 5\n", n);
    else
        printf("%d is not divisible by 5\n", n);
    return 0;
}
6) Profit or Loss
#include <stdio.h>
int main() {
    float cp, sp;
    printf("Enter Cost Price and Selling Price: ");
    scanf("%f %f", &cp, &sp);
    if (sp > cp)
        printf("Profit = %.2f\n", sp - cp);
    else if (cp > sp)
        printf("Loss = %.2f\n", cp - sp);
    else
        printf("No Profit No Loss\n");
    return 0;
}
7) Bill calculation
#include <stdio.h>
int main() {
    float price = 50;
    int quantity = 5;
    float amount = price * quantity;
    float discount = amount * 0.10;
    float discounted_total = amount - discount;
    float tax = amount * 0.05;
    float total_amount = discounted_total + tax;

    printf("Bill\n");
    printf("Price per item: %.2f\n", price);
    printf("Quantity Sold: %d\n", quantity);
    printf("===============================================\n");
    printf("Amount: %.2f\n", amount);
    printf("Discount 10%% = %.2f\n", discount);
    printf("Discounted Total = %.2f\n", discounted_total);
    printf("Tax 5%% = %.2f\n", tax);
    printf("===============================================\n");
    printf("Total Amount = %.2f\n", total_amount);
    return 0;
}
8) Size of different data types
#include <stdio.h>
int main() {
    printf("Size of int = %lu\n", sizeof(int));
    printf("Size of float = %lu\n", sizeof(float));
    printf("Size of char = %lu\n", sizeof(char));
    printf("Size of double = %lu\n", sizeof(double));
    return 0;
}
9) Find oldest among three
#include <stdio.h>
int main() {
    int ram, shyam, ajay;
    printf("Enter ages of Ram, Shyam and Ajay: ");
    scanf("%d %d %d", &ram, &shyam, &ajay);
    if (ram > shyam && ram > ajay)
        printf("Ram is oldest\n");
    else if (shyam > ajay)
        printf("Shyam is oldest\n");
    else
        printf("Ajay is oldest\n");
    return 0;
}
10) Switch case arithmetic operations
#include <stdio.h>
int main() {
    int a, b, choice;
    printf("Enter two numbers: ");
    scanf("%d %d", &a, &b);
    printf("1.Addition\n2.Subtraction\n3.Multiplication\n4.Division\n");
    printf("Enter choice: ");
    scanf("%d", &choice);
    switch(choice) {
        case 1: printf("Sum = %d\n", a + b); break;
        case 2: printf("Difference = %d\n", a - b); break;
        case 3: printf("Product = %d\n", a * b); break;
        case 4: if (b != 0) printf("Quotient = %d\n", a / b); else printf("Division by zero error\n"); break;
        default: printf("Invalid choice\n");
    }
    return 0;
}
11) Weekday switch case
#include <stdio.h>
int main() {
    int day;
    printf("Enter weekday number (1-7): ");
    scanf("%d", &day);
    switch(day) {
        case 1: printf("Monday\n"); break;
        case 2: printf("Tuesday\n"); break;
        case 3: printf("Wednesday\n"); break;
        case 4: printf("Thursday\n"); break;
        case 5: printf("Friday\n"); break;
        case 6: printf("Saturday\n"); break;
        case 7: printf("Sunday\n"); break;
        default: printf("Invalid day\n");
    }
    return 0;
}
12) Percentage from 5 subjects
#include <stdio.h>
int main() {
    int marks[5], sum = 0;
    // Assuming total marks for each subject is 100, so total is 500
    for (int i = 0; i < 5; i++) {
        printf("Enter marks for subject %d (out of 100): ", i+1);
        scanf("%d", &marks[i]);
        sum += marks[i];
    }
    // Ensure floating point division
    float percentage = (float)sum / 500.0 * 100.0;
    printf("Percentage = %.2f%%\n", percentage);
    return 0;
}
13) Check positive or negative
#include <stdio.h>
int main() {
    int num;
    printf("Enter a number: ");
    scanf("%d", &num);
    // Considering 0 as positive as per common convention
    if (num >= 0)
        printf("Number is positive (or zero)\n");
    else
        printf("Number is negative\n");
    return 0;
}
14) Even or Odd
#include <stdio.h>
int main() {
    int num;
    printf("Enter a number: ");
    scanf("%d", &num);
    if (num % 2 == 0)
        printf("Even number\n");
    else
        printf("Odd number\n");
    return 0;
}
15) Average of 10 students
#include <stdio.h>
int main() {
    int marks[10], sum = 0;
    for (int i = 0; i < 10; i++) {
        printf("Enter marks for student %d: ", i+1);
        scanf("%d", &marks[i]);
        sum += marks[i];
    }
    // Ensure floating point division
    float avg = (float)sum / 10.0;
    printf("Average = %.2f\n", avg);
    return 0;
}
16) Transpose of 3x3 matrix
#include <stdio.h>
int main() {
    int a[3][3];
    printf("Enter matrix elements (3x3 row by row):\n");
    for (int i = 0; i < 3; i++) {
        for (int j = 0; j < 3; j++) {
            scanf("%d", &a[i][j]);
        }
    }

    printf("\nOriginal Matrix:\n");
     for (int i = 0; i < 3; i++) {
        for (int j = 0; j < 3; j++) {
            printf("%d ", a[i][j]);
        }
        printf("\n");
    }

    printf("\nTranspose Matrix:\n");
    for (int i = 0; i < 3; i++) {
        for (int j = 0; j < 3; j++) {
            printf("%d ", a[j][i]); // Swap indices for transpose
        }
        printf("\n");
    }
    return 0;
}
17) Multiplication table from 11 to 20
#include <stdio.h>
int main() {
    for (int i = 11; i <= 20; i++) {
        printf("\n===== Table of %d =====\n", i);
        for (int j = 1; j <= 10; j++) {
            printf("%d x %d = %d\n", i, j, i * j);
        }
    }
    return 0;
}
18) Swap using function
#include <stdio.h>

// Function to swap values using pointers
void swap(int *x, int *y) {
    int temp = *x;
    *x = *y;
    *y = temp;
}

int main() {
    int a, b;
    printf("Enter two numbers (a and b): ");
    scanf("%d %d", &a, &b);
    printf("Before swapping: a = %d, b = %d\n", a, b);
    swap(&a, &b); // Pass addresses of a and b
    printf("After swapping: a = %d, b = %d\n", a, b);
    return 0;
}
19) Difference between adjacent elements
#include <stdio.h>
#define SIZE 5 // Define array size for easier modification

int main() {
    int arr[SIZE];
    printf("Enter %d numbers:\n", SIZE);
    for (int i = 0; i < SIZE; i++) {
        scanf("%d", &arr[i]);
    }

    printf("\nDifferences between adjacent elements:\n");
    // Loop up to SIZE-1 because we access arr[i+1]
    for (int i = 0; i < SIZE - 1; i++) {
        printf("Difference between arr[%d](%d) and arr[%d](%d) = %d\n",
               i + 1, arr[i + 1], i, arr[i], arr[i + 1] - arr[i]);
    }
    return 0;
}
20) Largest of two using function
#include <stdio.h>

// Function to find the largest of two integers
int largest(int a, int b) {
    // Using ternary operator for concise comparison
    return (a > b) ? a : b;
}

int main() {
    int x, y;
    printf("Enter two numbers: ");
    scanf("%d %d", &x, &y);
    int large = largest(x, y);
    printf("The largest number is = %d\n", large);
    return 0;
}
21) Addition and subtraction using functions
#include <stdio.h>

// Function for addition
int add(int a, int b) {
    return a + b;
}

// Function for subtraction
int sub(int a, int b) {
    return a - b;
}

int main() {
    int a, b;
    printf("Enter two numbers (a and b): ");
    scanf("%d %d", &a, &b);
    printf("Addition (%d + %d) = %d\n", a, b, add(a, b));
    printf("Subtraction (%d - %d) = %d\n", a, b, sub(a, b));
    return 0;
}
22) Sum of 2x2 matrices
#include <stdio.h>
#define ROWS 2
#define COLS 2

int main() {
    int a[ROWS][COLS], b[ROWS][COLS], sum[ROWS][COLS];

    printf("Enter elements for the first %dx%d matrix:\n", ROWS, COLS);
    for (int i = 0; i < ROWS; i++) {
        for (int j = 0; j < COLS; j++) {
            printf("Enter element a[%d][%d]: ", i, j);
            scanf("%d", &a[i][j]);
        }
    }

    printf("\nEnter elements for the second %dx%d matrix:\n", ROWS, COLS);
    for (int i = 0; i < ROWS; i++) {
        for (int j = 0; j < COLS; j++) {
            printf("Enter element b[%d][%d]: ", i, j);
            scanf("%d", &b[i][j]);
        }
    }

    // Calculate the sum
    for (int i = 0; i < ROWS; i++) {
        for (int j = 0; j < COLS; j++) {
            sum[i][j] = a[i][j] + b[i][j];
        }
    }

    printf("\nSum of the two matrices:\n");
    for (int i = 0; i < ROWS; i++) {
        for (int j = 0; j < COLS; j++) {
            printf("%d ", sum[i][j]);
        }
        printf("\n");
    }

    return 0;
}
23) Read and display array
#include <stdio.h>
#define SIZE 5

int main() {
    int arr[SIZE];
    printf("Enter %d integer elements for the array:\n", SIZE);
    for (int i = 0; i < SIZE; i++) {
         printf("Enter element %d: ", i + 1);
        scanf("%d", &arr[i]);
    }

    printf("\nThe array elements are:\n");
    for (int i = 0; i < SIZE; i++) {
        printf("%d ", arr[i]);
    }
    printf("\n");

    return 0;
}
24) Factorial using function
#include <stdio.h>

// Recursive function to calculate factorial
// Use unsigned long long for larger results
unsigned long long factorial(int n) {
    // Base case: factorial of 0 or 1 is 1
    if (n < 0) {
        printf("Factorial not defined for negative numbers.\n");
        return 0; // Indicate error or undefined
    }
    if (n == 0 || n == 1) {
        return 1;
    } else {
        // Recursive step: n * factorial(n-1)
        return (unsigned long long)n * factorial(n - 1);
    }
}

int main() {
    int num;
    printf("Enter a non-negative integer: ");
    scanf("%d", &num);

    if (num < 0) {
         // Factorial function already prints message
    } else {
        unsigned long long fact = factorial(num);
        printf("Factorial of %d = %llu\n", num, fact);
    }
    return 0;
}

1st Paper

1 A. Write a C program to print the first n numbers from the Fibonacci series.
#include <stdio.h>

int main() {
    int n, i;
    long long first = 0, second = 1, next;

    printf("Enter the number of terms: ");
    scanf("%d", &n);

    printf("Fibonacci Series: ");

    for (i = 1; i <= n; ++i) {
        printf("%lld, ", first);
        next = first + second;
        first = second;
        second = next;
    }
    printf("\n");
    return 0;
}
1 B. Write a program to swap values of two numbers using a function (call by reference).
#include <stdio.h>

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

int main() {
    int num1, num2;

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

    printf("Before swapping: num1 = %d, num2 = %d\n", num1, num2);

    swap(&num1, &num2); // Calling the swap function using call by reference

    printf("After swapping: num1 = %d, num2 = %d\n", num1, num2);

    return 0;
}

2nd Paper

1 A. Create a structure to specify the data on a student and write a program to print the names of students whose joining year is 2023.
#include <stdio.h>
#include <string.h>

#define MAX_STUDENTS 300

struct Student {
    int rollNumber;
    char name[50];
    char department[50];
    char course[50];
    int joiningYear;
};

int main() {
    struct Student students[MAX_STUDENTS];
    int n, i;

    printf("Enter the number of students (up to %d): ", MAX_STUDENTS);
    scanf("%d", &n);

    printf("Enter student data:\n");
    for (i = 0; i < n; i++) {
        printf("Enter details for student %d:\n", i + 1);
        printf("Roll Number: ");
        scanf("%d", &students[i].rollNumber);
        printf("Name: ");
        scanf("%s", students[i].name);
        printf("Department: ");
        scanf("%s", students[i].department);
        printf("Course: ");
        scanf("%s", students[i].course);
        printf("Year of Joining: ");
        scanf("%d", &students[i].joiningYear);
    }

    printf("\nStudents who joined in 2023:\n");
    for (i = 0; i < n; i++) {
        if (students[i].joiningYear == 2023) {
            printf("%s\n", students[i].name);
        }
    }

    return 0;
}
1 B. Write a program using conditional operators to determine whether a year entered through the keyboard is a leap year or not.
#include <stdio.h>

int main() {
    int year;

    printf("Enter a year: ");
    scanf("%d", &year);

    (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0) ?
        printf("%d is a leap year.\n", year) :
        printf("%d is not a leap year.\n", year);

    return 0;
}

3rd Paper

1 A. Write a C program to read two [3 x 3] matrix elements and find the difference of it.
#include <stdio.h>

int main() {
    int matrix1[3][3], matrix2[3][3], difference[3][3];
    int i, j;

    printf("Enter elements of the first 3x3 matrix:\n");
    for (i = 0; i < 3; i++) {
        for (j = 0; j < 3; j++) {
            scanf("%d", &matrix1[i][j]);
        }
    }

    printf("Enter elements of the second 3x3 matrix:\n");
    for (i = 0; i < 3; i++) {
        for (j = 0; j < 3; j++) {
            scanf("%d", &matrix2[i][j]);
        }
    }

    // Calculate the difference
    for (i = 0; i < 3; i++) {
        for (j = 0; j < 3; j++) {
            difference[i][j] = matrix1[i][j] - matrix2[i][j];
        }
    }

    printf("\nDifference of the two matrices:\n");
    for (i = 0; i < 3; i++) {
        for (j = 0; j < 3; j++) {
            printf("%d ", difference[i][j]);
        }
        printf("\n");
    }

    return 0;
}
1 B. Write a C program that calculates the factorial of a given number using recursion.
#include <stdio.h>

long long factorial(int n) {
    if (n >= 1)
        return n * factorial(n - 1);
    else
        return 1;
}

int main() {
    int num;
    long long fact;

    printf("Enter a positive integer: ");
    scanf("%d", &num);

    if (num < 0) {
        printf("Factorial of a negative number doesn't exist.\n");
    } else {
        fact = factorial(num);
        printf("Factorial of %d = %lld\n", num, fact);
    }

    return 0;
}

4th Paper

1 A. Write a program for the Addition of two matrices.
#include <stdio.h>
#include <stdlib.h> // For dynamic memory allocation (if needed for variable size)

int main() {
    int size, i, j;
    int **matrix1, **matrix2, **sum;

    printf("Input the size of the square matrix: ");
    scanf("%d", &size);

    // Allocate memory for the matrices
    matrix1 = (int **)malloc(size * sizeof(int *));
    matrix2 = (int **)malloc(size * sizeof(int *));
    sum = (int **)malloc(size * sizeof(int *));
    for (i = 0; i < size; i++) {
        matrix1[i] = (int *)malloc(size * sizeof(int));
        matrix2[i] = (int *)malloc(size * sizeof(int));
        sum[i] = (int *)malloc(size * sizeof(int));
    }

    printf("Input elements in the first matrix:\n");
    for (i = 0; i < size; i++) {
        for (j = 0; j < size; j++) {
            scanf("%d", &matrix1[i][j]);
        }
    }

    printf("Input elements in the second matrix:\n");
    for (i = 0; i < size; i++) {
        for (j = 0; j < size; j++) {
            scanf("%d", &matrix2[i][j]);
        }
    }

    // Calculate the sum
    for (i = 0; i < size; i++) {
        for (j = 0; j < size; j++) {
            sum[i][j] = matrix1[i][j] + matrix2[i][j];
        }
    }

    printf("The First matrix is :\n");
    for (i = 0; i < size; i++) {
        for (j = 0; j < size; j++) {
            printf("%d ", matrix1[i][j]);
        }
        printf("\n");
    }

    printf("The Second matrix is :\n");
    for (i = 0; i < size; i++) {
        for (j = 0; j < size; j++) {
            printf("%d ", matrix2[i][j]);
        }
        printf("\n");
    }

    printf("The Addition of two matrix is :\n");
    for (i = 0; i < size; i++) {
        for (j = 0; j < size; j++) {
            printf("%d ", sum[i][j]);
        }
        printf("\n");
    }

    // Free allocated memory
    for (i = 0; i < size; i++) {
        free(matrix1[i]);
        free(matrix2[i]);
        free(sum[i]);
    }
    free(matrix1);
    free(matrix2);
    free(sum);

    return 0;
}
1 B. Write a program to find the sum of n natural numbers using recursion.
#include <stdio.h>

int sumNaturalNumbers(int n) {
    if (n == 0)
        return 0;
    else
        return n + sumNaturalNumbers(n - 1);
}

int main() {
    int num;

    printf("Enter a positive integer: ");
    scanf("%d", &num);

    if (num < 0) {
        printf("Enter a positive integer.\n");
    } else {
        printf("Sum of first %d natural numbers = %d\n", num, sumNaturalNumbers(num));
    }

    return 0;
}

5th Paper

1 A. Write a program to check if a number is even or odd using a function.
#include <stdio.h>

void checkEvenOdd(int num) {
    if (num % 2 == 0) {
        printf("%d is an even number\n", num);
    } else {
        printf("%d is an odd number\n", num);
    }
}

int main() {
    int number;

    printf("Enter a number: ");
    scanf("%d", &number);

    checkEvenOdd(number);

    return 0;
}
1 B. Write a Program to Print Continuous Character Pattern
#include <stdio.h>

int main() {
    int rows, i, j;
    char ch = 'A';

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

    for (i = 1; i <= rows; i++) {
        for (j = 1; j <= i; j++) {
            printf("%c ", ch);
            ch++;
        }
        printf("\n");
    }

    return 0;
}

6th Paper

1 A. Write a program using a structure to accept the month and amount of rainfall and find the maximum rainfall and its respective month.
#include <stdio.h>
#include <string.h>

#define MAX_MONTHS 12 // Assuming you might want to extend it

struct Rainfall {
    char month[20];
    int amount;
};

int main() {
    struct Rainfall rainfall[MAX_MONTHS];
    int n, i, maxRainfall = -1, maxIndex = -1;

    printf("Enter the number of months: ");
    scanf("%d", &n);

    printf("Enter month and rainfall (e.g., January 20):\n");
    for (i = 0; i < n; i++) {
        scanf("%s %d", rainfall[i].month, &rainfall[i].amount);
    }

    for (i = 0; i < n; i++) {
        if (rainfall[i].amount > maxRainfall) {
            maxRainfall = rainfall[i].amount;
            maxIndex = i;
        }
    }

    if (maxIndex != -1) {
        printf("\nMaximum rainfall: %dmm\n", rainfall[maxIndex].amount);
        printf("Month of max rainfall: %s\n", rainfall[maxIndex].month);
    } else {
        printf("No rainfall data entered.\n");
    }

    return 0;
}
1 B. Write a program to find the transpose of a matrix of order (3*3).
#include <stdio.h>

int main() {
    int matrix[3][3], transpose[3][3];
    int i, j;

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

    // Calculate the transpose
    for (i = 0; i < 3; i++) {
        for (j = 0; j < 3; j++) {
            transpose[j][i] = matrix[i][j];
        }
    }

    printf("\nThe original matrix is:\n");
    for (i = 0; i < 3; i++) {
        for (j = 0; j < 3; j++) {
            printf("%d ", matrix[i][j]);
        }
        printf("\n");
    }

    printf("\nThe transpose of the matrix is:\n");
    for (i = 0; i < 3; i++) {
        for (j = 0; j < 3; j++) {
            printf("%d ", transpose[i][j]);
        }
        printf("\n");
    }

    return 0;
}

7th Paper

1 A. Write a program to print the output in the following format: * * * * * * * * * * * * * * * *
#include <stdio.h>

int main() {
    int rows, i, j;

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

    for (i = 1; i <= rows; i++) {
        for (j = 1; j <= i; j++) {
            printf("* ");
        }
        printf("\n");
    }

    return 0;
}
1 B. Write a program to perform the addition of 2 matrices of order (3*3).
#include <stdio.h>

int main() {
    int matrix1[3][3], matrix2[3][3], sum[3][3];
    int i, j;

    printf("Enter elements of the first 3x3 matrix:\n");
    for (i = 0; i < 3; i++) {
        for (j = 0; j < 3; j++) {
            scanf("%d", &matrix1[i][j]);
        }
    }

    printf("Enter elements of the second 3x3 matrix:\n");
    for (i = 0; i < 3; i++) {
        for (j = 0; j < 3; j++) {
            scanf("%d", &matrix2[i][j]);
        }
    }

    // Calculate the sum
    for (i = 0; i < 3; i++) {
        for (j = 0; j < 3; j++) {
            sum[i][j] = matrix1[i][j] + matrix2[i][j];
        }
    }

    printf("\nSum of the two matrices:\n");
    for (i = 0; i < 3; i++) {
        for (j = 0; j < 3; j++) {
            printf("%d ", sum[i][j]);
        }
        printf("\n");
    }

    return 0;
}

8th Paper

1 A. Write a program to find the sum of all elements of the array.
#include <stdio.h>
#include <stdlib.h> // For dynamic memory allocation

int main() {
    int size, i, *arr, sum = 0;

    printf("Input the number of elements: ");
    scanf("%d", &size);

    // Allocate memory for the array
    arr = (int *)malloc(size * sizeof(int));

    if (arr == NULL) {
        printf("Memory allocation failed!\n");
        return 1; // Indicate an error
    }

    printf("Input elements in the array:\n");
    for (i = 0; i < size; i++) {
        scanf("%d", &arr[i]);
        sum += arr[i];
    }

    printf("Sum of all elements in array is : %d\n", sum);

    // Free allocated memory
    free(arr);

    return 0;
}
B. Write a program to implement following pattern using for loop. ***** **** *** ** *
#include <stdio.h>

int main() {
    int rows, i, j;

    printf("Input Format: Number of rows\n");
    printf("Enter the number of rows: ");
    scanf("%d", &rows);

    printf("Output Format: Above Pattern\n");

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

    return 0;
}
Copied to clipboard!