C Language | Arrays 3

Single Dimension Numeric Array

Syntax:
datatype arrayname[size];

example:
int a[10];
float b[10];

On the execution of the above statement 10 memory locations are created. (Memory locations equal to the specified size are created in the memory.)

All the memory locations occupy continuous memory.

All memory locations store similar types of values, the same as that of the array. That is if the data type of the array is “int” then all the memory locations can store “int” values and if the data type of the array is “float” then all the memory locations can store “float” values. And so on.

Memory locations created in the memory are numbered sequentially starting from 0(zero) i.e. the first memory location is numbered 0(zero), the second one is numbered 1(one), and so on. The last memory location is numbered one less than the size of the array.

Question:1
C program to take input for 10 elements using an array. Further display all the elements.
Sol:

#include<stdio.h>

int main()
{
    int a[10],i;
    /* input */
    for(i=0;i<10;i++)
    {
        printf("Enter %d element ",i);
        scanf("%d",&a[i]);
    }
    /* display */
    for(i=0;i<10;i++)
    {
        printf("%d\n",a[i]);
    }

    return(0);
}
/* Output */
Enter 0 element 10
Enter 1 element 25
Enter 2 element 3
Enter 3 element 6
Enter 4 element 4
Enter 5 element 52
Enter 6 element 63
Enter 7 element 25
Enter 8 element 45
Enter 9 element 6
10
25
3
6
4
52
63
25
45
6

Questions:2
C program to take input for 10 elements using an array, display all the elements also calculate and print the sum of all the elements.
Sol:

#include<stdio.h>

int main()
{
    int a[10],i,s=0;
    /* input */
    for(i=0;i<10;i++)
    {
        printf("Enter %d element ",i);
        scanf("%d",&a[i]);
    }
    /* display */
    for(i=0;i<10;i++)
    {
        printf("%d\n",a[i]);
        s=s+a[i];
    }
    printf("Sum = %d\n",s);

    return(0);
}
/* Output */
Enter 0 element 10
Enter 1 element 20
Enter 2 element 30
Enter 3 element 40
Enter 4 element 50
Enter 5 element 60
Enter 6 element 70
Enter 7 element 80
Enter 8 element 90
Enter 9 element 100
10
20
30
40
50
60
70
80
90
100
Sum = 550

Questions:3
C program to take input for 5 elements using an array, display all the elements, and also print product of all the elements.
Sol:

#include<stdio.h>

int main()
{
    int a[5],i,p=1;
    /* input */
    for(i=0;i<5;i++)
    {
        printf("Enter %d element ",i);
        scanf("%d",&a[i]);
    }
    /* display */
    for(i=0;i<5;i++)
    {
        printf("%d\n",a[i]);
        p=p*a[i];
    }
    printf("Product = %d\n",p);

    return(0);
}
/* Output */
Enter 0 element 1
Enter 1 element 2
Enter 2 element 3
Enter 3 element 4
Enter 4 element 5
1
2
3
4
5
Product = 120

C Language Programming Tutorial

C Language Tutorial Home     Introduction to C Language     Tokens     If Condition      goto statement and Labelname     Switch Statements     For loop     While Loop     Do while loop     break and continue     Functions     Recursion     Inbuild Functions     Storage Classes     Preprocessor     Arrays     Pointers     Structures and Unions     File Handling     Projects