top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

Write Program using fprintf() print single floating point number right justified.......?

+3 votes
442 views

Using fprintf() print a single floating point number right-justified in a field of 20 spaces, no leading zeros, and 4 decimal places. The destination should be stderr and the variable is called num.

posted Nov 5, 2015 by Mohammed Hussain

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

2 Answers

0 votes
#include <stdio.h>

int main ()
{
   FILE *fp;
   int c;

   fp = fopen("file.txt","r");
   while(1)
   {
      c = fgetc(fp);
      if( feof(fp) )
      {
         break;
      }
      printf("%c", c);
   }
   fclose(fp);
   return(0);
}

Let us compile and run above program to produce the following result.

We are in 2012
answer Nov 5, 2015 by Shivaranjini
0 votes

Here is my ans, If I have correctly understood your question. Pleas have a look.

#include <stdio.h>

int main()
{
    float num = 0;

    printf("Enter the number:\n");
    scanf("%f", &num);

    fprintf(stderr, "output (as suggested): %20.4f\n", num);

    return 0;
}

Sample input output:

1.
 Enter the number:
 20.123
 output (as suggested):                    20.1230

 2.
  Enter the number:
  133.123456
  output (as suggested):                    133.1234
answer Nov 9, 2015 by Arshad Khan
...