Advertisement
sol4r

Swapping the first and the last digit of any 5 random integers in C

Mar 13th, 2023
682
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.61 KB | None | 0 0
  1. #include <stdio.h>
  2. #include<string.h>
  3. #include <stdlib.h>
  4. #include <time.h>
  5.  
  6. void Swap(int n) {
  7.     int a,length;
  8.     char str[20];
  9.     char temp1[1];
  10.     char temp2[1];
  11.     sprintf(str, "%d", n);
  12.     length = strlen(str)-1;
  13.     temp1[0] = str[0];
  14.     temp2[0] = str[length];
  15.     str[length] = temp1[0];
  16.     str[0] = temp2[0];
  17.     printf("\n%s\n",str);
  18. }
  19.  
  20.  
  21. int main()
  22. {
  23.     int i,lower,upper;
  24.     lower = 90;
  25.     upper = 9999;
  26.     for (i = 0; i < 5; i++) {
  27.         int rand_num = (rand() % (upper - lower + 1)) + lower;
  28.         printf("\n%d\n ", rand_num);
  29.         Swap(rand_num);
  30.  
  31.     }
  32.  
  33.  
  34. }
  35.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement