Home > Software engineering >  how to print 1 2 3 4 in double digits in c program
how to print 1 2 3 4 in double digits in c program

Time:02-11

I want to print a 4x4 matrix starting from 1 to 16 and printing just 1, 2, 3, and 4 are simply ruining the aesthetic of the output, I want it to be symmetric so please tell me how can I print 01, 02, 03 instead of 1, 2, 3

CodePudding user response:

Check iomanip

#include <iostream>
#include <iomanip>

int main() {
    const size_t N = 3;
    int mat[N][N] = {
        {1, 2, 3},
        {4, 5, 6},
        {7, 8, 9}
    };
    
    for (size_t i = 0; i < N;   i) {
        for (size_t j = 0; j < N;   j) {
            std::cout << std::setw(2) << std::setfill('0') << mat[i][j] << ' ';
        }
        std::cout << std::endl;
    }
    return 0;
}

CodePudding user response:

This code would do, what you asked:

#include <iostream>


int main()
{
    for(int i=0; i<4; i  ){
        for(int j=0; j<4; j  ){
            printf("d ", 4*i j 1);
        }
        printf("\n");    
    }

    return 0;
}

You´ll get this output:

01 02 03 04 
05 06 07 08 
09 10 11 12 
13 14 15 16
  • Related