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

C Program To Determine Designation of Employee Based On Salary

Rewrite the following C program using Conditional / Ternary Operator.

Source Code: C Program To Determine Designation of Employee Based On Salary Using nested if else condition

#include<stdio.h>

int main()
{
    float sal;

    printf("Enter the salary\n");
    scanf("%f", &sal);

    if(sal >= 25000 && sal <= 40000)
    {
        printf("Manager\n");
    }
    else
        if(sal >= 15000 && sal < 25000)
            printf("Accountant\n");
        else
            printf("Clerk\n");

    return 0;
}

Note: We need to rewrite the above C program using Ternary / Conditional Operator.

Good Example of Nested Ternary Operator
C Program To Check Leap Year Using Ternary Operator

Related Read:
Nested if else Statement In C
Ternary Operator / Conditional Operator In C
Relational Operators In C
Logical Operators In C

Ternary / Conditional Operator General Form

(expression_1) ? (expression_2) : (expression_3);

expression_1 is a comparison/conditional argument. expression_2 is executed/returned if expression_1 results in true, expression_3 gets executed/returned if expression_1 is false.

Ternary operator / Conditional Operator can be assumed to be shortened way of writing an if-else statement.

Expected Output for the Input

User Input:
Enter the salary
40000

Output:
Manager

Logic To Convert from if else To Conditional Operator

As you can see we’ve nested if else statement inside first else statement. So we’ll have nested conditional operator inside expression_3 of outer conditional operator.

Video Tutorial: C Program To Determine Designation of Employee Based On Salary


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

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

Source Code: C Program To Determine Designation of Employee Based On Salary using Conditional Operator

#include<stdio.h>

int main()
{
    int sal;

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

    ( sal >= 25000 && sal <= 40000 ) ?
    printf("Manager\n") :
    ( sal >= 15000 && sal <  25000)?
    printf("Accountant\n") :
    printf("Clerk\n");

    return 0;
}

Output 1:
Enter the salary
35000
Manager

Output 2:
Enter the salary
20000
Accountant

Output 3:
Enter the salary
10000
Clerk

Note: As you can see if you enter the salary as $40001 and above, it shows the designation as Clerk. That is because we’re not checking for salary above $40000. With the problem statement given above, we assume that this C program is for employees with salary less than $40000 and for designation Manager, Accountant and Clerk only.

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 Check If Sum of Square of Sine and Cosine of an Angle is 1

Write a C program to receive value of an angle in degrees and check whether sum of squares of sine and cosine of this angle is equal to 1.

Related Read:
Basic Arithmetic Operations In C
Calculate Power of a Number using pow(): C Program

Formula To Convert Angle From Degree To Radian

radian = degree x ( PI / 180.0)
where PI is 3.14159265359

Note: In C programming Language, we’ve a builtin constant M_PI which has value of PI.

Expected Output for the Input

User Input:
Enter angle in Degree
45

Output:
Sum of square of sin(45.00) and cos(45.00) is 1

Logic To Check If Sum of Square of Sine and Cosine of an Angle is 1

User enters the angle in degree. We convert angle from degree to radian.
radian = degree x ( PI / 180.0)
Next we use pow() method present in math.h library to sqaure the values of sin(angle) and cos(angle). We add the square of sin(angle) and cos(angle) and store it inside the variable sum, and display appropriate message to the user.

Video Tutorial: C Program To Check If Sum of Square of Sine and Cosine of an Angle is 1


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

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

Source Code: C Program To Check If Sum of Square of Sine and Cosine of an Angle is 1

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

int main()
{
    float sum = 0, angle, temp;

    printf("Enter angle in Degree\n");
    scanf("%f", &angle);

    temp   = angle;

    angle  = angle * ( M_PI / 180.0 );

    sum    = (  pow(sin(angle), 2) + pow(cos(angle), 2)  );

    if(sum == 1)
    {
        printf("Sum of square of sin(%0.2f) and cos(%0.2f) is 1\n", temp, temp);
    }
    else
    {
        printf("Sum of square of sin(%0.2f) and cos(%0.2f) is not 1\n", temp, temp);
    }

    return 0;
}

Output 1:
Enter angle in Degree
30
Sum of square of sin(30.00) and cos(30.00) is 1

Output 2:
Enter angle in Degree
45
Sum of square of sin(45.00) and cos(45.00) is 1

Output 3:
Enter angle in Degree
60
Sum of square of sin(60.00) and cos(60.00) is 1

Output 4:
Enter angle in Degree
75
Sum of square of sin(75.00) and cos(75.00) is 1

Output 5:
Enter angle in Degree
90
Sum of square of sin(90.00) and cos(90.00) is 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

C Program To Find Special Symbol or Not using Conditional Operator

Using Conditional / Ternary Operator determine, whether a character entered through the keyboard is a Special Symbol or not.

Note: ASCII values start from 0 to 255. So we have total of 266 ASCII values.

ASCII Code for Special Symbols
0 – 47
58 – 64
91 – 96
123 – 255

Also Check:
C Program To Find Lowercase Alphabet or Not using Conditional Operator

Related Read:
Relational Operators In C
Logical Operators In C
Ternary Operator / Conditional Operator In C
C Program To Print All ASCII Characters and Code

Ternary / Conditional Operator General Form

(expression_1) ? (expression_2) : (expression_3);

expression_1 is a comparison/conditional argument. expression_2 is executed/returned if expression_1 results in true, expression_3 gets executed/returned if expression_1 is false.

Ternary operator / Conditional Operator can be assumed to be shortened way of writing an if-else statement.

Expected Output for the Input

User Input:
Enter a Character
$

Output:
Character Entered Is a Special Symbol

Logic To Find Special Symbol or Not using Conditional Operator

Using Conditional Operator we write the condition, if user entered character is in between or equal to ASCII values 0 – 47 or 58 to 64 or 91 to 96 or greater than or equal to 123. We use Relational Operator and Logical Operators to accomplish the task.

If the condition in expression_1 is true, then whatever code is present in expression_2 gets executed. If condition is expression_1 is false then the code present in expression_3 gets executed.

Video Tutorial: C Program To Find Character is Special Symbol or Not using Conditional Operator


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

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

Source Code: C Program To Find Special Symbol or Not using Conditional Operator

#include<stdio.h>

int main()
{
    char ch;

    printf("Enter a Character\n");
    scanf("%c", &ch);

    ( (ch >= 0  && ch <= 47) ||
      (ch >= 58 && ch <= 64) ||
      (ch >= 91 && ch <= 96) ||
      (ch >= 123) ) ?
      printf("Character Entered Is a Special Symbol\n") :
      printf("Character Entered Is not a Special Symbol\n");

    return 0;
}

Output 1:
Enter a Character
#
Character Entered Is a Special Symbol

Output 2:
Enter a Character
a
Character Entered Is not a Special Symbol

Output 3:
Enter a Character
Z
Character Entered Is not a Special Symbol

Output 4:
Enter a Character
5
Character Entered Is not a Special Symbol

Output 5:
Enter a Character
$
Character Entered Is a Special Symbol

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