Design a function to swapping of two number

26 Sep 2022 Balmiki Mandal 0 C Programming

Design a Function for Swapping Two Numbers in C

"Design a function to swapping of two numbers" means that you are asked to write a function that takes two numbers as input and swaps their values. In other words, the function should exchange the values of the two input numbers so that the first number becomes the second number, and the second number becomes the first number.

 Swapping of two number in C, we can use the following algorithm:

  1. Create a temporary variable to store the value of the first number.
  2. Assign the value of the second number to the first number.
  3. Assign the value of the temporary variable to the second number.

This algorithm can be implemented in C as follows:

C Programming
void swap(int *a, int *b) {
  int temp = *a;
  *a = *b;
  *b = temp;
}

The swap() function takes two pointers to integers as input and swaps the values of the integers that the pointers point to.

To use the swap() function, we can simply pass in the addresses of the two numbers that we want to swap. For example, the following code swaps the values of the variables x and y:

C Programming
int x = 10;
int y = 20;

swap(&x, &y);

printf("x = %d, y = %d\n", x, y);

Output:

x = 20, y = 10

We can also use the swap() function to swap the values of two elements in an array. For example, the following code swaps the values of the first two elements in the array arr:

C Programming
int arr[5] = {1, 2, 3, 4, 5};
swap(&arr[0], &arr[1]);
printf("arr[0] = %d, arr[1] = %d\n", arr[0], arr[1]);

Output:

arr[0] = 2, arr[1] = 1

 write c program to Swapping of two number

#include<stdio.h>
void swap(int *,int *);
void main()
{
    int num1,num2;
    printf("Enetr two numnber:");
    scanf("%d %d",&num1,&num2);
    printf("Before swapping :%d %d\n",num1,num2);
    swap(&num1,&num2);
    printf("After swapping: %d %d\n",num1,num2);

}
void swap(int *n1,int *n2)
{
int temp;
temp=*n1;
*n1=*n2;
*n2=temp;
}

output:

Enter two numbers: 10 20
Before swapping:10 20
After swapping: 20 10

Further Reading:

Concatenating Two Strings in C: A Step-by-Step Guide

Design a function to count given character in given string

Design a function to reverse the given string

Design a function to search given character in given string in c

Design a function to reverse the word in given string

Design a function replace the words in reverse order in a given string line

design a function to reverse data in between two location

Design a function to find string length in c

Design a Function to print the string character by character

Design a function to print the elements of a given integers array.

Design a function for bubble sort in c

Design a function to swapping of two number

Design a function to check the given number is Armstrong or not if Armstrong return 1 else 0

 

Enroll Now:

C-Programming From Scratch to Advanced 2023-2024] "Start Supercharging Your Productivity!"

Contact Us:

  • For any inquiries, please email us at [[email protected]].
  • Follow us on insta  [ electro4u_offical_ ] for updates and tips.

 

Note: If you encounter any issues or specific errors when running this program, please let me know and I'll be happy to help debug them!

BY: Balmiki Mandal

Related Blogs

Post Comments.

Login to Post a Comment

No comments yet, Be the first to comment.