Skip to main content

C Program To Swap Two Numbers Without Using/Using Third Variable

 C PROGRAM TO SWAP TWO NUMBERS.

 

In this example, we will learn how to swap two numbers using third variable.

 

FLOWCHART TO SWAP TWO NUMBERS:

 

 

 

 

PROGRAM:

 

#include<stdio.h>
int main()
{
    int a,b,c;
    printf("Enter the value of a : ");
    scanf("%d",&a);
    printf("Enter the value of b : ");
    scanf("%d",&b);
    printf("\nBefore swapping values of a=%d and b=%d",a,b);
    c=a;
    a=b;
    b=c;
    printf("\nAfter swapping values of a=%d and b=%d",a,b);

}

 

OUTPUT:


 

 

C PROGRAM TO SWAP TWO NUMBERS WITHOUT USING THIRD VARIABLE.

 

            Here, we will see how to swap two numbers without using third variable.

           

FLOWCHART TO SWAP TWO NUMBERS WHITHOUT USING THIRD VARIABLE:

 

 

PROGRAM:

 

#include<stdio.h>
int main()
{
    int a,b;
    printf("Enter the value of a : ");
    scanf("%d",&a);
    printf("Enter the value of b : ");
    scanf("%d",&b);
    printf("\nBefore swapping values of a=%d and b=%d",a,b);
    a=a+b;
    b=a-b;
    a=a-b;
    printf("\nAfter swapping values of a=%d and b=%d",a,b);

}


OUTPUT:



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 input Values in a 2x2 square matrix and display the value

 C Program to input Values in a 2x2 square matrix and display the value     In this example, we will accept values from the user in a 2x2 square and matrix and then it is displayed. To solve this problem we must know what is matrices and arrays. What are Arrays?      An array is a data structure for storing more than one data member of similar data type.          Array can be of following types: One-dimensional Array Two-dimensional Array Multi-dimensional Array             In Array we must always initialize values. PROGRAM #include <stdio.h> #include <conio.h> int   main () {      int   a [ 2 ][ 2 ], i , j ;      printf ( "Enter the values of the matrix:" );      for ( i = 0 ; i < 2 ; i ++)     {          for ( j = 0 ; j < 2 ...

C Program to find the Transpose of a Matrix.

C Program to find the Transpose of  a Matrix.       In this example, we will find the transpose of a given matrix according to the requirements of the row size and column size.      Here in the output we have entered a 3x2 Matrix whose transpose will be 2x3 Matrix. PROGRAM #include <stdio.h> #include <conio.h> int   main () {      int   row , column , i , j , a [ 100 ][ 100 ];      printf ( "Enter the row size of the matrix :" );      scanf ( " %d " ,& row );      printf ( "Enter the column size of the matrix : " );      scanf ( " %d " ,& column );      printf ( "Enter the elements of the matrix : " );      for ( i = 0 ; i < row ; i ++)     {  ...

C program to calculate area of a circle using function.

C program to calculate area of a circle. In this example we will learn to calculate the area of a circle. Here in this program we will use 3.14 as the value of pi.  The formula to calculate the area of a circle is:                                                         area=   π  r² Flowchart To Calculate the Area of a Circle: PROGRAM: #include <conio.h> #include <stdio.h> int   main () {      float   radius , area ;      printf ( "Enter the radius of the circle : " );      scanf ( "%f" ,& radius );      area = 3.14 * radius * radius ;          printf ( "Area of the circle is : %.2f" , area );  ...