Home >Backend Development >C++ >Use C language division and modulo operators to print numbers in reverse order
How to print the given two-digit number in reverse order with the help of division and Modulo operator using C programming language?
So far, we had seen how to reverse the string using string function and without string function. Now let's see how to reverse the two-digit number without using the predefined function.
The logic we use to reverse the number with the help of operators is −
int firstno=number%10; //stores remainder int secondno=number/10;// stores quotient
Then print the first number followed by the second number and then you will get the reverse number of the given number.
In this example, we will take a two-digit number and apply the division and modulo operators to reverse the number −
#include<stdio.h> int main(){ int number; printf("enter a number:"); scanf("%4d",&number); int firstno=number%10; //stores remainder int secondno=number/10;// stores quotient printf("After reversing =%d%d</p><p>",firstno,secondno); return 0; }
enter a number:45 After reversing =54
In this example we will take a 3 digit number and apply division and Use the modulo operator to reverse the number −
Online Demo
#include<stdio.h> int main(){ int number,num1,num2,num3,result; printf("enter a number:"); scanf("%4d",&number); num1 = number / 100; num2 = (number % 100) / 10; num3 = number%10 ; result = 100*num3 + 10*num2 + num1; printf("After reversing =%d</p><p>",result); return 0; }
enter a number:479 After reversing =974
The above is the detailed content of Use C language division and modulo operators to print numbers in reverse order. For more information, please follow other related articles on the PHP Chinese website!