RoundOff a number

+2 votes
asked Sep 30, 2019 by anonymous
To roundoff a number. A program requried.

//input1: 5.8 output1: 6

//input2: 7.44 output2: 7

urgent

3 Answers

0 votes
answered Oct 1, 2019 by naveed
import java.io.*;

import static java.lang.Math.*;

public class A {

  

    // Function to round - off the number

    static void Round_off(double N, double n)

    {

        int h;

        double l, a, b, c, d, e, i, j, m, f, g;

        b = N;

        c = floor(N);

  

        // Counting the no. of digits to the left of decimal point

        // in the given no.

        for (i = 0; b >= 1; ++i)

            b = b / 10;

  

        d = n - i;

        b = N;

        b = b * pow(10, d);

        e = b + 0.5;

        if ((float)e == (float)ceil(b)) {

            f = (ceil(b));

            h = (int)(f - 2);

            if (h % 2 != 0) {

                e = e - 1;

            }

        }

        j = floor(e);

        m = pow(10, d);

        j = j / m;

        System.out.println("The number after rounding-off is "

                           + j);

    }

  

    // Driver main function

    public static void main(String args[])

    {

        double N, n;

  

        // Number to be rounded - off

        N = 139.59;

  

        // No. of Significant digits required in the no.

        n = 4;

  

        Round_off(N, n);

    }

}
0 votes
answered Oct 1, 2019 by Dave (290 points)
Good morning. Could you please help me better understand what you are looking for? Are you wanting an end user to enter a (decimal) number and have that round (up or down) to the nearest whole number? Which programming language are you looking for this in? DAVE
0 votes
answered Oct 3, 2019 by Uzair Anwar (360 points)
edited Oct 3, 2019 by Uzair Anwar

double a;

scanf("%d", &a);

int b=a-int(a);

if((b>0.5){

a=int(a)+1;

Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and and receive answers from other members of the community.
...