-->

program to swap any two values stored in array.Let there are 'n' numbers.


using turbo c++
-----------------------------------------------------------------------------------------------------------------------

//program to swap any two values stored in array.Let there are 'n' numbers.

#include <stdio.h>
#include<conio.h>
void main()
{
    int a[100],n,i,location,location1,j;
    printf("enter total number\n");
    scanf("%d",&n);
    for(i=0;i<n;i++)
    {
        printf("enter number for location=%d\n",i);
        scanf("%d",&a[i]);
    }
    printf("numbers before swapping, are:\n");
    for(i=0;i<n;i++)
    {

        printf("%d\n",a[i]);
                 
    }
    printf("enter number's first location to be swapped\n");
    scanf("%d",&location);
    printf("enter number's second location to be swapped\n");               
    scanf("%d",&location1);                                                 
    j=a[location-1];
    a[location-1]=a[location1-1];
    a[location1-1]=j;
    printf("numbers after swapping, are:\n");
    for(i=0;i<n;i++)
    {

        printf("%d\n",a[i]);
                 
    }
    
    getch();


}

-----------------------------------------------------------------------------------------------------------------------
using codeblocks/dev c++/online compiler with return concept
---------------------------------------------------------------------------------------------------------
//program to swap any two values stored in array.Let there are 'n' numbers.

#include <stdio.h>

int main()
{
    int a[100],n,i,location,location1,j;
    printf("enter total number\n");
    scanf("%d",&n);
    for(i=0;i<n;i++)
    {
        printf("enter number for location=%d\n",i);
        scanf("%d",&a[i]);
    }
    printf("numbers before swapping, are:\n");
    for(i=0;i<n;i++)
    {

        printf("%d\n",a[i]);
                 
    }
    printf("enter number's first location to be swapped\n");
    scanf("%d",&location);
    printf("enter number's second location to be swapped\n");               
    scanf("%d",&location1);                                                 
    j=a[location-1];
    a[location-1]=a[location1-1];
    a[location1-1]=j;
    printf("numbers after swapping, are:\n");
    for(i=0;i<n;i++)
    {

        printf("%d\n",a[i]);
                 
    }
    
    return 0;

}

No comments:

Post a Comment