Swap First and Last Digits of a Number( Using Loops)

How can I swap the first and last digits of a number in Java using for for-loops?

A method to swap the digits using for loop:

static int swapDigits(int x) {
System.out.print(x + " -> ");
int sign = Integer.signum(x);
x *= sign; // invert negative number if necessary

int last = x % 10;
int n = x / 10;
int s = 0; // sum for the digits in the middle
for (int p = 1; n > 10; n /= 10, last *= 10, p *= 10) {
s += p * (n % 10);
}
return sign * (10 * (last + s) + n); // restore the sign and get swapped digits
}

Tests:

System.out.println(swapDigits(12345));  // 12345 -> 52341
System.out.println(swapDigits(607081)); // 607081 -> 107086
System.out.println(swapDigits(98760)); // 98760 -> 8769 // leading 0 lost
System.out.println(swapDigits(-12345)); // -12345 -> -52341

How to swap first and last digit in number in c++

To swap two numbers, you need a temporary register

tmp=a;
a=b;
b=tmp;

However, if you are trying to swap digits in n, you need to change n. Which you have destroyed in your loop. Keep a copy of it beforehand?

Or simply note that floor(log(n)/log(10)) gives you the power of 10 for the first digit.

n=23456;
int firstdec = pow(10,floor(log(n)/log(10))); // eg 10000
int firstdig = n/firstdec; // eg 2
int lastdig = n%10; // eg 6
int removed = ((n-firstdig*firstdec)/10)*10 ; // eg 3450
int final = removed + lastdig*firstdec + firstdig; // eg 63452

Interchanging the first and last digits of a number in C

Hope already you know what to fix: printf("%d",&b).

Just for information. You could do it more simply:

int num;
scanf("%d",&num);

char snum[50];

// convert to string
int len = sprintf(snum, "%d", num);

// character swap
char temp = snum[0];
snum[0] = snum[len-1];
snum[len-1] = temp;

// convert to int or use the snum string
int result = atoi(snum);
printf("%d\n", result);

I have to change my number last digit and first digit but without using functions only with integers or loops. For example from 12345 to 52341

To exchange first and last digit in a number ABCDEF --> FBCDEA

num = 12345
res = 52341

the idea is that :

52341 = 12345 - 10005 + 50001

first digit fd = num % 10

decimal places multiplier df = 1

until num is not zero we do
last digit ld = num
num = num / 10
if num != 0 decimal places multiplier df = df*10

result = ABCDEF - F - A*100000 + A + F*100000

int m = numri;
int ld = 0; // last digit(most)
int fd = numri % 10; // first digit(least)
int df = 1; // last digit multiplier 10^n where n is decimal places
while (numri != 0) {
ld = numri;// this will be the last digit in last iteration
numri = numri / 10;
if(numri) df = df * 10;
}
int res = m - fd - ld * df + fd * df + ld;
cout<<res;

example:

if the num = 12345

fd = 12345 % 10 =5
df = 1

num = 12345 / 10 = 1234
df = df*10 = 10

num = 1234 / 10 = 123
df = df*10 = 100

num = 123 / 10 = 12
df = df*10 = 1000

fd = num = 12
num = 12 / 10 = 1
df = df*10 = 10000

fd = num = 1
num = 1/10 =0
df not changed
loop exit

result = 12345 - 5 - 1*10000 + 1 + 5*10000 = 62341


Related Topics



Leave a reply



Submit