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

AuthorDevendra Dode

Greetings, I'm Devendra Dode, a full-stack developer, entrepreneur, and the proud owner of Tutsmake.com. My passion lies in crafting informative tutorials and offering valuable tips to assist fellow developers on their coding journey. Within my content, I cover a spectrum of technologies, including 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. Whether you're starting out or looking for advanced examples, I provide step-by-step guides and practical demonstrations to make your learning experience seamless. Let's explore the diverse realms of coding together.

Leave a Reply

Your email address will not be published. Required fields are marked *