C Program to Swap First and Last Digit Of a Number

C Program to Swap First and Last Digit Of a Number

C program to swap first and last digit of a number; Through this tutorial, we will learn how to swap first and last digit of a number in c programs.

Programs to Swap First and Last Digit Of a Number in C

  • Program 1 – C Program to Swap First and Last Digit Of a Number
  • Program 2 – C Program to Swap First and Last Digit Of a Number

Program 1 – C Program to Swap First and Last Digit Of a Number

/* C Program to Swap First and Last Digit Of a Number */
 
#include <stdio.h>
#include <math.h>
 
int main()
{
  	int Number, FirstDigit, DigitsCount, LastDigit, a, b, SwapNum;
 
  	printf("\n Please Enter any Number that you wish  : ");
  	scanf("%d", & Number);
  	
  	DigitsCount = log10(Number); 	
  	FirstDigit = Number / pow(10, DigitsCount);
  	
  	LastDigit = Number % 10;
  	
  	a = FirstDigit * (pow(10, DigitsCount));
  	b = Number % a;
  	Number = b / 10;
  	
  	SwapNum = LastDigit * (pow(10, DigitsCount)) + (Number * 10 + FirstDigit);
	    
	printf(" \n The Number after Swapping First Digit and Last Digit =  %d", SwapNum);
 
  	return 0;
}

The output of the above c program; as follows:

Please Enter any Number that you wish  : 456
The Number after Swapping First Digit and Last Digit =  654

Program 2 – C Program to Swap First and Last Digit Of a Number

/* C Program to Swap First and Last Digit Of a Number */
 
#include <stdio.h>
#include <math.h>
 
int main()
{
  	int Number, FirstDigit, DigitsCount, LastDigit, a, b, SwapNum;
 
  	printf("\n Please Enter any Number that you wish  : ");
  	scanf("%d", & Number);
  	
  	DigitsCount = log10(Number); 	
  	FirstDigit = Number / pow(10, DigitsCount);
  	
  	LastDigit = Number % 10;
  	
  	SwapNum = LastDigit;
  	SwapNum = SwapNum  * (round(pow(10, DigitsCount)));
  	SwapNum = SwapNum + Number % (int)(round(pow(10, DigitsCount)));
  	SwapNum = SwapNum - LastDigit;
  	SwapNum = SwapNum + FirstDigit;
	    
	printf(" \n The Number after Swapping First Digit and Last Digit =  %d", SwapNum);
 
  	return 0;
}

The output of the above c program; as follows:

Please Enter any Number that you wish  : 586
The Number after Swapping First Digit and Last Digit =  685

Recommended C Programs

AuthorAdmin

My name is Devendra Dode. I am a full-stack developer, entrepreneur, and owner of Tutsmake.com. I like writing tutorials and tips that can help other developers. I share tutorials of PHP, Python, Javascript, JQuery, Laravel, Livewire, Codeigniter, Node JS, Express JS, Vue JS, Angular JS, React Js, MySQL, MongoDB, REST APIs, Windows, Xampp, Linux, Ubuntu, Amazon AWS, Composer, SEO, WordPress, SSL and Bootstrap from a starting stage. As well as demo example.

Leave a Reply

Your email address will not be published.