Problem Solving using C Language

0 of 85 lessons complete (0%)

Functions

Program: Swapping of 2 Numbers

Swapping of 2 numbers without Function

#include <stdio.h>
intmain()
{
intx,y,t;
printf("Enter two integers\n");
scanf("%d%d",&x,&y);
printf("Before Swapping\nFirst integer = %d\nSecond integer = %d\n",x,y);
 t=x;
 x=y;
 y=t;
printf("After Swapping\nFirst integer = %d\nSecond integer = %d\n",x,y);
return0;
}

Swapping of 2 numbers with Function

#include <stdio.h>
intmain()
{
intx,y,t;
printf("Enter two integers\n");
scanf("%d%d",&x,&y);
printf("Before Swapping\nFirst integer = %d\nSecond integer = %d\n",x,y);
 swap(x,y);
 printf("After Swapping\nFirst integer = %d\nSecond integer = %d\n",x,y);
return0;
}

voidswap(int x,inty) //Swap function definition
{
 intt;
 t =x;
 x=y;
 y=t;
 printf("After Swapping\nFirst integer = %d\nSecond integer = %d\n",x,y);
}

Swapping of 2 numbers with Function with Reference

#include <stdio.h>
voidswap(int*,int*);//Swap function declaration
intmain()
{
 intx,y;
 printf("Enter the value of x and y\n");
 scanf("%d%d",&x,&y);
 printf("Before Swapping\nx = %d\ny = %d\n",x,y);
 swap(&x,&y);
 printf("After Swapping\nx = %d\ny = %d\n",x,y);
 return0;
}

voidswap(int*a,int*b) //Swap function definition
{
 intt;
 t =*b;
 *b=*a;
 *a=t;
}