CS Electrical And Electronics
@cselectricalandelectronics
All PostsC_languageProgramming

C Programming Lab Exam Questions With Solutions For Engineers

Hello guys, welcome back to my blog. In this article, I will discuss the C programming lab exam questions with solutions for engineers, C programming viva questions, interview questions on C programming, etc.

If you have any doubts related to electrical, electronics, and computer science, then ask questions. You can also catch me on Instagram – Chetan Shidling. 

Also, read:

C Programming Lab Exam Questions

Note: Change the double inverted comma ( ” ) in the code to avoid an error.

01. Develop a program to solve simple computational problems using arithmetic expressions and the use of each operator leading to the simulation of a commercial calculator. (No built-in math function)?

#include<stdio.h>
int main()
{
    int a,b,choice;
    int result;
    printf("Enter two values\n");
    scanf("%d%d",&a,&b);
    printf("enter your choice\n");
    printf("1.Addition\n 2.Subtraction\n 3.Multiplication\n 4.Division\n 5.Modulus\n");
    scanf("%d",&choice);
    switch(choice)
    {
        case 1: result = a + b;
        break;
        case 2: result = a - b;
        break;
        case 3: result = a * b;
        break;
        case 4: result = a / b;
        break;
        case 5: result = a % b;
        break;
        default:printf("invalid choice");
    }
    printf("The Result = %d",result);
    return 0;
}

Output:

Enter two values
2
3
enter your choice
1.Addition
 2.Subtraction
 3.Multiplication
 4.Division
 5.Modulus
1
The Result = 5

02. Develop a program to compute the roots of a quadratic equation by accepting the coefficients. Print appropriate messages?

#include <stdio.h>
#include <math.h>
int main()
{
    float a, b, c, d, r1, r2;
    printf("Enter non-zero coefficients: ");
    scanf("%f %f %f", &a, &b, &c);
    d = (b * b) - 4 * a * c;
    if (d == 0)
    {
        r1 = r2 = (-b) / (2 * a);
        printf("Root are equal\n");
        printf("Root1 = %f\n and Root2 = %f\n", r1, r2);
    }
    else if (d > 0)
    {
        r1 = (-b + sqrt(d)) / (2 * a);
        r2 = (-b - sqrt(d)) / (2 * a);
        printf("Roots are real and distinct\n");
        printf("Root1 = %f\n", r1);
        printf("Root2 = %f\n", r2);
    }
    else
    {
        r1 = (-b) / (2 * a);
        r2 = sqrt(fabs(d)) / (2 * a);
        printf("Roots are complex\n");
        printf("Root1 = %f+i%f \n", r1, r2);
        printf("Root2 = %f-i%f \n", r1, r2);
    }
}

Output:

Enter non-zero coefficients: 1
2
1
Root are equal
Root1 = -1.000000
and Root2 = -1.000000

03. Develop a program to find the reverse of a positive integer and check for palindrome or not Display appropriate messages?

#include <stdio.h>
int main()
{
    int rev, digit, n, m;
    printf("Enter four digit number :");
    scanf("%d", &n);
    m = n;

    while (n != 0)
    {
        digit = n % 10;
        n = n / 10;
        rev = rev * 10 + digit;
    }
    printf("Reverse number is %d \n", rev);
    if (m == rev)
        printf("Number is a palindrome");
    else
        printf("Not a palindrome");
}

Output:

Enter four digit number :1234
Reverse number is 594321
Not a palindrome

04. An Electricity board charges the following rates for the use of electricity: for the first 200 units 80 paise per unit: for the next 100 units 90 paise per unit: beyond 300 units Rs 1 per unit. All users are changed a minimum of Rs. 100 as meter charge. If the total amount is more than Rs 400, then an additional surcharge of 15% of total amount is charged. Write a program to read the name of the user, number of units consumed and print out the charges.

#include <stdio.h>
#include <stdlib.h>
int main()
{
    char name[10];
    int units;
    const int mincharge = 100;
    const double slab1 = 0.80;
    const double slab2 = 0.90;
    const double slab3 = 1.00;
    const double surcharge = 0.15;
    double billamt = 0.0;
    printf("enter the name of consumer\n");
    scanf("%s", &name);
    printf("enter the number of units consumed\n");
    scanf("%d", &units);
    billamt = billamt + mincharge;
    if (units <= 200)
    {
        billamt = billamt + (units * slab1);
    }
    else if (units > 200 && units <= 300)
    {
        billamt = billamt + ((200 * slab1) + (units - 200) * slab2);
    }
    else
    {
        billamt = billamt + (200 * slab1) + (100 * slab2) + ((units - 300) * slab3);
    }
    if (billamt > 400)
    {
        billamt = billamt + (billamt * surcharge);
    }
    printf("consumer name=%s\n", name);
    printf("units consumed =%d\n", units);
    printf("billamount=%f rupees\n", billamt);
}

Output:

enter the name of consumer
Chetan
enter the number of units consumed
300
consumer name=Chetan
units consumed =300
billamount=350.000000 rupees

05. Introduce 1D Array manipulation and implement Binary search

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
int main()
{
    int n, i, low, mid, high;
    char a[20][20], key[20];
    printf("Enter the number of names\n");
    scanf("%d", &n);
    printf("Enter the names\n");
    for (i = 0; i < n; i++)
    {
        scanf("%s", a[i]);
    }
    printf("Enter the key\n");
    scanf("%s", key);
    low = 0;
    high = n - 1;
    while (low <= high)
    {
        mid = (high + low) / 2;
        if (strcmp(a[mid], key) == 0)
        {
            printf("Name found at %d\n", mid + 1);
            exit(0);
        }
        else if (strcmp(a[mid], key) > 0)
            high = mid - 1;
        else
            low = mid + 1;
    }
    printf("Unsuccessful search \n");
}

Output:

Enter the number of names
3
Enter the names
Chetan
Nitin
Ratan
Enter the key
2
Unsuccessful search

06. Implement using functions to check whether the given number is prime and display appropriate messages(No built-in math function)

#include <stdio.h>
#include <stdlib.h>
int Isprime(int n);
int main()
{
    int n;
    printf("Enter a positive integer\n");
    scanf("%d", &n);
    if (Isprime(n))
        printf("%d is a prime number\n", n);
    else
        printf("%d is not a prime number\n", n);
    return 0;
}
int Isprime(int n)
{
    int i;
    if (n == 1)
    {
        printf("its a unique number\n");
        exit(0);
    }
    for (i = 2; i <= n / 2; i++)
    {
        if (n % i == 0)
        {
            return (0);
        }
    }
    return 1;
}

Output:

Enter a positive integer
6
6 is not a prime number

07. Develop a program to introduce 2D Array manipulation and implement Matrix multiplication and ensure the rules of multiplication are checked

#include <stdio.h>
#include <stdlib.h>
int main()
{
    int m, n, p, q, i, j, k, a[10][10], b[10][10], c[10][10];
    printf("Enter the size of first matrix a\n");
    scanf("%d%d", &m, &n);
    printf("Enter the size of second matrix b\n");
    scanf("%d%d", &p, &q);
    if (n != p)
    {
        printf("Matix multiplication not possible\n");
        exit(0);
    }
    printf("Enter the first matix elements a\n");
    for (i = 0; i < m; i++)
    {
        for (j = 0; j < n; j++)
        {
            scanf("%d", &a[i][j]);
        }
    }
    printf("The Matrix a is \n");
    for (i = 0; i < m; i++)
    {
        for (j = 0; j < n; j++)
        {
            printf("%d\t", a[i][j]);
        }
        printf("\n");
    }
    printf("Enter the second matix elements b\n");
    for (i = 0; i < p; i++)
    {
        for (j = 0; j < q; j++)
        {
            scanf("%d", &b[i][j]);
        }
    }
    printf("The Matrix b is \n");
    for (i = 0; i < p; i++)
    {
        for (j = 0; j < q; j++)
        {
            printf("%d\t", b[i][j]);
        }
        printf("\n");
    }
    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] = c[i][j] + (a[i][k] * b[k][j]);
            }
        }
    }
    printf("Resultant matrix c\n");
    for (i = 0; i < m; i++)
    {
        for (j = 0; j < q; j++)
        {
            printf("%d\t", c[i][j]);
        }
        printf("\n");
    }
}

Output:

Enter the size of first matrix a
3
3
Enter the size of second matrix b
3
3
Enter the first matix elements a
2
3
4
5
1
2
1
4
3
The Matrix a is
2       3       4
5       1       2
1       4       3
Enter the second matix elements b
5
3
6
3
5
3
4
2
1
The Matrix b is
5       3       6
3       5       3
4       2       1
Resultant matrix c
35      29      25
36      24      35
29      29      21

08. Develop a program to compute Sin(x) using Taylor series approximation. Compare your result with the built-in Library function. Print both the result with appropriate messages

#include <stdio.h>
#include <math.h>
#define PI 3.142
int main()
{
    int n, i;
    float deg, x, sum = 0, term = 0;
    printf("Enter number of terms, say n\n");
    scanf("%d", &n);
    printf("Enter the degree\n");
    scanf("%f", &deg);
    x = (deg * PI) / 180;
    printf("In Radians = %f \n", x);
    term = x;
    sum = term;
    for (i = 3; i <= n; i += 2)
    {
        term = (-term * x * x) / (i * (i - 1));
        sum = sum + term;
    }
    printf("sin(%f)=%f\n", deg, sum);
    printf("Inbuilt function Sin(%f) = %f \n", deg, sin(x));
    printf("User function Sin(%f) = %f", deg, sum);
}

Output:

Enter number of terms, say n
4
Enter the degree
34
In Radians = 0.593489
sin(34.000000)=0.558648
Inbuilt function Sin(34.000000) = 0.559257
User function Sin(34.000000) = 0.558648

09. Develop a program to sort the given set of N numbers using Bubble sort

#include <stdio.h>
int main()
{
    int a[20], n, i, temp = 0, j;
    printf("Enter number of array elements\n");
    scanf("%d", &n);
    printf("Enter array elements\n");
    for (i = 0; i < n; i++)
        scanf("%d", &a[i]);
    for (i = 1; i < n; i++)
    {
        for (j = 0; j < n - i; j++)
        {
            if (a[j] > a[j + 1])
            {
                temp = a[j];
                a[j] = a[j + 1];
                a[j + 1] = temp;
            }
        }
    }
    printf("The sorted array is \n");
    for (i = 0; i < n; i++)
        printf("%d\n", a[i]);
}

Output:

Enter number of array elements
5
Enter array elements
44
3
66
5
4
The sorted array is
3
4
5
44
66

10. Develop a program to find the square root of a given number N and execute for all possible Inputs with appropriate messages. Note: Don’t use library function sqrt(n)

#include <stdio.h>
int main()
{
    float sqrt, temp, n;
    printf("Enter a number\n");
    scanf("%f", &n);
    sqrt = n / 2;
    temp = 0;
    while (temp != sqrt)
    {
        temp = sqrt;
        sqrt = (n / sqrt + sqrt) / 2;
    }
    printf("The square root of the number is %f", sqrt);
}

Output:

Enter a number
9
The square root of the number is 3.000000

11. Implement structures to read, write, and compute average marks and the students scoring above and below the average marks for a class of N students

#include <stdio.h>
#include <stdlib.h>
typedef struct
{
    char name[20];
    int usn;
    int marks;
} stud;
int main()
{
    int i, n;
    float avg = 0;
    stud st[50];
    printf("Enter the number of students\n");
    scanf("%d", &n);
    printf("enter the student details\n");
    for (i = 0; i < n; i++)
    {
        printf("\n name:");
        scanf("%s", st[i].name);
        printf("\n usn:");
        scanf("%d", &st[i].usn);
        printf("\n marks:");
        scanf("%d", &st[i].marks);
        avg = avg + st[i].marks;
    }
    avg = avg / n;
    for (i = 0; i < n; i++)
    {
        printf("\n name \t %s", st[i].name);
        printf("\n usn \t %d", st[i].usn);
        printf("\n marks\t:%d\n", st[i].marks);
        if (st[i].marks < avg)
            printf("the student is below the average\n");
        else
            printf("the student is above average\n");
    }
}

Output:

Enter the number of students
2
enter the student details

 name:Chetan

 usn:22

 marks:99

 name:Nikhil

 usn:43

 marks:89

 name    Chetan
 usn     22
 marks  :99
the student is above average

 name    Nikhil
 usn     43
 marks  :89
the student is below the average

12. Develop a program using pointers to compute the sum and standard deviation of all elements stored in an array of n real numbers.

#include <stdio.h>
#include <math.h>
int main()
{
    float a[10], *ptr, mean, sum = 0, var, sumstd = 0, std;
    int n, i;
    printf("Enter the number of elements\n");
    scanf("%d", &n);
    printf("Enter the array elements\n");
    for (i = 0; i < n; i++)
    {
        scanf("%f", &a[i]);
    }
    ptr = a;
    for (i = 0; i < n; i++)
    {
        sum = sum + (*ptr);
        ptr++;
    }
    mean = sum / n;
    ptr = a;
    for (i = 0; i < n; i++)
    {
        sumstd = sumstd + pow((*ptr - mean), 2);
        ptr++;
    }
    var = sumstd / n;
    std = sqrt(var);
    printf("Sum = %f \n", sum);
    printf("Mean = %f \n", mean);
    printf("Variance = %f \n", var);
    printf("Standard Deviation = %f", std);
}

Output:

Enter the number of elements
4
Enter the array elements
34
22
56
12
Sum = 124.000000
Mean = 31.000000
Variance = 269.000000
Standard Deviation = 16.401220

13. Implement Recursive functions for binary to Decimal Conversion

#include <stdio.h>
int binarytodecimal(int binarynum)
{
    if (!(binarynum / 10))
        return binarynum;
    return (binarynum % 10 + binarytodecimal(binarynum / 10) * 2);
}
int main()
{
    int binarynum;
    printf("Enter the binary number \n");
    scanf("%d", &binarynum);
    printf("equivalant decimal number is %d", binarytodecimal(binarynum));
    return 0;
}

Output:

Enter the binary number
10101
equivalant decimal number is 21

14. Write a function to implement string operations such as compare, concatenate, string length. Convince the parameters passing techniques

#include <stdio.h>
#include <string.h>
int main()
{
    int length(char str[100]);
    int compare(char s1[100], char s2[100]);
    void concat(char s1[100], char s2[100]);


        int option, result;
        char str[100], s1[100], s2[100];
        do
        {
            printf("1.String length \n");
            printf("2.string comparision \n");
            printf("3.string concatenation \n");
            printf("4.quit \n");
            printf("enter your choice \n");
            scanf("%d", &option);
            switch (option)
            {
            case 1:
                printf("enter string \n");
                scanf("%s", &str);
                result = length(str);
                printf("the length of string= %d\n", result);
                break;
            case 2:
                printf("enter 1st string\n");
                scanf("%s", &s1);
                printf("enter 2nd string\n");
                scanf("%s", &s2);
                result = compare(s1, s2);
                if (result == 0)
                {
                    printf("strings are equal \n");
                }
                else
                {
                    printf("strings are not equal \n");
                    break;
                }

            case 3:
                printf("enter two strings\n");
                scanf("%s%s", s1, s2);
                concat(s1, s2);
                printf("result=%s \n", s1);
                break;
            }
        } while (option <= 3);
        return 0;
}
    int length(char str[100])
    {
        int i = 0;
        while (str[i] != '\0')
            i++;
        return (i);
    }
    int compare(char s1[100], char s2[100])
    {
        int i = 0;
        while (s1[i] != '\0')
        {
            if (s1[i] > s2[i])
                return (1);
            else if (s1[i] < s2[i])
                return (-1);
            i++;
        }
        return 0;
    }
    void concat(char s1[100], char s2[100])
    {
        int i, j;
        i = 0;
        while (s1[i] != '\0')
            i++;
        for (j = 0; s2[j] != '\0'; i++, j++)
            s1[i] = s2[j];
        s1[i] = '\0';
    }

Output:

1.String length
2.string comparision
3.string concatenation
4.quit
enter your choice
1
enter string
Chetan
the length of string= 6
1.String length
2.string comparision
3.string concatenation
4.quit
enter your choice

I hope this article “C Programming Lab Exam Questions With Solutions For Engineers” may help you all a lot. Thank you for reading.

Also, read:

Author Profile

CS Electrical And ElectronicsChetu
Interest's ~ Engineering | Entrepreneurship | Politics | History | Travelling | Content Writing | Technology | Cooking
Share Now

CS Electrical And Electronics

Interest's ~ Engineering | Entrepreneurship | Politics | History | Travelling | Content Writing | Technology | Cooking