Showing posts with label Generate. Show all posts
Showing posts with label Generate. Show all posts

How To Write a C program that generates two random numbers ?

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.


  1. #include <stdio.h>
  2. #include <stdlib.h> // void srand(unsigned int); - int rand(); - NULL - RAND_MAX,
  3. #include <time.h> // time_t time(time_t*) - NULL
  4.  
  5. int main()
  6. {
  7.     srand((unsigned int)time(NULL)); // seeds srand with the number of seconds since January 1st 1970
  8.     int rand1 = rand(); //init the vars to random
  9.     int rand2 = rand(); //init the vars to random
  10.     int max = rand1 > rand2 ? rand1 : rand2; //Ternary (three) conditional operator which sets max = rand1 if true and max = rand2 if false
  11.     printf("Random 1: %d \n", rand1);
  12.     printf("Random 1: %d \n", rand2);
  13.     printf("The max of %d and %d is %d \n", rand1, rand2, max);
  14.     return 0;
  15. }

C Program Generates 10 Random Integers Between 0 and 99.

How to write a C Program that Generates 10 Random Integers Number Between 0 and 99 in C Programming Language ?


This C program generates 10 random integers between 0 and 99. 
The srand() function generates a seed value for the rand() function.

Solution For C Program:

C Program To Generating Fibonacci Series Using Recursion

How to write a C Program To Generating Fibonacci Series Using Recursion in C Programming Language ?

Solution For C Program :

/*C Program To Generating Fibonocci Series Using Recursion.*/

#include<stdio.h>
void main()
{
int a, b, j;
int fib(int);
printf("\nEnter N'th Number : "); scanf("%d", &a);
printf("\nPrinting Values of the Fibonacci Series :\n");
for(j = 0; j < a - 1; j++)
{
b = fib(j);
printf("%d,", b);
}
}
int fib(int n)
{
int x, y;
if(n == 0 || n == 1) return (1);
else
{
x = fib(n - 1);
y = fib(n - 2); return(x + y);
}
}

You may also learn these C Program/Code :

C Program To Swap Two Numbers Without Using Third Variable

C Program To Generate Multiplication Table

How to write a C Program to generate Multiplication Table in C Programming Language ?


Solution For C Program:

/*C Program To Generate Multiplication Table*/

#include<stdio.h>
void main()
{
int num, ctr;
printf("\nEnter Number for the Table : ");
scanf("%d", &num);
printf("\nNow Printing the Table...\n");
for(ctr = 1; ctr <= 10; ctr++)
printf("%d\t*\t%d\t=\t%d\n", num, ctr, num * ctr);
}


You may also learn these C Program/Code :

C Program To Swap Two Numbers Without Using Third Variable

C Program to Generate Specific Formats

How to write a C Program to generate specific formats in C Programming Language ?

Solution:

#include<stdio.h>
void main()
{
int r, s, n;
printf("\nEnter the Value of n : ");
scanf("%d", &n);
for(r = 1; r <= n; r++)
{
for(s = 1; s <= r; s++)
printf("*");
printf("\n");
}
}