Dr. A.P.J. Abdul Kalam Technical University, Lucknow
KCS101 / KCS201 Programming for Problem Solving - Using C Language
Lab Exercises
30. Write a program to swaps two elements using the concept of pointers.
/*
File: Prgrm30.c
Author: Aditya Saini
Date: Sept 30, 2019
Description: Program to swap two elements using the concept of pointers.
*/
#include <stdio.h>
void swap (int *, int *);
int main (void)
{
int variable_1, variable_2;
//Input the value of variables.
printf ("Input the value of first variable: ");
scanf ("%d", &variable_1);
printf ("Input the value of second variable: ");
scanf ("%d", &variable_2);
//Calling swap function.
swap (&veriable_1, &veriable_2);
//Output the result.
printf ("\nAfter Swapping\n");
printf ("Value of first variable: %d \n", variable_1);
printf ("Value of second variable: %d \n", variable_2);
return 0;
};
void swap (int *first, int *second)
{
int temp;
//Swapping the elements
temp = *first;
*first = *second;
*second = temp;
};
/*
File: Prgrm30.c
Author: Aditya Saini
Date: Sept 30, 2019
Description: Program to swap two elements using the concept of pointers.
*/
#include <stdio.h>
void swap (int *, int *);
int main (void)
{
int variable_1, variable_2;
//Input the value of variables.
printf ("Input the value of first variable: ");
scanf ("%d", &variable_1);
printf ("Input the value of second variable: ");
scanf ("%d", &variable_2);
//Input the value of variables.
printf ("Input the value of first variable: ");
scanf ("%d", &variable_1);
printf ("Input the value of second variable: ");
scanf ("%d", &variable_2);
//Calling swap function.
swap (&veriable_1, &veriable_2);
//Output the result.
printf ("\nAfter Swapping\n");
printf ("Value of first variable: %d \n", variable_1);
printf ("Value of second variable: %d \n", variable_2);
//Output the result.
printf ("\nAfter Swapping\n");
printf ("Value of first variable: %d \n", variable_1);
printf ("Value of second variable: %d \n", variable_2);
};
void swap (int *first, int *second)
{
int temp;
//Swapping the elements
temp = *first;
*first = *second;
*second = temp;
};
void swap (int *first, int *second)
{
int temp;
//Swapping the elements
temp = *first;
*first = *second;
*second = temp;
};
Output
Input the value of first variable: 15
Input the value of second variable: 17
After Swapping
Value of first variable: 17
Value of second variable: 15
Input the value of first variable: 15
Input the value of second variable: 17
After Swapping
Value of first variable: 17
Value of second variable: 15
Input the value of second variable: 17
After Swapping
Value of first variable: 17
Value of second variable: 15
No comments:
Post a Comment
Please do not post spam links.