Originally Posted by dwise1_aol
With your general lack of naming conventions, it's difficult to keep track of what variable does what, so you might be confusing yourself.
There's an old tradition from FORTRAN about naming loop-control variables which probably dated from 1966, a decade after the creation of FORTRAN. FORTRAN allowed you to have either integer (AKA "INTEGER") or floating-point (AKA "REAL") numeric variables, plus it would create the variable automatically as soon as you used it (which led to many stupendous bugs due to misspelling an existing variable). In order to determine whether to make this new variable INTEGER or REAL, FORTRAN compilers would use the first letter of the variable's name: if the name started with I through N then it would be an INTEGER (think of the first two letters of "INTEGER"), otherwise it would be a REAL. From that rule, the custom developed to name loop variables I, J, K, L, M, N, with the outer-most loop being I, the next one in J, the third one in K, etc.
This practice has spread to just about every programming language I've encountered, even C, so when any programmer encounters variables i, j, k, etc, he immediately recognizes them as very likely being loop variables. Similarly, every programmer will immediately declare his loop variables by those names without giving it a second thought.
You have two loops, so why not name those two loop variables i and j? You need to control the number of columns, so why not use c for that, incrementing c with each line to increase the number of columns by one each time? What you want to print out increases by one each time it's printed out regardless of which line or column it's in, so why not initialize a variable to 1 and just increment it each time? n is already taken, as are j and c, but i doesn't really serve any purpose. Or you could use a different variable name, use i to control the looping through the columns, and eliminate j.
In case you haven't figured out yet why you're getting that wrong output, you're printing out c, but you reinitialize c to 1 for each line, which you need to do if you're using it to control the number of columns printed (which you are doing), but not if that's the value to be printed.
So think about what each variable is supposed to do and what meaningful names you can give them.
Code:
#include<stdio.h>
int main()
{
int i,j,c,n,a;
printf("enter a number\n");
scanf("%d", &n);
i=1;
a=1;
while(i<=n)
{
j=1;
while(j<=i)
{
printf("%d ", a);
a++;
j++;
}
printf("\n");
i++;
}
}