Advertisement
Divyansh_Chourey

swap first and last digits of number

Mar 18th, 2024
366
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.51 KB | Source Code | 0 0
  1. #include<stdio.h>
  2. #include<math.h>
  3.  
  4. int main(){
  5.     int a, num, count=0, first_digit, last_digit;
  6.     printf("Enter the number: ");
  7.     scanf("%d", &num);
  8.     a=num;
  9.     while(a!=0){
  10.         a /= 10;
  11.         count++;
  12.     }
  13.     last_digit=num%10;
  14.     first_digit=num/(int)pow(10, (count-1));
  15.    
  16.     num=num-(first_digit*(int)pow(10, count-1));
  17.     num=(num-(last_digit))/10;
  18.    
  19.     if(count==2){
  20.         printf("%d", last_digit);
  21.         printf("%d", first_digit);
  22.     }
  23.     else{
  24.         printf("%d", last_digit);
  25.         printf("%d", num);
  26.         printf("%d", first_digit);
  27.     }
  28. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement