Class 11: C Programming Lab Solutions (Array and String Programs)

Array


42. WAP to read salary of n employees and count salary greater than 60,000.

#include <stdio.h>
int main() {
    int n, i, count = 0;
    float salary[100];

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

    for(i = 0; i < n; i++) {
        printf("Enter salary of employee %d: ", i + 1);
        scanf("%f", &salary[i]);

        if(salary[i] > 60000)
            count++;
    }

    printf("Number of employees having salary greater than 60000 = %d", count);

    return 0;
}

Output:

Enter number of employees: 5
Enter salary of employee 1: 50000
Enter salary of employee 2: 65000
Enter salary of employee 3: 70000
Enter salary of employee 4: 45000
Enter salary of employee 5: 80000
Number of employees having salary greater than 60000 = 3

43. WAP to read age of 20 students and count number of students of age 16.

#include <stdio.h>
int main() {
    int age[20], i, count = 0;

    for(i = 0; i < 20; i++) {
        printf("Enter age of student %d: ", i + 1);
        scanf("%d", &age[i]);

        if(age[i] == 16)
            count++;
    }

    printf("Number of students of age 16 = %d", count);

    return 0;
}

Output:

Enter age of student 1: 16
Enter age of student 2: 15
Enter age of student 3: 16
...
Enter age of student 20: 17
Number of students of age 16 = 2

44. WAP to read 50 numbers and calculate sum and average of it.

#include <stdio.h>
int main() {
    int num[50], i, sum = 0;
    float average;

    for(i = 0; i < 50; i++) {
        printf("Enter number %d: ", i + 1);
        scanf("%d", &num[i]);
        sum = sum + num[i];
    }

    average = sum / 50.0;

    printf("Sum = %d\nAverage = %.2f", sum, average);

    return 0;
}

Output:

Enter number 1: 1
Enter number 2: 2
Enter number 3: 3
...
Enter number 50: 50
Sum = 1275
Average = 25.50

45. WAP to read weight of 40 persons and find the heaviest weight.

#include <stdio.h>
int main() {
    float weight[40], heaviest;
    int i;

    for(i = 0; i < 40; i++) {
        printf("Enter weight of person %d: ", i + 1);
        scanf("%f", &weight[i]);
    }

    heaviest = weight[0];

    for(i = 1; i < 40; i++) {
        if(weight[i] > heaviest)
            heaviest = weight[i];
    }

    printf("Heaviest weight = %.2f", heaviest);

    return 0;
}

Output:

Enter weight of person 1: 55
Enter weight of person 2: 67
Enter weight of person 3: 72
...
Enter weight of person 40: 60
Heaviest weight = 72.00

46. WAP to sort an array in ascending order.

#include <stdio.h>
int main() {
    int a[100], n, i, j, temp;

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

    for(i = 0; i < n; i++) {
        printf("Enter element %d: ", i + 1);
        scanf("%d", &a[i]);
    }

    for(i = 0; i < n - 1; i++) {
        for(j = i + 1; j < n; j++) {
            if(a[i] > a[j]) {
                temp = a[i];
                a[i] = a[j];
                a[j] = temp;
            }
        }
    }

    printf("Array in ascending order: ");
    for(i = 0; i < n; i++)
        printf("%d ", a[i]);

    return 0;
}

Output:

Enter number of elements: 5
Enter element 1: 9
Enter element 2: 3
Enter element 3: 7
Enter element 4: 1
Enter element 5: 5
Array in ascending order: 1 3 5 7 9

47. WAP to sort an array in descending order.

#include <stdio.h>
int main() {
    int a[100], n, i, j, temp;

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

    for(i = 0; i < n; i++) {
        printf("Enter element %d: ", i + 1);
        scanf("%d", &a[i]);
    }

    for(i = 0; i < n - 1; i++) {
        for(j = i + 1; j < n; j++) {
            if(a[i] < a[j]) {
                temp = a[i];
                a[i] = a[j];
                a[j] = temp;
            }
        }
    }

    printf("Array in descending order: ");
    for(i = 0; i < n; i++)
        printf("%d ", a[i]);

    return 0;
}

Output:

Enter number of elements: 5
Enter element 1: 9
Enter element 2: 3
Enter element 3: 7
Enter element 4: 1
Enter element 5: 5
Array in descending order: 9 7 5 3 1

48. WAP to display a matrix of order 3x3.

#include <stdio.h>
int main() {
    int a[3][3], i, j;

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

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

    return 0;
}

Output:

Enter 9 elements of matrix:
1 2 3
4 5 6
7 8 9
Matrix is:
1 2 3
4 5 6
7 8 9

49. WAP to add two matrices of order mxn.

#include <stdio.h>
int main() {
    int a[10][10], b[10][10], c[10][10];
    int m, n, i, j;

    printf("Enter rows and columns: ");
    scanf("%d %d", &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]);
        }
    }

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

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

    return 0;
}

Output:

Enter rows and columns: 2 2
Enter elements of first matrix:
1 2
3 4
Enter elements of second matrix:
5 6
7 8
Sum of matrices:
6 8
10 12

50. WAP to subtract two 3x3 order matrices.

#include <stdio.h>
int main() {
    int a[3][3], b[3][3], c[3][3];
    int i, j;

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

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

    for(i = 0; i < 3; i++) {
        for(j = 0; j < 3; j++) {
            c[i][j] = a[i][j] - b[i][j];
        }
    }

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

    return 0;
}

Output:

Enter elements of first 3x3 matrix:
9 8 7
6 5 4
3 2 1
Enter elements of second 3x3 matrix:
1 1 1
1 1 1
1 1 1
Subtraction of matrices:
8 7 6
5 4 3
2 1 0

51. WAP to find transpose of a matrix.

#include <stdio.h>
int main() {
    int a[10][10], t[10][10];
    int m, n, i, j;

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

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

    for(i = 0; i < m; i++) {
        for(j = 0; j < n; j++) {
            t[j][i] = a[i][j];
        }
    }

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

    return 0;
}

Output:

Enter rows and columns: 2 3
Enter matrix elements:
1 2 3
4 5 6
Transpose of matrix:
1 4
2 5
3 6

52. WAP to check if 2 matrices are equal or not.

#include <stdio.h>
int main() {
    int a[10][10], b[10][10];
    int m, n, i, j, flag = 1;

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

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

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

    for(i = 0; i < m; i++) {
        for(j = 0; j < n; j++) {
            if(a[i][j] != b[i][j]) {
                flag = 0;
                break;
            }
        }
    }

    if(flag == 1)
        printf("Matrices are equal");
    else
        printf("Matrices are not equal");

    return 0;
}

Output:

Enter rows and columns: 2 2
Enter first matrix:
1 2
3 4
Enter second matrix:
1 2
3 4
Matrices are equal

String


53. WAP to count length of a string.

#include <stdio.h>
int main() {
    char str[100];
    int i = 0, length = 0;

    printf("Enter string: ");
    scanf("%s", str);

    while(str[i] != '\0') {
        length++;
        i++;
    }

    printf("Length = %d", length);

    return 0;
}

Output:

Enter string: Nepal
Length = 5

54. WAP to concatenate two strings.

#include <stdio.h>
int main() {
    char str1[100], str2[100];
    int i = 0, j = 0;

    printf("Enter first string: ");
    scanf("%s", str1);

    printf("Enter second string: ");
    scanf("%s", str2);

    while(str1[i] != '\0')
        i++;

    while(str2[j] != '\0') {
        str1[i] = str2[j];
        i++;
        j++;
    }

    str1[i] = '\0';

    printf("Concatenated string = %s", str1);

    return 0;
}

Output:

Enter first string: Ram
Enter second string: Koirala
Concatenated string = RamKoirala

55. WAP to reverse a string.

#include <stdio.h>
int main() {
    char str[100], rev[100];
    int i = 0, j, length = 0;

    printf("Enter string: ");
    scanf("%s", str);

    while(str[i] != '\0') {
        length++;
        i++;
    }

    for(i = length - 1, j = 0; i >= 0; i--, j++) {
        rev[j] = str[i];
    }

    rev[j] = '\0';

    printf("Reversed string = %s", rev);

    return 0;
}

Output:

Enter string: Nepal
Reversed string = lapeN

56. WAP to copy value of one string to another.

#include <stdio.h>
int main() {
    char str1[100], str2[100];
    int i = 0;

    printf("Enter string: ");
    scanf("%s", str1);

    while(str1[i] != '\0') {
        str2[i] = str1[i];
        i++;
    }

    str2[i] = '\0';

    printf("Copied string = %s", str2);

    return 0;
}

Output:

Enter string: Computer
Copied string = Computer

57. WAP to compare if two strings are equal or not.

#include <stdio.h>
int main() {
    char str1[100], str2[100];
    int i = 0, flag = 1;

    printf("Enter first string: ");
    scanf("%s", str1);

    printf("Enter second string: ");
    scanf("%s", str2);

    while(str1[i] != '\0' || str2[i] != '\0') {
        if(str1[i] != str2[i]) {
            flag = 0;
            break;
        }
        i++;
    }

    if(flag == 1)
        printf("Strings are equal");
    else
        printf("Strings are not equal");

    return 0;
}

Output:

Enter first string: Nepal
Enter second string: Nepal
Strings are equal

58. WAP to check if a string is palindrome or not.

#include <stdio.h>
int main() {
    char str[100], rev[100];
    int i = 0, j, length = 0, flag = 1;

    printf("Enter string: ");
    scanf("%s", str);

    while(str[i] != '\0') {
        length++;
        i++;
    }

    for(i = length - 1, j = 0; i >= 0; i--, j++) {
        rev[j] = str[i];
    }

    rev[j] = '\0';

    for(i = 0; str[i] != '\0'; i++) {
        if(str[i] != rev[i]) {
            flag = 0;
            break;
        }
    }

    if(flag == 1)
        printf("Palindrome");
    else
        printf("Not Palindrome");

    return 0;
}

Output:

Enter string: madam
Palindrome

59. WAP to read name of 100 employees and display them.

#include <stdio.h>
int main() {
    char name[100][50];
    int i, n;

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

    for(i = 0; i < n; i++) {
        printf("Enter name of employee %d: ", i + 1);
        scanf("%s", name[i]);
    }

    printf("Employee names are:\n");
    for(i = 0; i < n; i++) {
        printf("%s\n", name[i]);
    }

    return 0;
}

Output:

Enter number of employees: 3
Enter name of employee 1: Ram
Enter name of employee 2: Sita
Enter name of employee 3: Hari
Employee names are:
Ram
Sita
Hari

60. WAP to read string and convert it to uppercase.

#include <stdio.h>
int main() {
    char str[100];
    int i = 0;

    printf("Enter string: ");
    scanf("%s", str);

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

    printf("Uppercase string = %s", str);

    return 0;
}

Output:

Enter string: nepal
Uppercase string = NEPAL

Netra Koirala

Netra Koirala

Computer Science Educator

Passionate computer science educator and author. Provides free study notes, practical guides, and tutorials for Class 9, 10, 11, 12, and B.Sc CSIT students in Nepal. Years of teaching experience in computer science fundamentals.

Computer Science notes, tutorials, MCQs, and educational resources for Nepal students. Covering Class 9, SEE preparation, Class 11, Class 12, SLC, programming, DBMS, networking, HTML, JavaScript, PHP, OOP and more.

Featured Post

Grade 10 Computer Science: Specification Grid & Model Questions

Specification Grid & Model Questions of Computer Science | Grade 10 📚 Examination Resource Specification Grid & M...

Followers