Option 1: Reverse Number Program in C Using while Loop
#include
<stdio.h>
int main() {
int num, reverse = 0, remainder;
printf("Enter a number: ");
scanf("%d", &num);
// Loop runs until num becomes 0
while (num != 0) {
// Extract the last digit of the number
remainder = num % 10;
// Add the current last digit into the
reverse number
reverse = reverse * 10 + remainder;
num /= 10;
}
printf("Reversed number: %d\n",
reverse);
return 0;
}
Run Code
Output:
Enter a
number: 564
Reversed
number: 465
Explanation:
This C code
to reverse a number uses a while loop. It extracts each digit of the number
using the modulus operator (% 10) and appends it to the reversed number. The
original number is reduced by dividing it by 10 in each iteration.
This logic
is repeated until all digits are reversed. The reverse number program in C
using while loop efficiently handles integer inputs, providing a foundational
example of number manipulation.
You can
perform reverse operations on different types of data structures, and to understand
how it works in each case, you can follow the programs below:
C Program to
Reverse a Linked List
C Program to
Reverse a String
C Program to
Reverse an Array
If you want
more practice programs in C, you can explore our list of C programs.
Time
Complexity Analysis
Time
Complexity : O(d)
Space
Complexity Analysis
Space
Complexity : O(1)
Option 2: C
Program to Reverse a Number Using for Loop
#include
<stdio.h>
int main() {
int num, reverse = 0, remainder;
printf("Enter a number: ");
scanf("%d", &num);
// Reverse the number using for loop
for (; num != 0; num /= 10) {
remainder = num % 10;
reverse = reverse * 10 + remainder;
}
printf("Reversed number: %d\n",
reverse);
return 0;
}
Run Code
Output:
Enter a
number: 8321
Reversed
number: 1238
Explanation:
This C
program to reverse a number using for loop uses the modulus operator (%) to
extract the last digit of the number, which is appended to the reverse
variable. In each iteration of the loop, the number is reduced by dividing it
by 10 (num /= 10), effectively removing the last digit.
The loop
continues until the number becomes 0, ensuring all digits are processed. This
approach is simple and efficient, making it ideal for reversing positive
integers.
Time
Complexity Analysis
Time
Complexity : O(d)
Space
Complexity Analysis
Space Complexity
: O(1)
Option 3: C
Program to Reverse a Number Using Function
#include
<stdio.h>
// Function
returns the reverse
// of the
number
int
reverseNumber(int num) {
int reverse = 0, remainder;
while (num != 0) {
remainder = num % 10;
reverse = reverse * 10 + remainder;
num /= 10;
}
return reverse;
}
int main() {
int num;
printf("Enter a number: ");
scanf("%d", &num);
// Call the reverseNumber() function
printf("Reversed number: %d\n",
reverseNumber(num));
return 0;
}
Run Code
Output:
Enter a
number: 321
Reversed
number: 123
Explanation:
This C
program to reverse a number using function moves the reversal logic into a
standalone function reverseNumber. The function takes an integer as input, uses
a while loop to reverse its digits, and returns the reversed number.
By
modularizing the code, the program becomes more reusable and easier to
understand. The main() function only handles user input and output, while the
reversal logic is abstracted away.
This
separation of concerns ensures that the program can be extended or modified
without impacting its core functionality. It’s a simple yet powerful approach
for writing reverse number program in C.
Time
Complexity Analysis
Time
Complexity : O(d)
Space
Complexity Analysis
Space
Complexity : O(1)
Option 4:
Reverse Number Program in C Using Recursion
#include
<stdio.h>
// Recursive
function to reverse a number
int
reverseRecursively(int num, int reverse) {
// Base case:
// If the number becomes 0, return the
// reversed value
if (num == 0)
return reverse;
// Recursive case:
// 1. Extract last digit using (num % 10)
// 2. Add it to reverse after shifting
existing digits (reverse * 10)
// 3. Remove last digit from num using (num
/ 10)
return reverseRecursively(num / 10, reverse
* 10 + num % 10);
}
int main() {
int num;
printf("Enter a number: ");
scanf("%d", &num);
printf("Reversed number: %d\n",
reverseRecursively(num, 0));
return 0;
}
Run Code
Output:
Enter a
number: 213
Reversed
number: 312
Explanation:
This C
program to reverse a number using recursion relies on the concept of breaking
the problem into smaller subproblems. The function reverseRecursively extracts
the last digit of the number using the modulus operator (%) and appends it to
the reverse.
The number
is then reduced (num / 10), and the function calls itself with the updated
values. The recursion ends when the number becomes 0, at which point the
reversed number is returned. This approach is conceptually elegant and avoids
explicit loops.
However, for
large numbers, it may lead to stack overflow due to the recursion depth.
Time
Complexity Analysis
Time
Complexity : O(d)
Space
Complexity Analysis
Space
Complexity : O(1)
Option 5:
Reverse Number Program in C Using String
#include
<stdio.h>
#include
<string.h>
int main() {
// Declare two character arrays:
// 'num' to store input number as string
// 'reverse' to store the reversed result
char num[100], reverse[100];
printf("Enter a number: ");
scanf("%s", num);
// Find the length of the input string
int len = strlen(num);
// Loop through each character of the
string
for (int i = 0; i < len; i++) {
// Copy characters from end of 'num' to
'reverse'
// Example: last character goes to
first position
reverse[i] = num[len - i - 1];
}
// Add null character at the end to make it
a valid string
reverse[len] = '\0';
printf("Reversed number: %s\n",
reverse);
return 0;
}
Run Code
Output:
Enter a
number: 987654321
Reversed
number: 123456789
Explanation:
This
approach treats the number as a string, making it a simple C program to reverse
a number that avoids arithmetic operations. The program first reads the input
as a string using scanf().
The length
of the string is determined using strlen(). A for loop is used to copy
characters from the end of the original string to the beginning of a new string
(reverse). After reversing all characters, the null terminator (\0) is added to
mark the end of the string.
This method
is particularly useful for handling very large numbers or cases where digits
are represented as characters.
Time
Complexity Analysis
Time
Complexity : O(d)
Space
Complexity Analysis
Space
Complexity : O(1)
Option 6:
Reverse Number Program in C Without Using Temporary Variable
#include
<stdio.h>
int main() {
int num, reverse = 0;
printf("Enter a number: ");
scanf("%d", &num);
// Loop runs until the number becomes 0
while (num != 0) {
// Extract the last digit using the
modulus operator
int digit = num % 10;
// Add the digit to reverse after
shifting existing digits
// Example: 12 → 123 (reverse * 10 +
digit)
reverse = reverse * 10 + digit;
// Remove the last digit from the
number
// Example: 123 → 12
num /= 10;
}
printf("Reversed number: %d\n",
reverse);
return 0;
}
Run Code
Output:
Enter a
number: 12345
Reversed
number: 54321
Explanation:
This C
program to reverse a number without using temporary variable eliminates the
need for additional storage by directly updating the reverse variable. Each
digit of the number is appended to reverse in its correct reversed position
using multiplication and addition.
The original
number is reduced by dividing it by 10 in each iteration, ensuring all digits
are processed. This approach is memory-efficient, as no extra variables are
used beyond the essentials.
It’s an
excellent demonstration of how logical and arithmetic operators in C can be
combined to achieve efficient results in C programming.
Time
Complexity Analysis
Time
Complexity : O(d)
Space
Complexity Analysis
Space
Complexity : O(1)
No comments:
Post a Comment