How to print only values other than 0

1

Everyone, I have a question. It is possible in C to print only values other than 0.

An Example:

  • We have output: 2.56000, 5.00000, 3.60000 and 27.36800

  • And I want you to print as follows: 2.56, 5, 3.6 and 27.368

But with printing running in a loop where I can only put

printf("%.2f \n");

or

printf("%f \n");

or

printf("%.1f \n");

or

printf("%.3f \n");

Can anyone help me?

    
asked by anonymous 11.10.2017 / 02:08

2 answers

6

You can use the %g conversion specifier to solve your problem:

#include <stdio.h>

int main(void)
{
    printf( "%g\n",  2.560 );
    printf( "%g\n",  5.000 );
    printf( "%g\n",  3.600 );
    printf( "%g\n", 27.368 );

    return 0;
}

Output:

2.56
5
3.6
27.368
    
11.10.2017 / 02:15
0

So it's easier for you to start everything

#include <stdio.h>
#include <stdlib.h>
int main(){
  float num, num1=2.56000, num2=5.00000, num3=3.60000, num4=27.36800;
  printf("Informe um numero: ");
  scanf("%f",&num;

  while(num!=0){
    printf("%.2f \n",num1);
    printf("%.0f \n",num2);
    printf("%.1f \n",num3);
    printf("%.3f \n",num4);

    printf("Inf. um valor: ");
    scanf("%f",&num);
  }
  return 0;
}
    
12.10.2017 / 21:26