actually, no... if it would be rounded to the hundred place, it would turn 538 would turn to 500... i can't seem to find in the math.h library the function for rounding off if there is any!
actually, no... if it would be rounded to the hundred place, it would turn 538 would turn to 500... i can't seem to find in the math.h library the function for rounding off if there is any!
TO ALL THOSE WITH FRIENDSTER, ADD ME!
i'm [email protected]
a newbie in the field of Computer Science!! Pray for all the student's success!
You mean like dividing by 100, then multiplying by 100?
Quzah.
Hope is the first step on the road to disappointment.
Here is my code... I just have a TEENSY ITSY BITSY problem...Code:#include <stdio.h> #include <conio.h> int main(void){ int v=0; printf("Number please: "); scanf("%d",&v); if((v%10)<=4){ printf("Tens is %d", (v/10)*10); printf("\nHundreds is %d", (v/100)*100); printf("\nThousands is %d", (v/1000)*1000); printf("\nTen thousands is %d", (v/10000)*10000); printf("\nHundred Thousands is %d", (v/100000)*100000); } else{ printf("Tens is %d", ((v/10)+1)*10); printf("\nHundreds is %d", (v/100)*100); printf("\nThousands is %d", (v/1000)*1000); printf("\nTen thousands is %d", (v/10000)*10000); printf("\nHundred Thousands is %d", (v/100000)*100000); } getch(); return 0; }
How can i let the computer read the 1st digit of a 2 or more digit number so that if i want to round off the number 530....
Tens is 530
Hundreds is 500
Thousands is 1000
and not!
Thousands is 0
Last edited by Salem; 08-21-2006 at 06:10 AM. Reason: Stop trying to be creative and use the proper tags for code!!!
TO ALL THOSE WITH FRIENDSTER, ADD ME!
i'm [email protected]
a newbie in the field of Computer Science!! Pray for all the student's success!
> Tens is 530
> Hundreds is 500
> Thousands is 1000
But that would be a huge lie. I'm not very good at math, but computers shouldn't mess up the way I count on purpose. If I use this program to figure out that there were 5 hundreds, 3 tens and no ones, then that's what I should get. I have no idea that the computer rounded my input to the thousands place.
> and not!
> Thousands is 0
This is right though.
Here is a tip for integer rounding... add .5
Integers always round down, well technically they truncate all fractional parts. so if you take a number like 0.6 and put it in an int, the int will be 0, but if you add 0.5 first, it will be 1.1 and 1.1 put into an int is 1.
I remember having a hard time with that when I first started.
Finally! I got it! But it looks rather long... Is there no shortcuts in this??
Complicated, isn't it?Code:#include <stdio.h> #include <conio.h> int main(void){ int v=0; printf("Number please: "); scanf("%d",&v); if((10-(v%10))<(v%10)) printf("Tens value is %d", v+(10-(v%10))); else printf("Tens value is %d", (v/10)*10); if((100-(v%100))<(v%100)) printf("\nHundreds value is %d", v+(100-(v%100))); else printf("\nHundreds value is %d", (v/100)*100); if((1000-(v%1000))<(v%1000)) printf("\nThousands value is %d", v+(1000-(v%1000))); else printf("\nThousands value is %d", (v/1000)*1000); if((10000-(v%10000))<(v%10000)) printf("\nTen Thousands value is %d", v+(10000-(v%10000))); else printf("\nTen Thousands value is %d", (v/10000)*10000); if((100000-(v%100000))<(v%100000)) printf("\nHundred Thousands value is %d", v+(100000-(v%100000))); else printf("\nHundred Thousands value is %d", (v/100000)*100000); getch(); return 0; }
Last edited by nesir; 08-21-2006 at 10:57 PM.
TO ALL THOSE WITH FRIENDSTER, ADD ME!
i'm [email protected]
a newbie in the field of Computer Science!! Pray for all the student's success!
hey... what does getch() means, by the way? A friend of mine just told me to use #include <conio.h> and getch() when using DevC++ but he didn't tell me the exact function of it... I'm not used to use DevC++ because most of the time I use appserver... and appserver does not require any #include <> functions..
TO ALL THOSE WITH FRIENDSTER, ADD ME!
i'm [email protected]
a newbie in the field of Computer Science!! Pray for all the student's success!
> what does getch() means, by the way?
Remove it and find out?
It's one of the ways (not a particularly good one) of solving this FAQ issue
http://faq.cprogramming.com/cgi-bin/...&id=1043284385
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.
getch is a non-Standard function which reads a key. Your compiler may or may not have it. It's not provided by the language, rather by your compiler, if they've decided they want to write one called that.
I'll let you figure out what this does.
Hm... Here's another one.Code:int round( int num, size_t place ) { char buf[ BUFSIZ ] = {0}; size_t x; if( (x = sprintf( buf, "%d", num )) >= place ) { if( buf[ x - place + 1 ] > '4' ) { buf[ x - place ]++; } memset( buf + ( x - place + 1 ), '0', place - 1 ); } num = atoi( buf ); return num; }The rounding place on these is different, or rather, is treated differently. I'll let you figure out why, and how to fix it if you want. I didn't feel like using extra variables, and wanted something to make this interesting. Oh, and if you have an old compiler, it's going to crap itself when it sees that number.Code:int round( int num, size_t place ) { size_t y = 1000000000; switch( place ) { case 1: y /= 10; case 2: y /= 10; case 3: y /= 10; case 4: y /= 10; case 5: y /= 10; case 6: y /= 10; case 7: y /= 10; case 8: y /= 10; case 9: y /= 10; } if( num % y >= y / 2 ) num += y; num /= y; num *= y; return num; }
Ok, ok, one final one...Code:int round(int n,size_t p){int x[]={1,10,100,1000,10000,100000, 1000000,10000000,100000000,1000000000};return((n/x[p%x[1]])*x[ p%x[1]])+(n-(n/x[p%x[1]]*x[p%x[1]])<x[p%x[1]]/2?0:x[p%x[1]]);}
Hope is the first step on the road to disappointment.