Home > Backend Development > C++ > body text

Use C language division and modulo operators to print numbers in reverse order

PHPz
Release: 2023-08-27 12:53:05
forward
1236 people have browsed it

Use C language division and modulo operators to print numbers in reverse order

Problem

How to print the given two-digit number in reverse order with the help of division and Modulo operator using C programming language?

Solution

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
Copy after login

Then print the first number followed by the second number and then you will get the reverse number of the given number.

Program 1

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;
}
Copy after login

Output

enter a number:45
After reversing =54
Copy after login

Program 2

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;
}
Copy after login

Output

enter a number:479
After reversing =974
Copy after login

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!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template