﻿ C exercises and solutions: C array print a table of numbers

# C exercises and solutions: C array print a table of numbers

## C array: print a table of numbers exercise

By using two-dimensional array, write C program to display a table of numbers as shown below:

 1 2 3 4 5 6 7 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25

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

int main()
{

int tArr;
int i,j;

for(i=0;i<5;i++) //assign values to the two-dimensional array
for(j=0;j<=5;j++){
if(i==0) tArr[i][j]=j+1; //fill the first
if(i>0 && j==0)

tArr[i][j]=tArr[i-1]+1; //fetching the value of the last cell in the previous row
else
tArr[i][j]=tArr[i][j-1]+1; //fill subsequent cells
}

for(i=0;i<5;i++){ //print the array
for(j=0;j<5;j++)
printf("%d\t",tArr[i][j]);
printf("\n");
}
getch();
return 0;

} Code hunter if(i>0 && j==0) tArr[i][j]=tArr[i-1]+1; //fetching the value of the last cell in the previous row this is can not working condition. it give u a garbage output. you must use if..else if condition. else if(i>0 && j==0) tArr[i][j]=tArr[i-1]+1; its the right condition. Thank u 2016-08-28 xiaocould just add 5 to each of the element in the previous row and form the current row2016-05-22