16.Write a program in C to find the absolute value of a number using 'if-else' statements.
Purpose:
The purpose of the program is to calculate the absolute value of a given number. The absolute value of a number is its distance from zero on the number line, regardless of its sign. This program aims to provide a method to determine the positive value of any input number.
Agenda:
1. Prompt the user to input a number.
2. Check if the input number is positive or negative using 'if-else' statements.
3. If the number is negative, convert it to its positive equivalent by multiplying it by -1.
4. Display the absolute value of the input number to the user as the output.
Real-life applications:
1. In financial calculations, determining the absolute value of changes in stock prices or currency exchange rates.
2. In physics and engineering, calculating distances, velocities, or accelerations where only magnitude is required.
3. In temperature conversions, ensuring that temperature differentials are represented accurately without regard to direction.
4. In gaming applications, calculating distances between objects or characters on a game map without considering their relative positions.
a program in C to find the absolute value of a number using if else statements.
/* C language program to find the absolute value of a number using 'if-else' statements.*/
#include <stdio.h>
int main()
{
int num;
// Input the number
printf("Enter a number: ");
scanf("%d", &num);
// Check if the number is negative
if (num < 0) {
num = -num; // Change the sign to positive
}
// Output the absolute value
printf("The absolute value of the number is: %d\n", num);
return 0;
}