Tejaswini
Tejaswini
9 years ago

Write a c program to swap two numbers using function.

Solution(By Examveda Team)

Swapping program in c using function

#include<stdio.h>

void swap(int *,int *);

int main(){

        int a,b;

   

    printf("Enter any two integers: ");

    scanf("%d%d",&a,&b);

 

    printf("Before swapping: a = %d, b=%d",a,b);

 

    swap(&a,&b);

 

    printf("nAfter swapping: a = %d, b=%d",a,b);

    return 0;

}

 

void swap(int *a,int *b){

    int *temp;

    temp = a;

    *a=*b;

    *b=*temp;

}

 

Sample output:

Enter any two integers: 3 6

Before swapping: a = 3, b=6

After swapping: a = 6, b =3.


This Question Belongs to User Ask Question >> Miscellaneous

Join The Discussion

Comments ( 1 )

  1. Atul Tiwari
    Atul Tiwari :
    6 years ago

    there is a mistake ..
    int temp;
    temp = *a;
    *a = *b;
    *b = temp;

Related User Ask Questions