Q:

C programming with arrays

This was the question
int num,square;

int array1[100];
int array2[100];

for(int i=0;i<100;i++){
    printf("please enter the number %d:",i+1);
    scanf("%d",&num);
    if(num==-1){
        break;
    }
    else
        array1[i]=num;
}

for(int j=0;j<100;j++){
    square=array1[j]*array1[j];
    array2[j]=square;
}

for(int m=0;m<100;m++){
    printf("number %d is %d\n",m,array2[m]);
} 

this was the program i made but then the user might input only few int so i want to run the loop which prints the second array only the amount of tym the user inputted how can i do that?

A:

I have updated your code.
int num,square;

int array1[100];
int array2[100];
int i=0;

for(i=0;i<100;i++){
    printf("please enter the number %d:",i+1);
    scanf("%d",&num);
    if(num==-1){
        break;
    }
    else
        array1[i]=num;
}
/* This will only calculate square upto the numbers you have entered
   I have changed the condition to <i
 */
for(int j=0;j < i;j++){
    square=array1[j]*array1[j];
    array2[j]=square;
}
/* This will only print upto the numbers you have entered
   I have changed the condition to <i
 */
for(int m=0;m<i;m++){
    printf("number %d is %d\n",m,array2[m]);
} 

