Saturday, January 5, 2019

Swap Two Numbers Using Pointers in C

I am currently accepting programming work, it projects, school and application development.

programming projects, thesis and capstone projects, IT consulting.

work, computer tutorials, and web development work kindly contact me in the following email address for further details.  If you want to advertise on my website kindly contact me also in my email address also. Thank you.
My email address is the following jakerpomperada@gmail.com, jakerpomperada@aol.com, and jakerpomperada@yahoo.com.

My mobile number here in the Philippines is 09173084360.

My telephone number at home here in Bacolod City, Negros Occidental Philippines is  +63 (034) 4335675.

Here in Bacolod I also accepting computer repair, networking and Arduino Project development at a very affordable price.

My personal website is http://www.jakerpomperada.com

Problem 

Write a program that will ask the user to give two numbers and then 
the program will swap the arrangement of the two numbers and display
the result on the screen.

Solution

/* swap.c
   Author   : Jake Rodriguez Pomperada,BSCS,MAED-IT
   Date     : November 26, 2018  Monday  3:09 PM
   Location : Bacolod City, Negros Occidental
   Tool     : Dev C++ Version 5.11
   Website  : http://www.jakerpomperada.com
   Email    : jakerpomperada@jakerpomperada.com and jakerpomperada@gmail.com
*/
#include <stdio.h>

void swap(int *num1, int *num2);

int main()
{
    int num1=0, num2=0;
    printf("\n\n");
    printf("\tSwap Two Numbers");
    printf("\n\n");
    printf("\tEnter Two Numbers : ");
    scanf("%d%d", &num1, &num2);
    printf("\n\n");
    printf("\tBefore swapping in main");
    printf("\n\n");
    printf("\tValue of num1 = %d \n", num1);
    printf("\tValue of num2 = %d \n\n", num2);
    swap(&num1, &num2);
    printf("\n");
    printf("\tEnd of Program");
    printf("\n\n");
  
}

void swap(int * num1, int * num2)
{
    int temp;
    temp = *num1;
    *num1= *num2;
    *num2= temp;

    printf("\tAfter swapping in swap function ");
    printf("\n\n");
    printf("\tValue of num1 = %d. \n", *num1);
    printf("\tValue of num2 = %d. \n\n", *num2);

}

No comments:

Post a Comment