Natalie Natalie - 1 month ago 6
C Question

a1,a2...a10 equating to each element and counting elements

I want to make below stuff in a loop.
I want to make variable name a1,a2 ... to a10

In this each element equal to 1,2,3 to 10.

ie.. a1 = 1,a2 =2 ...a10 =10;

and i want to count 1 to 10 . Size of array gonna be 10 .

How to make this program in loop?

#include <stdio.h>
int main(){
long long n;
int count = 0;

for( i = 1; i <10 ; i++ )
{
n /= 10;
++count;
}

printf("Number of digits: %d", count);


}

Upper code doesn't work for a1 ,a2 to a10 .
How to make variable a + i ? that represent a1 a2 ..a10 euqal to to number
a + i = i; ??


I try to embed a picture here, it says I can't use picture .

Here is link : Kindly click here to see output i want

Kindly let me know, if question is not clear ?

Answer

Try this,

#include <stdio.h>
int main(){
long n;
int count = 0;
int a[10],i;

for( i = 0; i <10 ; i++ )
{
    n /= 10;
    ++count;
    a[i]=count;
    printf("a[%d]= %d\n",i+1,a[i]);

}

printf("Number of digits: %d", count);
}

enter image description here

Update :

 #include <stdio.h>
    int main(){
    long n;
    int count = 0;
    int a[10],i;

    for( i = 0; i < 10 ; i++ )
    {
        n /= 10;
        ++count;
        a[i]=i+1;
        printf("a[%d]= %d\n",i+1,a[i]);

    }

    printf("Number of digits: %d", count);
    }
Comments