#include<stdio.h>
#include<conio.h>

void main()
{
 int a[20],i;
 clrscr();
 printf("enter 20 numbers: ");
 for(i=0;i<20;i++)
    scanf("%d",&a[i]);
 for(i=0;i<20;i++)
    printf("%d\n",a[i]);

 getch();
}

Dani AI

Generated

Good points from and — they showed row-splitting and using fixed field widths. If the goal is specifically "arrange by column" (fill down each column, then move to the next column), use a transposition-style index map instead of printing the array in natural order.

Compute columns C (how many side-by-side), then rows R = (n + C - 1) / C. The element that should appear at column c and row r is at index idx = c * R + r (skip if idx >= n). To keep columns aligned, first scan each column to find the maximum printed width for that column, then print row by row using that width.

A compact example (reads 20 ints and prints 4 columns, top-to-bottom then left-to-right):

#include <stdio.h>
#include <stdlib.h>

void print_columns(const int *a, int n, int cols) {
    int rows = (n + cols - 1) / cols;
    int *width = malloc(cols * sizeof *width);
    char buf[64];
    for (int c = 0; c < cols; ++c) width[c] = 0;
    for (int c = 0; c < cols; ++c)
        for (int r = 0; r < rows; ++r) {
            int idx = c * rows + r;
            if (idx >= n) break;
            int len = snprintf(buf, sizeof buf, "%d", a[idx]);
            if (len > width[c]) width[c] = len;
        }
    for (int r = 0; r < rows; ++r) {
        for (int c = 0; c < cols; ++c) {
            int idx = c * rows + r;
            if (idx < n) printf("%*d", width[c], a[idx]);
            else printf("%*s", width[c], "");
            if (c + 1 < cols) putchar(' ');
        }
        putchar('\n');
    }
    free(width);
}

int main(void) {
    int a[20];
    for (int i = 0; i < 20; ++i) if (scanf("%d", &a[i]) != 1) return 1;
    print_columns(a, 20, 4);
    return 0;
}

Notes: pick cols so the total width fits your terminal. If you need balanced column sizes (some algorithms put fewer items in the last column), compute per-column counts first instead of using a fixed rows. Avoid nonstandard headers like conio.h for portable code; if your compiler lacks snprintf, use sprintf into a buffer and strlen to measure width.

Recommended Answers

All 5 Replies

Hi,
use int for main() and return 0. Then you could try if and else to break up a long print out like so:

#include<stdio.h>
#include<conio.h>

int main() {
    int a [20], i;
    int count = 0;
      //clrscr ();
      printf("enter 20 numbers:");
      for ( i = 0 ; i < 20 ; i++ ) 
        scanf( "%d", &a[i] );

      for ( i = 0 ; i < 20 ; ++i ) {
        if ( ( ++count %5 ) == 0 ) {
            printf( " %d\n", a [i] );
        }
        else 
        printf( "%d ", a [i] );
    }
    getch();

      return 0;
  }

Make your printf() format, occupy 80/number of columns you want to have including spaces between columns.

If I want 4 columns, I have my printf() format use 20 spaces, etc.

#include <stdio.h>

int main(void) {
   int n;

   for(n=0;n<200;n++) {
      printf(" %18d ",n);  //4 columns, each one 18+2 wide.
   }
   printf("\n");
   return 0;
}

ohh i seee thank you for the replies!

how if i want it to be arranged by column? because your code is arranged by rows.

Columnar printing becomes more difficult, because all monitors print up in row order. That means you have to use more than just basic print commands for output.

I haven't seen a program to help with this, but there may be one available. What are you trying to print and what is the width of the columns?

The easy way to do this is to use a large char array to represent your "page", and then rearrange the data in the array, so it's in the column ready format, you want.

For example, you keyboard:
Now is the time for all good men to come to the aid of their country, which when rearranged, becomes:

Now is the
time for all
good men to
come to the
aid of their
country.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.