
- C Programming Tutorial
- C - Home
- C - Overview
- C - Environment Setup
- C - Program Structure
- C - Basic Syntax
- C - Data Types
- C - Variables
- C - Constants
- C - Storage Classes
- C - Operators
- C - Decision Making
- C - Loops
- C - Functions
- C - Scope Rules
- C - Arrays
- C - Pointers
- C - Strings
- C - Structures
- C - Unions
- C - Bit Fields
- C - Typedef
- C - Input & Output
- C - File I/O
- C - Preprocessors
- C - Header Files
- C - Type Casting
- C - Error Handling
- C - Recursion
- C - Variable Arguments
- C - Memory Management
- C - Command Line Arguments
- C Programming useful Resources
- C - Questions & Answers
- C - Quick Guide
- C - Useful Resources
- C - Discussion
How to swap two arrays without using temporary variable in C language?
Swap two arrays without using Temp variable. Here, we are going to use Arithmetic Operators and Bitwise Operators instead of third variable.
The logic to read the first array is as follows −
printf("enter first array ele:
"); for(i = 0; i < size; i++){ scanf("%d", &first[i]); }
The logic to read the second array is as follows −
printf("enter first array ele:
"); for(i = 0; i < size; i++){ scanf("%d", &first[i]); }
The logic to swap the two arrays without using a third variable is as follows −
for(i = 0; i < size; i++){ first[i] = first[i] + sec[i]; sec[i] = first[i] - sec[i]; first[i] = first[i] - sec[i]; }
Program
Following is the C program to swap two arrays without using the Temp variable −
#include<stdio.h> int main(){ int size, i, first[20], sec[20]; printf("enter the size of array:"); scanf("%d", &size); printf("enter first array ele:
"); for(i = 0; i < size; i++){ scanf("%d", &first[i]); } printf("enter second array ele:
"); for(i = 0; i < size; i ++){ scanf("%d", &sec[i]); } //Swapping two Arrays for(i = 0; i < size; i++){ first[i] = first[i] + sec[i]; sec[i] = first[i] - sec[i]; first[i] = first[i] - sec[i]; } printf("
first array after swapping %d elements
", size); for(i = 0; i < size; i ++){ printf(" %d \t ",first[i]); } printf("sec array after Swapping %d elements
", size); for(i = 0; i < size; i ++){ printf(" %d \t ",sec[i]); } return 0; }
Output
When the above program is executed, it produces the following result −
enter the size of array:5 enter first array ele: 11 12 13 14 15 enter second array ele: 90 80 70 60 50 first array after swapping 5 elements 90 80 70 60 50 sec array after Swapping 5 elements 11 12 13 14 15
- Related Articles
- How to swap two numbers without using the third or a temporary variable using C Programming?
- Swap Numbers without using temporary variable in Swift Program?
- Swap two Strings without using temp variable in C#
- How to swap two numbers without using a temp variable in C#
- How to swap two String variables without third variable.
- Swap two Strings without using third user defined variable in Java
- How can I swap two strings without using a third variable in Java?
- Write a Golang program to swap two numbers without using a third variable
- Swapping two variable value without using third variable in C/C++
- How to quickly swap two arrays of the same size in C++?
- How to merge to arrays in C language?
- How to Swap Two Variables using Python?
- Arrays in C Language
- How to merge two arrays without duplication in android listview?
- How to find the size of a variable without using sizeof in C#?

Advertisements