C program to read values in an array and display it in reverse order
Introduction:
Arrays are a fundamental data structure in programming, used to store a collection of values of the same data type. In C programming language, arrays are used extensively in many applications. One common task when working with arrays is to read values into an array and display them in reverse order. In this article, we will discuss how to use C program to read values into an array and display them in reverse order.
Body:
To start with, we will create an array and prompt the user to enter values into the array. We will use a loop to read values into the array, and then another loop to display the values in reverse order. There are different approaches to displaying the values in reverse order, but in this article, we will be using a simple loop that starts at the last element of the array and moves backwards to the first element.
In C programming, arrays are indexed starting from zero. Therefore, the last element in the array is always the length of the array minus one. We will use this knowledge to loop through the array in reverse order and display the elements to the user.
Coding:
#include<stdio.h>
#include<conio.h>
main()
{
int num[10],i;
printf("Enter number\n");
for(i=0;i<10;i++)
{
scanf("%d",&num[i]);
}
printf("\nThe number in reverse order are:\n");
for(i=9;i>=0;i--)
{
printf("%d\n",num[i]);
}
getch();
}
Output of the program:
#include<conio.h>
main()
{
int num[100],i,n;
printf("Enter how many array elements\n");
scanf("%d",&n);
printf("entern number\n");
for(i=0;i<n;i++)
{
scanf("%d",&num[i]);
}
printf("\nThe number in reverse order are:\n");
for(i=n;i>=0;i--)
{
printf("%d\n",num[i]);
}
getch();
}
Output of the program:
Note: In above program elements of array are entered by the user according their choice and on the basis of that number are stored and display them in reverse order.
Conclusion:
In conclusion, C programming language is a powerful tool for building a wide range of applications. Arrays are a fundamental data structure in C, and knowing how to read values into an array and display them in reverse order is a useful skill. In this article, we have discussed how to use C program to read values into an array and display them in reverse order. With a basic understanding of C programming, you can build upon this program and create more complex applications that use arrays to store and manipulate data.
No comments:
Post a Comment