<\/span><\/h2>\n\n\n\n\/\/ C Program To Swap Two Numbers\nint main(){\n double a, b, temp;\n \n \/\/ Storing first Number\n printf(\"Enter First Number: \");\n scanf(\"%lf\", &a);\n \n \/\/ Storing Second Number\n printf(\"Enter Second Number: \");\n scanf(\"%lf\", &b);\n \n \/\/ Values Swapping \n \n temp = a; \/\/ Value of a assigned to temp \n a = b; \/\/ Value of a assigned to b\n b = temp; \/\/ Value of b assigned to temp\n \n \/\/ Displays Output\n printf(\"First Number after Swapping is: %.2lf \\n\", a);\n printf(\"Second Number after Swapping is: %.2lf\", b);\n \n return 0;\n}\n<\/pre>\n\n\n\nOutput<\/strong><\/p>\n\n\n\nEnter First Number: <\/strong>3.87\r\nEnter Second Number:<\/strong> 5.85\r\nFirst Number after Swapping is:<\/strong> 5.85 \r\nSecond Number after Swapping is:<\/strong> 3.87\r<\/code><\/pre>\n\n\n\n<\/span>How Does This Program Work ?<\/strong><\/span><\/h2>\n\n\n\ndouble a, b, temp;<\/strong>\r<\/code><\/pre>\n\n\n\nIn this example, we have declared three variables named as a<\/strong>, b<\/strong> and temp<\/strong>.<\/p>\n\n\n\n \/\/ Storing first Number\r\n printf(\"Enter First Number: \");\r\n scanf(\"%lf\", &a);\r\n \r\n \/\/ Storing Second Number\r\n printf(\"Enter Second Number: \");\r\n scanf(\"%lf\", &b);\r<\/strong><\/code><\/pre>\n\n\n\nThen, the user is asked to enter the first and second number which they want to swap.<\/p>\n\n\n\n
\/\/ Values Swapping \r\n \r\n temp = a; \/\/ Value of a assigned to temp \r\n a = b; \/\/ Value of a assigned to b\r\n b = temp; \/\/ Value of b assigned to temp\r<\/strong><\/code><\/pre>\n\n\n\nAfter that, temp variable is assigned the value of a variable. Then, the value of a variable is assigned to the b variable.<\/p>\n\n\n\n
And finally the temp variable( which holds the value of a variable) is assigned to b. <\/p>\n\n\n\n
This completes our swapping logic.<\/p>\n\n\n\n
\/\/ Displays Output\r\n printf(\"First Number after Swapping is: %.2lf \\n\", a);\r\n printf(\"Second Number after Swapping is: %.2lf\", b);<\/strong>\r<\/code><\/pre>\n\n\n\nAnd after all these, the output is printed using printf() function. Here, we have used %.2lf because we want the program to show only till 2 decimal places.<\/p>\n\n\n\n
Some used terms are as follow:<\/p>\n\n\n\n
#include <stdio.h><\/strong> – In the first line we have used #include, it is a preprocessor command that tells the compiler to include the contents of the stdio.h(standard input and output) file in the program.<\/p>\n\n\n\nThe stdio.h <\/strong>is a file which contains input and output functions like scanf()<\/strong> and printf()<\/strong> to take input and display output respectively.\u00a0<\/p>\n\n\n\nInt main()<\/strong> – Here main() is the function name and int is the return type of this function. The Execution of any Programming written in C language begins with main() function.\u00a0<\/p>\n\n\n\nscanf()<\/strong> – scanf() function is used to take input from the user.\u00a0<\/p>\n\n\n\nprintf()<\/strong> – printf() function is used to display and print the string under the quotation to the screen.<\/p>\n\n\n\n\/\/ <\/strong>– Used for Commenting in C<\/p>\n\n\n\n<\/span>Swap Numbers Without Using Temporary Variable<\/strong><\/span><\/h2>\n\n\n\n\/\/ C Program To Swap Two Numbers\n#include <stdio.h>\nint main(){\n double a, b;\n \n \/\/ Storing first Number\n printf(\"Enter First Number: \");\n scanf(\"%lf\", &a);\n \n \/\/ Storing Second Number\n printf(\"Enter Second Number: \");\n scanf(\"%lf\", &b);\n \n \/\/ Swapping logic\n a = a - b;\n b = a + b;\n a = b - a;\n \n \/\/ Displays Output\n printf(\"First Number after Swapping becomes: %.2lf \\n\", a);\n printf(\"Second Number after Swapping becomes: %.2lf\", b);\n \n return 0;\n}\n<\/pre>\n\n\n\nOutput<\/strong><\/p>\n\n\n\nEnter First Number:<\/strong> 3.57\r\nEnter Second Number:<\/strong> 7.43\r\nFirst Number after Swapping becomes:<\/strong> 7.43 \r\nSecond Number after Swapping becomes:<\/strong> 3.57\r<\/code><\/pre>\n\n\n\n \/\/ Swapping logic\r\n a = a - b;\r\n b = a + b;\r\n a = b - a;<\/strong>\r<\/code><\/pre>\n\n\n\nHere, in this program instead of using a temporary variable, we have created a simple logic to swap two numbers.<\/p>\n\n\n\n
<\/span>Conclusion<\/strong><\/span><\/h2>\n\n\n\nI hope after going through this post, you get to understand how to swap two numbers in C Programming language.<\/p>\n\n\n\n
If you still have any doubt regarding this, feel free to contact us in the Comment Section. We will be delighted to help you.<\/p>\n","protected":false},"excerpt":{"rendered":"
In this post, you will learn to swap two numbers in C Programming language using different methods.<\/p>\n","protected":false},"author":2,"featured_media":4007,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"site-sidebar-layout":"default","site-content-layout":"default","ast-site-content-layout":"","site-content-style":"default","site-sidebar-style":"default","ast-global-header-display":"","ast-banner-title-visibility":"","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","theme-transparent-header-meta":"default","adv-header-id-meta":"","stick-header-meta":"","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":"","astra-migrate-meta-layouts":"default","ast-page-background-enabled":"default","ast-page-background-meta":{"desktop":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-gradient":""},"tablet":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-gradient":""},"mobile":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-gradient":""}},"ast-content-background-meta":{"desktop":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-gradient":""},"tablet":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-gradient":""},"mobile":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-gradient":""}},"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[167,294],"tags":[222,237,236,206],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"https:\/\/www.codingbroz.com\/wp-content\/uploads\/2021\/08\/c-program-to-swap-two-numbers-codingbroz.png","jetpack-related-posts":[],"_links":{"self":[{"href":"https:\/\/www.codingbroz.com\/wp-json\/wp\/v2\/posts\/4004"}],"collection":[{"href":"https:\/\/www.codingbroz.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.codingbroz.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.codingbroz.com\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.codingbroz.com\/wp-json\/wp\/v2\/comments?post=4004"}],"version-history":[{"count":2,"href":"https:\/\/www.codingbroz.com\/wp-json\/wp\/v2\/posts\/4004\/revisions"}],"predecessor-version":[{"id":4008,"href":"https:\/\/www.codingbroz.com\/wp-json\/wp\/v2\/posts\/4004\/revisions\/4008"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.codingbroz.com\/wp-json\/wp\/v2\/media\/4007"}],"wp:attachment":[{"href":"https:\/\/www.codingbroz.com\/wp-json\/wp\/v2\/media?parent=4004"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.codingbroz.com\/wp-json\/wp\/v2\/categories?post=4004"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.codingbroz.com\/wp-json\/wp\/v2\/tags?post=4004"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}