How to create a random 5 digits number and get the last 3 digits in C

1 Answer

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

int main() {
    srand(time(NULL));

    int number5digits = rand() % 90000 + 10000; // Random number between 10000 and 99999

    // Extract the last 3 digits
    int lastThreeDigits = number5digits % 1000;

    printf("5-digits number: %d\n", number5digits);
    printf("Last 3 digits: %d\n", lastThreeDigits);

    return 0;
}



/*
run:

5-digits number: 82718
Last 3 digits: 718

*/

 



answered Feb 2, 2024 by avibootz
...