Swap two numbers using function

  

Problem Statement:-  Program to Swap two numbers using function.


Sample Input/Output:-


Sample Input First:

11

17

Sample Output First: 

17

11

Sample Input Second: 

39

85

Sample Output

85

39


Data requirement:-


  Input Data:- num1, num2


Additional Data:- temp


  Output Data:-num1, num2


Program in C

Here is the source code of the C Program to Swap two numbers using function.


Code:

#include <stdio.h>/*printf,scanf definitions */
/* This function swap two numbers using third variable*/
void swap_two_numbers(int *num1, int *num2)
{
    int temp = *num1;
    *num1 = *num2;
    *num2 = temp;
}
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:11

Enter the 2nd Number:17

***After swapping***

Number 1: 17        

Number 2: 11  


Program in C++

Here is the source code of the C++ Program to Swap two numbers using function.


Code:

#include <iostream>
using namespace std;
/* This function swap two numbers using third variable*/
void swap_two_numbers(int *num1, int *num2)
{
    int temp = *num1;
    *num1 = *num2;
    *num2 = temp;
}
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:39

Enter the 2nd Number:85

***After swapping***

Number 1: 85        

Number 2: 39   


Program in Java

Here is the source code of the Java Program to Swap two numbers using function.


Code:

import java.util.Scanner;

public class SwapTwoNumbers {
    /* This function swap two numbers using third variable */
    static void SwapTwoNumberUsingThird(int num1, int num2) {
        int temp = num1;
        num1 = num2;
        num2 = temp;

        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 SwapTwoNumberUsingThird with two Parameters */
        SwapTwoNumberUsingThird(num1, num2);
        sc.close();
    }
}

Input/Output:

Enter the 1st Number:

222

Enter the 2nd Number:

333

***After Swapping***

Number 1:333

Number 2:222

Program in Python

Here is the source code of the Python Program to Swap two numbers using function.


Code:

""" This function swap two numbers using third variable. """


def swap_two_numbers(num1, num2):
    temp = num1
    num1 = num2
    num2 = temp
    """ 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:5

Enter the 2nd Number:9

***After swapping***

Number 1:  9

Number 2:  5

Most Recommend Questions:-







More Questions:-



Post a Comment

0 Comments