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 [email protected]
* 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:nn");
scanf("%d", &call);
if(call==0 || call==1)
{
if(toss == call)
{
if(toss==1)
printf("You called it correctly ... it is tailn");
else
printf("You called it correctly ... it is headn");
}
else
{
if(toss==1)
printf("No way ...it is head !n");
else
printf("No way ... it is tail!n");
}
}
else
printf("Invalid call!!!!!nn");
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 Follow @c_program

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
(c) www.c-program-example.com

C program to implement SJF algorithm.

Write a C program to implement SJF algorithm.
Shortest Job First(SJF) is the CPU scheduling algorithm. In SJF, if the CPU is available it allocates the process which has smallest burst time, if the two process are same burst time it uses FCFS algorithm.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 [email protected]
* To find more C programs, do visit www.c-program-example.com
* and browse!
*
* Happy Coding
***********************************************************/

#include<stdio.h>
#include<conio.h>

void main()
{
int i,j,n,brust_time[10],start_time[10],end_time[10],wait_time[10],temp,tot;
float avg;
clrscr();
printf("Enter the No. of jobs:nn");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
printf("n n Enter %d process burst time:n",i);
scanf("%d",&brust_time[i]);
}

for(i=1;i<=n;i++)
{
for(j=i+1;j<=n;j++)
{
if(brust_time[i]>brust_time[j])
{
temp=brust_time[i];
brust_time[i]=brust_time[j];
brust_time[j]=temp;
}
}

if(i==1)
{
start_time[1]=0;
end_time[1]=brust_time[1];
wait_time[1]=0;
}

else
{
start_time[i]=end_time[i-1];
end_time[i]=start_time[i]+brust_time[i];
wait_time[i]=start_time[i];
}
}
printf("nn BURST TIME t STARTING TIME t END TIME t WAIT TIMEn");
printf("n ********************************************************n");
for(i=1;i<=n;i++)
{
printf("n %5d %15d %15d %15d",brust_time[i],start_time[i],end_time[i],wait_time[i]);
}
printf("n ********************************************************n");
for(i=1,tot=0;i<=n;i++)
tot+=wait_time[i];
avg=(float)tot/n;
printf("nnn AVERAGE WAITING TIME=%f",avg);
for(i=1,tot=0;i<=n;i++)
tot+=end_time[i];
avg=(float)tot/n;
printf("nn AVERAGE TURNAROUND TIME=%f",avg);
for(i=1,tot=0;i<=n;i++)
tot+=start_time[i];
avg=(float)tot/n;
printf("nn AVERAGE RESPONSE TIME=%fnn",avg);
getch();
}
Read more Similar C Programs
C Basic

Search Algorithms.

You can easily select the code by double clicking on the code area above.

To get regular updates on new C programs, you can Follow @c_program

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
(c) www.c-program-example.com

C Program to check whether two strings are anagrams of each other.

C Strings:
Write a c program to check whether two strings are anagrams of each other.
Two strings are said to be anagrams, if the characters in the strings are same in terms of numbers and value ,only arrangement or order of characters are may be different.
Example: “dfghjkl” and “lkjghdf” are anagrams of each other.
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 [email protected]
* To find more C programs, do visit www.c-program-example.com
* and browse!
* 
*                      Happy Coding
***********************************************************/

#include<stdio.h>
#include<string.h>
# define NO_OF_CHARS 256
int Anagram(char *str1, char *str2)
{
    // Create two count arrays and initialize all values as 0
    int count1[NO_OF_CHARS] = {0};
    int count2[NO_OF_CHARS] = {0};
    int i;
    
    // For each character in input strings, increment count in
    // the corresponding count array
    for (i = 0; str1[i] && str2[i];  i++)
    {
        count1[str1[i]]++;
        count2[str2[i]]++;
    }
    
    // If both strings are of different length. Removing this condition
    // will make the program fail for strings like "aaca" and "aca"
    if (str1[i] || str2[i])
    return 0;
    
    // Compare count arrays
    for (i = 0; i < NO_OF_CHARS; i++)
    if (count1[i] != count2[i])
    return 0;

    return 1;
}
main()
{
    char str[100], str1[100];
    int flag = 0;
    
    printf("Enter first stringn");
    gets(str);
    
    printf("Enter second stringn");
    gets(str1);
    
    flag=Anagram(str, str1);
    if (flag==1)
    printf(""%s" and "%s" are anagrams.n", str, str1);
    else
    printf(""%s" and "%s" are not anagrams.n", str, str1);
    
    return 0;
}

Read more c programs
C Basic
C Strings

You can easily select the code by double clicking on the code area above.

To get regular updates on new C programs, you can Follow @c_program

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
(c) www.c-program-example.com

K & R C Chapter 3 Exercise Solutions

We have already provided solutions to all the exercises in the bookC Programming Language (2nd Edition) popularly known as K & R C book.

In this blog post I will give links to all the exercises from Chapter 3 of the book for easy reference.

Chapter 3: Control Flow

  1. Exercise 3-1. Our binary search makes two tests inside the loop, when one would suffice (at the price of more tests outside). Write a version with only one test inside the loop and measure the difference in run-time.
    Solution to Exercise 3-1.
  2. Exercise 3-2.Write a function escape(s,t) that converts characters like newline and tab into visible escape sequences like n and t as it copies the string t to s . Use a switch . Write a function for the other direction as well, converting escape sequences into the real characters.
    Solution to Exercise 3-2.
  3. Exercise 3-3.Write a function expand(s1,s2) that expands shorthand notations like a-z in the string s1 into the equivalent complete list abc…xyz in s2 . Allow for letters of either case and digits, and be prepared to handle cases like a-b-c and a-z0-9 and -a-z . Arrange that a leading or trailing – is taken literally.
    Solution to Exercise 3-3.
  4. Exercise 3-4.In a two’s complement number representation, our version of itoa does not handle the largest negative number, that is, the value of n equal to -(2 to the power (wordsize – 1)) . Explain why not. Modify it to print that value correctly regardless of the machine on which it runs.
    Solution to Exercise 3-4.
  5. Exercise 3-5. Write the function itob(n,s,b) that converts the integer n into a base b character representation in the string s . In particular, itob(n,s,16) formats n as a hexadecimal integer in s
    Solution to Exercise 3-5.
  6. Exercise 3-6. Write a version of itoa that accepts three arguments instead of two. The third argument is a minimum field width; the converted number must be padded with blanks on the left if necessary to make it wide enough.
    Solution to Exercise 3-6.
You can purchase the book from here or here.

To get regular updates on new C programs, you can Follow @c_program

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

our page!

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

To browse more C Programs visit this link
(c) www.c-program-example.com

C Program to check matrix is magic square or not

Write a c program to check whether the given matrix is a magic square matrix or not.

A matrix is magic square matrix, if all rows sum, columns sum and diagonals sum are equal.

Example:
8   1   6
3   5   7
4   9   2

is the magic square matrix. As you can see numbers in first row add up to 15 (8 + 1 + 6), so do the numbers of 2nd row 3 + 5 + 7. Also the number in the last row 4 + 9 + 2. Similarly, the columns all add up to the same number 15. Hence, this matrix is a magic square matrix.

The Program

#include <stdio.h>
void main() {
int A[50][50];
int i, j, M, N;
int size;
int rowsum, columnsum, diagonalsum;
int magic = 0;
printf("Enter the order of the matrix:\n");
scanf("%d %d", &M, &N);
if(M==N) {
printf("Enter the elements of matrix \n");
for(i=0; i<M; i++) {
for(j=0; j<N; j++) {
scanf("%d", &A[i][j]);
}
}
printf("\n\nMATRIX is\n");
for(i=0; i<M; i++) {
for(j=0; j<N; j++) {
printf("%3d\t", A[i][j]);
}
printf("\n");
}
// calculate diagonal sum
diagonalsum = 0;
for(i=0; i<M; i++) {
for(j=0; j<N; j++) {
if(i==j) {
diagonalsum = diagonalsum + A[i][j];
}
}
}
// calculate row sum
for(i=0; i<M; i++) {
rowsum = 0;
for(j=0; j<N; j++) {
rowsum = rowsum + A[i][j];
}
if(rowsum != diagonalsum) {
printf("\nGiven matrix is not a magic square");
return;
}
}
// calculate column sum
for(i=0; i<M; i++) {
columnsum = 0;
for(j=0; j<N; j++) {
columnsum = columnsum + A[j][i];
}
if(columnsum != diagonalsum) {
printf("\nGiven matrix is not a magic square");
return;
}
}
printf("\nGiven matrix is a magic square matrix");
} else {
printf("\n\nPlease enter the square matrix order(m=n) \n\n");
}
}
view raw magic_square.c hosted with ❤ by GitHub

Sample Output

Read more about C Programming Language and read the C Programming Language (2nd Edition). by K and R.

Related Programs

To get regular updates on new C programs, you can Follow @c_program
You can discuss these programs on our Facebook Page. Start a discussion right now,

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

To browse more C Programs visit this link

(c) www.c-program-example.com

C Program to delete a file.

Write a C Program to delete a file.
To delete a file in c, We use the remove macro, which is defined in stdio.h. remove macro takes filename with extension as its argument and deletes the file, if it is in the directory and returns the zero, if deleted successfully.
Note that deleted file does not goes to the recycle bin, it is going to delete permanently.
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 [email protected]
* To find more C programs, do visit www.c-program-example.com
* and browse!
*
* Happy Coding
***********************************************************/

#include<stdio.h>
#include<conio.h>
#include<process.h>
main()
{
FILE *fp;
char filename[20];
int status;
clrscr();
printf("nEnter the file name:nn");
scanf("%s",filename);

fp = fopen(filename, r);

if(fp == NULL)
{
printf("Error: file not found! check the directory!!nn");
exit(0);
}
fclose(fp);
status = remove(file_name);

if( status == 0 )
printf("%s file deleted successfully.n",file_name);
else
{
printf("Unable to delete the filen");
perror("Error");
}

return 0;
}
Read more Similar C Programs
C Basic

C File i/o

You can easily select the code by double clicking on the code area above.

To get regular updates on new C programs, you can Follow @c_program

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
(c) www.c-program-example.com

C Program to demonstrate strspn function.

Write a C Program to demonstrate strspn function.
The strspn() function returns the index of the first character in string1 that doesn’t match any character in string2. If all the characters of the string2 matched with the string1, strspn returns the length of the string1. Read more about C Programming Language .

/***********************************************************
* 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 [email protected]
* To find more C programs, do visit www.c-program-example.com
* and browse!
*
* Happy Coding
***********************************************************/

#include<stdio.h>
#include<string.h>
#include<conio.h>
int main()
{
int spn;
char str1[] = "1234hjbcde09i";
char str2[] = "12345";
char str3[] = "a12d3456fd";
char char_list[] = "1234567890";
clrscr();
//It returns 4
spn = strspn (str1,char_list);
printf ("nnThe length of the initial numbers for str1 is %d.n",spn);

//It returns 5
spn = strspn (str2,char_list);
printf ("nThe length of the initial numbers for str2 is %d.n",spn);

//It returns 0
spn = strspn (string3,char_list);
printf ("nThe length of the initial numbers for string3 is %d.n",spn);
return 0;
}

Read more c programs
C Basic
C Strings

You can easily select the code by double clicking on the code area above.

To get regular updates on new C programs, you can Follow @c_program

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
(c) www.c-program-example.com

C program to draw a circle using c graphics.

Write C program to draw a circle using c graphics.
In this program we use the graphics.h library function to draw a circle. To use graphics.h, we have to install the drivers in to the the system by using the initgraph() function. In the program circle is the graphic function , which takes three parameters, first two are co-ordinates and the third one is the radius of the circle. Read more about C Programming Language.

/***********************************************************
* 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 [email protected]
* To find more C programs, do visit www.c-program-example.com
* and browse!
*
* Happy Coding
***********************************************************/

#include <graphics.h>

main()
{
int gd = DETECT, gm;
initgraph(&gd, &gm, "C:\TC\BGI");

circle(25, 25, 100);

getch();
closegraph();
return 0;
}
Read more Similar C Programs
Learn C Programming

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 Follow @c_program

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
(c) www.c-program-example.com

C program to count the leaves of the binary tree.

Write a C program to count the leaves of the binary tree.
Binary tree is the ordered directed tree data structure, in which each node has at most two nodes.
A node is called as a leaf node,  if it does not contains any child elements. In this program, We used the structures to create the binary tree.
Read more about C Programming Language .

/***********************************************************
* 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 [email protected]
* To find more C programs, do visit www.c-program-example.com
* and browse!
*
* Happy Coding
***********************************************************/

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

struct node
{
int data;
struct node* leftnode;
struct node* rightnode;
};

//get the leaves count
unsigned int getLeafCount(struct node* node)
{
if(node == NULL)
return 0;
if(node->leftnode == NULL && node->rightnode==NULL)
return 1;
else
return getLeafCount(node->leftnode)+
getLeafCount(node->rightnode);
}


struct node* newNode(int data)
{
struct node* node = (struct node*)
malloc(sizeof(struct node));
node->data = data;
node->leftnode = NULL;
node->rightnode = NULL;

return(node);
}


int main()
{

clrcsr();
struct node *root = newNode(1);
root->leftnode = newNode(2);
root->rightnode = newNode(3);
root->leftnode->leftnode = newNode(4);
root->leftnode->rightnode = newNode(5);


printf("nnLeaf count of the binary tree is %d", getLeafCount(root));

getch();
return 0;
}


Read more Similar C Programs
Data Structures
Breadth First Search(BFS)
Learn C Programming

You can easily select the code by double clicking on the code area above.

To get regular updates on new C programs, you can Follow @c_program

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
(c) www.c-program-example.com

C Program to find the size of a given file.

Write a C Program to find the size of a given file.
The function fseek() is used to set the file pointer at the specified position. In general, it is used to move the file pointer to the desired position within the file.
In this program we moved the file pointer to the end of file, and obtain the current position of the file pointer. Function rewind() also moves the file pointer to the beginning of the file, but syntax and number of parameters are different. Read more about C Programming Language .

/***********************************************************
* 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 [email protected]
* To find more C programs, do visit www.c-program-example.com
* and browse!
*
* Happy Coding
***********************************************************/


#include<stdio.h>
#include<process.h>
long file_size(FILE *fp)
{
long len;
//move file pointer to end of the file */
fseek(fp, 0L, 2);
//Obtain the current position of file pointer
len = ftell(fp);
return len;
}
void main(void)
{
FILE *fp;
char filename[20];
printf("nEnter the file name:nn");
scanf("%s",filename);

fp = fopen(filename, r);

if(fp == NULL)
{
printf("Error: file not found!nn");
exit(0);
}
printf("File size of %s is %ld bytesnn",filename, file_size(fp));
fclose(fp);
}

Read more Similar C Programs
C Basic

C File i/o

You can easily select the code by double clicking on the code area above.

To get regular updates on new C programs, you can Follow @c_program

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
(c) www.c-program-example.com