C Program to implement hashing.

Write a C Program to implement hashing.
Hashing is the function or routine used to assign the key values to the each entity in the database. Using hashing, We can easily access or search the values from database.
In this program we used the open addressing hashing, also called as closed hashing.
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<conio.h>
void main() {
int a[10] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
int n, value;
int temp, hash;
clrscr();
printf("nEnter the value of n(table size):");
scanf("%d", &n);
do {
printf("nEnter the hash value");
scanf("%d", &value);
hash = value % n;
if (a[hash] == 0) {
a[hash] = value;
printf("na[%d]the value %d is stored", hash, value);
} else {
for (hash++; hash < n; hash++) {
if (a[hash] == 0) {
printf("Space is allocated give other value");
a[hash] = value;
printf("n a[%d]the value %d is stored", hash, value);
goto menu;
}
}

for (hash = 0; hash < n; hash++) {
if (a[hash] == 0) {
printf("Space is allocated give other value");
a[hash] = value;
printf("n a[%d]the value %d is stored", hash, value);
goto menu;
}
}
printf("nnERRORn");
printf("nEnter '0' and press 'Enter key' twice to exit");

}

menu:

printf("n Do u want enter more");

scanf("%d", &temp);

}

while (temp == 1);

getch();

}

Read more Similar C Programs
C Basic
C Data Structure
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 demonstrate dynamic memory allocation example.

Write a C Program to demonstrate dynamic memory allocation example.
Dynamic memory allocation means you can allocate or relocate (manipulate) the memory at the run time, using malloc, calloc, and realloc functions.
Using malloc, We can allocate block of memory for a variable
Using calloc function, We can allocate multiple blocks of memory for a variable.
We can alter, reassign the allocated memory using the realloc function.
We can releasing the allocated memory using the free function. 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>

int main() {
int* grades;
int sum = 0, i, numberOfStudents;
float average;


printf("Enter the number of students: ");
scanf("%d", &numberOfStudents);
getchar();

if((grades = (int*) malloc(numberOfStudents * sizeof(int))) == NULL) {
printf("nError: Not enough memory to allocate grades arrayn");
exit(1);
}

printf("nEnter the grades of %d students (in separate lines):n", numberOfStudents);

for(i = 0; i < numberOfStudents; i++) {
scanf("%d", &grades[i]);
getchar();
}

/* calculate sum */
for(i = 0; i < numberOfStudents; i++)
sum += grades[i];

/* calculate the average */
average = (float) sum / numberOfStudents;

printf("nThe average of the grades of all students is %.2f",
average);

getchar();
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 generate and print prime numbers in a given range.

Write a C program to generate and print prime numbers in a given range. Also print the number of prime numbers.
Prime number is a whole number and greater than 1, which is divisible by one or itself.
Example: 2, 3, 5, 7, 11, 13………… 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 <conio.h>
#include <stdlib.h>
#include <math.h>

void main()
{
int M, N, i, j, flag, temp, count = 0;

clrscr();

printf("Enter the value of M and Nn");
scanf("%d %d", &M,&N);

if(N < 2)
{
printf("There are no primes upto %dn", N);
exit(0);
}
printf("Prime numbers aren");
temp = M;

if ( M % 2 == 0)
{
M++;
}
for (i=M; i<=N; i=i+2)
{
flag = 0;

for (j=2; j<=i/2; j++)
{
if( (i%j) == 0)
{
flag = 1;
break;
}
}
if(flag == 0)
{
printf("%dn",i);
count++;
}
}
printf("Number of primes between %d and %d = %dn",temp,N,count);
}



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 copy and concatenate strings without using standard functions.

C Program to copy and concatenate strings without using standard functions.
In this program , we copy one string from another, and without using the standard library function strcpy from string.h .
Here we appends the one string to another without using the strcat function. 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>

void newStrCpy(char *,char *);
void newStrCat(char *,char *);

void main()
{
char str1[80],str2[80];
int opn;
do
{
printf("Press 1- NewStrCpy t 2- NewStrCatt 3- Exitt Your Option?");
scanf("%d",&opn);
switch(opn)
{
case 1: flushall();
printf("n Read the Source String n");
gets(str1);
newStrCpy(str2,str1);
printf(" Copied String: %sn",str2);
break;
case 2: flushall();
printf(" Read the First String n");
gets(str1);
printf(" Read the Second String n");
gets(str2);
newStrCat(str1,str2);
printf("Concatenated String: n");
puts(str1);
break;
case 3: printf(" Exit!! Press a key . . .");
getch();
break;
default: printf(" Invalid Option!!! Try again !!!n");
break;
}
}while(opn != 3);
} /* End of main() */

void newStrCpy(char *d,char *s)
{
while( (*d++ = *s++));
}
void newStrCat(char *s, char *t)
{
while(*s)
s++;
while(*s++ = *t++);
}

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 Programs Exercise 7-8.

K and R C, Solution to Exercise 7-8:
K and R C Programs Exercises provides the solution to all the exercises in the C Programming Language (2nd Edition). You can learn and solve K&R C Programs Exercise.
C Program to print a set of files, starting each new one on a new page, with a title and running page for each file 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>

#define MAXBOT 3
#define MAXHDR 5
#define MAXLINE 100
#define MAXPAGE 66

/*print: print files - each new one on a new page */
main(int argc, char *argv[])
{

FILE *fp;
void fileprint(FILE *fp, char *fname);

if(argc == 1)
fileprint(stdin," ");
else
if((fp = fopen(*++argv,"r")) == NULL) {
fprintf(stderr,"find:can't open %sn",*argv);
exit(1);
} else {

fileprint(fp, *argv);
fclose(fp);
}
return 0;
}


//fileprint: print file name
void fileprint(FILE *fp, char *fname)
{
int lineno, pageno = 1;
char line[MAXLINE];
int heading(char *fname, int pageno);
lineno = heading(fname, pageno++);
while(fgets(line, MAXLINE, fp) != NULL) {
if(lineno == 1) {
fprintf(stdout,"f");
lineno = heading(fname, pageno++);
}
fputs(line, stdout);
if(++lineno > MAXPAGE - MAXBOT)
lineno = 1;
}
fprintf(stdout,"f");
}

//heading: put heading and enough blank lines
int heading(char *fname, int pageno)
{
int ln = 3;
fprintf(stdout,"nn");
fprintf(stdout,"%s page %dn", fname, pageno);
while(ln++ < MAXHDR)
fprintf(stdout,"n");
return ln;
}
Read more c programs

C Basic
C Strings
K and R C Programs Exercise

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 Programs Exercise 7-7.

K and R C, Solution to Exercise 7-7:
K and R C Programs Exercises provides the solution to all the exercises in the C Programming Language (2nd Edition). You can learn and solve K&R C Programs Exercise.
C program to modify the pattern-finding program of chapter 5(C Programming Language  2nd Edition, page no 117.) to take its input from a set of named files or, if no files are named as arguments, from the standard input. Should the file name be printed when a matching line is found.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<stdlib.h>

#define MAXLINE 1000

/*print lines that match pattern from 1st argument */
main(int argc, char *argv[])
{
char pattern[MAXLINE];
int c, excpet = 0,number = 0;
FILE *fp;
void fpat(FILE *fp, char *fname, char *pattern, int except, int number);

while(--argc > 0 && (*++argv)[0] == '-')
while(c = *++argv[0])
switch(c) {
case 'x':
except = 1;
break;
case 'n':
number = 1;
break;
default:
printf("find:illigal option %cn",c);
argc = 0;
break;
}
if(argc >= 1)
strcpy(pattern, *argv);
else{
printf("Usage:find[-x] [-n] pattern [file....]n");
exit(1);
}
if(argc == 1)
fpat(stdin,"",pattern,except,number);
else
while(--argc > 0)
if((fp = fopen(*++argv,"r")) == NULL) {
fprintf(stderr,"find:can't open %sn",*argv);
exit(1);
} else {
fpat(fp, *argv, pattern,except,number);
fclose(fp);}
return 0;
}

/*fpat: find pattern*/
void fpat(FILE *fp, char *fname, char *pattern, int except, int number)
{
char line[MAXLINE];
long loneno = 0;

while(fgets(line, MAXLINE, fp) != NULL){
++lineno;
if((strstr(line,pattern) != NULL) !=except) {
if(*fname)
printf("%s -",fname);
if(number)
printf("%ld: ",lineno);
printf("%s",line);
}
}
}



Read more c programs

C Basic
C Strings
K and R C Programs Exercise

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 Programs Exercise 6-5.

K and R C, Solution to Exercise 6-5:
K and R C Programs Exercises provides the solution to all the exercises in the C Programming Language (2nd Edition). You can learn and solve K&R C Programs Exercise.
Write a function undef that will remove a name and definition from the table maintained by lookup and install. 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
***********************************************************/
int undef(char * name) {
struct nlist * np1, * np2;

if ((np1 = lookup(name)) == NULL) /* name not found */
return 1;

for ( np1 = np2 = hashtab[hash(name)]; np1 != NULL;
np2 = np1, np1 = np1->next ) {
if ( strcmp(name, np1->name) == 0 ) { /* name found */

/* Remove node from list */

if ( np1 == np2 )
hashtab[hash(name)] = np1->next;
else
np2->next = np1->next;

/* Free memory */

free(np1->name);
free(np1->defn);
free(np1);

return 0;
}
}

return 1; /* name not found */
}


Read more c programs
C Basic
C Strings
K and R C Programs Exercise

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 Programs Exercise 6-2.

K and R C, Solution to Exercise 6-2:
K and R C Programs Exercises provides the solution to all the exercises in the C Programming Language (2nd Edition). You can learn and solve K&R C Programs Exercise.
Write a C program that reads a C program and prints in alphabetical order each group of variable names that are identical in the first 6 characters, but different somewhere thereafter. Don’t count words within strings and comments. Make 6 a parameter that can be from the command line. 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<ctype.h>
#include<string.h>
#include<stdlib.h>

struct tnode {
char *word;
int match;
struct tnode *left;
struct tnode *right;
};
#define MAXWORD
#define YES 0
#define NO 0

struct tnode *addtreex(struct tnode *, char *,int, int);
void treexprint(struct tnode *);
int getword(char *,int);


main(int argc, char argv[])
{
struct tnode *root;
char word[MAXWORD];
int found = NO;
int num;

num = (--argc && (*++argv)[0] == '-') ? atoi(argv[0] +1) : 6;
root = NULL;
while(getword(word, MAXWORD) != EOF) {
if(isalpha(word[0]) && strlen(word) >= num)
root = addtreex(root, word, num, &found);
found = NO;
}
treexprint(root);
return 0;
}
struct tnode *talloc(void);
int compare(char *,struct tnode *, int, int *);
//addtreex: add a node with w, at or below p
struct tnode *addtreex(struct tnode *p, char *w,int num, int *found)
{
int cond;
if(p == NULL) {
p = talloc();
p->word = strdup(w);
p->match = *found;
p->left = p->right = NULL;
}
else if((cond = compare(w,p,num,found)) < 0)
p->left = addtreex(p->left, w, num, found);
else if(cond > 0)
p->right = addtreex(p->right, w, num, found);
return p;
}

//compare:compare words and update p->match
int compare(char *s,struct tnode *p, int num, int *found)
{
int i;
char *t = p->word;
for(i = 0; *s == *t;i++,s++,t++)
if(*s == '')
return 0;
if(i >= num) {
*found = YES;
p->match = YES;
}
return *s - *t;
}

/*treexprint: in-order print of tree p if p->match == YES */
void treexprint(struct tnode *p)
{
if(p != NULL) {
treexprint(p->left);
if(p->match)
printf("%sn",p->word);
treexprint(p->right);
}
}


//getword: get next word or character from input
int getword(char *word, int lim)
{
int c, d, comment(void), getch(void);
void ungetch(int);
char *w = word;
while(isspace(c = getch()))
;
if(c !=EOF)
*w++ = c;
if(isalpha(c) || c == '-' || c == '#'){
for(; --lim > 0; w++)
if(!isalnum(*w = getch()) && *w != '-') {
ungetch(*w);
break;
}
}
else if(c == ''' || c == '\'){
for(; --lim > 0; w++)
if(((*w = getch()) == '\')
*++w = getch();
else if(*w == c) {
w++;
break;
}else if(*w == EOF)
break;
}else if(c == '/')
if((d = getch()) == '*')
c = comment();
else
ungetch(d);
*w = '';
return c;
}

Read more c programs
C Basic
C Strings
K and R C Programs Exercise

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 even numbers square and sum from 1 to 10.

C Program to find the even numbers square and sum from 1 to 10.
Even number is an integer, which is the multiple of two. In this program we use the for loop to produce the even numbers. Try to change the for loop limits you can get the various range results. 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<conio.h>
int main()
{
int i,j,sum = 0;
clrscr();
printf("nEven numbers and their squares from 1 to 10:n");
for(i =1; i <=10; i++)
{
if(i % 2 == 0)
{
j = i * i;
printf("%dtt%d",i,j);
printf("n");
sum = sum + j;
}
}
printf("nnSum of even numbers square from 1 to 10 is: %d",sum);
return 0;

}
Read more c programs
C Basic
Number system
K and R C Programs Exercise

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 show examples of the strtol function.

C program To show examples of the strtol function.
strtol() function converts the string to the long integer, and strtol() can accept the number in various bases, and converts it into the decimal number or base. 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 <stdlib.h>

main()
{
char num[10];

/* Test a valid number */
strcpy(num,"13");

printf("%s(Oct) is %i(Dec)n", num, strtol(num, NULL, 8));
printf("%s(Dec) is %i(Dec)n", num, strtol(num, NULL, 10));
printf("%s(hex) is %i(Dec)n", num, strtol(num, NULL, 16));

puts("----------------------------------");

/* Test a slightly valid number
* Returns the same results as
* above. */
strcpy(num, "13hzcd");

printf("%s(Oct) is %i(Dec)n", num, strtol(num, NULL, 8));
printf("%s(Dec) is %i(Dec)n", num, strtol(num, NULL, 10));
printf("%s(hex) is %i(Dec)n", num, strtol(num, NULL, 16));

puts("----------------------------------");

/* Test an invalid number
* Returns ZERO */
strcpy(num, "hzcd");

printf("%s(Oct) is %i(Dec)n", num, strtol(num, NULL, 8));
printf("%s(Dec) is %i(Dec)n", num, strtol(num, NULL, 10));
printf("%s(hex) is %i(Dec)n", num, strtol(num, NULL, 16));


puts("----------------------------------");

/* Test 0 base.
* This will look at the number
* and decide the base for its self!
*/
strcpy(num, "13");
printf("%s is %i(Dec)n", num, strtol(num, NULL, 0));

strcpy(num, "013");
printf("%s is %i(Dec)n", num, strtol(num, NULL, 0));

strcpy(num, "0x13");
printf("%s is %i(Dec)n", num, strtol(num, NULL, 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