Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • How to reverse a number without using % operator?

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 592
    Comment on it
    #include<string.h>
    
    int main() {
       int number1, number2;
       char str[10];
    
       printf("\nEnter a Number:::");
       scanf("%d", &number1);
    
       sprintf(str, "%d", number1);//step 1
       strrev(str);//step 2
       number2 = atoi(str);//step 3
    
       printf("\n Reverse of the number:::");
       printf("%d", &number2);
    
       return 0;
    }
    

    OUTPUT:

    Enter a Number ::: 1234
    Reverse of the number::: 4321
    

    Explaination of above code:-

    1. Storing the number into Form of String:-

      sprintf() function sends formatted "number1" to string variable "str".

    2. Using strrev() function to reverse the string "str"

      eg 1234 will be reversed as 4321

    3. Convert String to Number using atio() function:

      atoi() stands for "Alphabet to Integer". This function converts "String to Integer".

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: