Showing posts with label swapping. Show all posts
Showing posts with label swapping. Show all posts

22 October 2012

Swapping two numbers using Multiplication and Divide

5 comments

Q. How do I swap two numbers without using addition and subtraction in Bash


Ans:

#!/bin/bash
read -p "Enter first number :" first
read -p "Enter second number:" sec
echo ""
echo -e "Number before swapping is $first and $sec \n"
#swapping logic
first=$((first*sec))
sec=$((first/sec))
first=$((first/sec))
echo -e "Number after swapping is $first and $sec \n"
Read More...