C Program to Swap Two Numbers

Hello coders, in this post we will how to write a C Program to Swap Two Numbers, This is a very basic C program.

As you already know that this site does not contain only c programming solutions here, you can also find the solution for other problems. I.e. Web Technology, Data StructuresRDBMS ProgramsJava Programs Solutions,  Fiverr Skills Test answersGoogle Course AnswersLinkedin AssessmentLeetcode Solutions, and Coursera Quiz Answers.

C Program to Swap Two Numbers
C Program to Swap Two Numbers

if you want to learn C programming free of cost please visit:

C Program to Swap Two Numbers

#include<stdio.h>
int main() {
      double first, second, temp;
      printf("Enter first number: ");
      scanf("%lf", &first);
      printf("Enter second number: ");
      scanf("%lf", &second);
      // Value of first is assigned to temp
      temp = first;
      // Value of second is assigned to first
      first = second;
      // Value of temp (initial value of first) is assigned to second
      second = temp;
      // %.2lf displays number up to 2 decimal points
      printf("\nAfter swapping, firstNumber = %.2lf\n", first);
      printf("After swapping, secondNumber = %.2lf", second);
      return 0;
}

Output:

Enter first number: 1.20
Enter second number: 2.45
After swapping, firstNumber = 2.45
After swapping, secondNumber = 1.20

Conclusion

I hope after going through this post, you understand how to write a C Program to Swap Two Numbers, if there is any case program is not working and showing an error please let me know in the comment section.

Sharing Is Caring

Leave a Comment