Tuesday 3 July 2012

Swapping of numbers in C


Code -


//_________Swap 2 Numbers inputted by the user_________

//________________Include Header Files_____________

#include<stdio.h>

#include<conio.h>

//_______Header field included mainly for the gecth() function________

int main()

//_______Function from where program execution starts_________________

{
int first_number = 0, second_number = 0, temp_number = 0;

printf("\n\n\t\t   ______Program will Swap 2 Numbers_______");

printf("\n\n\n Enter the First Number  - ");

//___Prompting the user to enter First Number_____

scanf("%d",&first_number);

//___Reading the user_input and storing input in variable first_number___

printf("\n Enter the Second Number - ");

//__Prompting the user to enter Second Number___

scanf("%d",&second_number);

//___Reading the user_input and storing input in variable second_number___

//__Displaying the user input once again____

printf("\n\n\t Numbers before swapping :- \n\n\t\t First  - %d \n\n\t\t Second - %d", first_number, second_number);

//____________Swapping of two numbers________________

temp_number = second_number;

//__Assigning the value of second_number in a temp_number__

second_number = first_number;

//__Assigning the value of first_number to second_number___

first_number = temp_number;

//__Assigning the value in temp_nhumber to first_number___

//__Displaying the numbers after swapping___

printf("\n\n\t Numbers after swapping :- \n\n\t\t First  - %d \n\n\t\t Second - %d", first_number, second_number);

printf("\n\n________________________________________________________________________________");

getch();

return 0;

}

Follow Us on Facebook - Assignment Hub

No comments:

Post a Comment