0% found this document useful (0 votes)
3 views5 pages

C Programming Random Number Functions

The document contains C programming examples demonstrating the use of random number generation, basic arithmetic operations, and functions for calculating squares, checking even/odd status, finding factorials, and determining the largest of three numbers. It explains how to seed the random number generator for variability and includes function prototypes and definitions for various mathematical operations. Each example includes user input and output to illustrate the functionality.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views5 pages

C Programming Random Number Functions

The document contains C programming examples demonstrating the use of random number generation, basic arithmetic operations, and functions for calculating squares, checking even/odd status, finding factorials, and determining the largest of three numbers. It explains how to seed the random number generator for variability and includes function prototypes and definitions for various mathematical operations. Each example includes user input and output to illustrate the functionality.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

#include <stdio.

h> 🔍 Explanation
#include <stdlib.h>
 rand() generates pseudo-random numbers — meaning
#include <time.h> they follow a deterministic sequence unless you change
the seed.
 srand() sets that starting point (seed).
int main() {
 time(0) (or time(NULL)) returns the current time in
float r; seconds since January 1, 1970 (the Unix epoch).
// Seed random number generator
By using srand(time(0)), you ensure the seed value is different
srand(time(0)); each time you run the program (because the time changes every
// Generate a random number between 0 and 1
second).
This causes rand() to produce a different random sequence on
r = (float)rand() / RAND_MAX; each run.
printf ("Value of r= %f \n",r);
In short:

// Check which range it falls into  srand(time(0)) → randomizes the random number
if (r < 0.1)
generator.
 rand() → generates numbers based on that seed.
printf("X\n"); // Probability 0.1

else if (r < 0.4)

printf("Y\n"); // Probability 0.3 (0.1 to 0.4)

else

printf("Z\n"); // Probability 0.6 (0.4 to 1.0)

return 0;

FUNCTIONS
#include <stdio.h>

// Function declarations (prototypes)

int add(int a, int b);

int subtract(int a, int b);

int multiply(int a, int b);

float divide(int a, int b);

// Main function

int main() {

int x, y;

printf("Enter two numbers: ");


scanf("%d %d", &x, &y);

printf("\nSum = %d", add(x, y));

printf("\nDifference = %d", subtract(x, y));

printf("\nProduct = %d", multiply(x, y));

if (y != 0)

printf("\nQuotient = %.2f\n", divide(x, y));

else

printf("\nDivision not possible (denominator is 0)\n");

return 0;

// Function definitions

int add(int a, int b) {

return a + b;

int subtract(int a, int b) {

return a - b;

int multiply(int a, int b) {

return a * b;

float divide(int a, int b) {

return (float)a / b;

}
Example 1: Function to Find the Square of a Number
#include <stdio.h>

// Function declaration

int square(int n);

int main() {

int num, result;

printf("Enter a number: ");

scanf("%d", &num);

// Function call

result = square(num);

printf("Square of %d is %d\n", num, result);

return 0;

// Function definition

int square(int n) {

return n * n;

🔹 Example 2: Function to Check Whether a Number is Even or Odd


#include <stdio.h>

// Function declaration

void checkEvenOdd(int n);

int main() {

int num;

printf("Enter a number: ");


scanf("%d", &num);

// Function call

checkEvenOdd(num);

return 0;

// Function definition

void checkEvenOdd(int n) {

if (n % 2 == 0)

printf("%d is Even\n", n);

else

printf("%d is Odd\n", n);

🔹 Example 3: Function to Find Factorial of a Number


#include <stdio.h>

// Function declaration

int factorial(int n);

int main() {

int num;

printf("Enter a number: ");

scanf("%d", &num);

printf("Factorial of %d is %d\n", num, factorial(num));

return 0;

// Function definition
int factorial(int n) {

int i, fact = 1;

for (i = 1; i <= n; i++)

fact = fact * i;

return fact;

🔹 Example 4: Function to Find the Largest Among Three Numbers


#include <stdio.h>

// Function declaration

int largest(int a, int b, int c);

int main() {

int x, y, z, big;

printf("Enter three numbers: ");

scanf("%d %d %d", &x, &y, &z);

big = largest(x, y, z);

printf("Largest number = %d\n", big);

return 0;

// Function definition

int largest(int a, int b, int c) {

if (a >= b && a >= c)

return a;

else if (b >= a && b >= c)

return b;

else

return c;

You might also like