Following is a general way of printing 2D matrix such that every row is printed in separate lines.
How to print print without using any curly braces in for loops?
We strongly recommend you to minimize your browser and try this yourself first.
We can simply removed inner curly bracket as there is single line inner for loop. How to remove outer curly braces? Below is solution.
1 2 3 4 5 6
The actual trick is in following statement “\n”[j == n-1], explanation is as follows:
” \n” is a 2 character string where 1st character is space and 2nd character is newline, so we want space after each value except last for which we want a newline. In C, string literals are treated character arrays [See this for more details].
j == n-1 statement evaluates to 0 for internal elements (because for them j < n-1) and for last value(of each row) it becomes 1. So for internal elements, statement evaluate to " \n" i.e. " " and for last element of each row " \n" i.e. "\n" Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- How can we use Comma operator in place of curly braces?
- Program to print last 10 lines
- Lex program to take input from file and remove multiple spaces, lines and tabs
- Lex program to count the number of lines, spaces and tabs
- How to use getline() in C++ when there are blank lines in input?
- Implement your own tail (Read last n lines of a huge file)
- How to write long strings in Multi-lines C/C++?
- C Program to count number of lines in a file
- Print colored message with different fonts and sizes in C
- Print 1 to 100 in C++, without loop and recursion
- Print substring of a given string without using any string function and loop in C
- How to print range of basic data types without any library function and constant in C?
- Write a C program to print "GfG" repeatedly without using loop, recursion and any control structure?
- How to print a number 100 times without using loop and recursion in C?
- Print a number 100 times without using loop, recursion and macro expansion in C?
- Print system time in C++ (3 different ways)
- How will you print numbers from 1 to 100 without using loop?
- Write a C program to print "Geeks for Geeks" without using a semicolon
- Print "Even" or "Odd" without using conditional statement
- Print individual digits as words without using if or switch