C Language Programming Slips

C Language Practical Slips


Slip1
Q1.Write a C program to accept dimensions of a cylinder and display the surface area and volume of cylinder. [15 Marks]

#include <stdio.h>
int main()
{
    float radius, height;
    float surface_area, volume;
    printf("Enter  value for  radius and height of a cylinder : \n");
    scanf("%f%f", &radius, &height);

    surface_area = 2 * (22 / 7) * radius * (radius + height);
    volume = (22 / 7) * radius * radius * height;
    printf("Surface area of cylinder is: %f", surface_area);
    printf("\n Volume of cylinder is : %f", volume);
}

Q2.Create a structure employee (id,name,salary). Accept details of n employees and write a menu driven program to perform the following operations. [25 Marks]
a)Search employee by id
b)Display all employees

#include<stdio.h>
struct details
{
     char name[30];
     int eid;
     int salary;
}emp[5];
void emp_search(int r)
{
     int id,i;
     printf("\nEnter Employee-Id to be Searched : ");
     scanf("%d",&id);
     printf("----------------------------------------\n");
     for(i=0;i<r;i++)
     {
          if(emp[i].eid==id)
          {
               printf("Employee Id : %d",emp[i].eid);
               printf("\nName        : %s",emp[i].name);
               printf("\nSalary      : %d\n",emp[i].salary);
          }
     }
}

void display(int r)
{
     int i;
     printf("\nList of All Employees:\n");
     printf("-------------------------------\n");
     printf("Emp-Id\tEmp-Name  Salary\n");
     printf("--------------------------------\n");
     for(i=0;i<r;i++)
     {
          printf("%d\t%s\t  %d\n",emp[i].eid,emp[i].name,emp[i].salary);
     }
}
int main()
{
     int n,i,ch;
     printf("/*How Many Employee Record You Want to Add*/\n\nEnter Limit  : ");
     scanf("\n %d",&n);
     for(i=0;i<n;i++)
     {
          printf("-----------------------------------------");
          printf("\n\tEnter Details of Employee-%d",i+1);
          printf("\n-----------------------------------------");
          printf("\nName of Employee : ");
          scanf("%s",emp[i].name);
          printf("Employee-Id      : ");
          scanf("%d",&emp[i].eid);
          printf("Salary : ");
          scanf("%d",&emp[i].salary);
     }
     while(1)
     {
          printf("-----------------------------------------\n");
          printf("\t\tMenu\n");
          printf("-----------------------------------------");
          printf("\n 1:Search Employee by E-ID");
          printf("\n 2:List of All Employee");
          printf("\n 3:Exit");
          printf("\n----------------------------------------\n");
          printf("Enter Your Choice : ");
          scanf("\n %d",&ch);
          switch(ch)
          {
               case 1: emp_search(n);
               break;
               case 2: display(n);
               break;
               case 3: exit(0);
          }
     }
     return 0;
}

Slip2
Q1.Write a C program to accept radius of a circle and display the area and circumference of a circle. [15 Marks]

#include <stdio.h>

int main() {
    float radius, area, circumference;
    float pi = 3.14;

    printf("Enter the radius of the circle: ");
    scanf("%f", &radius);

    area = pi * radius * radius;
    circumference = 2 * pi * radius;

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

    return 0;
}

Q2.Write a program to calculate sum of following series up to n terms. [25 Marks]
Sum=X+X2/2!+X3/3!+…… (Note: Write separate user defined function to calculate power and factorial)

#include <stdio.h>

float power(float base, int exp) {
    float result = 1;
    for(int i = 1; i <= exp; i++) {
        result = result * base;
    }
    return result;
}

int factorial(int num) {
    int fact = 1;
    for(int i = 1; i <= num; i++) {
        fact = fact * i;
    }
    return fact;
}

int main() {
    int n;
    float x, sum = 0;

    printf("Enter value of x: ");
    scanf("%f", &x);

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

    for(int i = 1; i <= n; i++) {
        sum = sum + (power(x, i) / factorial(i));
    }

    printf("Sum of the series = %.2f\n", sum);

    return 0;
}


Slip3
Q1.Write a C program to accept temperatures in Fahrenheit (F) and display it in Celsius(C) and Kelvin (K) (Hint:C=5.0/9(F-32),K=C+273.15) [15 Marks]

#include <stdio.h>

int main() {
    float f, c, k;

    printf("Enter temperature in Fahrenheit: ");
    scanf("%f", &f);

    c = 5.0 / 9 * (f - 32);
    k = c + 273.15;

    printf("Temperature in Celsius = %.2f\n", c);
    printf("Temperature in Kelvin = %.2f\n", k);

    return 0;
}

Q2.Write a menu driven program to perform the following operations on strings using standard library functions: [25 Marks]
1.Length of String
2.Copy String
3.Connect Two Strings
4.Compare two strings

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

int main() {
    int choice = 1;
    char a[100], b[100], c[200];

    while (choice != 5) {
        printf("\n1. Length of String\n");
        printf("2. Copy String\n");
        printf("3. Join Two Strings\n");
        printf("4. Compare Two Strings\n");
        printf("5. Exit\n");
        printf("Enter choice: ");
        scanf("%d", &choice);
        getchar();

        switch (choice) {
            case 1:
                printf("Enter string: ");
                gets(a);
                printf("Length = %lu\n", strlen(a));
                break;
            case 2:
                printf("Enter string: ");
                gets(a);
                strcpy(b, a);
                printf("Copied string: %s\n", b);
                break;
            case 3:
                printf("Enter first string: ");
                gets(a);
                printf("Enter second string: ");
                gets(b);
                strcpy(c, a);
                strcat(c, b);
                printf("Joined string: %s\n", c);
                break;
            case 4:
                printf("Enter first string: ");
                gets(a);
                printf("Enter second string: ");
                gets(b);
                if (strcmp(a, b) == 0)
                    printf("Both are same\n");
                else
                    printf("Both are different\n");
                break;
            case 5:
                break;
            default:
                printf("Invalid choice\n");
        }
    }

    return 0;
}


Slip4
Q1.Write a C program to accept two numbers and print arithmetic and harmonic mean of the two numbers (Hint:AM=(a+b)/2 ,HM=ab/(a+b)) [15 Marks]

#include <stdio.h>

int main() {
    float a, b, am, hm;
    printf("Enter two numbers: ");
    scanf("%f%f", &a, &b);

    am = (a + b) / 2;
    hm = (a * b) / (a + b);

    printf("Arithmetic Mean = %.2f\n", am);
    printf("Harmonic Mean = %.2f\n", hm);

    return 0;
}

Q2.Create a structure Student (id,name,marks). Accept details of n students and write a menu driven program to perform the following operations. [25 Marks]
a)Search student by id
b)Display all students

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

struct Student {
    int id;
    char name[50];
    float marks;
};
int main() {
    struct Student s[100];
    int n, i, choice, searchId;
    printf("Enter number of students: ");
    scanf("%d", &n);
    for (i = 0; i < n; i++) {
        printf("Enter ID, Name and Marks of student %d:\n", i + 1);
        scanf("%d", &s[i].id);
        getchar();
        gets(s[i].name);
        scanf("%f", &s[i].marks);
    }
    while (1) {
        printf("\n1. Search student by ID\n");
        printf("2. Display all students\n");
        printf("3. Exit\n");
        printf("Enter choice: ");
        scanf("%d", &choice);
        switch (choice) {
            case 1:
                printf("Enter ID to search: ");
                scanf("%d", &searchId);
                for (i = 0; i < n; i++) {
                    if (s[i].id == searchId) {
                        printf("ID: %d, Name: %s, Marks: %.2f\n", s[i].id, s[i].name, s[i].marks);
                        break;
                    }
                }
                if (i == n) {
                    printf("Student not found\n");
                }
                break;
            case 2:
                for (i = 0; i < n; i++) {
                    printf("ID: %d, Name: %s, Marks: %.2f\n", s[i].id, s[i].name, s[i].marks);
                }
                break;
            case 3:
                return 0;
            default:
                printf("Invalid choice\n");
        }
    }

    return 0;
}


Slip5
Q1.Write a C program to accept dimensions length(l), breadth(b) and height(h) of a cuboids and display surface area and volume (Hint: surface area=2(lb+lh+bh), volume=lbh) [15 Marks]

#include <stdio.h>

int main() {
    float l, b, h, surfaceArea, volume;
    printf("Enter length, breadth and height: ");
    scanf("%f %f %f", &l, &b, &h);
    surfaceArea = 2 * (l * b + l * h + b * h);
    volume = l * b * h;
    printf("Surface Area = %.2f\n", surfaceArea);
    printf("Volume = %.2f\n", volume);

    return 0;
}

Q2.Write a program which accepts a sentence from the user and alters it as follows: Every space is replaced by *, case of all alphabets is reversed, digits are replaced by ? [25 Marks]

#include <stdio.h>

int main() {
    char str[100];
    int i = 0;

    printf("Enter a sentence: ");
    gets(str);

    while (str[i] != '\0') {
        if (str[i] == ' ') {
            str[i] = '*';
        } else if (str[i] >= 'a' && str[i] <= 'z') {
            str[i] = str[i] - 32;
        } else if (str[i] >= 'A' && str[i] <= 'Z') {
            str[i] = str[i] + 32;
        } else if (str[i] >= '0' && str[i] <= '9') {
            str[i] = '?';
        }
        i++;
    }

    printf("Changed sentence: %s", str);
    return 0;
}


Slip6
Q1.Write a C Program to accept a character from the keyboard and display its previous and next character in order. Ex. If character entered is ‘d’, display “The previous character is c”, “The next character is e”. [15 Marks]

#include <stdio.h>

int main() {
    char ch;
    char prev, next;

    printf("Enter a character: ");
    scanf("%c", &ch);

    prev = ch - 1;
    next = ch + 1;

    printf("The previous character is %c\n", prev);
    printf("The next character is %c\n", next);

    return 0;
}

Q2.Write a program to accept a string and then count the occurrences of a specific character of a string. [25 Marks]

#include <stdio.h>

int main() {
    char str[100];
    char ch;
    int i = 0, count = 0;
    printf("Enter a string: ");
    scanf("%s", str);
    printf("Enter the character to search: ");
    scanf(" %c", &ch);

    while (str[i] != '\0') {
        if (str[i] == ch) {
            count++;
        }
        i++;
    }
    printf("The character '%c' occurred %d times.\n", ch, count);
    return 0;
}


Slip7
Q1.Write a C program to accept the x and y coordinates of two points and compute the distance between the two points. [15 Marks]

#include <stdio.h>
#include <math.h>

int main() {
    float x1, y1, x2, y2, distance;
    printf("Enter x1 and y1: ");
    scanf("%f %f", &x1, &y1);
    printf("Enter x2 and y2: ");
    scanf("%f %f", &x2, &y2);
    distance = sqrt((x2 - x1)*(x2 - x1) + (y2 - y1)*(y2 - y1));
    printf("Distance between points = %.2f", distance);

    return 0;
}

Q2.Write a program to calculate Multiplication of two matrices of order m*n. [25 Marks]

#include <stdio.h>

int main() {
    int a[10][10], b[10][10], c[10][10];
    int m, n, p, q, i, j, k;

    printf("Enter rows and columns of first matrix: ");
    scanf("%d %d", &m, &n);

    printf("Enter rows and columns of second matrix: ");
    scanf("%d %d", &p, &q);

    if (n != p) {
        printf("Matrix multiplication not possible");
        return 0;
    }

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

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

    for (i = 0; i < m; i++) {
        for (j = 0; j < q; j++) {
            c[i][j] = 0;
            for (k = 0; k < n; k++) {
                c[i][j] += a[i][k] * b[k][j];
            }
        }
    }

    printf("Resultant matrix:\n");
    for (i = 0; i < m; i++) {
        for (j = 0; j < q; j++) {
            printf("%d ", c[i][j]);
        }
        printf("\n");
    }

    return 0;
}


Slip8
Q1.A cashier has currency notes of denomination 1, 5 and 10. Write a C program to accept the withdrawal amount from the user and display the total number of currency notes of each denomination the cashier will have to give. [15 Marks]

#include <stdio.h>

int main() {
    int amount;
    int ten, five, one;

    printf("Enter the amount to withdraw: ");
    scanf("%d", &amount);

    ten = amount / 10;
    amount = amount % 10;

    five = amount / 5;
    amount = amount % 5;

    one = amount;

    printf("10 Rupee notes: %d\n", ten);
    printf("5 Rupee notes: %d\n", five);
    printf("1 Rupee notes: %d\n", one);

    return 0;
}

Q2.Write a menu driven program to perform the following operation on m*n Matrix [25 Marks]
1.Calculate sum of upper triangular matrix elements
2.Calculate sum of diagonal elements

#include <stdio.h>

int main() {
    int m, n, i, j, choice;
    int matrix[10][10];
    int sum;

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

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

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

    choice = 0;
    while(choice != 3) {
        printf("\nMenu:\n");
        printf("1. Sum of upper triangular elements\n");
        printf("2. Sum of diagonal elements\n");
        printf("3. Exit\n");
        printf("Enter your choice: ");
        scanf("%d", &choice);

        switch(choice) {
            case 1:
                sum = 0;
                if(m == n) {
                    for(i = 0; i < m; i++) {
                        for(j = i; j < n; j++) {
                            sum += matrix[i][j];
                        }
                    }
                    printf("Sum of upper triangular elements = %d\n", sum);
                } else {
                    printf("Matrix must be square.\n");
                }
                break;

            case 2:
                sum = 0;
                if(m == n) {
                    for(i = 0; i < m; i++) {
                        sum += matrix[i][i];
                        if(i != n - i - 1) {
                            sum += matrix[i][n - i - 1];
                        }
                    }
                    printf("Sum of diagonal elements = %d\n", sum);
                } else {
                    printf("Matrix must be square.\n");
                }
                break;

            case 3:
                printf("Exiting...\n");
                break;

            default:
                printf("Invalid choice.\n");
        }
    }

    return 0;
}


Slip9
Q1.Write a C program to accept a character from the user and check whether the character is a vowel or consonant. [15 Marks]

#include <stdio.h>

int main() {
    char ch;

    printf("Enter a character: ");
    scanf("%c", &ch);

    if((ch >= 'A' && ch <= 'Z') || (ch >= 'a' && ch <= 'z')) {
        if(ch == 'A' || ch == 'E' || ch == 'I' || ch == 'O' || ch == 'U' ||
           ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u') {
            printf("It is a vowel.\n");
        } else {
            printf("It is a consonant.\n");
        }
    } else {
        printf("It is not an alphabet.\n");
    }

    return 0;
}

Q2.Write a program to accept two numbers as range and display multiplication table of all numbers within that range. [25 Marks]

#include <stdio.h>

int main() {
    int start, end, i, j;

    printf("Enter the starting number: ");
    scanf("%d", &start);

    printf("Enter the ending number: ");
    scanf("%d", &end);

    i = start;
    while(i <= end) {
        printf("Multiplication table of %d:\n", i);
        j = 1;
        while(j <= 10) {
            printf("%d x %d = %d\n", i, j, i * j);
            j++;
        }
        printf("\n");
        i++;
    }

    return 0;
}


Slip10
Q1.Write a C program to accept the x and y coordinate of a point and find the quadrant in which the point lies. [15 Marks]

#include <stdio.h>

int main() {
    int x, y;

    printf("Enter x coordinate: ");
    scanf("%d", &x);

    printf("Enter y coordinate: ");
    scanf("%d", &y);

    if(x > 0 && y > 0) {
        printf("Point lies in Quadrant 1\n");
    } else if(x < 0 && y > 0) {
        printf("Point lies in Quadrant 2\n");
    } else if(x < 0 && y < 0) {
        printf("Point lies in Quadrant 3\n");
    } else if(x > 0 && y < 0) {
        printf("Point lies in Quadrant 4\n");
    } else if(x == 0 && y == 0) {
        printf("Point lies at the origin\n");
    } else if(x == 0) {
        printf("Point lies on Y-axis\n");
    } else if(y == 0) {
        printf("Point lies on X-axis\n");
    }

    return 0;
}

Q2.Write a program, which accepts a number n and displays each digit in words.
Example:6702 Output=Six-Seven-Zero-Two [25 Marks]

#include <stdio.h>

int main() {
    int n, digit, rev = 0;

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

    while(n > 0) {
        rev = rev * 10 + n % 10;
        n = n / 10;
    }

    while(rev > 0) {
        digit = rev % 10;

        switch(digit) {
            case 0: printf("Zero"); break;
            case 1: printf("One"); break;
            case 2: printf("Two"); break;
            case 3: printf("Three"); break;
            case 4: printf("Four"); break;
            case 5: printf("Five"); break;
            case 6: printf("Six"); break;
            case 7: printf("Seven"); break;
            case 8: printf("Eight"); break;
            case 9: printf("Nine"); break;
        }

        rev = rev / 10;

        if(rev > 0) {
            printf("-");
        }
    }

    return 0;
}


Slip11
Q1.Write a C program to accept the cost price and selling price from the user. Find out if the seller has made a profit or loss and display how much profit or loss has been made. [15 Marks]

#include <stdio.h>

int main() {
    float cost, sell, diff;

    printf("Enter cost price: ");
    scanf("%f", &cost);

    printf("Enter selling price: ");
    scanf("%f", &sell);

    if(sell > cost) {
        diff = sell - cost;
        printf("Profit = %.2f\n", diff);
    } else if(cost > sell) {
        diff = cost - sell;
        printf("Loss = %.2f\n", diff);
    } else {
        printf("No profit, no loss\n");
    }

    return 0;
}

Q2.Accept radius from the user and write a program having menu with the following options and corresponding actions [25 Marks]
Options Actions
1.Area of Circle Compute area of circle and print
2.Circumference of Circle Compute Circumference of circle and print
3.Volume of Sphere Compute Volume of Sphere and print

#include <stdio.h>

int main() {
    int choice;
    float radius, area, circumference, volume;
    float pi = 3.14;

    printf("Enter radius: ");
    scanf("%f", &radius);

    while(1) {
        printf("\nMenu:\n");
        printf("1. Area of Circle\n");
        printf("2. Circumference of Circle\n");
        printf("3. Volume of Sphere\n");
        printf("4. Exit\n");
        printf("Enter your choice: ");
        scanf("%d", &choice);

        switch(choice) {
            case 1:
                area = pi * radius * radius;
                printf("Area of Circle = %.2f\n", area);
                break;
            case 2:
                circumference = 2 * pi * radius;
                printf("Circumference of Circle = %.2f\n", circumference);
                break;
            case 3:
                volume = (4.0/3.0) * pi * radius * radius * radius;
                printf("Volume of Sphere = %.2f\n", volume);
                break;
            case 4:
                return 0;
            default:
                printf("Invalid choice\n");
        }
    }

    return 0;
}


Slip12
Q1.Write a C program to calculate sum of digits of a given input number.[15 Marks]

#include <stdio.h>

int main() {
    int num, digit, sum = 0;

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

    while(num != 0) {
        digit = num % 10;
        sum = sum + digit;
        num = num / 10;
    }

    printf("Sum of digits = %d\n", sum);

    return 0;
}

Q2.Accept two numbers from user and write a menu driven program to perform the following operations [25 Marks]
1.swap the values of two variables
2.calculate arithmetic mean and harmonic mean of two numbers

#include <stdio.h>

int main() {
    int a, b, choice;
    float am, hm;

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

    while (1) {
        printf("\nMenu:\n");
        printf("1. Swap the values\n");
        printf("2. Calculate Arithmetic and Harmonic Mean\n");
        printf("3. Exit\n");
        printf("Enter your choice: ");
        scanf("%d", &choice);

        switch (choice) {
            case 1:
                {
                    int temp = a;
                    a = b;
                    b = temp;
                    printf("After swapping: a = %d, b = %d\n", a, b);
                }
                break;

            case 2:
                am = (a + b) / 2.0;
                hm = (2.0 * a * b) / (a + b);
                printf("Arithmetic Mean = %.2f\n", am);
                printf("Harmonic Mean = %.2f\n", hm);
                break;

            case 3:
                return 0;

            default:
                printf("Invalid choice. Try again.\n");
        }
    }

    return 0;
}


Slip13
Q1.Write a C program to accept the value of n and display sum of all odd numbers up to n. [15 Marks]

#include <stdio.h>

int main() {
    int n, i = 1, sum = 0;

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

    while (i <= n) {
        if (i % 2 != 0) {
            sum = sum + i;
        }
        i++;
    }

    printf("Sum of all odd numbers up to %d is %d\n", n, sum);

    return 0;
}

Q2.Write a program to accept a decimal number and convert it to binary, octal and hexadecimal number. [25 Marks]

#include <stdio.h>

int main() {
    int num, rem, i;
    int binary[32];
    printf("Enter a decimal number: ");
    scanf("%d", &num);
    printf("Octal: %o\n", num);
    printf("Hexadecimal: %X\n", num);
    i = 0;
    int temp = num;
    while (temp > 0) {
        binary[i] = temp % 2;
        temp = temp / 2;
        i++;
    }
    printf("Binary: ");
    while (i > 0) {
        i--;
        printf("%d", binary[i]);
    }
    printf("\n");

    return 0;
}


Slip14
Q1.Write a C program to check whether a input number is Armstrong number or not. [15 Marks]

#include <stdio.h>

int main() {
    int num, a, s = 0;
    printf("Enter Number: ");
    scanf("%d", &num);

    int temp = num;

    while (num > 0) {
        a = num % 10;
        s = s + (a * a * a);
        num = num / 10;
    }

    if (s == temp) {
        printf("Number is Armstrong\n");
    } else {
        printf("Number is Not Armstrong\n");
    }

    return 0;
}

Q2.Write a program to accept a number and count number of even, odd and zero digits within that number. [25 Marks]

#include <stdio.h>

int main() {
    int num, digit;
    int even = 0, odd = 0, zero = 0;
    printf("Enter a number: ");
    scanf("%d", &num);
    if (num == 0) {
        zero = 1;
    }
    while (num > 0) {
        digit = num % 10;
        if (digit == 0) {
            zero++;
        } else if (digit % 2 == 0) {
            even++;
        } else {
            odd++;
        }
        num = num / 10;
    }
    printf("Even digits: %d\n", even);
    printf("Odd digits: %d\n", odd);
    printf("Zero digits: %d\n", zero);

    return 0;
}


Slip15
Q1.Write a C program to check whether a input number is perfect number or not. [15 Marks]

#include <stdio.h>

int main() {
    int num, i, sum = 0;
    printf("Enter a number: ");
    scanf("%d", &num);
    i = 1;
    while (i < num) {
        if (num % i == 0) {
            sum = sum + i;
        }
        i++;
    }
    if (sum == num) {
        printf("The number is a Perfect Number\n");
    } else {
        printf("The number is Not a Perfect Number\n");
    }
    return 0;
}

Q2.Write a program having a menu with the following options and corresponding actions [25 Marks]
Options Actions
1.Area of square Accept length, Compute area of square and print
2.Area of Rectangle Accept length and breadth, Compute area of rectangle and print
3.Area of triangle Accept base and height, Compute area of triangle and print

#include <stdio.h>

int main() {
    int choice;
    float length, breadth, base, height, area;
    while (1) {
        printf("\nMenu:\n");
        printf("1. Area of Square\n");
        printf("2. Area of Rectangle\n");
        printf("3. Area of Triangle\n");
        printf("4. Exit\n");
        printf("Enter your choice: ");
        scanf("%d", &choice);
        switch (choice) {
            case 1:
                printf("Enter length of square: ");
                scanf("%f", &length);
                area = length * length;
                printf("Area of square: %.2f\n", area);
                break;
            case 2:
                printf("Enter length and breadth of rectangle: ");
                scanf("%f%f", &length, &breadth);
                area = length * breadth;
                printf("Area of rectangle: %.2f\n", area);
                break;
            case 3:
                printf("Enter base and height of triangle: ");
                scanf("%f%f", &base, &height);
                area = 0.5 * base * height;
                printf("Area of triangle: %.2f\n", area);
                break;
            case 4:
                return 0;
            default:
                printf("Invalid choice\n");
        }
    }

    return 0;
}


Slip16
Q1.Write a C program to calculate xy without using standard library function. [15 Marks]

#include <stdio.h>

int main() {
    int x, y, i, result = 1;
    printf("Enter base (x): ");
    scanf("%d", &x);
    printf("Enter exponent (y): ");
    scanf("%d", &y);
    for(i = 1; i <= y; i++) {
        result = result * x;
    }
    printf("Result: %d\n", result);

    return 0;
}

Q2.Write a program to display union and intersection of two 1D array. [25 Marks]

#include <stdio.h>

int main() {
    int a[100], b[100], unionArr[200], interArr[100];
    int n1, n2, i, j, k = 0, m = 0;
    printf("Enter number of elements in first array: ");
    scanf("%d", &n1);
    printf("Enter elements of first array:\n");
    for(i = 0; i < n1; i++) {
        scanf("%d", &a[i]);
        unionArr[k++] = a[i];
    }
    printf("Enter number of elements in second array: ");
    scanf("%d", &n2);
    printf("Enter elements of second array:\n");
    for(i = 0; i < n2; i++) {
        scanf("%d", &b[i]);
        int found = 0;
        for(j = 0; j < n1; j++) {
            if(b[i] == a[j]) {
                interArr[m++] = b[i];
                found = 1;
                break;
            }
        }
        if(!found) {
            unionArr[k++] = b[i];
        }
    }
    printf("Union of two arrays: ");
    for(i = 0; i < k; i++) {
        printf("%d ", unionArr[i]);
    }
    printf("\nIntersection of two arrays: ");
    for(i = 0; i < m; i++) {
        printf("%d ", interArr[i]);
    }

    return 0;
}


Slip17
Q1.Write a C program to display multiplication table of a given input number [15 Marks]

#include <stdio.h>

int main() {
    int num, i = 1;
    printf("Enter a number: ");
    scanf("%d", &num);
    while(i <= 10) {
        printf("%d x %d = %d\n", num, i, num * i);
        i++;
    }

    return 0;
}

Q2.Write a menu driven program to perform the following operation on m*n Matrix [25 Marks]
1.Display transpose of a matrix
2.Calculate sum of all odd elements of matrix

#include <stdio.h>

int main() {
    int m, n, i, j, choice;
    int matrix[10][10];
    printf("Enter rows and columns: ");
    scanf("%d %d", &m, &n);
    printf("Enter elements of matrix:\n");
    for(i = 0; i < m; i++) {
        for(j = 0; j < n; j++) {
            scanf("%d", &matrix[i][j]);
        }
    }
    while(1) {
        printf("\nMenu:\n");
        printf("1. Display Transpose of Matrix\n");
        printf("2. Sum of All Odd Elements\n");
        printf("3. Exit\n");
        printf("Enter your choice: ");
        scanf("%d", &choice);
        switch(choice) {
            case 1:
                printf("Transpose of Matrix:\n");
                for(j = 0; j < n; j++) {
                    for(i = 0; i < m; i++) {
                        printf("%d ", matrix[i][j]);
                    }
                    printf("\n");
                }
                break;
            case 2:
                {
                    int sum = 0;
                    for(i = 0; i < m; i++) {
                        for(j = 0; j < n; j++) {
                            if(matrix[i][j] % 2 != 0) {
                                sum += matrix[i][j];
                            }
                        }
                    }
                    printf("Sum of all odd elements: %d\n", sum);
                }
                break;
            case 3:
                return 0;
            default:
                printf("Invalid choice.\n");
        }
    }
    return 0;
}


Slip18
Q1.Write a C program to generate following triangle up to n lines. [15 Marks]
1
1 2
1 2 3

#include <stdio.h>

int main() {
    int i, j;
int n=3;
    for(i = 1; i <= n; i++) {
        for(j = 1; j <= i; j++) {
            printf("%d ", j);
        }
        printf("\n");
    }

    return 0;
}

Q2.Write a program to calculate sum of following series up to n terms. [25 Marks]
Sum=X-X2/2!+X3/3!-…… (Note: Write separate user defined function to calculate power and factorial)

#include <stdio.h>

int power(int x, int y) {
    int i, result = 1;
    for(i = 1; i <= y; i++) {
        result = result * x;
    }
    return result;
}

int factorial(int n) {
    int i, fact = 1;
    for(i = 1; i <= n; i++) {
        fact = fact * i;
    }
    return fact;
}

int main() {
    int x, n, i;
    float sum = 0;
    printf("Enter value of X: ");
    scanf("%d", &x);
    printf("Enter number of terms: ");
    scanf("%d", &n);
    for(i = 1; i <= n; i++) {
        float term = (float)power(x, i) / factorial(i);
        if(i % 2 == 0) {
            sum = sum - term;
        } else {
            sum = sum + term;
        }
    }
    printf("Sum of the series is: %.2f", sum);
   
    return 0;
}


Slip19
Q1.Write a C program to generate following triangle up to n lines. [15 Marks]

****

***

**

*

#include <stdio.h>

int main() {
    int i, j;
    int n = 4;
    for(i = n; i >= 1; i--) {
        for(j = 1; j <= i; j++) {
            printf("*");
        }
        printf("\n");
    }

    return 0;
}

Q2.Write a menu driven program to perform the following operation on m*n Matrix [25 Marks]
1.Find sum of diagonal elements of matrix
2.Find sum of all even numbers of matrix

#include <stdio.h>

int main() {
    int m, n, i, j, choice;
    int matrix[10][10];
    printf("Enter rows and columns of matrix: ");
    scanf("%d %d", &m, &n);
    printf("Enter elements of matrix:\n");
    for(i = 0; i < m; i++) {
        for(j = 0; j < n; j++) {
            scanf("%d", &matrix[i][j]);
        }
    }
    while(1) {
        printf("\nMenu:\n");
        printf("1. Sum of diagonal elements\n");
        printf("2. Sum of even elements\n");
        printf("3. Exit\n");
        printf("Enter your choice: ");
        scanf("%d", &choice);
        switch(choice) {
            case 1: {
                int sum = 0;
                for(i = 0; i < m && i < n; i++) {
                    sum += matrix[i][i];
                }
                printf("Sum of diagonal elements = %d\n", sum);
                break;
            }
            case 2: {
                int sum = 0;
                for(i = 0; i < m; i++) {
                    for(j = 0; j < n; j++) {
                        if(matrix[i][j] % 2 == 0) {
                            sum += matrix[i][j];
                        }
                    }
                }
                printf("Sum of even elements = %d\n", sum);
                break;
            }
            case 3:
                return 0;
            default:
                printf("Invalid choice\n");
        }
    }
    return 0;
}


Slip20
Q1.Write a C program to generate following triangle up to n lines. [15 Marks]
1
2 3
4 5 6

#include <stdio.h>

int main() {
    int i, j, num = 1;
    int n=3;
    for(i = 1; i <= n; i++) {
        for(j = 1; j <= i; j++) {
            printf("%d ", num);
            num++;
        }
        printf("\n");
    }

    return 0;
}

Q2.Write a program to calculate addition of two matrices [25 Marks]

#include <stdio.h>

int main() {
    int m, n, i, j;
    printf("Enter number of rows and columns: ");
    scanf("%d %d", &m, &n);
    int a[m][n], b[m][n], sum[m][n];
    printf("Enter elements of first matrix:\n");
    for(i = 0; i < m; i++) {
        for(j = 0; j < n; j++) {
            scanf("%d", &a[i][j]);
        }
    }
    printf("Enter elements of second matrix:\n");
    for(i = 0; i < m; i++) {
        for(j = 0; j < n; j++) {
            scanf("%d", &b[i][j]);
        }
    }
    printf("Sum of the matrices:\n");
    for(i = 0; i < m; i++) {
        for(j = 0; j < n; j++) {
            sum[i][j] = a[i][j] + b[i][j];
            printf("%d ", sum[i][j]);
        }
        printf("\n");
    }

    return 0;
}


Slip21
Q1.Write a C program to generate following triangle up to n lines. [15 Marks]
A
A B
A B C

#include <stdio.h>

int main() {
    int n = 3, i, j;
    for(i = 1; i <= n; i++) {
        for(j = 1; j <= i; j++) {
            printf("%c ", 'A' + j - 1);
        }
        printf("\n");
    }

    return 0;
}

Q2.Create a structure employee (eno,ename,salary). Accept details of n employees and write a menu driven program to perform the following operations options. [25 Marks]
1.Display all employees having salary > 5000
2.Display all employees

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

struct employee {
    int eno;
    char ename[20];
    float salary;
};

int main() {
    struct employee emp[100];
    int n, i, choice;
    printf("Enter number of employees: ");
    scanf("%d", &n);
    for(i = 0; i < n; i++) {
        printf("\nEnter employee %d details:\n", i+1);
        printf("Enter ID: ");
        scanf("%d", &emp[i].eno);
        printf("Enter Name: ");
        scanf("%s", emp[i].ename);
        printf("Enter Salary: ");
        scanf("%f", &emp[i].salary);
    }
    while(1) {
        printf("\n--- MENU ---\n");
        printf("1. Display employees with salary > 5000\n");
        printf("2. Display all employees\n");
        printf("3. Exit\n");
        printf("Enter your choice: ");
        scanf("%d", &choice);
        switch(choice) {
            case 1:
                printf("\nEmployees with salary > 5000:\n");
                for(i = 0; i < n; i++) {
                    if(emp[i].salary > 5000) {
                        printf("%d %s %.2f\n", emp[i].eno, emp[i].ename, emp[i].salary);
                    }
                }
                break;
            case 2:
                printf("\nAll employee details:\n");
                for(i = 0; i < n; i++) {
                    printf("%d %s %.2f\n", emp[i].eno, emp[i].ename, emp[i].salary);
                }
                break;
            case 3:
                return 0;
            default:
                printf("Invalid choice\n");
        }
    }
    return 0;
}


Slip22
Q1.Write a C program to generate following triangle up to n lines. [15 Marks]
A B C
A B
A

#include <stdio.h>

int main() {
    int n = 3, i, j;
    for(i = n; i >= 1; i--) {
        for(j = 0; j < i; j++) {
            printf("%c ", 'A' + j);
        }
        printf("\n");
    }

    return 0;
}


Q2.Write a menu driven program to perform the following operation on m*n Matrix [25 Marks]
1.Find sum of non diagonal elements of matrix
2.Find sum of all odd numbers of matrix

#include <stdio.h>

int main() {
    int m, n, i, j, choice, sum = 0;
    printf("Enter number of rows (m): ");
    scanf("%d", &m);
    printf("Enter number of columns (n): ");
    scanf("%d", &n);  
    int matrix[m][n];
    printf("Enter elements of the matrix:\n");
    for(i = 0; i < m; i++) {
        for(j = 0; j < n; j++) {
            scanf("%d", &matrix[i][j]);
        }
    }
    while(1) {
        printf("\nMenu:\n");
        printf("1. Find sum of non-diagonal elements\n");
        printf("2. Find sum of all odd numbers in matrix\n");
        printf("3. Exit\n");
        printf("Enter your choice: ");
        scanf("%d", &choice);
        sum = 0;
        switch(choice) {
            case 1:
                for(i = 0; i < m; i++) {
                    for(j = 0; j < n; j++) {
                        if(i != j) {  // Exclude diagonal elements
                            sum += matrix[i][j];
                        }
                    }
                }
                printf("Sum of non-diagonal elements: %d\n", sum);
                break;
            case 2:
                for(i = 0; i < m; i++) {
                    for(j = 0; j < n; j++) {
                        if(matrix[i][j] % 2 != 0) {  // Check if odd
                            sum += matrix[i][j];
                        }
                    }
                }
                printf("Sum of all odd numbers: %d\n", sum);
                break;
            case 3:
                return 0;
            default:
                printf("Invalid choice! Try again.\n");
        }
    }
    return 0;
}


Slip23
Q1.Write a C program to accept n elements of 1D array and then display sum of all elements of array. [15 Marks]

#include <stdio.h>

int main() {
    int n, i, sum = 0;
    printf("Enter the number of elements: ");
    scanf("%d", &n);
    int arr[n];
    printf("Enter %d elements:\n", n);
    for(i = 0; i < n; i++) {
        scanf("%d", &arr[i]);
        sum += arr[i];
    }
    printf("Sum of all elements: %d\n", sum);
   
    return 0;
}

Q2.Accept n integers in an array. Copy only the non-zero elements to another array (allocated using dynamic memory allocation). Calculate the sum and average of non-zero elements. [25 Marks]

#include <stdio.h>
#include <stdlib.h>

int main() {
    int n, i, count = 0, sum = 0;
    float avg;
    printf("Enter the number of elements: ");
    scanf("%d", &n);
    int arr[n];
    printf("Enter %d elements:\n", n);
    for(i = 0; i < n; i++) {
        scanf("%d", &arr[i]);
        if(arr[i] != 0) {
            count++;
        }
    }
    if(count == 0) {
        printf("No non-zero elements found!\n");
        return 0;
    }
    int *nonZeroArr = (int*)malloc(count * sizeof(int));
    int j = 0;
    for(i = 0; i < n; i++) {
        if(arr[i] != 0) {
            nonZeroArr[j++] = arr[i];
            sum += arr[i];
        }
    }
    avg = (float)sum / count;
    printf("Sum of non-zero elements: %d\n", sum);
    printf("Average of non-zero elements: %.2f\n", avg);
    free(nonZeroArr);
    return 0;
}


Slip24
Q1.Write a C program to find maximum elements of 1D array [15 Marks]

#include <stdio.h>

int main() {
    int n, i, max;  
    printf("Enter the number of elements: ");
    scanf("%d", &n);  
    int arr[n];
    printf("Enter %d elements:\n", n);
    for(i = 0; i < n; i++) {
        scanf("%d", &arr[i]);
    }
    max = arr[0];
    for(i = 1; i < n; i++) {
        if(arr[i] > max) {
            max = arr[i];
        }
    }
    printf("Maximum element: %d\n", max);
    return 0;
}

Q2.Create a structure Book (Bno,Bname,Price). Accept details of n Books and write a menu driven program to perform the following operations options. [25 Marks]
1.Display all Books having price > 500
2.Display Book having maximum price

#include <stdio.h>

struct Book {
    int Bno;
    char Bname[50];
    float Price;
};
int main() {
    int n, i, choice;  
    printf("Enter number of books: ");
    scanf("%d", &n);
    struct Book books[n];
    for(i = 0; i < n; i++) {
        printf("\nEnter Book No: ");
        scanf("%d", &books[i].Bno);
        printf("Enter Book Name: ");
        scanf(" %[^\n]", books[i].Bname);
        printf("Enter Price: ");
        scanf("%f", &books[i].Price);
    }
    while(1) {
        printf("\nMenu:\n");
        printf("1. Display Books with Price > 500\n");
        printf("2. Display Book with Maximum Price\n");
        printf("3. Exit\n");
        printf("Enter choice: ");
        scanf("%d", &choice);
        switch(choice) {
            case 1:
                printf("\nBooks with Price > 500:\n");
                for(i = 0; i < n; i++) {
                    if(books[i].Price > 500) {
 printf("Book No: %d, Name: %s, Price: %.2f\n",
books[i].Bno, books[i].Bname, books[i].Price);
                    }
                }
                break;
            case 2:
                {
                    int maxIndex = 0;
                    for(i = 1; i < n; i++) {
                        if(books[i].Price > books[maxIndex].Price) {
                            maxIndex = i;
                        }
                    }
                    printf("\nBook with Maximum Price:\n");
printf("Book No: %d, Name: %s, Price: %.2f\n",
books[maxIndex].Bno, books[maxIndex].Bname, books[maxIndex].Price);
                }
                break;
            case 3:
                return 0;

            default:
                printf("Invalid choice! Try again.\n");
        }
    }
    return 0;
}


Slip25
Q1.Write a C program to calculate sum of all even elements of a matrix. [15 Marks]

#include <stdio.h>

int main() {
    int m, n, i, j, sum = 0;
   
    printf("Enter number of rows: ");
    scanf("%d", &m);
    printf("Enter number of columns: ");
    scanf("%d", &n);

    int matrix[m][n];

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

    printf("Sum of all even elements in matrix = %d\n", sum);
    return 0;
}

Q2.Write a menu driven program for the following option [25 Marks]
1.Check input number is Armstrong or not
2.Check input number is Perfect or not

#include <stdio.h>

int isArmstrong(int num) {
    int temp = num, sum = 0, digit;
    while (temp > 0) {
        digit = temp % 10;
        sum += digit * digit * digit;
        temp /= 10;
    }
    return sum == num;
}

int isPerfect(int num) {
    int sum = 0;
    for(int i = 1; i < num; i++) {
        if(num % i == 0) {
            sum += i;
        }
    }
    return sum == num;
}

int main() {
    int choice, num;
   
    while(1) {
        printf("\nMenu:\n");
        printf("1. Check Armstrong Number\n");
        printf("2. Check Perfect Number\n");
        printf("3. Exit\n");
        printf("Enter your choice: ");
        scanf("%d", &choice);

        switch(choice) {
            case 1:
                printf("Enter a number: ");
                scanf("%d", &num);
                if(isArmstrong(num))
                    printf("%d is an Armstrong number\n", num);
                else
                    printf("%d is not an Armstrong number\n", num);
                break;

            case 2:
                printf("Enter a number: ");
                scanf("%d", &num);
                if(isPerfect(num))
                    printf("%d is a Perfect number\n", num);
                else
                    printf("%d is not a Perfect number\n", num);
                break;

            case 3:
                return 0;

            default:
                printf("Invalid choice! Try again.\n");
        }
    }
}


Slip26
Q1.Write a C program to calculate length of string without using standard functions. [15 Marks]

#include <stdio.h>

int main() {
    char str[100];
    int i = 0;
    printf("Enter a string: ");
    scanf("%s", str);
    while (str[i] != '\0') {
        i++;
    }
    printf("Length of the string = %d\n", i);
    return 0;
}

Q2.Write a program to display the elements of an array containing n integers in the Reverse order using a pointer to the array. [25 Marks]

#include <stdio.h>

int main() {
    int n, i;
   
    printf("Enter number of elements: ");
    scanf("%d", &n);
   
    int arr[n];
    int *ptr = arr;

    printf("Enter %d elements:\n", n);
    for (i = 0; i < n; i++) {
        scanf("%d", ptr + i);
    }

    printf("Array in reverse order:\n");
    for (i = n - 1; i >= 0; i--) {
        printf("%d ", *(ptr + i));
    }
   
    return 0;
}


Slip27
Q1.Write a program to count the occurrences of vowel from a input string. [15 Marks]

#include <stdio.h>

int main() {
    char str[100];
    int i, count = 0;
    printf("Enter a string: ");
    scanf("%s", str);
    for (i = 0; str[i] != '\0'; i++) {
        char ch = str[i];
        if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u' ||
            ch == 'A' || ch == 'E' || ch == 'I' || ch == 'O' || ch == 'U') {
            count++;
        }
    }
    printf("Total vowels: %d\n", count);
    return 0;
}

Q2.Create a structure Item (Ino,Iname,Price). Accept details of n Items and write a menu driven program to perform the following operations options. [25 Marks]
1.Display all Items having price > 800
2.Display Item record with Ino=2

#include <stdio.h>

struct Item {
    int Ino;
    char Iname[50];
    float Price;
};

int main() {
    int n, i, choice;
   
    printf("Enter number of items: ");
    scanf("%d", &n);

    struct Item items[n];

    for (i = 0; i < n; i++) {
        printf("Enter Ino, Iname, and Price for item %d: ", i + 1);
        scanf("%d %s %f", &items[i].Ino, items[i].Iname, &items[i].Price);
    }

    while (1) {
        printf("\nMenu:\n1. Display items with price > 800\n2. Display item with Ino = 2\n3. Exit\nEnter choice: ");
        scanf("%d", &choice);

        switch (choice) {
            case 1:
                printf("\nItems with price > 800:\n");
                for (i = 0; i < n; i++) {
                    if (items[i].Price > 800) {
                        printf("Ino: %d, Iname: %s, Price: %.2f\n", items[i].Ino, items[i].Iname, items[i].Price);
                    }
                }
                break;
            case 2:
                printf("\nItem with Ino = 2:\n");
                for (i = 0; i < n; i++) {
                    if (items[i].Ino == 2) {
                        printf("Ino: %d, Iname: %s, Price: %.2f\n", items[i].Ino, items[i].Iname, items[i].Price);
                    }
                }
                break;
            case 3:
                return 0;
            default:
                printf("Invalid choice!\n");
        }
    }
}


Slip28
Q1.Write a program to accept a string and then count the occurrences of a specific character of a string. [15 Marks]

#include <stdio.h>

int main() {
    char str[100], ch;
    int i, count = 0;

    printf("Enter a string: ");
    scanf("%s", str);  // Accepting string (single word)

    printf("Enter the character to count: ");
    scanf(" %c", &ch); // Space before %c to avoid buffer issues

    for (i = 0; str[i] != '\0'; i++) {
        if (str[i] == ch) {
            count++;
        }
    }

    printf("Character '%c' appears %d times in the string.\n", ch, count);
    return 0;
}

Q2.Write a program to accept two numbers as range and display multiplication table of all numbers within that range. [25 Marks]

#include <stdio.h>

int main() {
    int start, end, i, j;

    printf("Enter the starting number: ");
    scanf("%d", &start);
    printf("Enter the ending number: ");
    scanf("%d", &end);

    if (start > end) {
        printf("Invalid range! Start should be less than or equal to end.\n");
        return 0;
    }

    for (i = start; i <= end; i++) {
        printf("\nMultiplication Table of %d:\n", i);
        for (j = 1; j <= 10; j++) {
            printf("%d x %d = %d\n", i, j, i * j);
        }
    }

    return 0;
}


Slip29
Q1.Write a C program to calculate factorial of a number using user defined function. [15 Marks]

#include <stdio.h>

long factorial(int n) {
    long fact = 1;
    for (int i = 1; i <= n; i++) {
        fact *= i;
    }
    return fact;
}

int main() {
    int num;
    printf("Enter a number: ");
    scanf("%d", &num);

    if (num < 0) {
        printf("Factorial is not defined for negative numbers.\n");
    } else {
        printf("Factorial of %d is %ld\n", num, factorial(num));
    }
    return 0;
}

Q2.Write a program, which accepts a number n and displays each digit separated by tabs.
Example:6702 Output=6 7 0 2 [25 Marks]

#include <stdio.h>

int main() {
    int num, rev = 0, digit;

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

    while (num > 0) {
        rev = rev * 10 + (num % 10);
        num /= 10;
    }

    while (rev > 0) {
        digit = rev % 10;
        printf("%d\t", digit);
        rev /= 10;
    }

    printf("\n");
    return 0;
}


Slip30
Q1.Write a program to find sum of digits of a given input number using user defined Function [15 Marks]

#include <stdio.h>

int sumOfDigits(int num) {
    int sum = 0;
    while (num > 0) {
        sum += num % 10;
        num /= 10;
    }
    return sum;
}

int main() {
    int num;
    printf("Enter a number: ");
    scanf("%d", &num);
    printf("Sum of digits: %d\n", sumOfDigits(num));
    return 0;
}

Q2.Write a program to accept a number and count number of even, odd and zero digits within that number. [25 Marks]

#include <stdio.h>

int main() {
    int num, digit, even = 0, odd = 0, zero = 0;

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

    while (num > 0) {
        digit = num % 10;
        if (digit == 0)
            zero++;
        else if (digit % 2 == 0)
            even++;
        else
            odd++;
        num /= 10;
    }

    printf("Even digits: %d\n", even);
    printf("Odd digits: %d\n", odd);
    printf("Zero digits: %d\n", zero);

    return 0;
}


No comments: