Logo 
Search:

C Programming Articles

Submit Article
Home » Articles » C Programming » Homework HelpRSS Feeds

Program that will round a floating-point number to an indicated decimal place

Posted By: Lughaidh Fischer     Category: C Programming     Views: 4653

Write a program that will round a floating-point number
to an indicated decimal place i.e. no. 17.457 would
yield the value 17.46 when it is rounded off to 2 decimal places.

Code for Program that will round a floating-point number to an indicated decimal place in C Programming

#include<stdio.h>
#include<conio.h>
void main()
{
    char n[10];
    int i,j,k,m;
    clrscr();
    printf("\n\n PLEASE ENTER THE VALUE OF N: ");
        scanf("%s",n);
    for(i=0,j=0;n[i]!='.';i++)
        j++;
    for(i=0,k=0;n[i]!='\0';i++)
        k++;
        k--;
    for(i=k;i>j+2;i--)
    {
        if(n[i] < '5')
            n[i]='\0';
        if(n[i] >= '5')
        {
            n[i-1]=n[i-1]+1;
            n[i]='\0';
        }
    }
    printf("\n\n AFTER ROUND OF N %s",n);
getch();
}


--------------------------------- OUTPUT ---------------------------------




 PLEASE ENTER THE VALUE OF N: 12.362514


 AFTER ROUND OF N 12.36



--------------------------------------------------------------------------
  
Share: 



Lughaidh Fischer
Lughaidh Fischer author of Program that will round a floating-point number to an indicated decimal place is from Frankfurt, Germany.
 
View All Articles

Related Articles and Code:


 
Please enter your Comment

  • Comment should be atleast 30 Characters.
  • Please put code inside [Code] your code [/Code].

 
No Comment Found, Be the First to post comment!