- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to align the output using justificationsin C language?
By using justifications in printf statement we can arrange the data in any format.
Right Justification
To implement the right justification, insert a minus sign before the width value in the %s character.
printf("%-15s",text);
Program 1
Let’s take an example to print data in row and column-wise with the help of justification.
#include<stdio.h> int main(){ char a[20] = "Names", b[20]="amount to be paid"; char a1[20] = "Bhanu", b1[20]="Hari",c1[20]="Lucky",d1[20]="Puppy"; int a2=200,b2=400,c2=250,d2=460; printf("%-15s %-15s
", a, b); printf("%-15s %-15d
", a1,a2); printf("%-15s %-15d
", b1,b2); printf("%-15s %-15d
", c1, c2); printf("%-15s %-15d
", d1, d2); return 0; }
Output
Names amount to be paid Bhanu 200 Hari 400 Lucky 250 Puppy 460
Program 2
Consider the same example by changing the justification −
#include<stdio.h> int main(){ char a[20] = "Names", b[20]="amount to be paid"; char a1[20] = "Bhanu", b1[20]="Hari",c1[20]="Lucky",d1[20]="Puppy"; int a2=200,b2=400,c2=250,d2=460; printf("%2s %2s
", a, b); printf("%5s %5d
", a1,a2); printf("%2s %2d
", b1,b2); printf("%5s %5d
", c1, c2); printf("%2s %2d
", d1, d2); return 0; }
Output
Names amount to be paid Bhanu 200 Hari 400 Lucky 250 Puppy 460 Note: Alignment is note in proper if we not use correct justification
Advertisements