Project: Swap The Value Of A & B Using Pointers

Learn and Trouble Shoot Problems Related to C-Language here.

Project: Swap The Value Of A & B Using Pointers

Unread postby rajeevmahura » Thu May 22, 2008 6:02 pm

/* Program To Swap The Value Of A and B Using Pointers */
#include<stdio.h>
#include<conio.h>
void swap(int*a,int*b);
void main()
{
int x,y;
clrscr();
printf("Enter The Two Values To A & B for Swap:- ");
scanf("%d%d",&x,&y);
swap(&x,&y);
printf("\nA=%d\tB=%d",x,y);
getch();
}
void swap (int*a,int*b)
{
int temp;
temp=*a;
*a=*b;
*b=temp;
}
------------------------------------------------------------------
Input:- Enter The Two Values To A & B for Swap:- 3 7
Output:- A=7 B=3
--Regards--
Rajeev Mahura
Servers & Storage Professional
http://about.me/rajeevmahura
User avatar
rajeevmahura
 
Posts: 144
Joined: Tue May 20, 2008 5:22 pm
Location: Bangalore | New Delhi
Full Name: Rajeev Mahura
Date Of Birth: 15 Jan 1984
Address: New Delhi
Profession: DOEACC Trainer
DOEACC Center: DOEACC DELHI
Mobile No: 0

Re: Project: Swap The Value Of A & B Using Pointers

Unread postby manojthesingham » Tue Sep 01, 2009 7:51 am

#include<stdio.h>
#include<conio.h>
/*
swapping in C can be done by 3 ways
1) pass by value
2) pass by pointers
*/
void swap_by_value(int a,int b)
{
int t;
t = a;
a = b;
b = t;
printf("\n Cannot Achieve beyond this Scope");
printf("\n After Swap by Value : a = %d b = %d",a,b);
}
void swap_by_pointers(int *a,int *b)
{
int t;
t = *a;
*a = *b;
*b = t;
}

int main(int argc,char **argv)
{
int a,b;
scanf("%d",&a,printf("First : "));
scanf("%d",&b,printf("Second : "));
printf("\n\n Before Swap by Value : a = %d b = %d",a,b);
swap_by_value(a,b);

printf("\n\n Before Swap by Pointers : a = %d b = %d",a,b);
swap_by_pointers(&a,&b);
printf("\n After Swap by Pointers : a = %d b = %d",a,b);

getch();
return 1;
}
If world is running, then I am idle..
If I am idle, then the world is running
User avatar
manojthesingham
 
Posts: 15
Joined: Tue Sep 01, 2009 5:39 am
Location: Coimbatore,Tamilnadu
Full Name: manoj
Date Of Birth: 31 Jul 1989
Address: Coimbatore18Tamilnadu
Mobile No: 0


Return to C-Language

Who is online

Users browsing this forum: No registered users and 1 guest

cron