Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Is it possible to add variables with different names in a for loop in C?

I had something like this in mind but I don’t know how to implement it.

As i’m a beginner (on the CS50 course), i don’t really understand other answers I have came upon.

Thanks for your help!

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

for (int i = 1; i < 16; i++)
{
    int Ai= number % (10 * i);
}

>Solution :

No, you cannot do this, but you can create an array and append the values to the array. For example:

#include <stdio.h>

int A[16];
for (int i = 1; i < 15; i++)
{
    //The reason for using i-1 as the index of the array is that i starts as 1, not 0 (array indices start at 0).
    A[i - 1] = number % (10 * i);
}

//The code below only prints the array.
for (int i = 0; i < 16; i += 1){
    printf("A%d: %d\n", i+1, arr[i]);
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading