C++ | Generating Random Number

Join Programming and Development Chat Groups on Uphub.io

Introduction

This is a simple program for generating 10 random numbers. The program is extendable. Go enjoy the program. Lets begin…..

Program for random numbers in c++.

#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
void main()
{
//clear the screen.
clrscr();
//generate random number
for(int i=0;i<10;i++)
cout<<rand()<<endl;
//get character
getch();
}

Output
321
5466
123
5452
9021
8443
10
234
8952
20234

How does it work.

  1. No need for input just prints random number.

Extending it.

The program is extendable. This is a basic concept of c++ and can be used in any program. Go extend it.

Explanation.

  1. Include ‘iostream.h’ and ‘conio.h’ and ‘stdlib.h’ files.
  2. Add void main.
  3. Start program by first clearing the screen.
  4. Add a for loop.
  5. Add cout to print number.
  6. Get character.

At the end

You learnt creating the C++ program for generating random number. So now enjoy the program.

Please  share it.

Sharing options: