Problem Statement:- Program to Swap two numbers without using a third variable using functions.
Sample Input/Output:-
Data requirement:-
Input Data:- num1, num2
Output Data:-num1, num2
Program in C
Here is the source code of the C Program to Swap two numbers without using a third variable using function.
Code:
#include <stdio.h>/*printf,scanf definitions *//* This function swap two numbers without using third variable*/void swap_two_numbers(int *num1, int *num2){ *num1 = (*num1) + (*num2); *num2 = (*num1) - (*num2); *num1 = (*num1) - (*num2);}int main(){ int num1, num2; /* Get the input of two variables */ printf("Enter the 1st Number:"); scanf("%d", &num1); printf("Enter the 2nd Number:"); scanf("%d", &num2); /* Call Function swap_two_numbers with two Parameters*/ swap_two_numbers(&num1, &num2); /* Display the two numbers after swapping*/ printf("***After swapping***\n"); printf("Number 1: %d\nNumber 2: %d\n", num1, num2);}
Input/Output:
Enter the 1st Number:4
Enter the 2nd Number:7
***After swapping***
Number 1: 7
Number 2: 4
#include <stdio.h>/*printf,scanf definitions *//* This function swap two numbers without using third variable*/void swap_two_numbers(int *num1, int *num2){ *num1 = (*num1) + (*num2); *num2 = (*num1) - (*num2); *num1 = (*num1) - (*num2);}int main(){ int num1, num2; /* Get the input of two variables */ printf("Enter the 1st Number:"); scanf("%d", &num1); printf("Enter the 2nd Number:"); scanf("%d", &num2); /* Call Function swap_two_numbers with two Parameters*/ swap_two_numbers(&num1, &num2); /* Display the two numbers after swapping*/ printf("***After swapping***\n"); printf("Number 1: %d\nNumber 2: %d\n", num1, num2);}
Program in C++
Here is the source code of the C++ Program to Swap two numbers without using a third variable using function.
Code:
#include <iostream>using namespace std;/* This function swap two numbers without using third variable*/void swap_two_numbers(int *num1, int *num2){ *num1 = (*num1) + (*num2); *num2 = (*num1) - (*num2); *num1 = (*num1) - (*num2);}int main(){ int num1, num2; /* Get the input of two variables */ cout << "Enter the 1st Number:"; cin >> num1; cout << "Enter the 2nd Number:"; cin >> num2; /* Call Function swap_two_numbers with two Parameters*/ swap_two_numbers(&num1, &num2); /* Display the two numbers after swapping*/ cout << "***After swapping***\n"; cout << "Number 1: " << num1 << "\nNumber 2: " << num2;}
#include <iostream>using namespace std;/* This function swap two numbers without using third variable*/void swap_two_numbers(int *num1, int *num2){ *num1 = (*num1) + (*num2); *num2 = (*num1) - (*num2); *num1 = (*num1) - (*num2);}int main(){ int num1, num2; /* Get the input of two variables */ cout << "Enter the 1st Number:"; cin >> num1; cout << "Enter the 2nd Number:"; cin >> num2; /* Call Function swap_two_numbers with two Parameters*/ swap_two_numbers(&num1, &num2); /* Display the two numbers after swapping*/ cout << "***After swapping***\n"; cout << "Number 1: " << num1 << "\nNumber 2: " << num2;}
Input/Output:
Enter the 1st Number:123
Enter the 2nd Number:255
***After swapping***
Number 1: 255
Number 2: 123
Program in Java
Here is the source code of the Java Program to Swap two numbers without using a third variable using function.
Code:
import java.util.Scanner;
public class SwapTwoNumbersWithoutThird { /* This function swap two numbers without using third variable */ static void SwapTwoNumbers(int num1, int num2) { num1 = num1 + num2; num2 = num1 - num2; num1 = num1 - num2;
System.out.println("***After Swapping***"); System.out.println("Number 1:" + num1 + "\nNumber 2:" + num2); }
public static void main(String[] args) { Scanner sc = new Scanner(System.in); /* Get the input of two variables */ System.out.println("Enter the 1st Number:"); int num1 = sc.nextInt(); System.out.println("Enter the 2nd Number:"); int num2 = sc.nextInt(); /* Call Function SwapTwoNumbers with two Parameters */ SwapTwoNumbers(num1, num2); sc.close(); }}
Input/Output:
Enter the 1st Number:
52
Enter the 2nd Number:
78
***After Swapping***
Number 1:78
Number 2:52
import java.util.Scanner;
public class SwapTwoNumbersWithoutThird { /* This function swap two numbers without using third variable */ static void SwapTwoNumbers(int num1, int num2) { num1 = num1 + num2; num2 = num1 - num2; num1 = num1 - num2;
System.out.println("***After Swapping***"); System.out.println("Number 1:" + num1 + "\nNumber 2:" + num2); }
public static void main(String[] args) { Scanner sc = new Scanner(System.in); /* Get the input of two variables */ System.out.println("Enter the 1st Number:"); int num1 = sc.nextInt(); System.out.println("Enter the 2nd Number:"); int num2 = sc.nextInt(); /* Call Function SwapTwoNumbers with two Parameters */ SwapTwoNumbers(num1, num2); sc.close(); }}
Program in Python
Here is the source code of the Python Program to Swap two numbers without using a third variable using function.
Code:
""" This function swap two numbers without using third variable """
def swap_two_numbers(num1, num2): num1 = num1+num2 num2 = num1-num2 num1 = num1-num2 """ Display the two numbers after swapping """ print("***After swapping***") print("Number 1: ", num1, "\nNumber 2: ", num2)
""" Get the input of two variables """num1 = int(input("Enter the 1st Number:"))num2 = int(input("Enter the 2nd Number:"))""" Call Function swap_two_numbers with two Parameters """swap_two_numbers(num1, num2)
Input/Output:
Enter the 1st Number:14
Enter the 2nd Number:13
***After swapping***
Number 1: 13
Number 2: 14
""" This function swap two numbers without using third variable """
def swap_two_numbers(num1, num2): num1 = num1+num2 num2 = num1-num2 num1 = num1-num2 """ Display the two numbers after swapping """ print("***After swapping***") print("Number 1: ", num1, "\nNumber 2: ", num2)
""" Get the input of two variables """num1 = int(input("Enter the 1st Number:"))num2 = int(input("Enter the 2nd Number:"))""" Call Function swap_two_numbers with two Parameters """swap_two_numbers(num1, num2)
0 Comments
Please do not Enter any spam link in the comment box