May 7, 2012

C Program to toss a coin using random function.

Write a c program to toss a coin using random function.
In this Program,We use the rand()%2 function that will compute random integers 0 or 1.
Read more about C Programming Language . and read the C Programming Language (2nd Edition). by K and R.


/***********************************************************
* You can use all the programs on  www.c-program-example.com
* for personal and learning purposes. For permissions to use the
* programs for commercial purposes,
* contact info@c-program-example.com
* To find more C programs, do visit www.c-program-example.com
* and browse!
* 
*                      Happy Coding
***********************************************************/

#include <stdio.h>
#include <time.h>
#include <stdlib.h>

int main(void) {
    int toss = 0;
    int call = 0;
    srand(time(NULL));
    
    toss = rand() % 2;
    
    printf("Say head or tail! press 0 for head and 1 for tail:\n\n");
    scanf("%d", &call);
    if(call==0 || call==1)
    {
        if(toss == call)
        {
            if(toss==1)
            printf("You called it correctly ... it is tail\n");
            else
            printf("You called it correctly ... it is head\n");
        }
        else
        {
            if(toss==1)
            printf("No way ...it is head !\n");
            else
            printf("No way ... it is tail!\n");
        }
    }
    else
    printf("Invalid call!!!!!\n\n");
    return 0;
}
Read more Similar C Programs
Learn C Programming
Recursion
Number System
You can easily select the code by double clicking on the code area above.

To get regular updates on new C programs, you can

You can discuss these programs on our Facebook Page. Start a discussion right now,

our page!

Share this program with your Facebook friends now! by liking it


(you can send this program to your friend using this button)

Like to get updates right inside your feed reader? Grab our feed!


To browse more C Programs visit this link