My Profile

I, Das ShrikKrishna J. MCA III IMSCD&R, Ahmednagar.

Monday, 15 August 2011

Swap Values of Two Arrays

Write a Program in C to Swap Values of Two Arrays
#include<stdio.h>
#include<conio.h>
void main()
{
int swap,i,num1[2],num2[2];
clrscr();
printf("Enter two numbers for 1st array\n");
for(i=0;i<=1;i++) //Continue this loop for 2 values i.e. from 0 to 1
{
scanf("%d",&num1[i]);
}
printf("Enter two numbers for 2nd array\n");
for(i=0;i<=1;i++)
{
scanf("%d",&num2[i]);
}
printf("\nBefore swapping \nnum1 = %d, %d \nnum2 = %d,%d\n\n",num1[0],num1[1],num2[0],num2[1]);
//Swapping contents of num1 and num2
for(i=0;i<=1;i++)
{
swap=num1[i]; //Assigning value of num1 to temporary variable
num1[i]=num2[i]; //Assigning value of num2 to num1
num2[i]=swap; //Assigning value of temporary variable to num2
}
printf("After swapping \nnum1 = %d, %d \nnum2 = %d,%d\n\n",num1[0],num1[1],num2[0],num2[1]);
getch();
}

No comments:

Post a Comment

 
Design by Free WordPress Themes | Bloggerized by Lasantha - Premium Blogger Themes | cheap international calls