Sunday, April 5, 2020

Swapping Program In C With Of Without Temp Variable | C programming Example

  • C Program Example For Swap Two Number 


This Article From Learn c programming in Hindi Will help you to learn C Program Example For Swap Two Number and check out "selection sort program in c", "socket programming in c", "merge sort program in c","Kruskal's algorithm in c", "for loop c programming", "c struct example","simple program in c language", "simple CPP program", "c programming practice questions", "c program using for loop", "programming using c", "hello world program in c", "if statement in c ".



C Programming Example
C Programming Example


अदला-बदली के सिवा कुछ नहीं है। यह उनके बीच विनिमय करने के लिए 2 नंबर या कुछ का मान हो सकता है। उदाहरण के लिए, नीचे दिए गए उदाहरण पर विचार करें।


  •      स्वैप करने से पहले, ए और बी के मूल्य नीचे दिए गए हैं।
     ए = 10
     बी = २०
  •      स्वैप करने के बाद, A और B का मान नीचे होगा।
     ए = 20
     बी = १०

Source Code For C Programming Example

#include 
int main()
{
   int A, B, temp;
 
   printf("Please enter the 1st number : ");
   scanf("%d",&A);
   printf("\nPlease enter the 2nd number : ");
   scanf("%d",&B);
 
   printf("\nBefore swapping:\n");
   printf("A - %d \nB - %d", A, B);
 
   temp = A;
   A = B;
   B = temp;
 
   printf("\nAfter swapping:\n");
   printf("A - %d \nB - %d", A, B);
 
   return 0;
}

OUTPUT : -

Please enter the 1st number : 20
Please enter the 2nd number : 30
Before swapping:
A – 20
B – 30
After swapping:
A – 30
B – 20

 

  • C Program Example For Swap Two Number Without Temp Variable


#include 
int main()
{
   int A, B;
 
   printf("Please enter the 1st number : ");
   scanf("%d",&A);
   printf("\nPlease enter the 2nd number : ");
   scanf("%d",&B);
 
   printf("\nBefore swapping:\n");
   printf("A - %d \nB - %d", A, B);
 
   A = A - B;
   B = A + B;
   A = B - A;
 
   printf("\nAfter swapping:\n");
   printf("A - %d \nB - %d", A, B);
 
   return 0;
}

OUTPUT : -


Please enter the 1st number : 45
Please enter the 2nd number : 67
Before swapping:
A – 45
B – 67
After swapping:
A – 67
B – 45

0 Comments:

Post a Comment