{"id":4637,"date":"2023-01-23T12:39:23","date_gmt":"2023-01-23T12:39:23","guid":{"rendered":"https:\/\/gyanipandit.com\/programming\/?p=4637"},"modified":"2023-01-23T12:39:27","modified_gmt":"2023-01-23T12:39:27","slug":"c-program-to-swap-two-numbers","status":"publish","type":"post","link":"https:\/\/gyanipandit.com\/programming\/c-program-to-swap-two-numbers\/","title":{"rendered":"C program to swap two numbers"},"content":{"rendered":"<p>In this article, we are going to discuss how to swap two numbers by using the function or without a function. So let&#8217;s start!<\/p>\n<p>There are two ways to swap the number one is by using a third variable and another by adding or subtracting the values in it. So we going to discuss them one by one.<\/p>\n<h2 style=\"text-align: center;\">C program to Swap two numbers with function or without function<\/h2>\n<h4>Method 1: C program to swap two numbers using function.<\/h4>\n<p>\/\/ C program to swap two numbers by using the function<\/p>\n<p>#include&lt;stdio.h&gt;<br \/>\nint swap(int x, int y){<br \/>\nint temp =x;<br \/>\nx = y;<br \/>\ny = temp;<br \/>\n}<\/p>\n<p>int main(){<br \/>\nint x, y;<br \/>\nprintf(&#8220;Enter the value of x :&#8221;);<br \/>\nscanf(&#8220;%d&#8221;, &amp;x);<br \/>\nprintf(&#8220;Enter the value of y :&#8221;);<br \/>\nscanf(&#8220;%d&#8221;, &amp;y);<br \/>\nswap(x,y);<br \/>\nprintf(&#8220;After swapping the number are x=%d y=%d&#8221;,x,y);<br \/>\nreturn 0;<br \/>\n}<\/p>\n<p><strong>output :<\/strong><\/p>\n<p>Enter the value of x:22<br \/>\nEnter the value of y:33<br \/>\nAfter swapping the number are x=22 y=33<\/p>\n<p><strong>Step 1:<\/strong> First create a user-defined function and pass two parameters in it.<\/p>\n<p><strong>Step 2:<\/strong> After passing the argument we simply use the third variable in it and give the name as temp then we simply do \u2013 int temp =x;<br \/>\nx = y;<br \/>\ny = temp;<\/p>\n<p><strong>Step 3:<\/strong> After this, we simply go for the main method, and then simply we create two int-type variables to get input from the user.<\/p>\n<p><strong>Step 4:<\/strong> After that, we simply use scanf() to store the value of a variable.<\/p>\n<p><strong>Step 5:<\/strong> then we simply swap(x,y).<\/p>\n<p><strong>step 6:<\/strong> after the above steps we simply use printf() to get our desired output.<\/p>\n<h4>Method 2: C program to swap two numbers without using function.<\/h4>\n<p>#include&lt;stdio.h&gt;<br \/>\nint main(){<br \/>\nint num1, num2, temp;<br \/>\nprintf(&#8220;Enter value for num1 :&#8221;);<br \/>\nscanf(&#8220;%d&#8221;, &amp;num1);<br \/>\nprintf(&#8220;Enter value for num2 :&#8221;);<br \/>\nscanf(&#8220;%d&#8221;, &amp;num2);<br \/>\ntemp = num1;<br \/>\nnum1 = num2;<br \/>\nnum2 = temp;<br \/>\nprintf(&#8220;After swapping the number num1=%d, num2=%d&#8221;, num1,num2);<br \/>\nreturn 0;<br \/>\n}<\/p>\n<p><strong>Output<\/strong><\/p>\n<p>Enter a value for num1:22<br \/>\nEnter a value for num2:33<br \/>\nAfter swapping the numbers num1=33, num2=22<\/p>\n<p><strong>Step 1:<\/strong> First we take the int type variable to get input from user. And then also we use int type temp variable to store the value of x in it.<\/p>\n<p><strong>Step 2:<\/strong> After getting we simply use scanf() to store the value of variables.<\/p>\n<p><strong>Step 3:<\/strong> then we simply swap two variables such as temp = num1;<br \/>\nnum1 = num2;<br \/>\nnum2 = temp;<\/p>\n<p><strong>Step 4:<\/strong> After that, we simply use printf() to print our desired output.<\/p>\n<h4>Method 3: C program to swap two numbers without using a third variable<\/h4>\n<p>\/\/ C program to swap two numbers without using a third variable.<\/p>\n<p>#include&lt;stdio.h&gt;<br \/>\nint main(){<br \/>\nint num1, num2;<br \/>\nprintf(&#8220;Enter a\u00a0 value for num1 :&#8221;);<br \/>\nscanf(&#8220;%d&#8221;, &amp;num1);<br \/>\nprintf(&#8220;Enter a value for num2 :&#8221;);<br \/>\nscanf(&#8220;%d&#8221;, &amp;num2);<br \/>\nnum1 = num1 + num2;<br \/>\nnum2 = num1 &#8211; num2;<br \/>\nnum1 = num1 &#8211; num2;<br \/>\nprintf(&#8220;After swapping the value num1 = %dn&#8221;, num1);<br \/>\nprintf(&#8220;After swapping the value num2 = %d&#8221;, num2);<br \/>\nreturn 0;<br \/>\n}<\/p>\n<p><strong>Output<\/strong><\/p>\n<p>Enter a value for num1:22<br \/>\nEnter a value for num2:11<br \/>\nAfter swapping the numbers are num1=11, num2=22<\/p>\n<p>Enter a value for num1:22<br \/>\nEnter a value for num2:33<br \/>\nAfter swapping the numbers num1=33, num2=22<\/p>\n<p><strong>Step 1:<\/strong> First we take the int type variable to get input from the user.<\/p>\n<p><strong>Step 2:<\/strong> After getting we simply use scanf() to store the value of variables.<\/p>\n<p><strong>Step 3:<\/strong> then we simply swap two variables such as num1 = num1 + num2;<br \/>\nnum2 = num1 &#8211; num2;<br \/>\nnum1 = num1 &#8211; num2;<\/p>\n<p><strong>Step 4:<\/strong> After that, we simply use printf() to print our desired output.<\/p>\n<p>In this way, we simply understand how we swap two numbers by using these three methods. I hope this thing is clear to you. So for now keep learning and keep exploring.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this article, we are going to discuss how to swap two numbers by using the function or without a function. So let&#8217;s start! There are two ways to swap the number one is by using a third variable and another by adding or subtracting the values in it. So we going to discuss them [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[30],"tags":[29,107],"class_list":{"0":"post-4637","1":"post","2":"type-post","3":"status-publish","4":"format-standard","6":"category-c-program","7":"tag-c-program","8":"tag-c-program-to-swap-two-numbers"},"_links":{"self":[{"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/posts\/4637","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/comments?post=4637"}],"version-history":[{"count":1,"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/posts\/4637\/revisions"}],"predecessor-version":[{"id":4638,"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/posts\/4637\/revisions\/4638"}],"wp:attachment":[{"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/media?parent=4637"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/categories?post=4637"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/tags?post=4637"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}