rand() in C++ Programming


This program generates and prints 10 random numbers between 0 and 9 using the rand() function.

The program contains a function randnum() which has a for loop that runs 10 times. In each iteration of the loop, a random number between 0 and 9 is generated using the rand() function and stored in the random variable. This number is then printed on the console using the cout statement.

In the main() function, the randnum() function is called to execute the code in the function. Finally, the function returns 0 to indicate successful program execution.

Source Code

#include<iostream>
using namespace std;
void randnum()
{
    int random;
    for(int i=0; i<10; i++)
    {
        random=rand()%10;
        cout<<random<<endl;
    }
}
int main()
{
    randnum();
    return 0;
}
 
 

Output

1
7
4
0
9
4
8
8
2
4
To download raw file Click Here

Program List


Flow Control

IF Statement Examples


Switch Case


Goto Statement


Break and Continue


While Loop


Do While Loop


For Loop


Friend Function in C++


String Examples


Array Examples


Structure Examples


Structure & Pointer Examples


Structure & Functions Examples


Enumeration Examples


Template Examples


Functions


Inheritance Examples

Hierarchical Inheritance


Hybrid Inheritance


Multilevel Inheritance


Multiple Inheritance


Single Level Inheritance


Class and Objects

Constructor Example


Destructor Example


Operator Overloading Example


Operator and Function Example


List of Programs


Pointer Examples


Memory Management Examples


Pointers and Arrays


Virtual Function Examples