# Program to generate a random three digit even number

Last Updated : 18 Jan, 2024

Write a program to generate a random three-digit even number.

Examples:

Example 1: 482
Example 2: 736

Approach: To solve the problem, follow the below idea:

We can generate a random three-digit number by generating a random integer first and then modulo it by 900. Now, after modulo with 900, the remainder can be in the range 0 to 899, so in order to have the remainder as a three-digit number only, we can further add 100 to make the range 100 to 999. If the generated number is equal to 999, then decrement it by 1. Else, if the generated number is an odd number other than 999, increment it by 1 to get the random three-digit even number.

Step-by-step algorithm:

• Generate a random number and modulo it by 900.
• Now, add 100 to the remainder.
• If the generated number is 999, decrement it by 1 to get 998.
• Otherwise, if the generated number is some other three-digit odd number, increment it by 1 to get the required three-digit number.

Below is the implementation of the algorithm:

## C++

 `#include ` `#include ` `#include `   `using` `namespace` `std;`   `int` `main()` `{` `    ``srand``(``time``(NULL));` `    ``// Generate a random three-digit number` `    ``int` `number = (``rand``() % 900) + 100;` `    ``// Ensure it is even` `    ``if` `(number == 999) {` `        ``number--;` `    ``}` `    ``else` `if` `(number % 2 != 0) {` `        ``number++;` `    ``}` `    ``cout << ``"Generated number: "` `<< number << endl;` `    ``return` `0;` `}`

## C

 `#include ` `#include ` `#include `   `int` `main()` `{` `    ``srand``(``time``(NULL));` `    ``// Generate a random three-digit number` `    ``int` `number = (``rand``() % 900) + 100;` `    ``// Ensure it is even` `    ``if` `(number == 999) {` `        ``number--;` `    ``}` `    ``else` `if` `(number % 2 != 0) {` `        ``number++;` `    ``}` `    ``printf``("Generated number: %d\n", number);` `    ``return` `0;` `}`

## Java

 `import` `java.util.Random;`   `public` `class` `RandomNumberGenerator {` `    ``public` `static` `void` `main(String[] args)` `    ``{` `        ``Random rand = ``new` `Random();`   `        ``// Generate a random three-digit number` `        ``int` `number = rand.nextInt(``900``) + ``100``;`   `        ``// Ensure it is even` `        ``if` `(number == ``999``) {` `            ``number--;` `        ``}` `        ``else` `if` `(number % ``2` `!= ``0``) {` `            ``number++;` `        ``}`   `        ``System.out.println("Generated number: " + number);` `    ``}` `}`

## Python3

 `import` `random`     `def` `main():` `    ``# Generate a random three-digit number` `    ``number ``=` `random.randint(``100``, ``999``)`   `    ``# Ensure it is even` `    ``if` `number ``=``=` `999``:` `        ``number ``-``=` `1` `    ``elif` `number ``%` `2` `!``=` `0``:` `        ``number ``+``=` `1`   `    ``print``("Generated number:", number)`     `if` `__name__ ``=``=` `"__main__":` `    ``main()`

## C#

 `using` `System;`   `class` `Program {` `    ``static` `void` `Main()` `    ``{` `        ``Random rand = ``new` `Random();`   `        ``// Generate a random three-digit number` `        ``int` `number = rand.Next(100, 1000);`   `        ``// Ensure it is even` `        ``if` `(number == 999) {` `            ``number--;` `        ``}` `        ``else` `if` `(number % 2 != 0) {` `            ``number++;` `        ``}`   `        ``Console.WriteLine("Generated number: " + number);` `    ``}` `}`

## Javascript

 `function` `generateRandomNumber() {` `    ``// Generate a random three-digit number` `    ``let number = Math.floor(Math.random() * 900) + 100;`   `    ``// Ensure it is even` `    ``if` `(number === 999) {` `        ``number--;` `    ``} ``else` `if` `(number % 2 !== 0) {` `        ``number++;` `    ``}`   `    ``return` `number;` `}`   `// Example usage` `const generatedNumber = generateRandomNumber();` `console.log("Generated number:", generatedNumber);`

Output

```Generated number: 448
```

Time Complexity: O(1), The algorithm generates a random number in constant time.
Auxiliary Space: O(1)

Previous
Next