Notification texts go here Contact Us Click here

C Program to swap two numbers using Variables .

Please wait 0 seconds...
Scroll Down and click on Go to Link for destination
Congrats! Link is Generated




 Swap two numbers using third variable .

#include<stdio.h>
  int main()
{int a,b,temp;
printf("Enter a= ");
scanf ("%d",&a);
printf("Enter b= ");
scanf("%d",&b);
printf("Before Swapping:\n");
printf("a=%d\nb=%d",a,b);
temp=a;
a=b;
b=temp;
printf("\nAfter Swapping:");
printf("\na=%d\nb=%d",a,b);
return 0;
}

➤ OUTPUT :
Enter a= 5
Enter b= 9
Before Swapping:
a=5
b=9
After Swapping:
a=9
b=5



➤ Swap two numbers without using third variable .


#include<stdio.h>
int main()
{int a,b;
printf("Enter the value of a= ");
scanf ("%d",&a);
printf("Enter the value of b= ");
scanf("%d",&b);

printf("Before Swapping:\n");

printf("a=%d\nb=%d",a,b);
a=a+b;
b=a-b;
a=a-b;
printf("\nAfter Swapping:");
printf("\na=%d\nb=%d",a,b);
 return 0;
}

OUTPUT :
Enter value of a= 5
Enter value of b= 3
Before Swapping:
a=5
b=3
After Swapping:
a=3
b=5

إرسال تعليق

Cookie Consent
We serve cookies on this site to analyze traffic, remember your preferences, and optimize your experience.
Oops!
It seems there is something wrong with your internet connection. Please connect to the internet and start browsing again.
AdBlock Detected!
We have detected that you are using adblocking plugin in your browser.
The revenue we earn by the advertisements is used to manage this website, we request you to whitelist our website in your adblocking plugin.