Write a C program that generates two random numbers.
Print both numbers that are generated.
Then use a ternary (or conditional) operator to identify the largest of the 2 numbers and print the result.
Seed the random number generator so the numbers generated are not the same every time.
Submit the .c file only.
Sample Run:
Random 1:8809
Random 2:27141
The max of 8809 and 27141 is 27141.
- #include <stdio.h>
- #include <stdlib.h> // void srand(unsigned int); - int rand(); - NULL - RAND_MAX,
- #include <time.h> // time_t time(time_t*) - NULL
- int main()
- {
- srand((unsigned int)time(NULL)); // seeds srand with the number of seconds since January 1st 1970
- int rand1 = rand(); //init the vars to random
- int rand2 = rand(); //init the vars to random
- int max = rand1 > rand2 ? rand1 : rand2; //Ternary (three) conditional operator which sets max = rand1 if true and max = rand2 if false
- printf("Random 1: %d \n", rand1);
- printf("Random 1: %d \n", rand2);
- printf("The max of %d and %d is %d \n", rand1, rand2, max);
- return 0;
- }
Learn More :
Random
Number
- Find out the perfect number using c program
- Write a c program to find out H.C.F. of two numbers.
- Check the given number is armstrong number or not using c program.
- Write a c program to find largest among three numbers using conditional operator
- FIND OUT GENERIC ROOT OF A NUMBER - C PROGRAM.
- FIND PRIME FACTORS OF A NUMBER USING C PROGRAM
- Write a C program to find maximum or equal between two numbers ?
- How to Write a C program to find maximum between two numbers ?
- Write a C program to perform math operations on two input whole numbers. The operations are:
- Write a C program to find maximum between three numbers ?
- Sort Three Numbers - program reads in three Integers and displays them in ascending order.
- C Program to Enter an ODD number between 1 and 49.
- C program acquires keyboard 10 numbers for each of these numbers to determine if it is a first issue, by printing immediately message should at the end, if none of the numbers you entered was a first issue, print an appropriate message.
- C program generates a random number and uses an algorithm to generate 9 other numbers.
- C Program to Find Random Number
- C Program To Find LCM and HCF Of Two Number Using Function - 2
- C Program to find LCM and HCF Of Two Number Using Recursion - 3
- C Program To Find LCM and HCF Of Two Number -1
- C Program To Find Reverse Of Any Digit Number
- C Program To Find The Frequency Of A Number
- C Program To Print Prime Numbers Upto The Number You Want
- C Program To Print Sum Of n Digit Number
- C Program To Reverse A Number
- C Program To Search A Number Inside The Array
Generate
- C program generates a random number and uses an algorithm to generate 9 other numbers.
- C Program Generates 10 Random Integers Between 0 and 99.
- C Program To Generating Fibonacci Series Using Recursion
- C Program To Generate Multiplication Table
- C Program to Generate Specific Formats
- Write a c program to accept a string from user & generate following pattern (e.g, input "abcd")
- ACLK (32768 Hz) to generate a one second timer and toggles an LED