C

Reverse a Number in C using For Loop

In this tutorial, we are going to see how to reverse a number in C using For loop. The following C program reverses the number entered by the user, and then displays the reversed number on the screen.

For example, if the user enters 123 as input, 321 is displayed as output. We use the modulo operator (%) in the program to get the digits of a number.

There are four ways to reverse a number in C, by using for loop, while loop, recursion, or by creating a function.
100-multiple-choice-questions-in-c-programming100 Multiple Choice Questions In C Programming – Part 1This collection of 100 Multiple Choice Questions and Answers (MCQs) In C Programming : Quizzes & Practice Tests with Answer focuses on “C Programming”.  …Read More

 

Reverse a Number in C using For Loop
#include <stdio.h>
 
int main()
{
   int remainder, reversedNbr = 0, nbr;
 
   printf("Enter a number to reverse: ");
   scanf("%d", &nbr);
   
   for(;nbr!=0; nbr = nbr/10){
		remainder = nbr % 10;
		reversedNbr = reversedNbr * 10 + remainder;
   }
 
   printf("The reversed number is = %d\n", reversedNbr);
 
   return 0;
}

Output:

Enter a number to reverse: 123
The reversed number is = 321

 

mcqMCQPractice competitive and technical Multiple Choice Questions and Answers (MCQs) with simple and logical explanations to prepare for tests and interviews.Read More

Leave a Reply

Your email address will not be published. Required fields are marked *