100% swapping 2 numbers without using third variable
//program to swap two numbers without using third variable
#include <stdio.h>
void main()
{
int p,q;
printf("\n enter two integers:");
scanf("%d%d",&p,&q);
printf("\n values of p and q before swapping are %d and %d respectively",p,q); //suppose p=5, q=2
p=p+q; //afterthis is run, p=7 and q=2
q=p-q; //after this is run p=7 and q=5
p=p-q; //afterthis is run p=2 and q=5
printf("\n values of p and q after swapping are %d and %d respectively",p,q); }
values of p and q after swapping are 0.000000 and 0.000000 respectively
#include <stdio.h>
void main()
{
int p,q;
printf("\n enter two integers:");
scanf("%d%d",&p,&q);
printf("\n values of p and q before swapping are %d and %d respectively",p,q); //suppose p=5, q=2
p=p+q; //afterthis is run, p=7 and q=2
q=p-q; //after this is run p=7 and q=5
p=p-q; //afterthis is run p=2 and q=5
printf("\n values of p and q after swapping are %d and %d respectively",p,q); }
output ఇలా వచ్చె
enter two integers:5 2
values of p and q before swapping are 5 and 2 respectively
values of p and q after swapping are 2 and 5 respectively
values of p and q before swapping are 5 and 2 respectively
values of p and q after swapping are 2 and 5 respectively
Note:
పొరపాటున పైన కోడ్లో చివరిరెండు printf లలో %d ల బదులు %f లు పెడితే అప్పుడు https://www.programiz.com/c-programming/online-compiler/ లో ఔట్పుట్ ఈ క్రింది విధంగా వచ్చె
enter two integers:7 2
values of p and q before swapping are 0.000000 and 0.000000 respectivelyvalues of p and q after swapping are 0.000000 and 0.000000 respectively
కామెంట్లు
కామెంట్ను పోస్ట్ చేయండి
దయచేసి మీ సలహాలను సూచనలను స్పష్టంగా పేర్కొనగలరు. plz see that ur comments are 'acceptable' in a value based society.