The excitement level is so up that i couldn’t stop myself from writing this post.
Well , may not be a very big deal for everyone but it sure is a big thing to me. Yes , right now i feel like i am on cloud 9 π
Few days back , in one of our labs , we were supposed to write a C program and the question given was quite complicated for one to first understand.. It took me almost two hours,Β sitting and thinking on what should i do..
I knew every logic that the question had.. but i had to struggle enough to write the code.
I will mention the question along with an example so that it is clear π
The question “just” said to round an “integer” not any specific but can be rounded upto any roundup digit ( which means , we take the roundup digit ). Simple isn’t it ? π
Well , it should include all the rounding off rules :
1. If it is to be rounded till m places , we note the ‘n’th’ digit , and check the ‘n+1’th’ digit
a. if the n+1’th digit is greater than 5, the n’th digit increases by 1
b. if the n+1’th digit is less than 5, the n’th digit remains the same.
2. IfΒ it is to be rounded till m place, we note the note ‘n’th’ digit AND if the ‘n+1’th’ digit is 5
a. then the n’th digit , if even, then remains the same.
b. then the n’th digit , if odd , increase the n’th digit by 1.
Example –
1 . if the number is – 23476 : should be rounded to m= 4 places : so, n= 7 and n+1= 6
then the output should be – 2348
2. if the number is – 23474 : should be rounded to m= 4 places : so, n= 7 and n+1= 4
then the output should be – 2347
3. if the number is – 23465 : should be rounded to m = 4 places : so, n= 7 and n+1= 5
then the output should be – 2346
4. if the number is – 23475 : should be rounded to m = 4 places : so, n= 6 and n+1= 5
then the output should be – 2348
Lots of logic. Unluckily, none of the students could do it π¦ including me π many students tried google , but even google failed to answer them.
I felt bad, not because i couldn’t write the code in the class but because inspite of knowing the entire logic i couldn’t frame the code.
The day ended, i came back to the room and just bumped on the bed.. Stretched my legs , glued my eyes to the screen , took a pen and paper and started writing whatever i had in mind. My code was a little messy , it gave errors at few points. Finally , i asked for help from sayan da , and may be i couldn’t properly explain the question to him.
The only thing that i missed doing was:
y=num;
while(y!=0)
{
nobit++;
y=y/10;
}
and the rest everything was perfect. I was okay with the my code π thanks to kartick for making me help with this little thing.
Here is the code:
#include<stdio.h>
main()
{
int num, abc, bit, bit1, i, y, nobit=0, div=1, round;
printf(“please enter a number: “);
scanf(“%d”, &num);//enter number
y=num;
while(y!=0)
{
nobit++;
y=y/10;
}
printf(“Please enter the the roundup digit: “);
scanf(“%d”, &round);//enter rounding digit
for(i=nobit; i>ask;i–)
div*=10;
abc=num/div;
bit=abc%10;
bit1=(num/(div/10))%10
/*Β Β Β Β Β printf(“%d\n”,abc);
printf(“%d\n”,bit);
printf(“%d\n”, bit1);*/
if(bit1<5)
printf(“%d”, abc);
if(bit1>5)
printf(“%d”, abc+1);
if(bit1==5)ell
{
if(bit%2==1)
printf(“%d”,abc+1);
else
printf(“%d”,abc);
}
}
Felt really good after seeing my entire logic working right in front of my eyes.
This went a little long but was worth writing π
P.S – Dry run really helps π