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 print the PASCAL Triangle up to the Nth row where n is a value inputted by the user

 C Program to print the PASCAL Triangle up to the Nth row where n is a value inputted by the user.  .    In this example, we will print a Pascal's triangle upto to the value of the Nth row entered by the user. In the output section we have taken the value of n as 6 and the pascal's triangle is shown in the output. No, it is not that you have to take the value of n as 6, you can enter any value according to your choice. And the required pascal's tringle will be available on the screen.   What is Pascal's Triangle? Pascal's Triangle is a triangular arrangement of the binaomaial coefficients of the expansion (x+y) n  for positive integral values of n. PROGRAM  TO PRINT THE PASCAL TRIANGLE UP TO THE N-TH ROW WHERE N IS A VALUE INPUTTED BY THE USER: #include <stdio.h> #include <conio.h> void  main() {      int  n,coef=1,space,i,j;     printf( "Enter the value of n:" ); ...

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...

C program to convert a given temperature value from Fahrenheit to Centigrade and vice versa?

  Write a C program to convert a given temperature value from Fahrenheit to Centigrade and vice versa?          In this example we will learn how to convert a given temperature in Fahrenheit to Centigrade and to convert given  temperature value from Centigrade to Fahrenheit depending on the choice of the user.                To solve this program we should have basic knowledge in c and most importantly a clear concept on : Switch Statement             And we must know the formula of conversion from Fahrenheit to Centigrade(Celsius) and Centigrade(Celsius) to Fahrenheit. Fahrenheit To Centigrade Centigrade To Fahrenheit PROGRAM       #include <stdio.h> #include <conio.h> void   main () {      float   f , c ;      int   ch ;      printf ( "...

C Program to find out the biggest of three numbers using Nested if Statement.

 C Program to find out the biggest of three numbers using Nested if Statement. In this example we will learn how to find the biggest of three numbers using Nested if statement. LOGIC OF THE BIGGEST OF THREE NUMBERS: At first we have declared three variables a,b and c and we have stored the numbers inputted by the user in this three variables. Then we have to check if the value of a greater than b then it will check if a greater than c if it is greater then it will print the value of a is the biggest of the three numbers and if it is not greater than it will print c is the biggest of three numbers. If the value of a not greater than b then it will not execute the above statements  Then it will move to the second statement that is else if and will check if b greater than a if it is true then it will check if b greater than c if it is true it will print the value of b is the biggest of the three numbers if not then it will print the value of c is the biggest of three numbers....