PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Tuesday, August 16, 2022

[FIXED] How to format fraction output in terminal with printf

 August 16, 2022     c, format, output, printf, terminal     No comments   

Issue

This may be really dumb but I have been unable to print a fraction to my terminal. How do I make my fraction look like this fraction two thirds and not(2/3).

I've tried the code below but I don't know how I'll print multiple fractions on the same line.

printf("2\n");
printf("-\n");
printf("3\n");


Solution

Ugly, but if the example of one fraction on 3 lines, as you have shown in your post is okay, then this illustrates multiple fractions on the same 3 lines. Note the use of width specifier in format strings:

int main(int argc, char *argv[])
{
    const char array[3][5] = {{'1','2','3','4','5' },
                              {'-','-','-','-','-'},
                              {'2','3','4','5','6' }};
    for(int i=0;i<3;i++)
    {
        for(int j=0;j<5;j++)
        {
            printf("%6c", array[i][j]);
            //       | width specifier, change value as needed for formatting.
        }
        printf("\n");
    }
    return 0;
}

Output:
enter image description here

For a prettier output, you could always use unicode characters, but then you would need to use wchar, and still be limited to seeing Vulgar fraction form: 1/2.



Answered By - ryyker
Answer Checked By - Clifford M. (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing