Sunday 14 December 2008

History Of C

In the beginning was Charles Babbage and his Analytical Engine, a machine
he built in 1822 that could be programmed to carry out different computations.
Move forward more than 100 years, where the U.S. government in
1942 used concepts from Babbage’s engine to create the ENIAC, the first
modern computer.
Meanwhile, over at the AT&T Bell Labs, in 1972 Dennis Ritchie was working
with two languages: B (for Bell) and BCPL (Basic Combined Programming
Language). Inspired by Pascal, Mr. Ritchie developed the C programming
language.

Linked List implementation

#include"m_list.h"

void main()
{
list *first=NULL,*second=NULL,*third=NULL;
int choice,i;
char ch='y';
while(1)
{
clrscr();
printf("
case 1: Create list");
printf("
case 2: Add in the list");
printf("
case 3: Delete in the list");
printf("
case 4: Append two list");
printf("
case 5: show list");
printf("
case 6: Exit");
printf("
Enter your choice : ");
scanf("%d",&choice);
switch(choice)
{
case 1: //create list
while(ch!='n')
{
printf("Enter element : ");
scanf("%d",&i);
create(&first,i);
printf("Enter element (y/n) : ");
fflush(stdin);
scanf("%c",&ch);
}
break;
case 2: //add in the list
int c;
clrscr();
printf("case 1: Add in Beginning");
printf("case 2: Add in End");
printf("case 3: Add After a given element");
printf("case 4: Return to main menu");
printf("Enter your choice : ");
scanf("%d",&c);
switch(c)
{
case 1: add_at_beg(&first);
break;
case 2: add_at_end(&first);
break;
case 3: add_after_given_element(&first);
break;
case 4: break;
}
break;
case 3:
clrscr();
printf("case 1: Delete in Beginning");
printf("case 2: Delete in End");
printf("case 3: Delete a specified element");
printf("case 4: Return to main menu");
printf("Enter your choice : ");
scanf("%d",&c);
switch(c)
{
case 1: del_at_beg(&first);
break;
case 2: del_at_end(&first);
break;
case 3: del_specified_element(&first);
break;
case 4: break;
}
break;
case 4:
char ch='y';
printf("Enter element in second list : ");
while(ch!='n')
{
printf("Enter element : ");
scanf("%d",&i);
create(&second,i);
printf("Enter element (y/n) : ");
fflush(stdin);
scanf("%c",&ch);
}
append(&third,first,second);

break;
case 5: //show list
clrscr();
printf("
case 1: List 1");
printf("
case 2: List 2");
printf("
case 3: List 3");
printf("
Enter choice : ");
scanf("%d",&choice);
switch(choice)
{
case 1: show(first);break;
case 2: show(second);break;
case 3: show(third);break;
}
break;
case 6: exit(0);

}


}
}


*********************************
#include
#include
#include
#include
typedef struct list
{
int info;
struct list *next;
};

//.................Function Declaration ...........

void create(struct list **p,int i)
{
struct list *temp,*q=*p;
temp=(struct list*)malloc(sizeof(struct list));
temp->info=i;
temp->next=NULL;
if(*p==NULL)
*p=temp;
else
{
while(q->next!=NULL)
q=q->next;
q->next=temp;
}
}
int append(struct list **t,struct list *f,struct list *s)
{
struct list *temp=*t;
if(f==NULL && s==NULL)
return 0;
while(f)
{
create(t,f->info);
f=f->next;
}
while(s)
{
create(t,s->info);
s=s->next;
}

return 0;
}
void show(struct list *p)
{
if(p==NULL)
printf(" List is Empty");
else
while(p)
{
printf("%d ",p->info);
p=p->next;
}
getch();
}
void add_at_beg(struct list **l)
{
struct list *temp=(struct list *)malloc(sizeof(struct list));
printf("
Enter element : ");
scanf("%d",&temp->info);
temp->next=NULL;
if(*l==NULL)
*l=temp;
else
{
temp->next=*l;
*l=temp;
}
}
void del_at_beg(struct list **l)
{
list *temp;
if(*l==NULL)
{
printf("
List is empty");
getch();
}
else
{
temp=*l;
*l=(*l)->next;
free(temp);
}
}
void add_at_end(struct list **l)
{
list *temp,*p;
temp=(struct list *)malloc(sizeof(struct list));
printf("
Enter element : ");
scanf("%d",&temp->info);
temp->next=NULL;
if(*l==NULL)
*l=temp;
else
{
p=*l;
while(p->next!=NULL)
p=p->next;
p->next=temp;
}
}

void del_at_end(struct list **l)
{
list *temp,*p;
if(*l==NULL)
{
printf("
List is Empty");
getch();
}
else if((*l)->next==NULL)
{
temp=*l;
*l=NULL;
free(temp);
}
else
{
p=*l;
while(p->next->next!=NULL)
p=p->next;
temp=p->next->next;
p->next=NULL;
free(temp);
}
}
void add_after_given_element(list **l)
{
list *temp,*p;
int m;
temp=(struct list *)malloc(sizeof(struct list));
printf("
Enter element : ");
scanf("%d",&temp->info);
printf("
Enter position after which element inserted : ");
scanf("%d",&m);
temp->next=NULL;
if(*l==NULL)
*l=temp;
else
{
p=*l;
while(p->next!=NULL)
if(p->info==m)
break;
else
p=p->next;

temp->next=p->next;
p->next=temp;

}
}
void del_specified_element(list **l)
{
list *temp,*p,*q;
int m;
printf("
Enter element which is deleted : ");
scanf("%d",&m);
if(*l==NULL)
{
printf("
List is Empty");
getch();
}
else if((*l)->next!=NULL && (*l)->info==m)
{
temp=*l;
*l=(*l)->next;
free(temp);
}
else if((*l)->next==NULL && (*l)->info==m)
{
temp=*l;
*l=NULL;
free(temp);
}
else
{
p=*l;
while(p!=NULL)
if(p->info==m)
break;
else
{
q=p;
p=p->next;
}
temp=p;
q->next=p->next;
free(temp);
}
}

Program for finding the transpose of a martix in sparse form

#include <stdio.h>
#include <conio.h>
int a[100][100],b[100][100];

void main()
{
int i,m,n,p,q,col,t;
clrscr();
printf("Enter the no. of rows");
scanf("%d", &a[0][0]);
printf("Enter the no. of cols");
scanf("%d", &a[0][1]);
printf("Enter the number of non zero terms");
scanf("%d", &a[0][2]);

for(i=1;i<=a[0][2];i++)
{
printf("Enter the value (that is non zero)");
scanf("%d",&a[i][2]);
printf("Enter the row for %d : ",a[i][2]);
scanf("%d",&a[i][0]);
printf("Enter the col for %d : ",a[i][2]);
scanf("%d",&a[i][1]);
}

/* Printing for testing the sparse input */
printf("
*****************************
The martix you entered is
************************

Row Col Value ");
for ( i = 0;i <= a[0][2];i++)
{
printf("%d %d %d " , a[i][0],a[i][1],a[i][2]);
}

/* Calling function for evaluation of transpose */

m = a[0][0];
n = a[0][1];
t = a[0][2];

b[0][0] = n;
b[0][1] = m;
b[0][2] = t;

q=1;

for( col = 1; col <=n; col++)
{
for(p = 1; p<=t;p++)
{
if(a[p][1] == col)
{
b[q][0] = a[p][1];
b[q][1] =a[p][0];
b[q][2] = a[p][2];
q++;
}
}
} //end of outer for loop

/* Printing the transposed matrix */

getch();
printf("
The Transpose of the above matrix is ");
for ( i = 0;i <= a[0][2];i++)
{
printf("%d %d %d " , b[i][0],b[i][1],b[i][2]);
}
getch();
}

XOR list example

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

struct xnode
{
int data;
unsigned long direction;
};

struct xnode *add_data(int data, struct xnode* list);
void walk_list(struct xnode *list);

int main(void)
{

struct xnode *l2 = add_data(2, NULL);
struct xnode *l1 = add_data(1, l2);
struct xnode *l3 = add_data(3, l2);
struct xnode *l4 = add_data(4, l3);

printf("front -> back....\n");
walk_list(l1);
printf("back -> front....\n");
walk_list(l4);

return 0;
}

struct xnode *add_data(int data, struct xnode *list)
{
struct xnode *newxnode = malloc(sizeof(struct xnode));

assert(newxnode);
newxnode->direction = (unsigned long)list;
newxnode->data = data;

if(list != NULL)
list->direction ^= (unsigned long)newxnode;

return newxnode;
}

void walk_list(struct xnode *list)
{
unsigned long prev = 0;

while(list != NULL) {
unsigned long next = prev ^ list->direction;
printf("%d ", list->data);
prev = (unsigned long)list;
list = (struct xnode *)next;
}

printf("\n");
}

Program for demonstration of Tree Operations - INSERTION, INORDER .

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

struct node
{
struct node *left;
int data;
struct node *right;
};

void main()
{
void insert(struct node **,int);
void inorder(struct node *);
void postorder(struct node *);
void preorder(struct node *);
struct node *ptr;
int will,i,num;
ptr = NULL;
ptr->data=NULL;
clrscr();

printf("Enter the number of terms you want to add to the tree.");
scanf("%d",&will);

/* Getting Input */
for(i=0;i
{
printf("Enter the item");
scanf("%d",&num);
insert(&ptr,num);
}

getch();
printf("INORDER TRAVERSAL");
inorder(ptr);
getch();
printf("PREORDER TRAVERSAL");
preorder(ptr);
getch();
printf("POSTORDER TRAVERSAL");
postorder(ptr);
getch();
}



void insert(struct node **p,int num)
{


if((*p)==NULL)
{ printf("Leaf node created.");
(*p)=malloc(sizeof(struct node));
(*p)->left = NULL;
(*p)->right = NULL;
(*p)->data = num;
return;
}
else
{ if(num==(*p)->data)
{
printf("REPEATED ENTRY ERROR
VALUE REJECTED");
return;
}
if(num<(*p)->data)
{
printf("
Directed to left link.");
insert(&((*p)->left),num);
}
else
{
printf("Directed to right link.");
insert(&((*p)->right),num);
}
}
return;
}


void inorder(struct node *p)
{
if(p!=NULL)
{
inorder(p->left);
printf("Data :%d",p->data);
inorder(p->right);
}
else
return;
}


void preorder(struct node *p)
{
if(p!=NULL)
{
printf("Data :%d",p->data);
preorder(p->left);
preorder(p->right);
}
else
return;
}


void postorder(struct node *p)
{
if(p!=NULL)
{
postorder(p->left);
postorder(p->right);
printf("
Data :%d",p->data);
}
else
return;
}

Search An Element in Linked List

#include stdio.h>
#include conio.h>
#include malloc.h>
struct linlst
{
int info;
struct link *next;
}
start, *node;

int search(int);
void main()
{
int no,i,item,pos;
clrscr();
start.next=NULL;
node=&start;
printf("How many nodes, you want in linked list? ");
scanf("%d",&no);
printf(" ");
for(i=0;i
{
node->next=(struct linlst *)malloc(sizeof(struct linlst));
printf("Enter element in node %d: ",i+1);
scanf("%d",&node->info);
node=node->next;
}
node->next=NULL;
printf("Linked list(only with info field) is:");

node=&start;
while(node->next!=NULL)
{
printf("%d ",node->info);
node=node->next;
}
printf("Enter item to be searched : ");
scanf("%d",&item);
pos=search(item);
if(pos<=no)
printf("Your item is at node %d",pos);
else
printf("Sorry! item is no in linked list.a");
getch();
}

int search(int item)
{
int n=1;
node=&start;
while(node->next!=NULL)
{
if(node->info==item)
break;
else
n++;
node=node->next;
}
return n;
}

Program for demonstration of Tree Operations - INSERTION, INORDER .

# include
# include
# include

struct node
{
struct node *left;
int data;
struct node *right;
};

void main()
{
void insert(struct node **,int);
void inorder(struct node *);
void postorder(struct node *);
void preorder(struct node *);
struct node *ptr;
int will,i,num;
ptr = NULL;
ptr->data=NULL;
clrscr();

printf("Enter the number of terms you want to add to the tree.");
scanf("%d",&will);

/* Getting Input */
for(i=0;i
{
printf("Enter the item");
scanf("%d",&num);
insert(&ptr,num);
}

getch();
printf("INORDER TRAVERSAL");
inorder(ptr);
getch();
printf("PREORDER TRAVERSAL");
preorder(ptr);
getch();
printf("POSTORDER TRAVERSAL");
postorder(ptr);
getch();
}



void insert(struct node **p,int num)
{


if((*p)==NULL)
{ printf("Leaf node created.");
(*p)=malloc(sizeof(struct node));
(*p)->left = NULL;
(*p)->right = NULL;
(*p)->data = num;
return;
}
else
{ if(num==(*p)->data)
{
printf("REPEATED ENTRY ERROR
VALUE REJECTED");
return;
}
if(num<(*p)->data)
{
printf("
Directed to left link.");
insert(&((*p)->left),num);
}
else
{
printf("Directed to right link.");
insert(&((*p)->right),num);
}
}
return;
}


void inorder(struct node *p)
{
if(p!=NULL)
{
inorder(p->left);
printf("Data :%d",p->data);
inorder(p->right);
}
else
return;
}


void preorder(struct node *p)
{
if(p!=NULL)
{
printf("Data :%d",p->data);
preorder(p->left);
preorder(p->right);
}
else
return;
}


void postorder(struct node *p)
{
if(p!=NULL)
{
postorder(p->left);
postorder(p->right);
printf("
Data :%d",p->data);
}
else
return;
}

Binary search

#define TRUE 0
#define FALSE 1

int main(void)
{
int array[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
int left = 0;
int right = 10;
int middle = 0;
int number = 0;
int bsearch = FALSE;
int i = 0;

printf("ARRAY: ");
for(i = 1; i <= 10; i++)
printf("[%d] ", i);
printf("\nSearch for Number: ");
scanf("%d", &number);

while(bsearch == FALSE && left <= right)
{
middle = (left + right) / 2;

if(number == array[middle])
{
bsearch = TRUE;
printf("** Number Found **\n");
} else {
if(number < array[middle]) right = middle - 1;
if(number > array[middle]) left = middle + 1;
}
}

if(bsearch == FALSE)
printf("-- Number Not found --\n");

return 0;
}

2d example insertion sort

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

struct node
{
int number;
struct node *next;
};

struct node *head = NULL;

/* insert a node directly at the right place in the linked list */
void insert_node(int value);

int main(void)
{
struct node *current = NULL;
struct node *next = NULL;
int test[] = {8, 3, 2, 6, 1, 5, 4, 7, 9, 0};
int i = 0;

/* insert some numbers into the linked list */
for(i = 0; i < i =" 0;">next != NULL)
{
printf("%4d\t%4d\n", test[i++], head->number);
head = head->next;
}

/* free the list */
for(current = head; current != NULL; current = next)
next = current->next, free(current);

return 0;
}

void insert_node(int value)
{
struct node *temp = NULL;
struct node *one = NULL;
struct node *two = NULL;

if(head == NULL) {
head = (struct node *)malloc(sizeof(struct node *));
head->next = NULL;
}

one = head;
two = head->next;

temp = (struct node *)malloc(sizeof(struct node *));
temp->number = value;

while(two != NULL && temp->number <>number) {
one = one->next;
two = two->next;
}

one->next = temp;
temp->next = two;
}

bubble sort

#include <stdio.h>


void bubble_sort(int a[], int size);

int main(void) {
int arr[10] = {10, 2, 4, 1, 6, 5, 8, 7, 3, 9};
int i = 0;

printf("before:\n");
for(i = 0; i < 10; i++) printf("%d ", arr[i]);
printf("\n");

bubble_sort(arr, 10);

printf("after:\n");
for(i = 0; i < 10; i++) printf("%d ", arr[i]);
printf("\n");

return 0;
}

void bubble_sort(int a[], int size)
{
int switched = 1;
int hold = 0;
int i = 0;
int j = 0;

size -= 1;

for(i = 0; i < size && switched; i++)
{
switched = 0;
for(j = 0; j < size - i; j++)
if(a[j] > a[j+1])
{
switched = 1;
hold = a[j];
a[j] = a[j + 1];
a[j + 1] = hold;
}
}
}

Bubble sort - linked list

#define MAX 10

struct lnode
{
int data;
struct lnode *next;
} *head, *visit;

/* add a new entry to the linked list */
void llist_add(struct lnode **q, int num);
/* preform a bubble sort on the linked list */
void llist_bubble_sort(void);
/* print the entire linked list */
void llist_print(void);

int main(void) {
/* linked list */
struct lnode *newnode = NULL;
int i = 0; /* a general counter */

/* load some random values into the linked list */
for(i = 0; i < MAX; i++)
{
llist_add(&newnode, (rand() % 100));
}

head = newnode;
printf("Before bubble sort:\n");
llist_print();
printf("After bubble sort:\n");
llist_bubble_sort();
llist_print();

return 0;
}

/* adds a node at the end of a linked list */
void llist_add(struct lnode **q, int num)
{
struct lnode *tmp;

tmp = *q;

/* if the list is empty, create first node */
if(*q == NULL) {
*q = malloc(sizeof(struct lnode));
tmp = *q;
} else {
/* go to last node */
while(tmp->next != NULL)
tmp = tmp->next;

/* add node at the end */
tmp->next = malloc(sizeof(struct lnode));
tmp = tmp->next;
}

/* assign data to the last node */
tmp->data = num;
tmp->next = NULL;
}

/* print the entire linked list */
void llist_print(void)
{
visit = head;

while(visit != NULL)
{
printf("%d ", visit->data);
visit = visit->next;
}
printf("\n");
}

/* preform a bubble sort on the linked list */
void llist_bubble_sort(void) {
struct lnode *a = NULL;
struct lnode *b = NULL;
struct lnode *c = NULL;
struct lnode *e = NULL;
struct lnode *tmp = NULL;

/*
// the `c' node precedes the `a' and `e' node
// pointing up the node to which the comparisons
// are being made.
*/
while(e != head->next)
{
c = a = head;
b = a->next;
while(a != e)
{
if(a->data > b->data)
{
if(a == head)
{
tmp = b -> next;
b->next = a;
a->next = tmp;
head = b;
c = b;
} else {
tmp = b->next;
b->next = a;
a->next = tmp;
c->next = b;
c = b;
}
} else
{
c = a;
a = a->next;
}
b = a->next;
if(b == e)
e = a;
}
}
}

Bubble sort in string array

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


#define MAX 50
#define N 2000

void sort_words(char *x[], int y);
void swap(char **, char **);

int main(void)
{
char word[MAX];
char *x[N];
int n = 0;
int i = 0;

for(i = 0; scanf("%s", word) == 1; ++i)
{
if(i >= N)
printf("Limit reached: %d\n", N), exit(1);

x[i] = calloc(strlen(word)+1, sizeof(char));
strcpy(x[i], word);
}

n = i;
sort_words(x, n);
for(i = 0; i < n; ++i)
printf("%s\n", x[i]);

return(0);
}

void sort_words(char *x[], int y)
{
int i = 0;
int j = 0;

for(i = 0; i < y; ++i)
for(j = i + 1; j < y; ++j)
if(strcmp(x[i], x[j]) > 0)
swap(&x[i], &x[j]);
}

void swap(char **p, char **q)
{
char *tmp;

tmp = *p;
*p = *q;
*q = tmp;
}

Merge sort - linked list

struct node {
int number;
struct node *next;
};

/* add a node to the linked list */
struct node *addnode(int number, struct node *next);
/* preform merge sort on the linked list */
struct node *mergesort(struct node *head);
/* merge the lists.. */
struct node *merge(struct node *head_one, struct node *head_two);

int main(void) {
struct node *head;
struct node *current;
struct node *next;
int test[] = {8, 3, 2, 6, 1, 5, 4, 7, 9, 0};
int i;

head = NULL;
/* insert some numbers into the linked list */
for(i = 0; i < 10; i++)
head = addnode(test[i], head);

/* sort the list */
head = mergesort(head);

/* print the list */
printf(" before after\n"), i = 0;
for(current = head; current != NULL; current = current->next)
printf("%4d\t%4d\n", test[i++], current->number);

/* free the list */
for(current = head; current != NULL; current = next)
next = current->next, free(current);

/* done... */
return 0;
}

/* add a node to the linked list */
struct node *addnode(int number, struct node *next) {
struct node *tnode;

tnode = (struct node*)malloc(sizeof(*tnode));

if(tnode != NULL) {
tnode->number = number;
tnode->next = next;
}

return tnode;
}

/* preform merge sort on the linked list */
struct node *mergesort(struct node *head) {
struct node *head_one;
struct node *head_two;

if((head == NULL) || (head->next == NULL))
return head;

head_one = head;
head_two = head->next;
while((head_two != NULL) && (head_two->next != NULL)) {
head = head->next;
head_two = head->next->next;
}
head_two = head->next;
head->next = NULL;

return merge(mergesort(head_one), mergesort(head_two));
}

/* merge the lists.. */
struct node *merge(struct node *head_one, struct node *head_two) {
struct node *head_three;

if(head_one == NULL)
return head_two;

if(head_two == NULL)
return head_one;

if(head_one->number < head_two->number) {
head_three = head_one;
head_three->next = merge(head_one->next, head_two);
} else {
head_three = head_two;
head_three->next = merge(head_one, head_two->next);
}

return head_three;
}

Insertion sort in linked list

struct lnode {
char *str;
struct lnode *next;
};

struct lnode *insert(char *data, struct lnode *list);
void free_list(struct lnode *list);
void print_list(struct lnode *list);

int main(void) {
char line[1024];
struct lnode *list;

list = NULL;
while((fgets(line, 1024, stdin)) != NULL)
list = insert(line, list);

print_list(list);
free_list(list);
return 0;
}

struct lnode *insert(char *data, struct lnode *list) {
struct lnode *p;
struct lnode *q;

/* create a new node */
p = (struct lnode *)malloc(sizeof(struct lnode));
/* save data into new node */
p->str = strdup(data);

/* first, we handle the case where `data' should be the first element */
if(list == NULL || strcmp(list->str, data) > 0) {
/* apperently this !IS! the first element */
/* now data should [be|becomes] the first element */
p->next = list;
return p;
} else {
/* search the linked list for the right location */
q = list;
while(q->next != NULL && strcmp(q->next->str, data) < 0) {
q = q->next;
}
p->next = q->next;
q->next = p;
return list;
}
}

void free_list(struct lnode *list) {
struct lnode *p;

while(list != NULL) {
p = list->next;
free(list);
list = p;
}
}

void print_list(struct lnode *list) {
struct lnode *p;

for(p = list; p != NULL; p = p->next)
printf("%s", p->str);
}

Hsort, heap sort

/* array of MAXARRAY length ... */
#define MAXARRAY 5

/* preform the heapsort */
void heapsort(int ar[], int len);
/* help heapsort() to bubble down starting at pos[ition] */
void heapbubble(int pos, int ar[], int len);

int main(void) {
int array[MAXARRAY];
int i = 0;

/* load some random values into the array */
for(i = 0; i < MAXARRAY; i++)
array[i] = rand() % 100;

/* print the original array */
printf("Before heapsort: ");
for(i = 0; i < MAXARRAY; i++)
{
printf(" %d ", array[i]);
}
printf("\n");

heapsort(array, MAXARRAY);

/* print the `heapsorted' array */
printf("After heapsort: ");
for(i = 0; i < MAXARRAY; i++)
{
printf(" %d ", array[i]);
}
printf("\n");

return 0;
}

void heapbubble(int pos, int array[], int len)
{
int z = 0;
int max = 0;
int tmp = 0;
int left = 0;
int right = 0;

z = pos;
for(;;) {
left = 2 * z + 1;
right = left + 1;

if(left >= len)
return;
else if(right >= len)
max = left;
else if(array[left] > array[right])
max = left;
else
max = right;

if(array[z] > array[max])
return;

tmp = array[z];
array[z] = array[max];
array[max] = tmp;
z = max;
}
}

void heapsort(int array[], int len)
{
int i = 0;
int tmp = 0;

for(i = len / 2; i >= 0; --i)
heapbubble(i, array, len);

for(i = len - 1; i > 0; i--)
{
tmp = array[0];
array[0] = array[i];
array[i] = tmp;
heapbubble(0, array, i);
}
}

Isort, insertion sort

#include <stdio.h>


void isort(float arr[], int n);
int fm(float arr[], int b, int n);

int main(void)
{
float arr1[5] = {4.3, 6.7, 2.8, 8.9, 1.0};
float arr2[5] = {4.3, 6.7, 2.8, 8.9, 1.0};
int i = 0;

isort(arr2, 5);

printf("\nBefore\tAfter\n--------------\n");

for(i = 0; i < 5; i++)
printf("%.2f\t%.2f\n", arr1[i], arr2[i]);

return 0;
}

int fm(float arr[], int b, int n) {
int f = b;
int c;

for(c = b + 1; c < n; c++)
if(arr[c] < arr[f])
f = c;

return f;
}

void isort(float arr[], int n)
{
int s, w;
float sm;

for(s = 0; s < n - 1; s++)
{
w = fm(arr, s, n);
sm = arr[w];
arr[w] = arr[s];
arr[s] = sm;
}
}

Msort Merge sort

#define MAXARRAY 10

void mergesort(int a[], int low, int high);

int main(void) {
int array[MAXARRAY];
int i = 0;

/* load some random values into the array */
for(i = 0; i < MAXARRAY; i++)
array[i] = rand() % 100;

/* array before mergesort */
printf("Before :");
for(i = 0; i < MAXARRAY; i++)
printf(" %d", array[i]);

printf("\n");

mergesort(array, 0, MAXARRAY - 1);

/* array after mergesort */
printf("Mergesort :");
for(i = 0; i < MAXARRAY; i++)
printf(" %d", array[i]);

printf("\n");
return 0;
}

void mergesort(int a[], int low, int high) {
int i = 0;
int length = high - low + 1;
int pivot = 0;
int merge1 = 0;
int merge2 = 0;
int working[length];

if(low == high)
return;

pivot = (low + high) / 2;

mergesort(a, low, pivot);
mergesort(a, pivot + 1, high);

for(i = 0; i < length; i++)
working[i] = a[low + i];

merge1 = 0;
merge2 = pivot - low + 1;

for(i = 0; i < length; i++)
{
if(merge2 <= high - low)
if(merge1 <= pivot - low)
if(working[merge1] > working[merge2])
a[i + low] = working[merge2++];
else
a[i + low] = working[merge1++];
else
a[i + low] = working[merge2++];
else
a[i + low] = working[merge1++];
}
}

reversing a linked list

#include "stdio.h"
#include "stdlib.h"

#define MAX 10 /* max of 10 elements */

struct lnode
{
int number;
struct lnode *next;
};

/* add a lnode at the beginning of the list */
void llist_add_begin(struct lnode **n, int val);
/* reverse the whole list */
void llist_reverse(struct lnode **n);
/* display the whole linked list */
void llist_display(struct lnode *n);

int main(void)
{
struct lnode *new = NULL;
int i = 0;

/* insert some numbers */
for(i = 0; i <= MAX; i++)
llist_add_begin(&new, i);

printf("linked list before reversal:");
llist_display(new);
llist_reverse(&new);
printf("linked list after reversal:");
llist_display(new);

return 0;
}

/* add a lnode at the beginning of the list */
void llist_add_begin(struct lnode **n, int val)
{
struct lnode *temp = NULL;

/* add new node */
temp = malloc(sizeof(struct lnode));
temp->number = val;
temp->next = *n;
*n = temp;
}

/* reverse the whole list */
void llist_reverse(struct lnode **n)
{
struct lnode *a = NULL;
struct lnode *b = NULL;
struct lnode *c = NULL;
a = *n, b = NULL;

while(a != NULL)
{
c = b, b = a, a = a->next;
b->next = c;
}

*n = b;
}

/* display the whole linked list */
void llist_display(struct lnode *n)
{
while(n != NULL)
printf(" %d", n->number), n = n->next;

printf("\n");
}

Qcksort, quick sort

#include <stdio.h>

#define MAXARRAY 10

void quicksort(int arr[], int low, int high);

int main(void)
{
int array[MAXARRAY] = {0};
int i = 0;

/* load some random values into the array */
for(i = 0; i < MAXARRAY; i++)
array[i] = rand() % 100;

/* print the original array */
printf("Before quicksort: ");
for(i = 0; i < MAXARRAY; i++)
{
printf(" %d ", array[i]);
}
printf("\n");

quicksort(array, 0, (MAXARRAY - 1));

/* print the `quicksorted' array */
printf("After quicksort: ");
for(i = 0; i < MAXARRAY; i++) {
printf(" %d ", array[i]);
}
printf("\n");

return 0;
}

/* sort everything inbetween `low' <-> `high' */
void quicksort(int arr[], int low, int high)
{
int i = low;
int j = high;
int y = 0;
/* compare value */
int z = arr[(low + high) / 2];

/* partition */
do {
/* find member above ... */
while(arr[i] < z) i++;

/* find element below ... */
while(arr[j] > z) j--;

if(i <= j)
{
/* swap two elements */
y = arr[i];
arr[i] = arr[j];
arr[j] = y;
i++;
j--;
}
} while(i <= j);

/* recurse */
if(low < j)
quicksort(arr, low, j);

if(i < high)
quicksort(arr, i, high);
}

Qserch , string, dynamic pointer array

#include "stdio.h"
#include "stdlib.h"
#include "string.h"

void sortstrarr(void *array, unsigned n);
static int cmpr(const void *a, const void *b);

int main (void)
{
char **strarray = NULL;
int i = 0, strcount = 0;
char line[1024];

while((fgets(line, 1024, stdin)) != NULL)
{
if(strlen(line) == 1)
continue;

strarray = (char **)realloc(strarray, (strcount + 1) * sizeof(char *));
strarray[strcount++] = strdup(line);
}

printf("### Before ###\n");
for(i = 0; i < strcount; i++)
printf("%2d: %s", i, strarray[i]);

sortstrarr(strarray, strcount);

printf("### After ###\n");
for(i = 0; i < strcount; i++)
printf("%2d: %s", i, strarray[i]);

/* free mem... */
for(i = 0; i < strcount; i++)
free(strarray[i]);

free(strarray);
return 0;
}

static int cmpr(const void *a, const void *b)
{
return strcmp(*(char **)a, *(char **)b);
}

void sortstrarr(void *array, unsigned n)
{
qsort(array, n, sizeof(char *), cmpr);
}

String array Qsort

#include
#include
#include

void sortstrarr(void *array, unsigned n);
static int cmpr(const void *a, const void *b);

int main(void)
{
char line[1024];
char *line_array[1024];
int i = 0;
int j = 0;

while((fgets(line, 1024, stdin)) != NULL)
if(i < 1024)
line_array[i++] = strdup(line);
else
break;

sortstrarr(line_array, i);

while(j < i)
printf("%s", line_array[j++]);

return 0;
}

static int cmpr(const void *a, const void *b)
{
return strcmp(*(char **)a, *(char **)b);
}

void sortstrarr(void *array, unsigned n)
{
qsort(array, n, sizeof(char *), cmpr);
}

Quick Sort : array of pointers to structures

#include "stdio.h"
#include "string.h"
#include "stdlib.h"

struct node
{
char *str;
};

/* compare function for qsort */
static int cmpr(const void *a, const void *b);

int main(void)
{
struct node **strarray = NULL;
int i = 0, count = 0;
char line[1024];

while(fgets(line, 1024, stdin) != NULL)
{
/* add ONE element to the array */
strarray = (struct node **)realloc(strarray, (count + 1) * sizeof(struct node *));

/* allocate memory for ONE `struct node` */
strarray[count] = (struct node *)malloc(sizeof(struct node));

/* copy the data into the new element (structure) */
strarray[count]->str = strdup(line);
count++;
}

/* before sorting ... */
printf("Before:\n");
for(i = 0; i < count; i++)
{
printf("[%d]->str: %s", i, strarray[i]->str);
}

/* qsort array of structures */
qsort(strarray, count, sizeof(*strarray), cmpr);

/* after sorting ... */
printf("\n--\nAfter:\n");
for(i = 0; i < count; i++)
{
printf("[%d]->str: %s", i, strarray[i]->str);
}

/* free all strarray elements */
for(i = 0; i < count; i++)
{
free(strarray[i]->str);
free(strarray[i]);
i++;
}
free(strarray);

return 0;
}

/* compare function for qsort */
static int cmpr(const void *a, const void *b)
{
struct node * const *one = a;
struct node * const *two = b;

return strcmp((*one)->str, (*two)->str);
}

Selection sort linked list

#include "stdio.h"
#include "stdlib.h"

#define MAX 10

struct lnode
{
int data;
struct lnode *next;
} *head, *visit;

/* add a new entry to the linked list */
void llist_add(struct lnode **q, int num);
/* preform a selection sort on the linked list */
void llist_selection_sort(void);
/* print the entire linked list */
void llist_print(void);

int main(void)
{
/* linked list */
struct lnode *newnode = NULL;
int i = 0; /* a general counter */

/* load some random values into the linked list */
for(i = 0; i < MAX; i++) {
llist_add(&newnode, (rand() % 100));
}

head = newnode;
printf("Before selection sort:\n");
llist_print();
printf("After selection sort:\n");
llist_selection_sort();
llist_print();

return 0;
}

/* adds a node at the end of a linked list */
void llist_add(struct lnode **q, int num)
{
struct lnode *temp;

temp = *q;

/* if the list is empty, create first node */
if(*q == NULL) {
*q = malloc(sizeof(struct lnode));
temp = *q;
} else {
/* go to last node */
while(temp->next != NULL)
temp = temp->next;

/* add node at the end */
temp->next = malloc(sizeof(struct lnode));
temp = temp->next;
}

/* assign data to the last node */
temp->data = num;
temp->next = NULL;
}

/* print the entire linked list */
void llist_print(void)
{
visit = head;

/* traverse the entire linked list */
while(visit != NULL)
{
printf("%d ", visit->data);
visit = visit->next;
}
printf("\n");
}

void llist_selection_sort(void)
{
struct lnode *a = NULL;
struct lnode *b = NULL;
struct lnode *c = NULL;
struct lnode *d = NULL;
struct lnode *tmp = NULL;

a = c = head;
while(a->next != NULL)
{
d = b = a->next;
while(b != NULL) {
if(a->data > b->data)
{
/* neighboring linked list node */
if(a->next == b)
{
if(a == head)
{
a->next = b->next;
b->next = a;
tmp = a;
a = b;
b = tmp;
head = a;
c = a;
d = b;
b = b->next;
} else {
a->next = b->next;
b->next = a;
c->next = b;
tmp = a;
a = b;
b = tmp;
d = b;
b = b->next;
}
} else
{
if(a == head)
{
tmp = b->next;
b->next = a->next;
a->next = tmp;
d->next = a;
tmp = a;
a = b;
b = tmp;
d = b;
b = b->next;
head = a;
} else
{
tmp = b->next;
b->next = a->next;
a->next = tmp;
c->next = b;
d->next = a;
tmp = a;
a = b;
b = tmp;
d = b;
b = b->next;
}
}
} else
{
d = b;
b = b->next;
}
}
c = a;
a = a->next;
}
}

Matrix Multiplication

void main()
{
int row1=0,
col1=1,
row2=0,
col2=0,
**matrix1,
**matrix2,
**result;

clrscr();
printf(" Enter number of row for first matrix ");
scanf("%d",&row1);

while (col1!=row2)
{
printf(" Enter number of column for first matrix ");
scanf("%d",&col1);

printf(" Enter number of row for second matrix ");
scanf("%d",&row2);

if (col1!=row2)
{
clrscr();
printf("Column number of first matrix must be same as the row number of second matrix");
}


}


printf(" Enter number of column for second matrix ");
scanf("%d",&col2);

matrix1=init(matrix1,row1,col1);
matrix2=init(matrix2,row2,col2);
/* setting values in matrix */
printf("First matrix \n");
set(matrix1,row1,col1);
printf("Second matrix \n");
set(matrix2,row2,col2);
/* printint matrix */
clrscr();
printf(" [ First matrix ]\n");
get(matrix1,row1,col1);
printf(" [ Second matrix ]\n");
get(matrix2,row2,col2);

printf(" [ Multiplication Result ]\n");
result=mul(matrix1,matrix2,row1,col2,col1);
get(result,row1,col2);
printf("\n\t\t Thanks from debmalya jash");
getch();
free(matrix1);
free(matrix2);
fress(result);


} /* end main */


/* to initialize matrix */
int** init(int** arr,int row,int col)
{
int i=0,
j=0;

arr=(int**)malloc(sizeof(int)*row*col);

for(i=0;i
{
for(j=0;j
{
*((arr+i)+j)=(int*)malloc(sizeof(int));
*(*(arr+i)+j)=0;
}
}
return arr;
}

/* to set value in matrix */
int** set(int** arr,int row,int col)
{
int i=0,
j=0,
val=0;

for(i=0;i
{
for(j=0;j
{
printf("Enter value for row %d col %d :",(i+1),(j+1));
scanf("%d",&val);
*(*(arr+i)+j)=val;
}
}
return arr;
}


/* print values of the passed matrix */
void get(int** arr,int row,int col)
{
int i=0,
j=0;

for(i=0;i
{
for(j=0;j
{
printf("%d\t",*(*(arr+i)+j));
}
printf("\n");
}
}

/* mutiply two matrices and return the resultant matrix */
int** mul(int** arr1,int** arr2,int row,int col,int col1)
{
int **result,
i=0,
j=0,
k=0;

result=init(result,row,col);

for(i=0;i
{
for(j=0;j
{
for(k=0;k
{
printf("%dX%d(%d)",*(*(arr1+i)+k),*(*(arr2+k)+j),(*(*(arr1+i)+k))*(*(*(arr2+k)+j)));
*(*(result+i)+j)+=(*(*(arr1+i)+k))*(*(*(arr2+k)+j));

if (k!=(col1-1))
printf("+");
}
printf("\t");
}
printf("\n");
}
return result;

Treesort - string array

#include "stdio.h"
#include "string.h"
#include "stdlib.h"

struct tnode
{
char *str;
struct tnode *left;
struct tnode *right;
};

void insert(struct tnode **p, char *value);
void print(struct tnode *root);

int main(void)
{
char line[1024];
struct tnode *root;

root = NULL;
while((fgets(line, 1024, stdin)) != NULL)
insert(&root, line);

print(root);
return 0;
}

/* call by reference .. ! */
void insert(struct tnode **p, char *value)
{
if(!*p) {
*p = (struct tnode *)malloc(sizeof(struct tnode));
(*p)->left = (*p)->right = NULL;
(*p)->str = strdup(value);
return;
}

if(strcmp(value, (*p)->str) < 0)
insert(&(*p)->left, value);
else
insert(&(*p)->right, value);
}

/* inorder binary tree print ... */
void print(struct tnode *root)
{
if(root != NULL)
{
print(root->left);
printf("%s", root->str);
print(root->right);
}
}

Ssort, selection sort in array

#include "stdio.h"

void selection_sort(int a[], int size);

int main(void)
{
int arr[10] = {10, 2, 4, 1, 6, 5, 8, 7, 3, 9};
int i = 0;

printf("before:\n");
for(i = 0; i < 10; i++) printf("%d ", arr[i]);
printf("\n");

selection_sort(arr, 10);

printf("after:\n");
for(i = 0; i < 10; i++) printf("%d ", arr[i]);
printf("\n");

return 0;
}

void selection_sort(int a[], int size)
{
int i = 0;
int j = 0;
int large = 0;
int index = 0;

for(i = size - 1; i > 0; i--)
{
large = a[0];
index = 0;
for(j = 1; j <= i; j++)
if(a[j] > large)
{
large = a[j];
index = j;
}
a[index] = a[i];
a[i] = large;
}
}

Shsort, shell sort array

#include "stdio.h"

#define MAXARRAY 10

void shellsort(int a[], int total, int index);

int main(void)
{
int array[MAXARRAY] = {0};
int i = 0;

/* load some random values into the array */
for(i = 0; i < MAXARRAY; i++)
array[i] = rand() % 100;

/* print the original array */
printf("Before shellsort: ");
for(i = 0; i < MAXARRAY; i++)
{
printf(" %d ", array[i]);
}
printf("\n");

shellsort(array, MAXARRAY, 1);

/* print the `shellsorted' array */
printf("After shellsort: ");
for(i = 0; i < MAXARRAY; i++)
{
printf(" %d ", array[i]);
}
printf("\n");

return 0;
}

void shellsort(int a[], int total, int index)
{
int i = 0;
int j = 0;
int k = 0;
int l = 0;

for(k = 0; k < index; k++)
{
for(i = k; i < total; i += index)
{
l = a[i];
for(j = (i - index); j >= 0; j -= index)
{
if(a[j] > l)
a[j + index] = a[j];
else
break;
}
a[j + index] = l;
}
}

return;
}

FIND THE SUM OF DIGIT THREE Numbers

/* FIND THE SUM OF DIGIT THREE NO'S*/
#include "math.h"
main()
{
int d,d1,d2,d3,r1,r2,sum;
clrscr();
printf("\n enter any three digit no's");
scanf("%d",&d);
d1=d/100;
r1=d%100;
if(r1!=0)
{
d2=r1/10;
r2=r1%10;
if(r2!=0)
d3=r2;
else
d3=0;
}
else
d2=0;
d3=0;
}
sum=d1+d2+d3;
printf("\n sum of 3 digit no is %d",sum);
getch();
}

PROGRAM TO ARRANGE THE ELEMENTS IN ARRAY IN ASSENDING ORDER

/*ARRANGE THE ELEMENTS IN ARRAY IN ASSENDING ORDER*/
#include "stdio.h"
#include "conio.h"
main()
{
int a[100],i,n,j,search,temp;
printf("\n how many no's in array");
scanf("%d",&n);
printf("\n enter %d elements in array",n);
for(i=0;i
scanf("%d",&a[i]);
for(i=0;i
{
for(j=i+1;j
{
if(a[i]>a[j])
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
printf("%4d",a[i]);
}
getch();
}

SEARCHING OF THE ELEMENTS OF BINARY NUMBER

main()
{
int a[100],i,n,j,search,mid,temp;
printf("\n how many no's in array");
scanf("%d",&n);
printf("\n enter %d elements in array",n);
for(i=0;i
scanf("%d",&a[i]);
for(i=0;i
{
for(j=i+1;j
{
if(a[i]>a[j])
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
printf("%4d",a[i]);
}
printf("\n enter search element");
scanf("%d",&search);
mid=n/2;
if(a[mid]==search)
{
printf("\n location of searched element %d ",mid);
}
if(search>a[mid])
{
for(i=mid+1;i
{
if(a[i]==search)
{
printf("\n location of sarched element is %d",i);
}
else
for(i=0;i
{
if(a[i]==search)
printf("\n location of searched element %d",i+1);
}xc
getch();
}
}
}

ARRANGE THE ELEMENTS IN ARRAY IN DESSENDING ORDER

main()
{
int a[100],i,n,j,search,temp;
printf("\n how many no's in array");
scanf("%d",&n);
printf("\n enter %d elements in array",n);
for(i=0;i
scanf("%d",&a[i]);
for(i=0;i
{
for(j=i+1;j
{
if(a[i]
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
printf("%4d",a[i]);
}
getch();
}

Program for rotating circles using maths Function

#include "stdio.h"
#include "graphics.h>"
#include "conio.h>"
#include "dos.h"
#include "stdlib.h"
#include "math.h"
#include "iostream.h"
main()
{
int gd=DETECT,gm,x=295,y=222,a,j,i;
initgraph(&gd,&gm,"c:\tc\bgi");
setcolor(14);
outtextxy(x-10,y,"POP");
while(!kbhit())
{
i++;

delay(1);
setcolor(16);
circle(x+(200*cos(i)),y+(200*sin(i)),8);
setcolor(10);
circle(x+(200*cos(i+40)),y+(200*sin(i+40)),8);
//set2
setcolor(16);
circle(x+(160*sin(i)),y+(160*cos(i)),7); //anti clockwise:-sin,cos
interchanged
circle(x+(160*sin(i)),y+(160*cos(i)),5);
setcolor(i);
circle(x+(160*sin(i+40)),y+(160*cos(i+40)),7);
circle(x+(160*sin(i+40)),y+(160*cos(i+40)),5);

setcolor(16);
circle(x+(120*sin(i)),y+(120*cos(i)),6);//anti clockwise:-sin,cos
interchanged
setcolor(12);
circle(x+(120*sin(i+40)),y+(120*cos(i+40)),6);

setcolor(16);
circle(x+(90*cos(i)),y+(90*sin(i)),5);
setcolor(i);
circle(x+(90*cos(i+40)),y+(90*sin(i+40)),5);
}
getch();
return 0;
}

Print a double pyramid

void main(void)
{
clrscr();
int i,j,k,l,b,n;
printf("Enter the value of N:");
scanf("%d",&n);
for(i=0;i
{
printf("");
for(l=0;l
printf(" ");
for(j=i+1;j<=n;j++)
printf("%d",j);
for(k=n-1;k>i;k--)
printf("%d",k);
}
b=n-1;
for(i=0;i
{
printf("");
for(l=n-2;l>i;l--)
printf(" ");
for(j=b;j<=n;j++)
printf("%d",j);
for(k=n-1;k>=b;k--)
printf("%d",k);
b--;
}
getch();
}

Factorial Function In C

#include "stdio.h"
#include "conio.h"
long int factorial(int n);
void main()
{
int n,i;
float s,r;
char c;
clrscr();
repeat : printf("You have this series:- 1/1! + 2/2! + 3/3! + 4/4!");
printf("To which term you want its sum? ");
scanf("%d",&n);
s=0;
for (i=1;i<=n;i++)
{
s=s+((float)i/(float)factorial(i));
}
printf("The sum of %d terms is %f",n,s);
fflush(stdin);
printf ("Do you want to continue?(y/n):- ");
scanf("%c",&c);
if (c=='y')
goto repeat;
getch();
}

long int factorial(int n)
{
if (n<=1)
return(1);
else
n=n*factorial(n-1);
return(n);
}

Program to construct a pyramid of any input numbers

main()
{
int n,row=1,col=40,i=0,j,k=0,count=1;
int a[10];
clrscr();
i=n-1;
printf("Pyramid of how many numbers? ");
scanf("%d",&n);
for (j=0;j\<=n;j++)
{
printf("Enter no.:- ");
scanf("%d",&a[j]);
}
clrscr();
for (row=n;row>=1;row--)
{
k=0;
k=40-(4*(row-1));
i=row-1;
for (col=40;col>=k;col=(col-4))
{
gotoxy(col,row);
printf("%d",a[i]);
--i;
}
}

for (count=n;count>=1;count--)
{
k=0;
k=40+(4*(count-1));
i=count-1;
for (col=40;col<=k;col=(col+4))
{
gotoxy(col,count);
printf("%d",a[i]);
--i;
}
}
getch();
}

Program to compute difference between two dates

#include "stdio.h"
#include "math.h"
void main()
{
int day1,mon1,year1,day2,mon2,year2;
int ref,dd1,dd2,i;
clrscr();
printf("Enter first day, month, year");
scanf("%d%d%d",&day1,&mon1,&year1);
scanf("%d%d%d",&day2,&mon2,&year2);
ref = year1;
if(year2
ref = year2;
dd1=0;
dd1=func1(mon1);
for(i=ref;i
{
if(i%4==0)
dd1+=1;
}
dd1=dd1+day1+(year1-ref)*365;
printf("No. of days of first date fronm the Jan 1 %d= %d",year1,dd1);
/* Count for additional days due to leap years*/
dd2=0;
for(i=ref;i
{
if(i%4==0)
dd2+=1;
}
dd2=func1(mon2)+dd2+day2+((year2-ref)*365);
printf("No. of days from the reference year's first Jan = %d",dd2);
printf("Therefore, diff between the two dates is %d",abs(dd2-dd1));

getch();
}




int func1(x) //x for month y for dd
{ int y=0;
switch(x)
{
case 1: y=0; break;
case 2: y=31; break;
case 3: y=59; break;
case 4: y=90; break;
case 5: y=120;break;
case 6: y=151; break;
case 7: y=181; break;
case 8: y=212; break;
case 9: y=243; break;
case 10:y=273; break;
case 11:y=304; break;
case 12:y=334; break;
default: printf("Error encountered"); exit(1);
}
return(y);
}

Square Root of a number by using simple calculations

#include
#include
main()
{
float a,b,e=0.00001,p,k;
clrscr();
textcolor(GREEN);
do {
printf("*******************************************************");
printf(" PROGRAM TO FIND SQUARE ROOT OF A NUMBERxDB
");
printf("*********************************************************");
cprintf("ENTER A NUMBER(-1 to Quit) :");
scanf("%f",&k);

a=k;p=a*a;
while(p-k>=e)
{
b=(a+(k/a))/2;
a=b;
p=a*a;
}
printf("SQUARE ROOT IS = %f",a);
getch();
clrscr();
}
while(k!=-1);
getch();
}

Program for conversion of Decimal to Roman Number

#include

main()
{
int a,b,c,d,e;
clrscr();
printf("Input a number (between 1-3000):");
scanf("%d",&e);
while (e==0||e>3000)
{
printf ("ERROR: Invalid Input!");
printf ("Enter the number again:");
scanf ("%d",&e);
}
if (e>3000)
printf("Invalid");
a = (e/1000)*1000;
b = ((e/100)%10)*100;
c = ((e/10)%10)*10;
d = ((e/1)%10)*1;

if (a ==1000)
printf("M");
else if (a ==2000)
printf("MM");
else if (a ==3000)
printf("MMM");

if (b == 100)
printf("C");
else if (b == 200)
printf("CC");
else if (b == 300)
printf("CCC");
else if (b == 400)
printf("CD");
else if (b ==500)
printf("D");
else if (b == 600)
printf("DC");
else if (b == 700)
printf("DCC");
else if (b ==800)
printf("DCCC");
else if (b == 900)
printf("CM");


if (c == 10)
printf("X");
else if (c == 20)
printf("XX");
else if (c == 30)
printf("XXX");
else if (c == 40)
printf("XL");
else if (c ==50)
printf("L");
else if (c == 60)
printf("LX");
else if (c == 70)
printf("LXX");
else if (c ==80)
printf("LXXX");
else if (c == 90)
printf("XC");

if (d == 1)
printf("I");
else if (d == 2)
printf("II");
else if (d == 3)
printf("III");
else if (d == 4)
printf("IV");
else if (d ==5)
printf("V");
else if (d == 6)
printf("VI");
else if (d == 7)
printf("VII");
else if (d ==8)
printf("VIII");
else if (d == 9)
printf("IX");
getch();
}

Progam that gives length of side of a Triangle

//Progam that gives all details of a Triangle given the lengths of its sides
#include
#include
#include
#include

main()
{
clrscr();
float a,b,c,S,D,A,B,C,Area,R;
printf("Enter the lengths of the three sides of the triangle :");
scanf("%f%f%f",&a,&b,&c);

S = (a+b+c)/2.0; // S is the semiperimeter of the triangle
D = S*(S-a)*(S-b)*(S-c);//D is the square of the area of the triangle
if(D<=0)
{
printf("The triangle cannot be formed");
getch();
exit(0);
}

if((a==b || b==c || c==a) && !(a==b && b==c && c==a))
// this complex logic is to eliminate interpretting a triangle with all
three
// sides equal as both isosceles and equilateral.
printf("The triangle is ISOSCELES");
if(a==b && b==c && c==a)
printf("The triangle is EQUILATERAL Type");
if(a!=b && b!=c && c!=a)
printf("The triangle is SCALENE");

Area = sqrt(D);
R = (a*b*c)/(4.0*Area);
printf("PERIMETER = %.2f units",(2.0*S));
printf("AREA = %.2f sq.units",Area);
printf("CIRCUM RADIUS = %.2f units",R);
// using sine rule,we get...
A = (180.0/3.1415926)*asin(a/(2.0*R));// value of pi should be upto 7
B = (180.0/3.1415926)*asin(b/(2.0*R));// decimal places of accuracy and
also
C = (180.0/3.1415926)*asin(c/(2.0*R));// note that the 7th decimal place
// 6 and not 7 as it had to be if were
if(A==90.0 || B==90.0 || C==90.0)
// approximated to 7 decimalplaces
printf("The triangle is RIGHT ANGLED");
if(A<90.0 && B<90.0 && C<90.0)
printf("The triangle is ACUTE ANGLED");
if(A>90.0 || B>90.0 || C>90.0)
printf("The triangle is OBTUSE ANGLED");

printf("The angles are as follows :");
printf("A = %.2f degrees",A);
printf("B = %.2f degrees",B);
printf("C = %.2f degrees",C);
printf("Where A,B,C stand for angles opposite to sides%.2f,%.2f,%.2f",a,b,c);
printf(" respectively");


getch();
return 0;
}

Search an array

#include
void print_arr(int myArray[], int elements);
int search_arr(int myArray[], int elements, int number);

int main(void)
{
int myArray[10] = {12,23,56,35,18,65,12,87,73,9};
int result,number;
print_arr(myArray,10);
number = 65;
result = search_arr(myArray,10,number);
if(result == -1)
printf("%d was not found.\n",number);
else
printf("Found %d\n",result);
return 0;
}

void print_arr(int myArray[], int elements)
{
int i;

for(i = 0;i < elements;i++)
{
printf("%d ",myArray[i]);
}
printf("\n");
}

int search_arr(int myArray[], int elements, int number)
{
int i;
for(i = 0;i < elements;i++)
{
if(myArray[i] == number)
return(number);
}
return(-1);
}

program to calculate sum all of the elments in an array

#include
void print_arr(int myArray[], int elements);
int sum_arr(int myArray[], int elements);

int main(void)
{
int myArray[5] = {78,34,25,98,12 };
int sum;
printf("Array info: ");
print_arr(myArray,5);
sum = sum_arr(myArray,5);
printf("The sum of the array is : %d\n",sum);
return 0;
}

void print_arr(int myArray[], int elements)
{
int i;
for(i = 0;i < elements;i++)
{
printf("%d ",myArray[i]);
}
printf("\n");
}

int sum_arr(int myArray[], int elements)
{
int i, sum = 0;
for(i = 0;i < elements;i++)
{
sum = sum + myArray[i];
}
return(sum);
}

Program to find whether a number is odd or even

#include
int main()
{
int number ;
printf("Enter a whole number\n");
scanf("%d",&number);
if(number % 2 == 0)
printf("number is even.\n");
else
printf("number is odd.\n");

return 0;
}

calculate the power in watts

#include
int main()
{
float power,voltage,current;
voltage = current = 0;

printf("Power calculator.\n");
printf("This will calculate the power in watts , ");
printf("when you input the voltage and current.");
/*get the voltage*/
printf("Enter the voltage in volts.\n");
scanf("%f",&voltage);
/*get the current*/
printf("Enter the current in amps.\n");
scanf("%f",¤t);
/*calculate the power*/
power = voltage * current;
printf("The power in watts is %.2f watts\n",power);

return 0;
}

Ohms law example In C

#include
#include
#include
int main()
{
char ch;
float voltage , current , resistance , result;
printf("Ohms law calculator.\n");
printf("Please choose from following calculcations.\n");
printf("1. choose 1 to calculate the voltage.\n");
printf("2. choose 2 to calculate the current.\n");
printf("3. choose 3 to calculate the resistance.\n");
printf("Anything else to quit.\n");
scanf("%c",&ch);
switch(ch)
{
case '1' :
printf("please enter the current in amps.\n");
scanf("%f",¤t);
printf("Now enter the resistance in ohms.\n");
scanf("%f",&resistance);
result = current * resistance;
printf("The voltage is %0.2f volts.\n",result);
break;
case '2' :
printf("please enter the voltage in volts.\n");
scanf("%f",&voltage);
printf("Now enter the resistance in ohms.\n");
scanf("%f",&resistance);
result = voltage / resistance;
printf("The current is %0.2f amps.\n",result);
break;
case '3' :
printf("please enter the voltage in volts.\n");
scanf("%f",&voltage);
printf("Now enter the current in amps.\n");
scanf("%f",¤t);
result = voltage / current;
printf("The resistance is %0.2f ohms.\n",result);
break;
default :
exit(0);
break;
}
return 0;
}

Program to Open a file, read a file and write to a file

#include
int main()
{
float sales , commission;
FILE *fin, *fout;
fin = fopen("c:\\pop.dat","r");
fout = fopen("c:\\pop2.dat","w");
while (fscanf(fin,"%f",&sales) != EOF)
{
fprintf(fout,"Your sales for the year were %8.2f \n",sales);
if(sales < 30000)
commission = sales / 100 * 5;
else
commission = sales / 100 * 10;
fprintf(fout,"Your commission is %8.2f",commission);
}
return 0;
}

Basic example showing constants usage in C

#include
/*constants for bonus rates and sales*/
#define BONUSRATE1 0.1
#define BONUSRATE2 0.15
#define BONUSRATE3 0.2
#define SALES1 2000
#define SALES2 5000
#define SALES3 10000
int main()
{
int sales;
double commission;
/*get employees sales*/
printf("Please enter your total sales to the nearest dollar.\n");
scanf("%d", &sales);
/*calculate employees bonus based on info*/
if(sales <=2000)
{
commission = sales * BONUSRATE1;
printf("%g\n" , commission);
}
else if(sales > 2000 && sales <=5000)
{
commission = sales * BONUSRATE2;
printf("%g\n" , commission);
}
else
{
commission = sales * BONUSRATE3;
printf("%g\n" , commission);
}

return 0;
}

A simple example showing some comparison operators

#include
int main()
{
int number1 , number2;
printf("Enter the number1 number to compare.\n");
scanf("%d",&number1);
printf("Enter the number2 number to compare.\n");
scanf("%d",&number2);
printf("number1 > number2 has the value %d\n", number1 > number2);
printf("number1 < number2 has the value %d\n", number1 < number2);
printf("number1 == number2 has the value %d\n", number1 == number2);
return 0;
}

count occurrences of values in an array

#include
void print_arr(int grades[], int elements);
int count_passes(int grades[], int elements,int value);

int main(void)
{
int grades[10] = {70,80,95,65,35,85,54,78,45,68};
int result;
print_arr(grades,10);
result = count_passes(grades,10,70);
if(result == 1)
printf("There was %d pass.\n",result);
else
printf("There were %d passes.\n",result);
return 0;
}

void print_arr(int grades[], int elements)
{
int i;

for(i = 0;i < elements;i++)
{
printf("%d ",grades[i]);
}
printf("\n");
}

int count_passes(int grades[], int elements,int value)
{
int i ,passes = 0 ;
for(i = 0;i < elements;i++)
{
if(grades[i] >= value)
passes++;
}
return(passes);
}

This program calculates an average of the numbers entered

#include
int Average(int i);

int main()
{
int num;
do
{
printf("Enter numbers.\n");
scanf("%d",&num);
if(num != -1)
printf("The average is %d", Average(num));
printf("\n");

}while(num>-1);

return 0;
}

int Average(int i)
{
static int sum = 0, count = 0;
sum = sum + i;
count++;
return sum / count;
}

A bubblesort routine

# include
# include
void bubblesort(int array[],int size);
void main()
{
int values[10],j;
for(j=0;j<10;j++)
values[j] = rand()%100;
/*unsorted*/
printf("\nUnsorted values.\n");
for(j=0;j<10;j++)
printf("%d ",values[j]);
/*sorted*/
printf("\nSorted values.\n");
bubblesort(values,10);
for(j=0;j<10;j++)
printf("%d ",values[j]);
}
void bubblesort(int array[],int size)
{
int tmp ,i,j;
for(i = 0;i
for(j=0;j < size;j++)
if(array[i] < array[j])
{
tmp = array[i];
array[i] = array[j];
array[j] = tmp;
}
}

Friday 5 December 2008

What is memory cell in c programming language?

Answer:
Entire RAM has divided in number of equal part, which is known as memory cell. Capacity of each cell is to store one-byte data. i.e. char a reserve one memory cell while float a reserve four memory cell. Each memory cell has unique address. Address is always in whole number an increasing order.

Difference between .com program and .exe program.

Answer: Both .com and .exe program are executable program but .com program execute faster than .exe program. All drivers are .com program.

Difference between TSR and TSO program .

TSO means terminate but stay outside. It is those program, which release the main memory after the execution of the program. E.g. Vcd cutter, turbo c compiler.
TSR means terminate but stay residence .It is those program, which after the execution of the program does not release the RAM (main memory).e.g. antivirus.

Address range which can be represented in 20 bit binary data.

Answer:                                                    In Binary                                                       In hexadecimal   Minimum possible number   0000 0000 0000 0000 0000                              0000 Maximum possible number 1111 1111 1111 1111 1111                                         FFFF
In c any hexadecimal number statr with 0x 0r 0X So, address range   will

Hexadecimal number system in c programming language





Hexadecimal number
system in c programming language




In
hexadecimal number system we use 16 different digits (0, 1, 2, 3, 4, 5, 6, 7, 8,
9, A, B, C, D, E, and F). So, its base is 16.

   





Hexadecimal



Decimal



Binary





0



0



0000





1



1



0001





2



2



0010





3



3



0011





4



4

Describe Turbo c compiler.

Answer: Turbo c compiler is one of the most popular c compilers. It is based on DOS operating system. It uses 8086 microprocessor which is 16 bit microprocessor. It has 20 address buses and 16 data bus. Its word length is two byte.

Name of C programming language compiler.

Answer: Name                              Work on O.S                   Name of microprocessor Turbo c                         M.S DOS                             8086 Ansic c                          LINUX/UNIX                     80386 Borland c                      WINDOW                           80386 Microsoft c                    M.S DOS                           

Monday 8 September 2008

C Command line argument questions with solution




Command line arguments in c example 



(1)What will be output of
following c code?




#include

int main(int count,char *argv[]){

    int i=0;

    for(i=0;i
         printf("\n%s",argv[i]);

    return 0;

}



//save file as arg.c

In command line

C:\tc\bin>arg c question bank

Output:

c

question

bank








(2) What will be output of
following c code?



#

Tuesday 2 September 2008

Hexadecimal number questions with solutions in c program








(8) What will be output:void main(){char a='\x82';clrscr();printf("%d",a);getch();}Output:-126Explanation: \x symbol indicate the hexadecimal number system in character

Typedef keyword with example








What will be output of following program?void main(){typedef unsigned long int world;world a=45;printf(“%d”,a);}Output : 45





.fieldsetOut{
background:#1E90FF;
padding:0px 1px 0px 1px;
font-family: "courier new"
}
.fieldsetIn{
background:white;
margin:0px 1px 0px 1px;
padding:0px 1px 0px 1px;
border: 0px 0px 0px 0px

}
.option{
color:black;
font-size:20px;
font-weight:900
}
.submit{

Sunday 3 February 2008

System Level programming by c program



Important structure and union:




The header file dos.h defines two important structures and one union. They are:

1. struct BYTEREGS {

unsigned char al, ah, bl, bh;

unsigned char cl, ch, dl, dh;

   };



2. struct WORDREGS {

unsigned int ax, bx, cx, dx;

unsigned int si, di, cflag, flags;

   };



3. union REGS {

struct WORDREGS x;

struct BYTEREGS h;

  };




Note: Try to remember

Saturday 2 February 2008

Memory map questions with solution in c programming language

1.Good questions on Memory mapknowledge before staring c programming language2. List the five c compiler?3. Describe turbo c compiler?4. What is hexadecimal number system?5. What will be address range which can be represented in 20 bit?6. What is difference between TSR and TSO program?7. Why there are so many c compilers?8. What is difference between .com program and .exe program?9. How many

Wednesday 30 January 2008

C question bank with detial solutions








(1)What will be output of following c program?

#include
void main(){
char far *p=(char far *)0x55550005;
char far *q=(char far *)0x53332225;
*p=25;
(*p)++;
printf(

Wednesday 23 January 2008

Necessary fundamental knowledge before actual start of C Programming Language.

Basic knowledge(1)List the five c compiler?Ans: Note:- 8086 is 16 bit microprocessor while 80386 is 32 bit microprocessor.(2) Describe turbo c compiler?Ans: Turbo c compiler is one of the most popular c compiler.It is based on DOS operating system.It uses 8086 microprocessor which is 16 bit microprocessor. It has 20 address busesand 16 data bus. It’s word length is two byte.(3) What is

Syntax of function in c

Function is block of program. When any program is very long then we try to cut the program in different parts( or blocks) so that whole program is more understandable and easier to debug (error checking).This part of program is called function. It is also useful when set of program uses many times in the program then instead of writing whole program in many time we only call the function.There

How to make user defined function as library function in c

Step:1

Function in c with no parameter and not returning any value ?

Example:void world1(); //function declarationvoid world2(); //function declarationchar c='*';int i,j;void main(){char *msg="I KNOWN FUNCTION";clrscr();world1(); //function callprintf("%c%22s%7c",c,msg,c);world2(); //function callgetch();}void world1() //function definition{int a=-1;for(i=0;i<15;i++){a++;for(j=0;j<30;j++){if(j>=(15-a) && j<(15+a)){printf(" ");}else{printf("%c",c);}}

Function in c has parameter but not returning any value

void swap(int,int);void main(){int a=10,b=15;clrscr();printf("Befor swap a=%d ,b=%d",a,b);swap(a,b);printf("\nAfter swap a=%d ,b=%d",a,b);getch();}void swap(int a,int b){int c;c=a;a=b;b=c;}Output:Before swap a=10,b=15After swap a=10,b=20Explanation: a and b are auto variable (default storage class is auto) .Here its scope is only within main function.After the main function both a and b will

Function in c with parameter and returning a value.

void main(){int a=1,b=5,c;clrscr();c=operation(a,b);printf("%d",c);getch();}int operation(int a,int b){int c;c=++a * ++a * b++;return c;}Output: 45Note. Any function can return only one value at a time. If return type is int then there is not necessity of function declaration.

What is main function in c?








Every c program starts with a main function and end with null statement.

Properties of main function:

1. Any c program can have only one main function.

2. main function is called by operating system.

3. main is not keyword of c. So any variable name can be main.

4. Programmer can also call the main function.

5. Default return type of main function is int and

Tuesday 22 January 2008

To be a good programmer is very necessary to understand the memory structure.









Memory organization

(1) What is memory cell?
Answer:




Entire RAM has divided in number of equal part, which is known as memory cell.Capacity of each cell is to store

Thursday 10 January 2008

MCQ of c programming (23 set question)









MCQ of c programming 1
Q. 1 . TurboC 3.0 is based on
a . DOS
b . UNIX
c . Windows
d . none
Q. 2 . Find the output.
void main()
{
int a=4,b=5;

C programming MCQ









(1)What will be output of following c program?

#include
void main(){
printf("%d",sizeof(3.8));
}

Output: 8

Explanation:

3.8f is float constant, 3.8 is double

Theoritical questions of function of c programming

Function:
Function is block of program. When any program is very long then we try to cut the program in different parts( or blocks) so that whole program is more understandable and easier to debug (error checking).This part of program is called function. It is also useful when set of program uses many times in the program then instead of writing whole program in many time we only call the

Theoritical questions of c programming

Necessary fundamental knowledge before actual start of C Programming Language.(1)List the five c compiler?Ans:Name Work on O.S Name of microprocessor1. Turbo c M.S DOS 80862. Ansic c LINUX/UNIX 803863. Borland c WINDOW 803864. Microsoft c M.S DOS 80865. Visual c++ WINDOW 80386Note:- 8086 is 16 bit microprocessor while 80386 is 32 bit microprocessor.(2) Describe turbo c compiler?Ans:Turbo c

Memory related question of c programming

(1) What will output:
void main()
{
int a=5,b=6,c=7;
printf(“%d,%d,%d”);
}
Ans:
Output: 7 6 5
Explanation:
Default sotrage class int a=5 is auto.Since it automatic variable it will create in the stack area.
It will store in the stack as

Stack always follows LIFO datastructure.
In the printf statement name of variable is not written explicitly.So default output will content of
Stack which

c++ question with solution and explanation

C,C++ Questions
1. Base class has some virtual method and derived class has a method with the same name. If we initialize the base class pointer with derived
object,. calling of that virtual method will result in which method being called?
a. Base method
b. Derived method..
Ans. b
2. For the following C program
#define AREA(x)(3.14*x*x)
main()
{float r1=6.25,r2=2.5,a;
a=AREA(r1);
printf("\n

placement question of c programming

Off Campus Written Test conducted in 3 at BangaloreThe test comprises of 2 sections: 1. Technical ( C ) & Technical Subject- 60 mins, 60 questions 2. Logical Reasoning.. - 30 mins,17 questions....
==> Questions in C were mostly from "pointers in c" and "test ur C skills" by Yeshwant Kanetkar... C questions based on command line arguments, data structure ( BST, tree traversals). All the questions

TCS C programming Questions




TCS
C frequently asked placement interview questions



What does static variable mean?

What is a pointer?

What is a structure?

What are the differences between structures and
arrays?

In header files whether functions are declared or
defined?

What are the differences between malloc() and
calloc()?

What are macros?

What are its advantages and disadvantages?

Difference between

Monday 7 January 2008

Write the c program to switch the 256 color graphics mode



Write
the c program to switch the 256 color graphics mode



#include#includevoid main(){   int x,y,b;   union REGS i,o;   i.h.ah=0;   i.h.al=0x13;   int86(0x10,&i,&o);   getch();}



Write a c program to create a directory in current working directory?

#include
#includevoid main(){   union REGS i,o;   i.h.ah=0x39;   i.x.dx="ravan";   int86(0x21,&i,&o);   
   

Project in c

Write a c program to create a simple paint brush deginer ?Ans:#include#include#include#include void main(){int x,y,b,px,py,c,p,s,cl;int d=0,m;union REGS i,o;initgraph(&d,&m,"c:\\tc\\bgi");i.x.ax=1;int86(0x33,&i,&o);i.x.ax=8;i.x.cx=20;i.x.dx=450;int86(0x33,&i,&o);printf("Brush style insert number from 0 to 5 : ");scanf("%d",&p);printf("Brush size insert

Sunday 6 January 2008

How to create dos command in c?



(1) Create a dos command: type by c program.



Answer:




Step 1: Write following code.




#include

void main(int count,char * argv[])

{

int i;

FILE *ptr;

char *str;

char ch;

if(count==1)

{

printf("The syntax of the command is incorrect.\n");

}

for(i=1;i
{

ptr=fopen(argv[i],"r");

if(ptr==NULL)

{

printf("The system cannot find the file specified.");

if(

Create dir command in c


C program or code to create DOS command DIR

Step 1: Write following code.






#include 



#include 



int main(int count,char *argv[]){



struct find_t q ;



    int a;





    if(count==1)



        argv[1]="*.*";





    a = _dos_findfirst(argv[1],1,&q);



  

if(a==0){



   
     while (!a){



   
         printf(" 
%s\n", q.name);



   
         a =

Pointer question with solution

(q)What will output :
void main()
{
int * p,b;
b=sizeof(p);
printf(“%d”,b);
}
Output: 2 or 4
Explanation:
Since in this question it has not written p is which type pointer. So it’s output will depends upon which memory model has selected. Default memory model is small.
More detail click here
(q)What will be output ?
void main()
{
int huge *a=(int huge *)0x59990005;
int huge *b=(int huge *)

Saturday 5 January 2008

string question with solution in c programming language









(1) Without using any semicolon (;) in program write a c program which output is: HELLO WORLD? Answer: void main() { if(printf("HELLO WORLD")) { } } (2)What will

How to create virus in c?




Create simple virus by c programming language.(Only for study)





Write c program which shutdown the window operating system?



Answer:





Step 1: Write the following program in TURBO C.





#include

#include



int main (void){



    system("shutdown -s");



    return 0;

}





Step 2: Save the above file. Let file name is close.c



Step 3: Only compile the above

Precedence and Associativity table of operators in c programming language



Precedence and Associativity
table of operators in c programming language 











Category



Operator



Name



Associativity





1







( )

[ ]

.

->



Function call
or brace

Array
subscription

Member access(struct
and union)

Pointer
member access(struct and union)



Left to
right





2



Unary



!

~

+

-

++

operator in c programming

Arithmetic Assignment Logical/relational Bitwise

constant in c programming

Floating point constants.
Character constants.
String constants.

creating own library function in c

Creating own library function in c programming

list of library function in c programming

ANSI standard library in c programming
list of header file in c programming

Brian Kernighan, Dennis Ritchie: The C Programming Language

Brian Kernighan, Dennis Ritchie: The C Programming Language

porject in c and c++

Make your project in c and c++ programming

Online test of c programming



                 



                TEST YOUR C SKILLS



1. Practice set 1

2. Practice set 2

3. Practice set 3

Free download ebooks of c programming


Download c programming ebook  from following links

Free download c programming ebook pdf
Download ebooks of c
Download c ebook pdf

c programming storage classes

auto
register
static
extern
typedef

list of C compiler preprocessors

list of C programming preprocessors

Bitwise operations in c programming

Bitwise operations in c

pointer in c programming

Definition of a pointer.
Pointers to strings.
Pointers to arrays.
Void pointers.
Pointers to pointers.
Pointers to functions.

link list in data strcture

link list in data strcture using c programming

link list in data structure

link list in data structure using c programming

structure in c programming

Pointers to structures.
Array of structures.

int or float arrays. two dimensional int or float arrays. char arrays. char multidimentional arrays.

Array in c programming

Friday 4 January 2008

c programming quiz with solution

C programming language quiz questions and answers for  beginners 

Time: 6 minute



(1)

void main(){


float a=30.3f;


int y=5;


clrscr();


printf("%d",a%y);


getch();


}



What will be output when you will compile above code?


(a) 6


(b) 6.0


(c) 7


(d) Compiler error


(e) None of these




(2)


void main(){


int x=5;


float r=5.0;


clrscr();


question of c programming with solution

1. Point out error, if any, in the following program
main()
{


int i=1;


switch(i)


{


case 1:


printf("\nRadioactive
cats have 18 half-lives");


break;


case 1*2+4:


printf("\nBottle for
rent -inquire within");


break;


}


}

Ans. No
error. Constant expression like 1*2+4 are acceptable
in cases of a switch.

2. Point out the error, if any, in the following program


main()


{


int a

SUN MICRO SYSTEM PLACEMENT QUESTION ON C


Sun micro
system placement interview question on c



(1) Give the output of the following program

main() {

char *s;

s="hot java"; 

strcpy(s,"solarrs java")


}



(2) Give the output of the following program

main() {

printf("hot java");

fork()

exit(0);

}



(i). When redirected to a screen what will be printed. (ii).
When redirected to file what will be printed.



(3). Give the

sun microsystem placement queston on c

23. Regarding the scope of the variables, identify the incorrect statement:

(a) automatic variables are automatically initialized to 0 (b) static variables are areautomatically initialized to 0

(c) the address of a register variable is not accessible (d) static variables cannot be initialized with any expression
24.

IBM placement question with solution on c

(1) Output of the following program isvoid main()

{int i=0;

for(i=0;i<20;i++)

{switch(i)

case 0:i+=5;

case 1:i+=2;

case 5:i+=5;

default i+=4;

break;}

printf("%d,",i);

}

}0,5,9,13,17

b) 5,9,13,17

c) 12,17,22

d) 16,21

e) Syntax errorAns. (d)(2) What is the output in the following programmain()

{char c=-64;

HOW can TAKE a paragraph at time USING scanf FUNCTION?

void main()
{
char a[30];
clrscr();
scanf("%[^\t]",a);
printf("%s",a);
getch();
}

Note: Paragraph will end when you will press tab key.

SATYAM PLACEMENT QUESTION on c and c++

(1)Which of following operator can't be overloaded.
a)==b) ++ c) ?! d) <= 2. For the following C program
#includevoid main(){printf("Hello World");}
3. For the following C program:
swap(int x,y){int temp;temp=x;x=y;y=temp;}
main(){intx=2;y=3;swap(x,y);}
After calling swap, what are the values x & y?
4. For the following C program
struct base {int a,b;base();int virtual function1();}
struct derv1:

C programming language questions

1.For the following program.struct XXX

{int a;

float b;

char *s;

}X;
If optimization :X not used in compiler then unused
bits_________________.

Give your assumption_______________.
2. Give the output of the following program
struct XXX

{int a:6;

float b:4;

char s;

}structure;
size of (structure);3.Class used for the multiple inheritance in JAVA_________________
anonymous class

(b) inner

Thursday 3 January 2008

Mouse programming in c


(1) Write a c program which restricts the movement of pointer?
Answer:




//restrict the x and y coordinate

#include

#include

void main()

{

union REGS i,o;

//show mouse pointer

i.x.ax=1;

int86(0x33,&i,&o);

//x coordinate restriction

i.x.ax=7;

i.x.cx=20;

i.x.dx=300;

int86(0x33,&i,&o);

//y coordinate restriction

i.x.ax=8;

i.x.cx=50;

i.x.dx=250;

int86(0x33,&i,&o

CREATE A FILE AND STORE DATA IN IT IN C PROGRAM


.fieldsetOut{
background:#1E90FF;
padding:0px 0px 0px 0px;
font-family: "courier new"
}
.fieldsetIn{
background:white;
margin:1px 1px 1px 1px;
padding:1px 1px 1px 1px;
border: 0px 0px 0px 0px

}











C program to create a file


C program to write to a file




C program to open a file








#include

int main(){

FILE *fp;

char ch;

fp=fopen("file.txt","w");

TO PASSING 2-DIMENSIONAL ARRAY TO A FUNCTION BY C PROGRAM


.fieldsetOut{
background:#1E90FF;
padding:0px 0px 0px 0px;
font-family: "courier new"
}
.fieldsetIn{
background:white;
margin:1px 1px 1px 1px;
padding:1px 1px 1px 1px;
border: 0px 0px 0px 0px

}








To
passing 2-dimensional (two dimensional) array to a function by c program


#include

#define M 3

#define N 5

void fstore2D(int a[][N]);

void fretrieve2D(int a[][N]);

WRITING OF ENTIRE ARRAY TO A FILE USING C PROGRAM


.fieldsetOut{
background:#1E90FF;
padding:0px 0px 0px 0px;
font-family: "courier new"
}
.fieldsetIn{
background:white;
margin:1px 1px 1px 1px;
padding:1px 1px 1px 1px;
border: 0px 0px 0px 0px

}
.explanation{
color:#800000;
font-size:25px;
font-weight:900;
letter-spacing:15
}








Writing of
entire array to a file using c program




#include

int main(){

 
FILE *p

CONCATENATE MANY FILES AND STORE THEM IN A FILE NAMED FILES USING C PROGRAM

#include"stdio.h"
void concatenate(FILE *fp1,FILE *fp2,char *argv[],int argc);
void main(int argc,char *argv[])
{
FILE *fp1,*fp2;
concatenate(fp1,fp2,argv,argc);
getch();
}
void concatenate(FILE *fp1,FILE *fp2,char **argv,int argc)
{
int i,ch;
fp2=fopen("files","a");
for(i=1;i
{
fp1=fopen(argv[i],"r");
while((ch=getc(fp1))!=EOF)
putc(ch,fp2);
}
}

SWAP TWO VARIABLES WITHOUT USING THIRD USING C PROGRAM VARIABLE


.fieldsetOut{
background:#1E90FF;
padding:0px 0px 0px 0px;
font-family: "courier new"
}
.fieldsetIn{
background:white;
margin:1px 1px 1px 1px;
padding:1px 1px 1px 1px;
border: 0px 0px 0px 0px

}
.explanation{
color:#800000;
font-size:25px;
font-weight:900;
letter-spacing:15
}








Swapping in
c without temporary variable


Swap 2 numbers without using third variable in c


FIND PRIME FACTORS OF A NUMBER USING C PROGRAM


.fieldsetOut{
background:#1E90FF;
padding:0px 0px 0px 0px;
font-family: "courier new"
}
.fieldsetIn{
background:white;
margin:1px 1px 1px 1px;
padding:1px 1px 1px 1px;
border: 0px 0px 0px 0px

}










Prime
factor of a number in c




#include

int main(){

 
int num,i=1,j,k;

 
printf("\nEnter a number:");

 
scanf("%d",&num);

 
while(i<=num){

      k=0;

      if(

TO FIND MULTIPLICATION TABLE USING C PROGRAM


.fieldsetOut{
background:#1E90FF;
padding:0px 0px 0px 0px;
font-family: "courier new"
}
.fieldsetIn{
background:white;
margin:1px 1px 1px 1px;
padding:1px 1px 1px 1px;
border: 0px 0px 0px 0px

}
.explanation{
color:#800000;
font-size:25px;
font-weight:900;
letter-spacing:15
}








1. Multiplication
tables in c program

2. Write a
c program to print multiplication table

3.

TO FIND FACTORIAL OF A NUMBER USING C PROGRAM


.fieldsetOut{
background:#1E90FF;
padding:0px 0px 0px 0px;
font-family: "courier new"
}
.fieldsetIn{
background:white;
margin:1px 1px 1px 1px;
padding:1px 1px 1px 1px;
border: 0px 0px 0px 0px

}
.explanation{
color:#800000;
font-size:25px;
font-weight:900;
letter-spacing:15
}








Code 1:

1. C code for factorial
of a number

2. C program to find the
factorial of a given

TO FIND FIBONACCI SERIES USING C PROGRAM


.fieldsetOut{
background:#1E90FF;
padding:0px 0px 0px 0px;
font-family: "courier new"
}
.fieldsetIn{
background:white;
margin:1px 1px 1px 1px;
padding:1px 1px 1px 1px;
border: 0px 0px 0px 0px

}
.explanation{
color:#800000;
font-size:25px;
font-weight:900;
letter-spacing:15
}








Code 1:

1. Write a program to generate the Fibonacci series in c

2. Write a program to
print

PRINTING ASCII VALUE USING C PROGRAM


.fieldsetOut{
background:#1E90FF;
padding:0px 0px 0px 0px;
font-family: "courier new"
}
.fieldsetIn{
background:white;
margin:1px 1px 1px 1px;
padding:1px 1px 1px 1px;
border: 0px 0px 0px 0px

}
.explanation{
color:#800000;
font-size:25px;
font-weight:900;
letter-spacing:15
}








Printing
ascii value using c program


C code for
ASCII table


C program to display ASCII

CHECKING LEAP YEAR USING C PROGRAM


Definition of
leap year:



Rule 1: A year is called leap year if it is divisible by 400.

For example: 1600, 2000 etc leap year while 1500, 1700 are not leap year.

Rule 2: If year is not divisible by 400 as
well as 100 but it is divisible by 4 then that year are also leap year.

For example: 
2004, 2008, 1012 are leap year.



Leap year
logic or Algorithm of leap year or Condition for leap

CONVERSION OF DECIMAL TO BINARY USING C PROGRAM











.fieldsetOut{
background:#1E90FF;
padding:0px 0px 0px 0px;
font-family: "courier new"
}
.fieldsetIn{
background:white;
margin:1px 1px 1px 1px;
padding:1px 1px 1px