Skip to main content

C Program to Swap the values of two variables using function.

C Program to Swap the values of two variables using function.

    In this example, we swap the values of two variables using function. We have declared a function swap.

PROGRAM

#include<stdio.h>
#include<conio.h>
void swap(int*,int *);
int main()
{
    int a,b;
    printf("Enter the values of a and b : ");
    scanf("%d%d",&a,&b);
    printf("Before swapping the values in main a=%d , b=%d\n",a,b);
    swap(&a,&b);
    printf("After swapping the values in main a=%d , b=%d\n",a,b);
    getch();
}
void swap(int *a,int *b)
{
    int temp;
    temp=*a;
    *a=*b;
    *b=temp;
    printf("After swapping values in function a=%d, b=%d\n",*a,*b);
}


OUTPUT

 Enter the values of a and b : 10

 20

 Before swapping the values in main a=10 , b=20

 After swapping values in function a=20, b=10  

 After swapping the values in main a=20 , b=10  


NOTE: You Can Comment Your Code if you have solved differently and we will pin it in the comment section.  Let's Learn together. 


MOTTO: 

You Learn, I Learn together We Learn.

Comments

Popular posts from this blog

C Program to Check Even or Odd Without Modulous and Bitwise Operator

  C Program to Check Even or Odd Without Modulous and Bitwise Operator                   In this example we will check even or odd without Modulous Operator. To do this program we should know what is even and odd number.                   Even Number: Even numbers are those numbers which are divisible by 2. That means if we divide a number by 2 and if its remainder appears 0 then it is an even number.                                 For Example: 2,4,6,8,10,12,.... are all even numbers.                              ...

C Program to check if a given square matrix is symmetric or not using function

 C Program to check if a given square matrix is symmetric or not using function.     In this example, we will check if a given square matrix entered by the user is symmetric or not. Here in the output we have taken a square matrix of 3x3. PROGRAM TO CHECK IF A GIVEN SQUARE MATRIX IS SYMMETRIC OR NOT USING FUNCTION #include <stdio.h> #include <conio.h> void  symmetric( int , int , int  [50][50]); void  main() {      int  row,column,i,j,a[50][50];     printf( "Enter the size of the matrices: " );     scanf( "%d" ,&row);     column=row;     printf( "Enter the matrix : " );      for (i=0;i<row;i++)     {          for (j=0;j<column;j++)         {    ...

C Program to Check a Whether a Number is Divisible by 5 or Not

 C Program to Check a Whether a Number is Divisible by 5 or Not            In this program we will check a number entered by the user will be divisible by 5 or Not. We have solved this problem using if-else statement. Program: #include <stdio.h> #include <conio.h> int  main() {      int  x;     printf( "Enter a number" );     scanf( "%d" ,&x);      if (x% 5 == 0 )         printf( "The number %d is divisible by 5" ,x);      else         printf( "The number %d is not divisible by 5" ,x);      return   0 ; } Output: NOTE:   You Can Comment Your Code if you have solved differently and we will pin it in the comment section.  Let's Learn toget...

Overview of C - Notes

 Overview of C - Notes   Importance of C Programming:   C is a robust language and has a rich set of built in functions, data types and operators which can be used to write any complex programs. Program written in C are efficient and fast due to availability of several data types and operators. C has the capability of an assembly language with the feature of high level language, so it is well suited for writing in both system software and application software. C is highly portable language, code written in one machine can be moved to other which is very important and powerful feature. C also supports low level features like bit level programming. C has high level constructs and it is more user friendly as its syntaxes approaches to English like language. History of C Language:                 In 1972, Dennis Ritchie developed a programming language at the AT&T Bell Laboratories in USA...

C Program to find Mean and Standard deviation(SD) for a set of n numbers using array.

 C Program to find Mean and Standard deviation(SD) for a set of n numbers using array.     In this example, we will calculate the mean and standard deviation for a set of n numbers entered by the user using array.    PROGRAM TO FIND THE MEAN AND STANDARD DEVIATION(SD) FOR A SET OF N NUMBERS USING ARRAY. #include <stdio.h> #include <math.h> #include <conio.h> void   main () {      int   n , i ;      float   x [ 50 ], mean , sd , sum ;      printf ( "How many numbers you want to enter : " );      scanf ( " %d " ,& n );      printf ( "Enter the numbers " );      for ( i = 0 ; i < n ; i ++)      {          scanf ( " %f " ,& x [ i ]);     }      f...