Swapping of two numbers

Swapping of two numbers in c:

Write a program to swap two numbers.

Input:

10

20

Output:

Before swapping the values: 10,20

After swapping the values: 20,10


Program:

#include <stdio.h>  

int main()  

{  

    int a,b,temp;

   scanf("%d",&a);  

   scanf("%d",&b); 

   printf("Before swapping the values: %d,%d\n",a,b); 

   temp=a;

   a=b;

   b=temp;

    printf("After swapping the values: %d,%d",a,b); 

}  


Second Method:

Write a program to swap two numbers using call by value and call by reference.


Program:

#include <stdio.h>  

void swap (int *a, int *b)  

{  

    int temp;   

    temp = *a;  

    *a=*b;  

    *b=temp;  

}  

int main()  

{  

    int a,b;

   scanf("%d",&a);  

   scanf("%d",&b);  

    printf("Before swapping the values: %d,%d\n",a,b); 

    swap(&a,&b);  

    printf("After swapping the values: %d,%d",a,b); 


Python:

Input:

10

20

Output:

Before swapping the values: 10 20

After swapping the values: 20 10


Program:

a=int(input())

b=int(input())

print("Before swapping the values:",a,b)

temp=a

a=b

b=temp

print("After swapping the values:",a,b)


Post a Comment

Previous Post Next Post

Contact Form