March 11, 2019

C Program to Generate Random Numbers

The following is a C Program to Generate Random Numbers on screen after taking input from user. Let's get started.

C Program To Generate Random Numbers
C Program To Generate Random Numbers

Overview:


Commands

stdlib.h includes functions involving memory allocation, process control, conversions and others and
rand() function is used in C to generate random numbers.

Program

#include<stdio.h>
#include<stdlib.h>
int main()
{
    int i,n;
    printf("Five random numbers in [1,100]\n");    
    for(i=1;i<=5;i++)
    {    
        n=rand()%100+1;
        printf("%d\n",n);    
    }
    return 0;
}



Output

Random Numbers Generated
Random numbers Generated


All programs compiled in this post are compiled online.

Books I Prefer:






I hope this article was helpful to understand C Program to Generate Random Numbers! Comment below, if you've got any question. Head back soon for another interesting article on C Programming.