Tech Unleashed

Wohoo!! We're on YouTube

Saturday, 15 January 2022

C program for "Two numbers are input through the keyboard into two locations C and D. Write a program to interchange the contents of C and D without using third variable."

Problem:  C program for "Two numbers are input through the keyboard into two locations C and D. Write a program to interchange the contents of C and D without using third variable."

Logic used : 
Add C and D and store the value in C.
Subtract D from C and store the value in D.
Again subtract D from C and store the value in C.

Demo :
c = c + d;
d = c - d;
c = c - d;

C program :

// Program by Shyam Kanth

#include<stdio.h>

int main(){
    int c,d;
    printf("Enter C : ");
    scanf("%d",&c);
    printf("Enter D : ");
    scanf("%d",&d);
    printf("Before swapping : \n");
    printf("C = %d and D = %d.\n",c,d);
    c = c+d;
    d = c-d;
    c = c-d;
    printf("After swapping : \n");
    printf("C = %d and D = %d.",c,d);
    return 0;
}

Codes used : 
  • #include < stdio.h > : Header file for input ouptut
  • int : Integer data type
  • c,d : Variable used
  • printf : Function for displaying message
  • scanf : Function for taking input
  • %d : Format specifier for integer value
Do comment, if any doubt
Know about me : Shyam Kanth

No comments:

Post a Comment