

{"id":116566,"date":"2023-08-17T19:17:55","date_gmt":"2023-08-17T13:47:55","guid":{"rendered":"https:\/\/data-flair.training\/blogs\/?p=116566"},"modified":"2023-08-17T19:17:29","modified_gmt":"2023-08-17T13:47:29","slug":"c-program-to-reverse-a-number","status":"publish","type":"post","link":"https:\/\/data-flair.training\/blogs\/c-program-to-reverse-a-number\/","title":{"rendered":"C Program to Reverse a Number"},"content":{"rendered":"<p>Reversing a number is a common task in programming that involves reversing the order of its digits. In this article, we will explore how to write a C program to reverse a number, along with the code, results, and alternative approaches to achieve the same result.<\/p>\n<h3>Code to Reverse a Number in C:<\/h3>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">#include &lt;stdio.h&gt;\r\nint reverseNumber(int number) { int reversedNumber = 0;\r\nwhile (number != 0) {\r\nint remainder = number % 10;\r\nreversedNumber = reversedNumber * 10 + remainder;\r\nnumber \/= 10;\r\nreturn reversedNumber;\r\n}\r\nint main() {\r\nint number;\r\nprintf(\"Enter a number: \"); scanf(\"%d\", &amp;number);\r\nint reversedNumber = reverseNumber (number); \r\nprintf(\"Reversed number: %d\\n\", reversedNumber); \r\nreturn 0;}\r\n<\/pre>\n<p><strong>Explanation:<\/strong><\/p>\n<p>1. We define a function `reverseNumber()` that takes an integer as an input and returns the reversed number.<\/p>\n<p>2. Inside the function, we initialize a variable `reversedNumber` to store the reversed number.<\/p>\n<p>3. We use a `while` loop to iterate until the number becomes zero.<\/p>\n<p>4. In each iteration, we find the remainder of the number when divided by 10 using the modulo operator `%`.<\/p>\n<p>5. We then update the `reversedNumber` by multiplying it by 10 and adding the remainder.<\/p>\n<p>6. We divide the number by 10 to remove the last digit in each iteration.<\/p>\n<p>7. Finally, we return the `reversedNumber` once the loop is complete.<\/p>\n<p>8. In the `main()` function, we prompt the user to enter a number, read it using `scanf()`, and store it in the `number` variable.<\/p>\n<p>9. We call the `reverseNumber()` function with `number` as the argument and store the result in `reversedNumber`.<\/p>\n<p>10. We print the reversed number using `printf()`.<\/p>\n<h3>Alternate Approaches:<\/h3>\n<p>While the above code efficiently reverses a number, there are alternative approaches that can achieve the same result. Here are a few such methods:<\/p>\n<h4>Using an Array to reverse a Number in C:<\/h4>\n<p>&#8211; Convert the number into a string or character array.<\/p>\n<p>&#8211; Reverse the order of characters in the array.<\/p>\n<p>&#8211; Convert the reversed array back to an integer.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">#include &lt;stdio.h&gt;\r\n#include &lt;stdlib.h&gt;\r\n#include &lt;string.h&gt;\r\nint reverseNumber(int number) { \r\nchar dataFlair[20];\r\nsprintf(dataFlair, \"%d\", number); \r\nint length = strlen(dataFlair);\r\nfor (int i = 0; i &lt; length \/ 2; i++) \r\n{ char temp = dataFlair[i];\r\n}\r\ndataFlair[i] = dataFlair[length - i - 1]; dataFlair[length - i - 1] = temp;\r\nreturn atoi(dataFlair);\r\n}\r\nint main() {\r\nint number;\r\nprintf(\"Enter a number: \"); scanf(\"%d\", &amp;number);\r\nint reversedNumber = reverseNumber (number); \r\nprintf(\"Reversed number: %d\\n\", reversedNumber); \r\nreturn 0;}\r\n<\/pre>\n<h4>C Program to reverse a number Using Recursion:<\/h4>\n<p>&#8211; Divide the number by ten recursively until it becomes zero.<\/p>\n<p>&#8211; In each recursive call, extract the last digit of the number.<\/p>\n<p>&#8211; Concatenate the digits to form the reversed number.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">#include &lt;stdio.h&gt;\r\n#include &lt;stdlib.h&gt;\r\n#include &lt;string.h&gt;\r\nint reverseNumber(int number) { char dataFlair[20];\r\nprintf(dataFlair, \"%d\", number); \r\nint length = strlen(dataFlair);\r\nfor (int i = 0; i &lt; length \/ 2; i++) \r\n{ char temp = dataFlair[i];\r\n}\r\ndataFlair[i] = dataFlair[length - i - 1]; \r\ndataFlair[length - i - 1] = temp;\r\nreturn atoi(dataFlair);\r\n}\r\nint main() {\r\nint number;\r\nprintf(\"Enter a number: \");\r\nscanf(\"%d\", &amp;number);\r\nint reversedNumber = reverseNumber (number); \r\nprintf(\"Reversed number: %d\\n\", reversedNumber); return 0;\r\n}\r\n\r\n<\/pre>\n<h4>C Program to reverse a number Using Mathematical Operations:<\/h4>\n<p>&#8211; Calculate the number of digits in the given number.<\/p>\n<p>&#8211; Extract the last digit using modulo and divide the number by 10.<\/p>\n<p>&#8211; Multiply the extracted digit by the power of 10 based on its position.<\/p>\n<p>&#8211; Add the extracted digit to the reversed number.<\/p>\n<p>&#8211; Repeat the above steps until all digits are reversed.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">#include &lt;stdio.h&gt; \r\n#include &lt;math.h&gt;\r\nint reverseNumber(int number) { \r\nint reversedNumber = 0;\r\nint numDigits = log10(number) + 1;\r\nwhile (number != 0) {\r\nint remainder = number % 10; \r\nreversedNumber += remainder * pow(10, numDigits - 1);\r\nnumber \/= 10;\r\nnumDigits--;\r\n}\r\nreturn reversedNumber;\r\n}\r\nint main() {\r\nint number;\r\nprintf(\"Enter a number: \");\r\nscanf(\"%d\", &amp;number);\r\nint reversedNumber = reverseNumber (number); \r\nprintf(\"Reversed number: %d\\n\", reversedNumber); \r\nreturn 0;\r\n}\r\n<\/pre>\n<p>These alternative approaches provide different ways to reverse a number using arrays, recursion, and mathematical operations. But the end result is the same for all\u2026<\/p>\n<p><b>Output<\/b><\/p>\n<div class=\"code-output\">Enter a number: 1234567<br \/>\nReversed number: 7654321<\/div>\n<h3>Conclusion<\/h3>\n<p>Reversing a number is a fundamental programming task that can be approached using various techniques. The C program provided in this article demonstrates an efficient method to reverse a number using a loop. Additionally, we explored alternative approaches involving arrays, recursion, and mathematical operations. Choosing the right approach depends on the requirements and constraints of the specific scenario.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Reversing a number is a common task in programming that involves reversing the order of its digits. In this article, we will explore how to write a C program to reverse a number, along&#46;&#46;&#46;<\/p>\n","protected":false},"author":581,"featured_media":117870,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[19488],"tags":[27999,28000],"class_list":["post-116566","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-c-programming","tag-c-program-to-reverse-a-number","tag-reverse-number-in-c"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.8 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>C Program to Reverse a Number - DataFlair<\/title>\n<meta name=\"description\" content=\"Learn to write C Program to Reverse a Number. See various ways like using arrays, recursion, and mathematical operations with examples.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/data-flair.training\/blogs\/c-program-to-reverse-a-number\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"C Program to Reverse a Number - DataFlair\" \/>\n<meta property=\"og:description\" content=\"Learn to write C Program to Reverse a Number. See various ways like using arrays, recursion, and mathematical operations with examples.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/data-flair.training\/blogs\/c-program-to-reverse-a-number\/\" \/>\n<meta property=\"og:site_name\" content=\"DataFlair\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/DataFlairWS\/\" \/>\n<meta property=\"article:published_time\" content=\"2023-08-17T13:47:55+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2023\/08\/c-program-to-reverse-a-number.webp\" \/>\n\t<meta property=\"og:image:width\" content=\"1200\" \/>\n\t<meta property=\"og:image:height\" content=\"628\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/webp\" \/>\n<meta name=\"author\" content=\"DataFlair Team\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@DataFlairWS\" \/>\n<meta name=\"twitter:site\" content=\"@DataFlairWS\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"DataFlair Team\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"3 minutes\" \/>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"C Program to Reverse a Number - DataFlair","description":"Learn to write C Program to Reverse a Number. See various ways like using arrays, recursion, and mathematical operations with examples.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/data-flair.training\/blogs\/c-program-to-reverse-a-number\/","og_locale":"en_US","og_type":"article","og_title":"C Program to Reverse a Number - DataFlair","og_description":"Learn to write C Program to Reverse a Number. See various ways like using arrays, recursion, and mathematical operations with examples.","og_url":"https:\/\/data-flair.training\/blogs\/c-program-to-reverse-a-number\/","og_site_name":"DataFlair","article_publisher":"https:\/\/www.facebook.com\/DataFlairWS\/","article_published_time":"2023-08-17T13:47:55+00:00","og_image":[{"width":1200,"height":628,"url":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2023\/08\/c-program-to-reverse-a-number.webp","type":"image\/webp"}],"author":"DataFlair Team","twitter_card":"summary_large_image","twitter_creator":"@DataFlairWS","twitter_site":"@DataFlairWS","twitter_misc":{"Written by":"DataFlair Team","Est. reading time":"3 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/data-flair.training\/blogs\/c-program-to-reverse-a-number\/#article","isPartOf":{"@id":"https:\/\/data-flair.training\/blogs\/c-program-to-reverse-a-number\/"},"author":{"name":"DataFlair Team","@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/person\/c187795dc82ab948373cca526df7c445"},"headline":"C Program to Reverse a Number","datePublished":"2023-08-17T13:47:55+00:00","mainEntityOfPage":{"@id":"https:\/\/data-flair.training\/blogs\/c-program-to-reverse-a-number\/"},"wordCount":457,"commentCount":0,"publisher":{"@id":"https:\/\/data-flair.training\/blogs\/#organization"},"image":{"@id":"https:\/\/data-flair.training\/blogs\/c-program-to-reverse-a-number\/#primaryimage"},"thumbnailUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2023\/08\/c-program-to-reverse-a-number.webp","keywords":["c program to reverse a number","reverse number in c"],"articleSection":["C Tutorials"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/data-flair.training\/blogs\/c-program-to-reverse-a-number\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/data-flair.training\/blogs\/c-program-to-reverse-a-number\/","url":"https:\/\/data-flair.training\/blogs\/c-program-to-reverse-a-number\/","name":"C Program to Reverse a Number - DataFlair","isPartOf":{"@id":"https:\/\/data-flair.training\/blogs\/#website"},"primaryImageOfPage":{"@id":"https:\/\/data-flair.training\/blogs\/c-program-to-reverse-a-number\/#primaryimage"},"image":{"@id":"https:\/\/data-flair.training\/blogs\/c-program-to-reverse-a-number\/#primaryimage"},"thumbnailUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2023\/08\/c-program-to-reverse-a-number.webp","datePublished":"2023-08-17T13:47:55+00:00","description":"Learn to write C Program to Reverse a Number. See various ways like using arrays, recursion, and mathematical operations with examples.","breadcrumb":{"@id":"https:\/\/data-flair.training\/blogs\/c-program-to-reverse-a-number\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/data-flair.training\/blogs\/c-program-to-reverse-a-number\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/data-flair.training\/blogs\/c-program-to-reverse-a-number\/#primaryimage","url":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2023\/08\/c-program-to-reverse-a-number.webp","contentUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2023\/08\/c-program-to-reverse-a-number.webp","width":1200,"height":628,"caption":"c program to reverse a number"},{"@type":"BreadcrumbList","@id":"https:\/\/data-flair.training\/blogs\/c-program-to-reverse-a-number\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Blog Home","item":"https:\/\/data-flair.training\/blogs\/"},{"@type":"ListItem","position":2,"name":"C Tutorials","item":"https:\/\/data-flair.training\/blogs\/category\/c-programming\/"},{"@type":"ListItem","position":3,"name":"C Program to Reverse a Number"}]},{"@type":"WebSite","@id":"https:\/\/data-flair.training\/blogs\/#website","url":"https:\/\/data-flair.training\/blogs\/","name":"DataFlair","description":"Learn Today. Lead Tomorrow.","publisher":{"@id":"https:\/\/data-flair.training\/blogs\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/data-flair.training\/blogs\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/data-flair.training\/blogs\/#organization","name":"DataFlair","url":"https:\/\/data-flair.training\/blogs\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/logo\/image\/","url":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2016\/07\/Data-Flair.png","contentUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2016\/07\/Data-Flair.png","width":106,"height":48,"caption":"DataFlair"},"image":{"@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/DataFlairWS\/","https:\/\/x.com\/DataFlairWS","https:\/\/www.linkedin.com\/company\/dataflair-web-services-pvt-ltd\/","https:\/\/www.youtube.com\/user\/DataFlairWS"]},{"@type":"Person","@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/person\/c187795dc82ab948373cca526df7c445","name":"DataFlair Team","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/2302ebc438084d2f1f993edc1996a0aae01332e81f3227cba8df0c48ec010ca4?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/2302ebc438084d2f1f993edc1996a0aae01332e81f3227cba8df0c48ec010ca4?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/2302ebc438084d2f1f993edc1996a0aae01332e81f3227cba8df0c48ec010ca4?s=96&d=mm&r=g","caption":"DataFlair Team"},"description":"DataFlair Team provides high-impact content on programming, Java, Python, C++, DSA, AI, ML, data Science, Android, Flutter, MERN, Web Development, and technology. We make complex concepts easy to grasp, helping learners of all levels succeed in their tech careers.","url":"https:\/\/data-flair.training\/blogs\/author\/dfteam6\/"}]}},"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/116566","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/users\/581"}],"replies":[{"embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/comments?post=116566"}],"version-history":[{"count":7,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/116566\/revisions"}],"predecessor-version":[{"id":118498,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/116566\/revisions\/118498"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/media\/117870"}],"wp:attachment":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/media?parent=116566"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/categories?post=116566"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/tags?post=116566"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}