C Program To Print Elements of Array In Reverse Order

Lets write a c program to print or display the elements of an array in reverse order.

Related Read:
Basics of Arrays: C Program

Note: This is a very simple program but still a very important one, because we’ll be using some form of logic to print elements of an array. So better we know ins and outs of printing array elements in whichever order the program demands. So please pay attention to the logic.

Video Tutorial: C Program To Print Elements of Array In Reverse Order


[youtube https://www.youtube.com/watch?v=bodsp3EAG1I]

YouTube Link: https://www.youtube.com/watch?v=bodsp3EAG1I [Watch the Video In Full Screen.]

Source Code: C Program To Print Elements of Array In Reverse Order

#include<stdio.h>

int main()
{
    int a[5], i;

    printf("Enter 5 integer numbers\n");
    for(i = 0; i < 5; i++)
        scanf("%d", &a[i]);

    printf("Array elements are:\n");
    for(i = 4; i >= 0; i--)
        printf("%d\n", a[i]);

    return 0;
}

Output:
Enter 5 integer numbers
1
2
3
4
5
Array elements are:
5
4
3
2
1

Since the array size is 5, the last index of the array will be (5-1) which is 4. So we initialize i to 4, and keep decrementing the value of i by 1 for each iteration of the for loop. Control exits for loop once i value is equal to 0. In arrays the index starts from 0. Inside for loop, for each iteration, we print the value of i.

#include<stdio.h>

#define N 5

int main()
{
    int a[N], i;

    printf("Enter %d integer numbers\n", N);
    for(i = 0; i < N; i++)
        scanf("%d", &a[i]);

    printf("Array elements are:\n");
    for(i = N-1; i >= 0; i--)
        printf("%d\n", a[i]);

    return 0;
}

Output:
Enter 5 integer numbers
1
2
3
4
5
Array elements are:
5
4
3
2
1

Here we initialize value of i to the last index of the array, which is N-1. We iterate through the for loop until i value is 0(which is the first index of the array), for each iteration of the for loop we decrement the value of i by 1. Inside for loop we print the value of a[i].

For list of all c programming interviews / viva question and answers visit: C Programming Interview / Viva Q&A List

For full C programming language free video tutorial list visit:C Programming: Beginner To Advance To Expert

C Program to Print Natural Numbers from 1 to N In Reverse Order using for loop

Lets write a simple C program to print natural numbers from 1 to N in reverse order, using for loop.

Related Read:
For Loop In C Programming Language
C Program to Print Natural Numbers from 1 to N using for loop

C Program to Print Natural Numbers from 1 to N In Reverse Order using for loop


[youtube https://www.youtube.com/watch?v=07rwVW3TVUo]

YouTube Link: https://www.youtube.com/watch?v=07rwVW3TVUo [Watch the Video In Full Screen.]


Source Code: C Program to Print Natural Numbers from 1 to N In Reverse Order using for loop

 
#include<stdio.h>

int main()
{
    int num, count;

    printf("Enter a positive number\n");
    scanf("%d", &num);

    printf("\nNatural numbers from %d to 1 are:\n", num);

    for(count = num; count >= 1; count--)
    {
        printf("%d\n", count);
    }

    return 0;
}

Output:
Enter a positive number
14

Natural numbers from 14 to 1 are:
14
13
12
11
10
9
8
7
6
5
4
3
2
1

Natural numbers from 10 to 1 are:

Logic To Print Natural Numbers from 1 to N In Reverse Order using for loop

We ask the user to enter a positive number, and store it inside a variable num. We assign value of num to variable count. We iterate through the loop until the count is equal to zero. For example, if user enters num = 5, we iterate the loop 5 times. In for loops modification section we keep decrementing the value of variable count. Once the value of count becomes 0, we exit the for loop. For each iteration we print the value of count.

This way we printout the natural numbers from 1 to N, in reverse order.

For list of all c programming interviews / viva question and answers visit: C Programming Interview / Viva Q&A List

For full C programming language free video tutorial list visit:C Programming: Beginner To Advance To Expert

C Program To Print Floyd’s Triangle In Reverse

Lets write C program to print Floyd’s Triangle in reverse, using nested while loop.

Floyd’s Triangle: is a right angled Triangle formed with natural numbers.

Related Read:
while loop in C programming
Nested While Loop: C Program
C Program To Print Floyd’s Triangle

Logic To Print Floyd’s Triangle In Reverse

We ask the user to input the number of rows of Floyd’s Triangle, we store it inside variable num. We assign 1 to variable nn(natural number). Variable num has the number of natural numbers to be printed in particular row. The inner while loop prints the natural numbers upto num.

For Example, if user enters num = 5, the following Triangle will be printed:

1 2 3 4 5
6 7 8 9
10 11 12
13 14
15

Note that the Triangle printed is a right angled Triangle and has 5 rows of natural numbers.

Source Code: C Program To Print Floyd’s Triangle In Reverse

#include < stdio.h >

int main()
{
    int num, nn = 1, count;

    printf("Enter no of rows of Floyd's Triangle\n");
    scanf("%d", &num);
    
    printf("\n");
    while(num)
    {
        count = 1;
        while(count <= num)
        {
            printf("%d  ", nn);
            nn++;
            count++;
        }
        printf("\n");
        num--;
    }

    return 0;
}

Output 1:
Enter no of rows of Floyd’s Triangle
5

1 2 3 4 5
6 7 8 9
10 11 12
13 14
15

Output 2:
Enter no of rows of Floyd’s Triangle
14

1 2 3 4 5 6 7 8 9 10 11 12 13 14
15 16 17 18 19 20 21 22 23 24 25 26 27
28 29 30 31 32 33 34 35 36 37 38 39
40 41 42 43 44 45 46 47 48 49 50
51 52 53 54 55 56 57 58 59 60
61 62 63 64 65 66 67 68 69
70 71 72 73 74 75 76 77
78 79 80 81 82 83 84
85 86 87 88 89 90
91 92 93 94 95
96 97 98 99
100 101 102
103 104
105

Video Tutorial: C Program To Print Floyd’s Triangle In Reverse


[youtube https://www.youtube.com/watch?v=7CTZGsGm6yE]

YouTube Link: https://www.youtube.com/watch?v=7CTZGsGm6yE [Watch the Video In Full Screen.]

For list of all c programming interviews / viva question and answers visit: C Programming Interview / Viva Q&A List

For full C programming language free video tutorial list visit:C Programming: Beginner To Advance To Expert

C Program to Print Natural Numbers from 1 to N In Reverse Order using While loop

Lets write a simple C program to print natural numbers from 1 to N in reverse order, using while loop.

Related Read:
while loop in C programming
C Program to Print Natural Numbers from 1 to N using While loop

Source Code: C Program to Print Natural Numbers from 1 to N In Reverse Order using While loop

 
#include < stdio.h >

int main()
{
    int num;

    printf("Enter a positive number\n");
    scanf("%d", &num);

    printf("\nNatural numbers from 1 to %d are:\n", num);

    while(num)
    {
        printf("%d  ", num);
        num--;
    }

    printf("\n");

    return 0;
}

Output 1:
Enter a positive number
10

Natural numbers from 1 to 10 are:
10 9 8 7 6 5 4 3 2 1

Output 2:
Enter a positive number
14

Natural numbers from 1 to 14 are:
14 13 12 11 10 9 8 7 6 5 4 3 2 1

C Program to Print Natural Numbers from 1 to N In Reverse Order using While loop


[youtube https://www.youtube.com/watch?v=RDh7zjoLYNg]

YouTube Link: https://www.youtube.com/watch?v=RDh7zjoLYNg [Watch the Video In Full Screen.]


Logic To Print Natural Numbers from 1 to N In Reverse Order using While loop

We ask the user to enter a positive number, and store it inside a variable num. We iterate through the loop until the number is equal to zero. For example, if user enters num = 5, we iterate the loop 5 times. Inside the while loop we keep decrementing the value of variable num. Once the value of num becomes 0, we exit the loop. For each iteration we print the value of num.

This way we printout all the natural numbers from 1 to N, in reverse order.

For list of all c programming interviews / viva question and answers visit: C Programming Interview / Viva Q&A List

For full C programming language free video tutorial list visit:C Programming: Beginner To Advance To Expert

Reverse Given Number And Check For Palindrome: C++

Cpp program to read a number, reverse the given number and check whether it is palindrome or not.

Full Source code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include< iostream .h>
#include< conio .h>
 
void main()
{
  int num, rem, sum = 0, temp;
  clrscr();
 
  cout< <"Enter a number\n";
  cin>>num;
 
  temp = num;
 
  while( num )
  {
    rem = num % 10;
    num = num / 10;
    sum = sum * 10 + rem;
  }
 
cout< <"\nReversed Number: "<<sum<<endl;
 
  if( temp == sum )
   cout<<temp<<" is a palindrome";
  else
   cout<<temp<<" in NOT a palindrome";
 
  getch();
}

In this program we assign variable sum = 0 to avoid garbage values in sum before the calculation, which would result in wrong output.
We store the user entered value in temp, so that we can compare it with the final result, to determine whether the given number is palindrome or not.

1
2
3
4
5
6
 while( num )
  {
    rem = num % 10;
    num = num / 10;
    sum = sum * 10 + rem;
  }

Here the loop executes until the value of num is zero.

If user enters 301, we apply the modulus to get the individual values.
Ex:
301 % 10 = 1
30 % 10 = 0
3 % 10 = 3

We get 301, 30 and 3 by dividing the original value by 10.
Ex:
301 user entered value.
301 / 10 = 30
30 / 10 = 3

Now the sum.

sum = sum * 10 + rem;

1 = 0 * 10 + 1
10 = 1 * 10 + 0
103 = 10 * 10 + 3

So the reverse of 301 is 103, which is not a palindrome.

Video Tutorial: Reverse Given Number And Check For Palindrome: C++


[youtube https://www.youtube.com/watch?v=j8Vv4wgdwoA]

YouTube Link: https://www.youtube.com/watch?v=j8Vv4wgdwoA [Watch the Video In Full Screen.]



Output:
Enter a number
301
Reversed Number: 103
301 in NOT a palindrome