C program to reverse an array
C program to
reverse an array
Here the program is
given after running on the computer. The program given here is explained in a
very easy way for you to understand.
Example of reverse an array,
INPUT: 10
20 30 40 50 60
OUTPUT: 60
50 40 30 20 10
Program:
#include
<stdio.h>
#include
<conio.h>
void main
()
{
int a[20] ,
temp[20], I, j, n;
clrscr ();
printf (“
Enter the number of elements in array - - - -“);
scanf (“%d”,
&n);
printf (“ -
- - - Enter %d numbers separated by space - - - -\n”, n );
for (i=0;
i<n; i++)
{
scanf (“ %d”,
&a[i]);
}
for (i=n-1,
j=0; i>=0; i--, j++)
temp[j] =
a[i];
for 9i=0;
i<n; i++)
a[i] =
temp[i];
printf (“Numbers
in reverse order are - - - - \n”);
for 9i=0;
i<n; i++)
printf (“%d\t”,
a[i]);
getch ();
}
OUTPUT
Enter how
many numbers (<=50) - - - - 6
- - - -
Enter 6 numbers separated by space - - - -
10 20 30 40
50 60
Numbers in
reverse order are - - - -
60 50
40 30 20
10
Explanation :
In this
program, we first enter n numbers, where n is entered by the user. For this, we
use a for loop to enter the value of n different numbers. Then to print their
values in reverse order, we take a for loop for (i=n-1, j=0; i>=0; i--, j++)
to copy element into array temp string from end of array a. We then take
another for loop to copy the reversed array back to the original array.
Here is some another programs of
array that all programmer should know.
Program:
C program to
enter the value of five numbers and then print their values.
#include
<stdio.h>
#include
<conio.h>
void main
()
{
int i,
a[5];
clrscr ();
printf (“Enter
5 numbers - - - - -\n”);
for (i=0;
i<5; i++)
{
printf (“Enter
number a[%d] :”, i);
scanf (“%d”,
&a[i]);
}
printf (“Numbers
entered are - - - - -“);
for (i=0;
i<5; i++)
printf (“\n\tElement
a[%d] is : %d”, i, a[i] );
getch ();
}
OUTPUT
Enter 5
numbers - - - - -
Enter
number a[0] : 10
Enter
number a[1] : 20
Enter
number a[2] : 30
Enter
number a[3] : 40
Enter
number a[4] : 50
Numbers
entered are - - - - -
Enter
number a[0] : 10
Enter
number a[1] : 20
Enter
number a[2] : 30
Enter
number a[3] : 40
Enter
number a[4] : 50
Explanation:
In this program, we enter five numbers and
print their values. For this , we need to process each element. Processing each
element is known as traversing an array. This can simply be done by using a for
loop or any other looping statement. In this program, taking for loop, we enter
the values of 5 different numbers and then print out their values using printf
statement. The number of passes through the loop will therefore equal to number
of array elements to be processed.
A program to
print the number of days from start of year to specified date (for non- leap year)
#include
<stdio.h>
#include
<conio.h>
void main
()
{
int I;
int month,
day, t_day;
int
d_per_mo[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
clrscr ();
printf (“Enter
month (1 to 12) :”);
scanf (“Enter
day 91 to 31):”);
scanf (“%d”,
&day);
t_day =
day;
for (i=0;
i<month-1; i++)
t_day +=
d_per_mo[i];
printf (“Total
days passed in year till %d/%d = %d”, day, month, t_day);
getch ();
}
OUTPUT
Enter month (1 to 12) : 4
Enter month (1 to 12) : 4
Enter day (
1 to 31): 22
Total days
passed in year till 22/4 = 112
Explanation:
Suppose we
enter month = 4 and day = 22 then t_day = 112
for i=0;
t_day = 22+d_per_month[0]=22+31=53
for i=1;
t_day = 53+d_per_month[1]=53+28=81
for i=2;
t_day = 81+d_per_month[2]=81+31=112
for i=3;
month – 1 = 3 so 3<3 is false and we exit the loop.
Leave a Comment