Computer for SEE and NEB

It is a complete SEE and NEB solution for computer science. It includes Computer Fundamentals, Database (SQL), Programming in C QBASIC, CSS, JavaScript, and PHP for beginners.

Breaking

Post Top Ad

Your Ad Spot

Friday, September 30, 2022

C program exchange two numbers using function

 C program exchange two numbers using function 


C program exchange two numbers using function



Introduction:

The C programming language is one of the most widely used programming languages in the world. It is known for its efficiency, simplicity, and flexibility. One of the essential tasks in programming is swapping two values. Swapping means exchanging the values of two variables. This process can be done using a temporary variable or without a temporary variable. In this article, we will discuss how C program exchange two numbers using function 


C Program Exchange Two Numbers Using Function


The need to swap two numbers


Swapping two numbers is a common task in programming. It is used in various applications such as sorting, searching, and mathematical calculations. Sometimes it is also needed to exchange the values of two variables in a program.

Simple and Efficient way to swap two numbers

There are two ways to swap two numbers in C programming. One is by using a temporary variable, and the other is without using a temporary variable. Here, we will discuss a simple and efficient way to swap two numbers in C programming using a function.


Step by step guide to implement the program


Follow the below steps to implement the C program exchange two numbers using function:

  1. Define a function to swap two numbers in C.
  2. Inside the function, declare two integer variables to store the values of two numbers to be swapped.
  3. Read the values of two numbers from the user.
  4. Swap the values of two numbers using a third variable.
  5. Print the swapped values of two numbers.


Programming code:

The below code demonstrates the C program exchange two numbers using function:

#include <stdio.h>
int a=12,b=23;
void change()
{
int p;
printf("\nValues Before Exchange a=%d, b=%d\n",a,b);
p=a;
a=b;
b=p;
printf("\nValues After Exchange a=%d, b=%d",a,b) ;
}
int main()
{
printf("Enter Value for a : ");
scanf("%d",&a);
printf("\nEnter Value for b : ");
scanf("%d",&b);
change();
return 0;
}

Explanation of the coding:


This code is a C program that swaps the values of two variables using a function.

First, two integer variables 'a' and 'b' are declared and initialized with values 12 and 23 respectively.

Then, a function 'change()' is defined, which takes no arguments and returns no value. Within this function, a third integer variable 'p' is declared.

The current values of 'a' and 'b' are printed to the console using the 'printf()' function.

Next, the value of 'a' is stored in 'p' using the assignment operator '='.

Then, the value of 'b' is assigned to 'a', effectively swapping the values of 'a' and 'b'.

Finally, the value of 'p' is assigned to 'b'. The new values of 'a' and 'b' are printed to the console using 'printf()'.

In the main function, the user is prompted to enter a value for 'a' using 'scanf()', and then prompted to enter a value for 'b'.

The 'change()' function is then called, which swaps the values of 'a' and 'b'.

The program then returns 0, indicating successful completion.

Output of the program:




FAQs


Q. What is the need to swap two numbers in programming? 
Ans: Swapping two numbers is required in various applications such as sorting, searching, and mathematical calculations. Sometimes it is also needed to exchange the values of two variables in a program.

Q. Can we swap two numbers without using a temporary variable in C? 
Ans: Yes, we can swap two numbers without using a temporary variable in C. This can be done using arithmetic operations or bitwise XOR operation.

Q. What are the advantages of using a function to swap two numbers in C? 
Ans: Using a function to swap two numbers in C makes the program more organized and easy to understand. It also helps to avoid code duplication, reduces errors, and improves code reusability.

Conclusion


Swapping two numbers is a common task in programming, and it can be done in various ways. In this article, we discussed a simple and efficient way to swap two numbers in C using a function. We also provided a step-by-step guide and a code example to help you implement this program. By using a function to swap two numbers, you can make your code more organized, easy to understand, and error-free. We hope this article has helped you learn how C program exchange two numbers using function.





No comments:

Post a Comment

Post Top Ad

Your Ad Spot

Pages