Get the App
SLTechnology News&Howtos  ›  Development  › 

How to exchange the values of two numbers in C language

Shulou Source: shulou.com Published: 2022-06-01 01:50:52 09月21日 Update

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.

Tags: Two numbers input language articles number content appropriateness variables examples ideas new knowledge more steps points knowledge knowledge points articles details results Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Xiaomi Shulou Technology Microsoft Huawei Apple