Home / Programs / Reverse a Number using Command Line Argument
Programming Example

Reverse a Number using Command Line Argument

👁 3,217 Views
💻 Practical Program
📘 Step by Step Learning
Write a C program which will reverse all the digits of a Number using Command Line Arguments to reverse the digits of a number

Program Code

#include<stdio.h>
#include<stdlib.h>

int main(int argc, char *argv[])
{
    if(argc==1)
    {
        printf("No Arguments");
        return 0;
    }
    else
    {
        int n,reverseNumber,temp,rem;
        n=atoi(argv[1]);
        temp=n;
        reverseNumber=0;

    while(temp)
    {
        rem=temp%10;
        reverseNumber=reverseNumber*10+rem;
        temp=temp/10;
    }
    
    printf("%d",reverseNumber);
    return 0;
}
}

Output

123

321

Explanation

no

How to learn from this program

First read the algorithm, then study the program code line by line. After that, compare the code with the output and finally go through the explanation. This approach helps learners understand both the logic and the implementation properly.