Program to Reverse a Number

REVERSE A NUMBER:

This program takes an integer input from the user, reverses it, and then prints the reversed number.

For example, if the user enters 123, the program will print 321.


PROGRAM:

 #include <stdio.h>

#include <string.h>

int main()

{

    int n,rem,rev=0;

    printf("Enter a number: ");

    scanf("%d",&n);

    while(n!=0)

    {

        rem=n%10;

        rev=rev*10+rem;

        n=n/10;

    }

    printf("Reverse of the number is: %d",rev);

}


TIME COMPLEXITY:

The time complexity is indeed O(log n) because the while loop iterates through each digit in the input number, and the number of digits is proportional to the logarithm of the number. 

SPACE COMPLEXITY:

The program uses a constant amount of extra space to store the variables rem, rev, and n, which doesn't depend on the size of the input number. Therefore, the space complexity is O(1), indicating a constant amount of extra space used.


OUTPUT:



Comments

Popular posts from this blog

Swap two numbers without using third variable

Program to check leap year

Check a number is palindrome or not