Saturday, January 4, 2014

C Program to Keep Several Data in an Array and Later We'll Print them One by One

Objectives:
-to Learn the use of array
-to Learn to keep several data in an array
-to Learn how to target a data from an array to show

The C Program given below will ask you to provide three data one after another. Then the data will be saved into an array. Later we will show the data from the array.

#include<stdio.h>
void main()
{
int a[2];
printf("Enter the First Data: ");
scanf("%d",&a[0]);
printf("\nEnter the Second Data: ");
scanf("%d",&a[1]);
printf("\nEnter the Third Data: ");
scanf("%d",&a[2]);
printf("The First Data is: %d",a[0]);
printf("\nThe Second Data is: %d",a[1]);
printf("\nThe Third Data is: %d",a[2]);
}
C Program,use of array,target a data from an array
Description of the Codes
Line 04: int a[2];
This is the fourth line of the program. There is declared an integer type array by int a[2];. a[2] is an array where we may keep three data. If we use a[9] then we may keep ten data into the array.

Line 06: scanf("%d",&a[0]);
We will keep the first data by the line scanf("%d",&a[0]);. Similarly we will keep the second and third data by the lines scanf("%d",&a[1]); and scanf("%d",&a[2]);

Line 11: printf("The First Data is: %d",a[0]);
This is the eleventh line of the program, we have to use to print the data kept in a[0]. Similarly we have to use printf("\nThe Second Data is: %d",a[1]); and printf("\nThe Third Data is: %d",a[2]); to print the data kept in a[1] and a[2].

The rest lines are described in the previous posts. You may read them to understand this program properly.


No comments:

Post a Comment