Swap the value using c coderinme

Swap the value using c

This is a program to interchange the value of two variables. Note :- (both ways using temporary variable and not using any other value).

#include<stdio.h>
void main(){
	int s,f,tempr,n;

	printf("Hi, Please enter value of two variables S and F:\n");
 
 	scanf("%d",&s);

	scanf("%d",&f);
	
	printf("Now  Press 1 for temp_variable method \nor  2 for simple method\n");
 

	scanf("%d",&n);

	if(n==1){
		printf("First Method:\n");
		tempr=f;
		f=s;
		s=tempr;
		
		printf("value of S and F =%d and %d\n",s,f);
	}
	else{
		printf("Second Method:\n");
		s=s+f;
		s=s-f;
		f=f-s;
		
		printf("value of S and F =%d and %d\n",s,f);
	}
	
}

output of Swap the value

Hi, Please enter value of two variables S and F:
12 34
Now Press 1 for temp_variable method
or 2 for simple method
2
Second Method:
value of S and F =12 and 22

1
First Method:
value of S and F =34 and 12

We can aslo do this by using pointer
Click here for pointer

Swap the value
All rights reserved. No part of this Post may be copied, distributed, or transmitted in any form or by any means, without the prior written permission of the website admin, except in the case of brief quotations embodied in critical reviews and certain other noncommercial uses permitted by copyright law. For permission requests, write to the owner, addressed “Attention: Permissions Coordinator,” to the admin @coderinme

Leave a reply:

Your email address will not be published.