Question

In: Computer Science

Write a C program that takes a double value and rounds it up or off using...

Write a C program that takes a double value and rounds it up or off using ternary expression in the program. Example, if -2.5 is inputted the value should give -3.0

Solutions

Expert Solution

I have uploaded the Images of the code, Typed code and Output of the Code. I have provided explanation using comments(read them for better understanding).

Images of the Code:


Note: If the below code is missing indentation please refer code Images

Typed Code:

/*header files*/
#include <stdio.h>
int main()
{
//initializing double datatype variables
double x,rem,y;
printf("Enter value: ");
//getting input from the user
scanf("%lf",&x);
//storing decimal value to rem
//rem = x - (int)x
//let us assume x = 4.5 ==> 4.5 - (int) 4.5 ==> 4.5 - 4 = 0.5
rem = x - (int)x;
//for positive numbers
//if x >= 0 then condition is true
// rem < 0.5 then y = int(x)
// otherwise y = int(x)+1
//for negative numbers
//if x < 0 then condition is false
// rem > -0.5 then y = int(x)
// otherwise y = int(x)-1
y = (x>=0)?(rem<0.5)?((int)x):((int)x + 1):(rem>-0.5)?((int)x):((int)x - 1);
//printing y
printf("Rounded value: %.1lf",y);
return 0;
}
//code ended here

Program using if else to check positive or negative number and round it up or off:

/*header files*/
#include <stdio.h>
int main()
{
//initializing double datatype variables
double x,rem,y;
printf("Enter value: ");
//getting input from the user
scanf("%lf",&x);
//storing decimal value to rem
//rem = x - (int)x
//let us assume x = 4.5 ==> 4.5 - (int) 4.5 ==> 4.5 - 4 = 0.5
rem = x - (int)x;
//for positive numbers
//if x >= 0 then
if(x>=0)
{
// if rem < 0.5 then y = int(x)
// otherwise y = int(x)+1
y = (rem<0.5)?((int)x):((int)x + 1);
  
}
//for negative numbers
//if x < 0 then
else
{
// if rem > -0.5 then y = int(x)
// otherwise y = int(x)-1
y = (rem>-0.5)?((int)x):((int)x - 1);
}
//printing y
printf("Rounded value: %.1lf",y);
return 0;
}

Output:


If You Have Any Doubts. Please Ask Using Comments.

Have A Great Day!


Related Solutions

Using c# , Write a program using a switch statement that takes one character value from...
Using c# , Write a program using a switch statement that takes one character value from the user and checks whether the entered value is an arithmetic operator (+, -, * , /) If not the program display a message that it not of the operators ( (+, -, * , /) .
Write a C++ program that takes in a set of daily average temperatures (up to a...
Write a C++ program that takes in a set of daily average temperatures (up to a maximum of 30): 1.Ask the user for a temperature 2.If the user enters a -1 then stop asking for temperatures. 3. After the user is done entering temperatures: a. Print out the temperatures entered. b. print out the average, high and low temperatures. To get average, use: average = (sum of temps) divided by (count of temps) to get max or min, either keep...
Write a program in C or in Java, that takes an integer value N from the...
Write a program in C or in Java, that takes an integer value N from the command line, generates N random points in the unit square, and computes the distance separating the closest pair of points. A unit square is a square with sides of length 1, at points (0, 0), (0, 1), (1, 0), and (1, 1). If you wish to avoid the command-line processing, you can just assume you will generate a fixed number of points, say between...
Write a function in C# that takes an array of double as the parameter, and return...
Write a function in C# that takes an array of double as the parameter, and return the average
USING C# 1. Write a program that takes outputs a string, an integer and a floating-point...
USING C# 1. Write a program that takes outputs a string, an integer and a floating-point number separated by commas. Sample output: Bob Marley, 20, 5.2 2. 2. Write a program that asks the user for a string of letters of any size (no spaces), and finally a special character (values 33 to 47 in the Ascii table, or ‘!’ to ‘/’). Generate a random number of any size, integer or floating point, and combine those three pieces of information...
Using loop statements, write a C++ program which takes the number of items that a shopper...
Using loop statements, write a C++ program which takes the number of items that a shopper wants to buy, and then takes the price of each item, and at the end tells the shopper how much she must pay. This is a sample of the output: How many items do you have in your basket? 3 Enter the price in dollar? 10.25 Enter the price in dollar? 20.75 Enter the price in dollar? 67.5 You must pay 98.5 $ today.
Integer value calculator: Write a program using C++ that acts as a calculator. The program should...
Integer value calculator: Write a program using C++ that acts as a calculator. The program should ask the user for two numbers and an operator. Then the program must print the result using the two input values and the operator. Prompts and input requirements. Ask the user for two integer values and a value that indicates the operation. Likely you will want to choose the normal operators: + - * / %. Output Requirements: Make your output easy to understand,...
Write a program in C that takes the length and the integers to be stored in...
Write a program in C that takes the length and the integers to be stored in an array and shifts array by N positions. Example: Input the number of elements to store in the array (max 10) : 5 Input 5 integers to be stored : Index - 0 : 12 Index - 1 : 29 Index - 2 : 68 Index - 3 : 32 Index - 4 : 97 Input number of shifts : 2 Expected Output :...
Write a program that takes its input from a file of number type double and outputs...
Write a program that takes its input from a file of number type double and outputs the average of the numbers in the file to the screen. The file contains nothing but numbers of the type double separated by blanks and/ or line breaks. If this is being done as a class assignment, obtain the file name from your instructor. File name: pr01hw05input.txt 78.0 87.5 98.1 101.0 4.3 17.2 78.0 14.5 29.6 10.2 14.2 60.7 78.3 89.3 29.1 102.3 54.1...
The program should be written in C++ with comments Write a program that takes graduation rates...
The program should be written in C++ with comments Write a program that takes graduation rates (per 1000 of the population) for North, South, East, West and Central United States. Input the number from each of the regions in a function returning an int with the graduation rate to the main program. Also figure out which region has the highest graduation rate in another function and display the result from inside that particular function. So your function prototypes should be...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT