This document presents an algorithm for swapping two numbers without using a third variable. It takes two numbers as input from the user, uses mathematical equations to swap the values by adding and subtracting the numbers from each other, and outputs the swapped values. The time complexity of the algorithm is analyzed by measuring the execution time, which is found to be 0.000229 seconds. The conclusion is that using only two variables reduces the compilation time significantly compared to approaches using a third variable.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0 ratings0% found this document useful (0 votes)
32 views4 pages
Itp Assignment 1 - IIT2019
This document presents an algorithm for swapping two numbers without using a third variable. It takes two numbers as input from the user, uses mathematical equations to swap the values by adding and subtracting the numbers from each other, and outputs the swapped values. The time complexity of the algorithm is analyzed by measuring the execution time, which is found to be 0.000229 seconds. The conclusion is that using only two variables reduces the compilation time significantly compared to approaches using a third variable.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4
Swapping two given numbers without using
third variable Animesh Choudhury IIT2019143
I Semester B.Tech, Department of Information Technology,
Indian Institute of Information Technology, Allahabad, India
Abstract: In this paper, we have devised {
an algorithm that swaps the two given clock_t start,end; numbers without using third variable to double cpu_time_used; reduce compilation time. start = clock(); We have discussed the time and memory complexity of the algorithm. int a,b;
printf("Enter first number a=");
I. INTRODUCTION scanf("%d",&a); Swapping means interchanging. In this printf("Enter second number b="); paper we have tried to swap two numbers scanf("%d",&b); without using third variable, : For example, if in C program you have a=a+b; taken two variables a and b where a = 4 and b=a-b; b=5, then before swapping a = 4, b = 5 a=a-b; after swapping a = 5, b = 4.. The idea is to reduce the number of printf("The value of a & b after swaping operations needed to calculate this problem, are %d",a ); for instance three variables consumes printf(" and %d",b ); space. printf(" respectively."); II. ALGORITHM DESCRIPTION end = clock(); This algorithm takes two numbers from cpu_time_used=((double)(end-start))/ user & swaps them without using third CLOCKS_PER_SEC; variable. printf("\nCode execution time (in s) =");
III. ALGORITHM printf("%f",cpu_time_used);
#include <stdio.h> return 0;
#include <time.h> }
int main() IV. ANALYSIS
Step 1: Initialize variable a and b by input
given by user.
Step 2: By mathematical equations,
swapping of two given number took place.
VI. CONCLUSION
From this paper we can conclude that by
using only two variables to compile the program of swapping two numbers the execution time can be reduced significantly.