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 using third variable."
Logic used :
Store the value of C in a temporary variable
Assign the value of C in D
Then assign the value of temporary variable in D.
Demo :
temp = c;
c = d;
d = temp;
C program :
// Program by Shyam Kanth#include<stdio.h>int main(){int c,d,temp;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);temp = c;c = d;d = temp;printf("After swapping : \n");printf("C = %d and D = %d.",c,d);return 0;}
- #inlcude < stdio.h > : Header file for input output
- int : Integer data type
- c,d,temp : Variable used
- printf : Function for displaying messages
- scanf : Function for taking input
Do comment, if any doubt
Know about me : Shyam Kanth
No comments:
Post a Comment