C Program to Check the Noise Levels
In this tutorial, we will learn to write a program that has the capability to tell the description of the sound level based on the input provided by the user.
In this tutorial, we will learn to write a program that has the capability to tell the description of the sound level based on the input provided by the user.
Write a program that will display the description of the sound level, based on the criteria below:
Based on the user input (which is an integer representing a decibel), the
appropriate description needs to be displayed. Check out the
following examples for more clarity.
Input:
50
Output:
Quiet
Explanation:
50 lies within the range of 0 - 60, and hence “Quiet” is displayed.
Input:
-10
Output:
Negative value is not allowed.
Explanation:
If the user input is negative, then it's not a valid input, so the appropriate message must be displayed.
Input:
1000
Output:
This value is off the charts! Try again.
Explanation:
If the user input is higher than the highest decibel value, then it's not a valid input, so we need to display an error message.
You need to write the complete program using the following template as the starter:
#include <stdio.h>
int main() {
int dec;
// The prompt.
printf("Enter the number: ");
scanf("%d", &dec);
// Write your code here.
return 0;
}
Copy and paste the above code in your favorite IDE and start writing your code after the comment // Write your code here
.
Pro tip: First, try writing the code on your own. Try every possible thing you can to come up with the solution. If you still can’t figure it out, then check the solution and see what went wrong. This is how you learn. 🙂
Input | Output | |
---|---|---|
Test Case 1 | 0 | Quiet |
Test Case 2 | 10000 | This value is off the charts! Try again. |
Test Case 3 | 100 | Very loud |
Test Case 4 | -50 | Negative value is not allowed. |
Don’t forget to try the code on your own before diving into the solution.
Following are the multiple approaches to the same problem:
We can use if-else ladder to check multiple conditions as shown in the following code:
#include <stdio.h>
int main() {
int dec;
// The prompt.
printf("Enter the number: ");
scanf("%d", &dec);
// Write your code here.
if (dec < 0) {
printf("Negative value is not allowed.\n");
} else if (dec >= 0 && dec <= 60) {
printf("Quiet\n");
} else if (dec > 60 && dec <= 70) {
printf("Conversational\n");
} else if (dec > 70 && dec <= 90) {
printf("Loud\n");
} else if (dec > 90 && dec <= 110) {
printf("Very Loud\n");
} else if (dec > 110 && dec <= 129) {
printf("Dangerous\n");
} else if (dec > 129 && dec <= 194) {
printf("Very Dangerous\n");
} else {
printf("This value is off the charts! Try again.");
}
return 0;
}
Output:
Enter the number: 10
Quiet
You can check the above program for multiple test cases and the code works just fine.
If you closely observe the previous code, there is no need to check the first condition in every ‘if’ statement. For example, it is clear that when ‘dec’ is not less than 60, then it must be greater than 60. This is because we are using an ‘if-else’ ladder due to which a specific conditional expression will only be checked when the conditions prior to it are not satisfied (or False). So, for instance, it is clear that the conditional expression dec > 60 && dec <= 70 will be evaluated only when dec < 0 and dec >= 0 && dec <= 60 fails. Hence, there is no need to check the first condition in each conditional expression.
The following program is the modified version of the previous program:
#include <stdio.h>
int main() {
int dec;
// The prompt.
printf("Enter the number: ");
scanf("%d", &dec);
// Write your code here.
if (dec < 0) {
printf("Negative value is not allowed.\n");
} else if (dec <= 60) {
printf("Quiet\n");
} else if (dec <= 70) {
printf("Conversational\n");
} else if (dec <= 90) {
printf("Loud\n");
} else if (dec <= 110) {
printf("Very Loud\n");
} else if (dec <= 129) {
printf("Dangerous\n");
} else if (dec <= 194) {
printf("Very Dangerous\n");
} else {
printf("This value is off the charts! Try again.");
}
return 0;
}
Output:
Enter the number: 0
Quiet
There is even a better way to write the program for this problem according to me. We can replace the if-else statements with switch as shown in the following program:
#include <stdio.h>
int main() {
int dec;
// The prompt.
printf("Enter the number: ");
scanf("%d", &dec);
// Write your code here.
if (dec < 0) {
printf("Negative value is not allowed.\n");
} else {
switch(dec) {
case 0 ... 60:
printf("Quiet\n");
break;
case 61 ... 70:
printf("Conversational\n");
break;
case 71 ... 90:
printf("Loud\n");
break;
case 91 ... 110:
printf("Very Loud\n");
break;
case 111 ... 129:
printf("Dangerous\n");
break;
case 130 ... 194:
printf("Very Dangerous\n");
break;
default:
printf("This value is off the charts! Try again.\n");
}
}
return 0;
}
Output:
Enter the number: -10
Negative value is not allowed.
The program above is the same as the previous ones in terms of logic. The only difference is that the if-else ladder is replaced by a switch statement. You may have noticed the three dots (…) in every case mentioned. These three dots are used to represent a specific range. The rest of the code is self-explanatory.
Leave a comment