rounding to an input value

W

whojustdenyme

Hey guys, I'm new to C and programming..so I need some help.
I need a way to round an INPUT number to an INPUT value

so for ex:

enter a decimal: 3.1459
enter the number to round: 3

Answer: 3.146

Here's what I have so far...


#include<stdio.h>
#include<math.h>
int main()
{
float decVal; //DECIMAL VALUE
int round; //# OF PLACES TO ROUND
float a; //ANSWER AFTER ROUNDING

printf("Enter a number as a decimal value (ie. 3.1459): ");
scanf("%f", &decVal);
printf("Enter the number of places to round (ie. 3): ");
scanf("%d", &round);



printf("The number %f rounded to %d decimal places is %f", decVal,
round);
return(0);
}
 
S

Spiros Bousbouras

Hey guys, I'm new to C and programming..so I need some help.
I need a way to round an INPUT number to an INPUT value

so for ex:

enter a decimal: 3.1459
enter the number to round: 3

Answer: 3.146

Here's what I have so far...


#include<stdio.h>
#include<math.h>
int main()
{
float decVal; //DECIMAL VALUE
int round; //# OF PLACES TO ROUND
float a; //ANSWER AFTER ROUNDING

printf("Enter a number as a decimal value (ie. 3.1459): ");
scanf("%f", &decVal);
printf("Enter the number of places to round (ie. 3): ");
scanf("%d", &round);



printf("The number %f rounded to %d decimal places is %f", decVal,
round);

You have left out an argument here.
return(0);
}

So you have written the user interface but you have made
no effort with regards to the actual problem. A bit of
a sneaky way to meet the requirement we have around
here that you need to have made an effort before receiving
help.

But anyway , can you solve the problem in case you want
to round decVal to the nearest integer ? Solve this one
first and we'll take it from there.
 
W

whojustdenyme

Spiros said:
You have left out an argument here.


So you have written the user interface but you have made
no effort with regards to the actual problem. A bit of
a sneaky way to meet the requirement we have around
here that you need to have made an effort before receiving
help.

But anyway , can you solve the problem in case you want
to round decVal to the nearest integer ? Solve this one
first and we'll take it from there.


#include<stdio.h>
#include<math.h>
int main()
{
int decVal; //DECIMAL VALUE
int round; //# OF PLACES TO ROUND
float a; //ANSWER AFTER ROUNDING
float x;
float y;

printf("Enter a number as a decimal value (ie. 3.1459): ");
scanf("%d", &decVal);
printf("Enter the number of places to round (ie. 3): ");
scanf("%d", &round);

printf("%d",decVal);


printf("The number %d rounded to %d decimal places is %f", decVal,
round);
return(0);
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,228
Members
46,818
Latest member
SapanaCarpetStudio

Latest Threads

Top