This is a companion discussion topic for the original entry at https://toph.co/p/ascii-progress-bar

This is a companion discussion topic for the original entry at https://toph.co/p/ascii-progress-bar

/*

Why getting WA in the last test case. But I find everything OK.

My code:

*/

```
#include <stdio.h>
#include <math.h>
int main(void)
{
float rate;
int near;
scanf ("%f", &rate);
near = (int) floor(rate);
int tm = (int) (rate / 10.0);
printf ("[");
for (int i=0; i < tm; ++i){
printf ("+");
}
for (int k=10; k > tm; --k)
printf (".");
printf ("] %d%c\n", near, '%');
return 0;
}
```

Whst should be the output

The output for 55% should equal to 50%or 60% ?

floor it down to the nearest 10

55% will be treated as 50%.

```
#include<iostream>
using namespace std;
int main()
{
float f;
int n;
cin >> f;
n = (int)f;
int floor = n / 10;
if(floor <= 10)
{
cout << "[";
for(int i=1; i<=10; i++)
{
if(i <= floor)
cout << "+";
else
cout << ".";
}
cout << "]" << " " << n << "%" << endl;
}
return 0;
}
```

Why I am getting wrong answer?

Try using `double f`

instead of `float`

.

It worked. Thanks!

#include<stdio.h>

int main()

{

printf(βShould I solve it like this?β);

int a;

scanf("%d",&a);

int c=a;

printf("[");

a=a/10;

int b=10-a;

while(aβ){

printf("+");

}

while(bβ){

printf(".");

}

printf("] %d",c);

char z=β%β;

printf("%c",z);

return 0;

}

Iβm surprised. Why people are solving it in the complex way?

Many man. many ideas. Thats why.

@hjr265 bro.

as the problem statement says that-- floor it down to the nearest 10 (62 becomes 60, 65 becomes 60, 68 becomes 60 and so on.).

but in input 62.3% have output of 62%.

why?

use double insted of float

Floor it down to nearest 10 to draw the progress bar.

Floor it down to the nearest integer to print the percentage.

```
x=float(input())
x=int(x)
y=x//10
z=10-y
print('[',end='')
for i in range(10):
if 1<=y<=10:
print('+',end='')
y-=1
elif 1<=z<=10:
print('.',end='')
z-=1
print(']',end='')
print(x)
```

What is the problem here

```
#include<stdio.h>
void main(){
float n;
scanf("%f",&n);
int a,b,c;
a=n/1;
b=a/10;
c=10-b;
printf("[");
for(int i=1;i<=b;i++){
if(i<=a){
printf("%c",43);
}
}
for(int i=1;i<=c;i++){
if(i<=c){
printf("%c",46);
}
}
printf("] ");
printf("%d",a);
printf("%c",37);
}
```

whatβs wrong with my code???

@Mir.252324 Follow these steps.

**1.**

`void main()`

` ^~~~ change the return type to int`

[This lineβs inspired by @hjr265 ]

**2.** Check the quotes. Change all β to ".

**3.** Your codeβs giving incorrect output for cases like 52.999999.

Change

to `double n`

# ββββββββββββββββββββ

Explanation: `double`

has 2x the precision of `float`

, as the name says. In general, `double`

has 15 decimal digits of precision, while `float`

has 7.

# ββββββββββββββββββββ

Calculations:

`double`

has 52 mantissa bits + 1 hidden bit: log(2^{53})/log(10) = **15.95 digits**

`float`

has 23 mantissa bits + 1 hidden bit: log(2^{24})/log(10) = **7.22 digits**

**This precision loss could lead to greater truncation errors in cases.**

You can use **static_cast(n)** to cut down the decimal portion without rounding the double number