How to pick a random digit from a number in C

1 Answer

0 votes
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <time.h>

int main(void) {
    int num = 213859;
    char str[6];

    sprintf(str,"%d", num);

    int size = strlen(str);
  
    srand((unsigned int)time(NULL));
  
    int digit = str[rand() % (size - 1)] - '0';
  
    printf("%d", digit);
 
    return 0;
}
 
 
 
/*
run:
   
5
   
*/


 



answered Feb 2, 2024 by avibootz

Related questions

1 answer 183 views
1 answer 181 views
1 answer 120 views
1 answer 171 views
1 answer 146 views
1 answer 146 views
146 views asked Nov 22, 2022 by avibootz
...