<\/span><\/h2>\n\n\n\npackage main\n\nimport (\n\t\"fmt\"\n)\n\nfunc main() {\n\tvar a, b, c int\n\n\tfmt.Print(\"Enter the first integer: \")\n\tfmt.Scanln(&a)\n\n\tfmt.Print(\"Enter the second integer: \")\n\tfmt.Scanln(&b)\n\n\tfmt.Println(\"Before Swapping:- \")\n\tfmt.Println(\"First number = \", a)\n\tfmt.Println(\"Second number = \", b)\n\n\tc = a\n\ta = b\n\tb = c\n\n\tfmt.Println(\"After Swapping:- \")\n\tfmt.Println(\"First number = \", a)\n\tfmt.Println(\"Second number = \", b)\n}\n<\/pre>\n\n\n\nOutput<\/strong><\/p>\n\n\n\nEnter the first integer: 23\r\nEnter the second integer: 47\r\nBefore Swapping:- \r\nFirst number = 23\r\nSecond number = 47\r\nAfter Swapping:- \r\nFirst number = 47\r\nSecond number = 23<\/code><\/pre>\n\n\n\n<\/span>How Does This Program Work?<\/strong><\/span><\/h2>\n\n\n\n\tvar a, b, c int\n<\/pre>\n\n\n\nWe have declared three integer data type variables named a<\/strong>, b<\/strong>, and c<\/strong>.<\/p>\n\n\n\n\tfmt.Print(\"Enter the first integer: \")\n\tfmt.Scanln(&a)\n\n\tfmt.Print(\"Enter the second integer: \")\n\tfmt.Scanln(&b)\n<\/pre>\n\n\n\nThe user is asked to enter two integers. The first integer is stored in a<\/strong> named variable, while the second integer is stored in the b<\/strong>-named variable.<\/p>\n\n\n\n\tfmt.Println(\"Before Swapping:- \")\n\tfmt.Println(\"First number = \", a)\n\tfmt.Println(\"Second number = \", b)\n<\/pre>\n\n\n\nNow, we are displaying the values stored in the first and second variables before swapping. We can use this data as a reference point to check whether the numbers are really swapped or not.<\/p>\n\n\n\n
\tc = a\n\ta = b\n\tb = c\n<\/pre>\n\n\n\nWe assign the value of the first variable “a”<\/strong> to the third variable “c.”<\/strong> Here a = 23 and c = 23.<\/p>\n\n\n\nAfter that, the value of the second variable “b”<\/strong> is assigned to “a.”<\/strong> Here, a = 47 and b = 47.<\/p>\n\n\n\nSince the value of the “a” variable has been swapped, it\u2019s now time to swap the value of the “b” variable. We assigned the value of the “c”<\/strong> variable to the second variable, “b.”<\/strong> Here, b = 23 and c = 23.<\/p>\n\n\n\nFollowing the preceding procedure, we obtain a = 47<\/strong> and b = 23<\/strong>.<\/p>\n\n\n\n\tfmt.Println(\"After Swapping:- \")\n\tfmt.Println(\"First number = \", a)\n\tfmt.Println(\"Second number = \", b)\n<\/pre>\n\n\n\nSince the two numbers are swapped, we will print the swapped values on the console screen with the help of the fmt.Println()<\/strong> method.<\/p>\n\n\n\n<\/span>Conclusion<\/strong><\/span><\/h2>\n\n\n\nToday you learned how to write a program to swap two numbers using the Go programming language<\/strong>.<\/p>\n\n\n\nIn this program, we have talked about only one way to swap two numbers, but there are other ways to swap two numbers too.<\/p>\n\n\n\n
We will talk about those methods in some other posts. Till then, if you have any doubts, leave your queries in the comment section.<\/p>\n","protected":false},"excerpt":{"rendered":"
In this post, we will learn how to write a program to swap two numbers using the Go programming language.<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"site-sidebar-layout":"default","site-content-layout":"","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":"","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":[808],"tags":[809],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","jetpack-related-posts":[],"_links":{"self":[{"href":"https:\/\/www.codingbroz.com\/wp-json\/wp\/v2\/posts\/8475"}],"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=8475"}],"version-history":[{"count":4,"href":"https:\/\/www.codingbroz.com\/wp-json\/wp\/v2\/posts\/8475\/revisions"}],"predecessor-version":[{"id":8480,"href":"https:\/\/www.codingbroz.com\/wp-json\/wp\/v2\/posts\/8475\/revisions\/8480"}],"wp:attachment":[{"href":"https:\/\/www.codingbroz.com\/wp-json\/wp\/v2\/media?parent=8475"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.codingbroz.com\/wp-json\/wp\/v2\/categories?post=8475"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.codingbroz.com\/wp-json\/wp\/v2\/tags?post=8475"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}