How to exchange the values of two numbers in C language
In this article, the editor introduces in detail "how to exchange the values of two numbers in C language". The content is detailed, the steps are clear, and the details are handled properly. I hope this article "how to exchange the values of two numbers in C language" can help you solve your doubts. Let's follow the editor's ideas to learn new knowledge.
Use temporary variables
The following example demonstrates swapping the values of two floating-point numbers.
# include
Int main () {
Double firstNumber, secondNumber, temporaryVariable
Printf ("enter first number:")
Scanf ("% lf", & firstNumber)
Printf ("enter the second number:")
Scanf ("% lf", & secondNumber)
/ / assign the first number to temporaryVariable
TemporaryVariable = firstNumber
/ / assign the value of the second number to firstNumber
FirstNumber = secondNumber
/ / assign temporaryVariable to secondNumber
SecondNumber = temporaryVariable
Printf ("\ nAfter exchange, firstNumber =% .2lf\ n", firstNumber)
Printf ("after exchange, secondNumber =% .2lf", secondNumber)
Return 0
}
Running result:
Enter the first number: 1 enter the second number: 2 after the exchange, firstNumber = 2.00 after the exchange, secondNumber = 1.00 read here, this "C language how to exchange the value of two numbers" article has been introduced, want to master the knowledge of this article also need to practice and use in order to understand, if you want to know more related content of the article, welcome to pay attention to the industry information channel.