Program to generate a random single digit number Last Updated : 18 Jan, 2024 Comments Improve Suggest changes Like Article Like Report Write a program to generate a random single-digit number. Example 1: 7Example 2: 3 Approach: To solve the problem, follow the below idea: To generate a random single-digit number, we can first generate a random integer and then take apply modulo to get the last digit of that random integer. This last digit can be the random single digit number. Step-by-step algorithm: Generate a random integer.Take modulo 10 of the random integer to get a single digit random number.Below is the implementation of the algorithm: C++ #include <iostream> #include <cstdlib> #include <ctime> using namespace std; int main() { srand(time(NULL)); int randomDigit = rand() % 10; cout << randomDigit << endl; return 0; } C #include <stdio.h> #include <stdlib.h> #include <time.h> int main() { srand(time(NULL)); int randomDigit = rand() % 10; printf("%d\n", randomDigit); return 0; } Java import java.util.Random; public class RandomDigit { public static void main(String[] args) { Random random = new Random(); int randomDigit = random.nextInt(10); System.out.println(randomDigit); } } Python3 import random random_digit = random.randint(0, 9) print(random_digit) C# using System; class Program { static void Main() { Random random = new Random(); int randomDigit = random.Next(0, 10); Console.WriteLine(randomDigit); } } JavaScript const randomDigit = Math.floor(Math.random() * 10); console.log(randomDigit); Output8 Time Complexity: O(1) as the time complexity of generating a random number is constant.Auxiliary Space: O(1) Create Quiz Comment S srinam Follow 0 Improve S srinam Follow 0 Improve Article Tags : DSA Explore DSA FundamentalsLogic Building Problems 2 min read Analysis of Algorithms 1 min read Data StructuresArray Data Structure 3 min read String in Data Structure 2 min read Hashing in Data Structure 2 min read Linked List Data Structure 3 min read Stack Data Structure 2 min read Queue Data Structure 2 min read Tree Data Structure 2 min read Graph Data Structure 3 min read Trie Data Structure 15+ min read AlgorithmsSearching Algorithms 2 min read Sorting Algorithms 3 min read Introduction to Recursion 15 min read Greedy Algorithms 3 min read Graph Algorithms 3 min read Dynamic Programming or DP 3 min read Bitwise Algorithms 4 min read AdvancedSegment Tree 2 min read Binary Indexed Tree or Fenwick Tree 15 min read Square Root (Sqrt) Decomposition Algorithm 15+ min read Binary Lifting 15+ min read Geometry 2 min read Interview PreparationInterview Corner 3 min read GfG160 3 min read Practice ProblemGeeksforGeeks Practice - Leading Online Coding Platform 1 min read Problem of The Day - Develop the Habit of Coding 5 min read Like