C Program To Calculate Volume of Cylinder

Given the values for radius and height of a cylinder, write a C program to calculate volume of the Cylinder.

Formula To Calculate Volume of Cylinder

volume = Base_Area x height;

Base of the Cylinder is a Circle. Area of Circle is PI x Radius2

So lets replace Base_Area with Area of Circle: PI x Radius2.

Therefore, volume = (PI x Radius2) x height;

volume of Cylinder

where PI is 3.14159265359;

Related Read:
Basic Arithmetic Operations In C

Expected Output for the Input

User Input:
Enter Radius and Height of the Cylinder
2
5

Output:
Volume of Cylinder is 62.831856

Logic To Calculate Volume of Cylinder

We ask the user to enter values for radius and height of the Cylinder. If user enters 2m and 5m. Then we use the formula to calculate the Volume of Cylinder:

volume = (PI x Radius2) x height;

User input:
radius = 2m;
height = 5m;

PI value is 3.14159265359;

volume = PI x Radius2 x height;
volume = 3.14 x (2m)2 x (5m);
volume = 3.14 x 4(m)2 x 5(m);
volume = 3.14 x 20(m)3;
volume = ‭62.8‬ m3;

So volume of cylinder is 62.8 cubic meter.

Video Tutorial: C Program To Calculate Volume of Cylinder


[youtube https://www.youtube.com/watch?v=WZMPJEGs0cI]

YouTube Link: https://www.youtube.com/watch?v=WZMPJEGs0cI [Watch the Video In Full Screen.]

Source Code: C Program To Calculate Volume of Cylinder

#include<stdio.h>

int main()
{
    const float PI = 3.14159265359;
          float r, h, volume;

    printf("Enter Radius and Height of the Cylinder\n");
    scanf("%f%f", &r, &h);

    volume = PI * r * r * h;

    printf("Volume of Cylinder is %f\n", volume);

    return 0;
}

Output 1:
Enter Radius and Height of the Cylinder
2
5
Volume of Cylinder is 62.831856

Output 2:
Enter Radius and Height of the Cylinder
2
3
Volume of Cylinder is 37.699112

For list of all c programming interviews / viva question and answers visit: C Programming Interview / Viva Q&A List

For full C programming language free video tutorial list visit:C Programming: Beginner To Advance To Expert

Modulus or Modulo Division In C Programming Language

Today lets learn about Modulus or Modulo or Modular Division in C programming language.

Division Example
10 / 5 = 2 (quotient)

Modulo Division Example
10 % 5 = 0 (remainder)

Note:
Division operation returns Quotient.
Modulo Division operation returns Remainder.

quotient = dividend / divisor;
remainder = dividend % divisor;

Related Read:
Using Scanf in C Program
Basic Arithmetic Operations In C

Expected Output for the Input

User Input:
Enter value of a and b for modular division
10
5

Output:
10 mod 5 = 0

Important Points To Remember About Modulo Division

1. Modulo Division can only be used with Integers and not with Floating point numbers.

2. When numerator is smaller than denominator, then numerator itself is returned as remainder.

3. When numerator is greater than denominator, then remainder is returned.

4. In modulo Division operation, remainder will always have the same sign as that of the dividend or numerator.

5. Symbol of modular division is %.

Video Tutorial: Modulus or Modulo Division In C Programming Language


[youtube https://www.youtube.com/watch?v=3Gz_CLMiGFQ]

YouTube Link: https://www.youtube.com/watch?v=3Gz_CLMiGFQ [Watch the Video In Full Screen.]

Source Code: Modulus or Modulo Division In C Programming Language

#include<stdio.h>

int main()
{
    int a, b;

    printf("Enter value of a and b for modular division\n");
    scanf("%d%d", &a, &b);

    printf("%d mod %d = %d\n", a, b, (a%b));

    return 0;
}

Output 1:
Enter value of a and b for modular division
10
3
10 mod 3 = 1

Output 2:
Enter value of a and b for modular division
-10
2
-10 mod 2 = 0

Output 3:
Enter value of a and b for modular division
-10
3
-10 mod 3 = -1

Output 4:
Enter value of a and b for modular division
10
-3
10 mod -3 = 1

Output 5:
Enter value of a and b for modular division
-10
-3
-10 mod -3 = -1

For list of all c programming interviews / viva question and answers visit: C Programming Interview / Viva Q&A List

For full C programming language free video tutorial list visit:C Programming: Beginner To Advance To Expert

21 Matchstick Game: C Program

Write a C program for a matchstick game being played between the computer and a user. Your program should ensure that the computer always wins. Rules for the game are as follows:

– There are 21 matchsticks.
– The computer asks the player to pick 1, 2, 3 or 4 matchsticks.
– After the person picks, the computer does its picking.
– Whoever is forced to pick up the last matchstick loses the game.

Related Read:
while loop in C programming
if else statement in C
Relational Operators In C

Logic of 21 Matchstick Game C Program

There are in total 21 match sticks to start the game. First we ask the user to pick either 1 or 2 or 3 or 4 matches per pick. Once the user makes his/her pick, computer makes the picking(same rules apply to the computer i.e., it can pick either 1 or 2 or 3 or 4 matches per pick). The trick is, computers pick is always 5 minus the pick of the user. For example, if computers pick is variable c and user pick is stored in variable p, then:
c = 5 – p;
This makes sure computer always wins the game. That is, the last pick will always be of the user.

Note: We have 1 as the condition in while loop to make sure the while loop keeps executing until a break statement is occurred inside the loop to terminate the execution of the loop. while(1) is considered as an infinite loop(unless we have some ways to break out of the loop programmatically).

Note:
break; breaks out of the loop or terminates the execution of the loop.

continue; skips execution of all the code after it in the loop and goes for the next iteration of the loop.

Video Tutorial: 21 Matchstick Game: C Program


[youtube https://www.youtube.com/watch?v=VO_5XTLxKmc]

YouTube Link: https://www.youtube.com/watch?v=VO_5XTLxKmc [Watch the Video In Full Screen.]

Source Code: 21 Matchstick Game: C Program

#include<stdio.h>

int main()
{
    int m = 21, p, c;

    while(1)
    {
        printf("\nNumber of Match sticks left = %d\n", m);
        printf("Pick 1 or 2 or 3 or 4 matches\n");
        scanf("%d", &p);

        if(p > 4 || p < 1)
            continue;

        m = m - p;

        printf("Number of matches left = %d\n", m);

        c = 5 - p;

        printf("out of which computer picked up %d\n", c);

        m = m - c;

        if(m == 1)
        {
            printf("\nNumber of matches left = %d\n", m);
            printf("You lost the Game\n");
            break;
        }
    }

    return 0;
}

Output:
Number of Match sticks left = 21
Pick 1 or 2 or 3 or 4 matches
2
Number of matches left = 19
out of which computer picked up 3

Number of Match sticks left = 16
Pick 1 or 2 or 3 or 4 matches
3
Number of matches left = 13
out of which computer picked up 2

Number of Match sticks left = 11
Pick 1 or 2 or 3 or 4 matches
1
Number of matches left = 10
out of which computer picked up 4

Number of Match sticks left = 6
Pick 1 or 2 or 3 or 4 matches
4
Number of matches left = 2
out of which computer picked up 1

Number of matches left = 1
You lost the Game

For list of all c programming interviews / viva question and answers visit: C Programming Interview / Viva Q&A List

For full C programming language free video tutorial list visit:C Programming: Beginner To Advance To Expert

C Program To Find Range of Set of Numbers

Write a C program to find the range of a set of numbers entered through the keyboard. Range is the difference between the smallest and biggest number in the list.

Example: If biggest number in the list is 5 and smallest number in the list is 1. The difference between them is the range. i.e., 5 – 1 = 4. So range = 4.

Related Read:
while loop in C programming
if else statement in C
Relational Operators In C
C Program To Find Absolute Value of a Number

Expected Output for the Input

User Input:
Enter the limit
5
Enter 5 numbers
1
2
3
4
5

Output:
Small Number = 1
Big Number = 5
Range is 4

Logic To Find Range of Set of Numbers

First we ask the user to enter the length or the size of the list, and store it inside the variable limit. If the user enters the list size as 5, then we ask the user to enter 5 numbers.

Next we ask the user to enter the first number. We assign the first number entered by the user to variables small and big. Since user already entered 1 number into the list, we decrement the value of variable limit by 1.

Next we take remaining inputs inside the while loop. For each iteration of the while loop we decrement the value of variable limit by 1, until the value of limit is 0. Once value of limit is zero, control exits while loop.

For each input(inside the while loop), we check if the value entered is bigger than the value present in variable big. If its true, we assign the bigger number to variable big. We also check if the value entered is smaller than the value present in variable small. If its true, we assign the smaller number to variable small.

Once the control exits the while loop, variable big will have the biggest number in the list and variable small will have the smallest number in the list.

Finally, we use below formula to calculate range of the list:
range = big – small;

Video Tutorial: C Program To Find Range of Set of Numbers


[youtube https://www.youtube.com/watch?v=-yN6KsA8d-k]

YouTube Link: https://www.youtube.com/watch?v=-yN6KsA8d-k [Watch the Video In Full Screen.]

Source Code: C Program To Find Range of Set of Numbers

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

int main()
{
    int small, big, range, num, limit;

    printf("Enter the limit\n");
    scanf("%d", &limit);

    printf("Enter %d numbers\n", limit);
    scanf("%d", &num);

    small = big = num;

    limit = limit - 1;

    while(limit)
    {
        scanf("%d", &num);

        if(num > big)
        {
            big = num;
        }

        if(num < small)
        {
            small = num;
        }

        limit--;
    }

    range = big - small;

    printf("Small Number = %d\nBig Number = %d\n", small, big);
    printf("Range is %d\n", abs(range));

    return 0;
}

Output 1:
Enter the limit
5
Enter 5 numbers
0
-1
2
3
4
Small Number = -1
Big Number = 4
Range is 5

Output 2:
Enter the limit
6
Enter 6 numbers
10
9
8
7
6
5
Small Number = 5
Big Number = 10
Range is 5

Note: abs() returns the absolute value of a number. Absolute value is always positive.

For list of all c programming interviews / viva question and answers visit: C Programming Interview / Viva Q&A List

For full C programming language free video tutorial list visit:C Programming: Beginner To Advance To Expert

C Program To Calculate Overtime Pay of 10 Employees

Write a C program to calculate overtime pay of 10 employees. Overtime is paid at the rate of Rs 12 per hour for every hour worked above 40 hours. Assume that employees do not work for fractional part of an hour.

Related Read:
while loop in C programming
if else statement in C
Relational Operators In C

Logic To Calculate Overtime Pay of 10 Employees

We ask the user to enter the number of hours each employee has worked. Then we calculate overtime worked by each employee. 40 hours is considered normal work hours. Whatever number of hours worked above 40 hours will be considered for Overtime Pay. We calculate the Overtime worked and then for every hour we pay Rs 12 as Overtime pay.

Video Tutorial: C Program To Calculate Overtime Pay of 10 Employees


[youtube https://www.youtube.com/watch?v=MV8AZWyWUww]

YouTube Link: https://www.youtube.com/watch?v=MV8AZWyWUww [Watch the Video In Full Screen.]

Source Code: C Program To Calculate Overtime Pay of 10 Employees

#include<stdio.h>

int main()
{
    int hours, count = 1, ot = 0;

    while(count <= 10)
    {
        printf("\nEnter no of hours employee %d has worked\n", count);
        scanf("%d", &hours);

        if(hours > 40)
        {
            ot = ot + (hours - 40);

            printf("Employee %d has worked %d hours\n", count, hours);
            printf("Overtime pay is Rs %d\n", (hours-40)*12);
        }
        else
        {
            printf("no of hours worked is %d, which is less than 40 hours, so no over time pay for employee %d\n", hours, count);
        }
        count++;
    }

    printf("\nTotal Overtime pay is Rs %d\n", (ot*12));

    return 0;
}

Output:

Enter no of hours employee 6 has worked
45
Employee 6 has worked 45 hours
Overtime pay is Rs 60

Enter no of hours employee 7 has worked
39
no of hours worked is 39, which is less than 40 hours, so no over time pay for employee 7

Enter no of hours employee 8 has worked
41
Employee 8 has worked 41 hours
Overtime pay is Rs 12

Enter no of hours employee 9 has worked
42
Employee 9 has worked 42 hours
Overtime pay is Rs 24

Enter no of hours employee 10 has worked
43
Employee 10 has worked 43 hours
Overtime pay is Rs 36

Total Overtime pay is Rs 252

For list of all c programming interviews / viva question and answers visit: C Programming Interview / Viva Q&A List

For full C programming language free video tutorial list visit:C Programming: Beginner To Advance To Expert