Q:

Write C program to swap first and last digit of a number

0

Write C program to swap first and last digit of a number

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
 
int main(void) {
 
    int num, last, first, temp, swap, count = 0;
 
    printf("Enter any number: ");
    scanf("%d", &num);
 
    temp = num;
    last = temp % 10;
    count = (int)log10(temp);
 
    while(temp>=10)
    {
        temp /= 10;
    }
    first = temp;
    swap = (last * pow(10, count) + first) + (num - (first * pow(10, count) + last));
 
    printf("Last Digit: %d\n", last);
 
    printf("First Digit: %d\n", first);
 
    printf("%d is swapped to %d\n", num, swap);
    return 0;
}

Result:

Enter any number: 123456

Last Digit: 6

First Digit: 1

123456 is swapped to 623451

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Write C program to find the sum of first and last ... >>
<< Write C program to find sum of odd numbers between...