🎓 Top 15 Udemy Courses (80-90% Discount): My Udemy Courses - Ramesh Fadatare — All my Udemy courses are real-time and project oriented courses.
▶️ Subscribe to My YouTube Channel (178K+ subscribers): Java Guides on YouTube
▶️ For AI, ChatGPT, Web, Tech, and Generative AI, subscribe to another channel: Ramesh Fadatare on YouTube
1. Introduction
Reversing a number is a frequent operation in computing. For instance, checking for palindromic numbers requires the number and its reverse to be compared. In this guide, we'll understand how to write a C program that can reverse a given number.
2. Program Overview
Our program will:
1. Ask the user to input a number.
2. Reverse the entered number.
3. Display the reversed number to the user.
3. Code Program
#include <stdio.h> // Integrate the Standard I/O library for necessary input-output functions
int main() { // Program initiation with the main function
int n, reversedNumber = 0, remainder; // Declare the original number, its reversed form, and a variable for the remainder
// Request the user for a number
printf("Enter an integer: ");
scanf("%d", &n); // Read the entered number
while (n != 0) {
remainder = n % 10; // Extract the last digit
reversedNumber = reversedNumber * 10 + remainder; // Add the extracted digit to the reversed number
n /= 10; // Reduce n by one digit
}
printf("Reversed Number = %d\n", reversedNumber); // Display the reversed number
return 0; // Signify successful completion
}
Output:
Enter an integer: 12345 Reversed Number = 54321
4. Step By Step Explanation
1. #include <stdio.h>: This line includes the standard input and output library, allowing us to use input and output functions.
2. int main(): This is where our program starts.
3. Variable Declaration:
- n: Stores the user's number.
- reversedNumber: Will store the reverse of the number.
- remainder: Helps in extracting the last digit from the number.
4. User Input: The program prompts the user to input a number and then captures it in n.
5. The Reversal Process:
- A while loop runs as long as n is not zero.
- Within the loop:
- We use the modulus operation (%) to get the last digit of n and store it in the remainder.
- We multiply the current reversedNumber by 10 and add the remainder to it. This effectively appends the last digit of n to the reverse.
- We then divide n by 10 (using /=) to drop its last digit.
- This process continues until all digits are processed, and we obtain our reversed number.
6. Output: The program then showcases the reversed number.
The strategy utilized here highlights the importance of loop constructs and basic arithmetic operations in solving problems effectively.
My Top and Bestseller Udemy Courses. The sale is going on with a 70 - 80% discount. The discount coupon has been added to each course below:
Build REST APIs with Spring Boot 4, Spring Security 7, and JWT
[NEW] Learn Apache Maven with IntelliJ IDEA and Java 25
ChatGPT + Generative AI + Prompt Engineering for Beginners
Spring 7 and Spring Boot 4 for Beginners (Includes 8 Projects)
Available in Udemy for Business
Building Real-Time REST APIs with Spring Boot - Blog App
Available in Udemy for Business
Building Microservices with Spring Boot and Spring Cloud
Available in Udemy for Business
Java Full-Stack Developer Course with Spring Boot and React JS
Available in Udemy for Business
Build 5 Spring Boot Projects with Java: Line-by-Line Coding
Testing Spring Boot Application with JUnit and Mockito
Available in Udemy for Business
Spring Boot Thymeleaf Real-Time Web Application - Blog App
Available in Udemy for Business
Master Spring Data JPA with Hibernate
Available in Udemy for Business
Spring Boot + Apache Kafka Course - The Practical Guide
Available in Udemy for Business
Comments
Post a Comment
Leave Comment