144
October 29, 2011 [C LANGUAGE PROGRAMS BY MR.K-MANIKANTA] For More @ www.stylewap.in Manikanta Page 1 M.C.A Calculate Electricity Bill with if-else condition Calculate Electricity Bill with if-else condition <=100 Rs.4/units > 100 and <=300 Rs.4.50/units >300 and <=500 Rs.4.75/units >500 Rs.5/units #include<stdio.h> #include<conio.h> void main () { int unit, total; clrscr (); printf("Enter Total Units:"); scanf ("%d", &unit); if (unit<=100) { total=unit*4; } else if (unit>100 && unit<=300) { total=unit*4.5; } else if (unit >300 && unit<=500) { total=unit*4.75; } else { total=unit*5; } printf("Total: %d", total); getch (); } Method 2:(Coded by: Manpreet Singh Dhindsa, Patiala) #include<stdio.h> #include<conio.h> void main() { int units; float total_bill; clrscr(); printf("Enter the no. of units"); scanf("%d",&units);

70893566 C Language Programs

Embed Size (px)

Citation preview

Page 1: 70893566 C Language Programs

October 29, 2011 [C LANGUAGE PROGRAMS BY MR.K-MANIKANTA] For More @ www.stylewap.in

Manikanta Page 1 M.C.A

Calculate Electricity Bill with if-else condition

Calculate Electricity Bill with if-else condition

<=100 Rs.4/units

> 100 and <=300 Rs.4.50/units

>300 and <=500 Rs.4.75/units

>500 Rs.5/units

#include<stdio.h>

#include<conio.h>

void main ()

{

int unit, total;

clrscr ();

printf("Enter Total Units:");

scanf ("%d", &unit);

if (unit<=100)

{

total=unit*4;

}

else if (unit>100 && unit<=300)

{

total=unit*4.5;

}

else if (unit >300 && unit<=500)

{

total=unit*4.75;

}

else

{

total=unit*5;

}

printf("Total: %d", total);

getch ();

}

Method 2:(Coded by: Manpreet Singh Dhindsa, Patiala)

#include<stdio.h>

#include<conio.h>

void main()

{

int units;

float total_bill;

clrscr();

printf("Enter the no. of units");

scanf("%d",&units);

Page 2: 70893566 C Language Programs

October 29, 2011 [C LANGUAGE PROGRAMS BY MR.K-MANIKANTA] For More @ www.stylewap.in

Manikanta Page 2 M.C.A

if(units <= 100)

total_bill = units * 4;

else if(units <= 300)

total_bill = units * 4.5;

else if(units <= 500)

total_bill = units * 4.75;

else

total_bill = units * 5;

printf("the bill to be paid is = %f", total_bill);

getch();

}

Add two matrices and store the result

#include<stdio.h>

#include<conio.h>

void main()

{

int a[3][3],b[3][3],c[3][3],i,j;

clrscr();

printf("Enter the elements into matrix A\n");

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

for(j=0;j<3;j++)

scanf("%d",&a[i][j]);

printf("\nEnter the elements into matrix B\n");

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

for(j=0;j<3;j++)

scanf("%d",&b[i][j]);

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

for(j=0;j<3;j++)

c[i][j]=a[i][j]+b[i][j];

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

{

printf("\t\t");

for(j=0;j<3;j++)

printf("%d\t",c[i][j]);

printf("\n\a");

}

getch();

}

2d example insertion sort

#include <stdio.h>

#include <conio.h>

struct node

Page 3: 70893566 C Language Programs

October 29, 2011 [C LANGUAGE PROGRAMS BY MR.K-MANIKANTA] For More @ www.stylewap.in

Manikanta Page 3 M.C.A

{

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 *));

Page 4: 70893566 C Language Programs

October 29, 2011 [C LANGUAGE PROGRAMS BY MR.K-MANIKANTA] For More @ www.stylewap.in

Manikanta Page 4 M.C.A

temp->number = value;

while(two != NULL && temp->number <>number) {

one = one->next;

two = two->next;

}

one->next = temp;

temp->next = two;

}

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;

}

}

A simple example showing some comparison operators

#include

int main()

{

int number1 , number2;

printf("Enter the number1 number to compare.\n");

Page 5: 70893566 C Language Programs

October 29, 2011 [C LANGUAGE PROGRAMS BY MR.K-MANIKANTA] For More @ www.stylewap.in

Manikanta Page 5 M.C.A

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;

}

Add numbers using command line arguments (CLA)

#include<stdio.h>

#include<conio.h>

#include<stdlib.h>

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

{

int sum=0,i;

//Compare if proper number of arguments have been entered

if(argc<3)

{

printf("Insufficient number of arguments:\n");

getch();

return 0;

}

//Add all the numbers entered using atoi function

for(i=1;i<argc;i++)

{

sum+=atoi(argv[i]);

}

//print the sum

printf("Ans=%d",sum);

getch();

}

Add Pointers

#include<stdio.h>

#include<conio.h>

void main()

{

int a[10],sum=0;

int *j;

int i,n;

clrscr();

Page 6: 70893566 C Language Programs

October 29, 2011 [C LANGUAGE PROGRAMS BY MR.K-MANIKANTA] For More @ www.stylewap.in

Manikanta Page 6 M.C.A

printf("Enter how many elements u want to add:");

scanf("%d",&n);

printf("Enter the elements:");

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

{

scanf("%d",&a[i]);

}

j=&a[0];

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

{

sum=sum+*j;

j++;

}

printf("sum=%d",sum);

}

Addition of Two Matrices

//Addition of Matrix

#include <stdio.h>

#include <conio.h>

int m1,n1,m2,n2,i,j,k,z[10][10]={0};

void value_sub(int a,int b,int arr[][10] )

{

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

{

for(j=0;j<b;j++)

{

printf(―Mat[%d%d] = ―,i+1,j+1);

scanf(―%d‖,&arr[i][j]);

fflush(stdin);

}

printf(―‖);

}

}

void mat_mul(int a,int b,int arr[][10],int brr[][10])

{

int k=0;

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

Page 7: 70893566 C Language Programs

October 29, 2011 [C LANGUAGE PROGRAMS BY MR.K-MANIKANTA] For More @ www.stylewap.in

Manikanta Page 7 M.C.A

{

for(j=0;j<b;j++)

{

z[i][j]+=arr[i][j]+brr[i][j];

printf(―%d\t‖,z[i][j]);

}

printf(―\n\n‖);

}

}

int main()

{

int A[10][10]={0},B[10][10]={0};

printf(―Enter the column and row of first matrix(m x n)\n‖);

scanf(―%d%d‖,&m1,&n1);

printf(―Enter the column and row of second matrix(m x n)\n‖);

scanf(―%d%d‖,&m2,&n2);

printf(―\n\n‖);

if (n1==m1||n2==m2)

{

value_sub(m1,n1,A);

printf(―\n\n‖);

value_sub(m2,n2,B);

printf(―\n\n‖);

mat_mul(m1,n2,A,B);

Page 8: 70893566 C Language Programs

October 29, 2011 [C LANGUAGE PROGRAMS BY MR.K-MANIKANTA] For More @ www.stylewap.in

Manikanta Page 8 M.C.A

}

else

printf(―Addition of Matrix cannot be done‖);

getch();

}

AREA OF CIRCLE

#include

void main ()

{

float r,c;

clrscr();

printf ("Enter Radius: ");

scanf ("%f",&r);

c=3.14*r*r;

printf ("\nArea is : %.2f",c);

getch ();

}

Output

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];

Page 9: 70893566 C Language Programs

October 29, 2011 [C LANGUAGE PROGRAMS BY MR.K-MANIKANTA] For More @ www.stylewap.in

Manikanta Page 9 M.C.A

a[i]=a[j];

a[j]=temp;

}

}

printf("%4d",a[i]);

}

getch();

}

ATM programing

ATM C programing language Program code

/*Note Pin code is 1234*/

#include<stdio.h>

#include<conio.h>

void main(void)

{ unsigned long amount=1000,deposit,withdraw;

int choice,pin=0,k=0;

char another='y';

while(pin!=1234)

{ clrscr();

gotoxy(30,25);

printf("Enter pin:");

scanf("%d",&pin);

}

clrscr();

do

{

printf("********Welcome to ATM Service**************\n");

printf("1. Check Balance\n");

printf("2. Withdraw Cash\n");

printf("3. Deposit Cash\n");

printf("4. Quit\n");

printf("*********************************************\n\n");

printf("Enter your choice: ");

scanf("%d",&choice);

switch(choice)

{

case 1:

printf("\nYour Balance is Rs : %lu ",amount);

break;

case 2:

printf("\nEnter the amount to withdraw: ");

scanf("%lu",&withdraw);

if(withdraw%100!=0)

Page 10: 70893566 C Language Programs

October 29, 2011 [C LANGUAGE PROGRAMS BY MR.K-MANIKANTA] For More @ www.stylewap.in

Manikanta Page 10 M.C.A

{

printf("\nPlease enter

amount in multiples of 100");

}else if(withdraw>(amount-500))

{

printf("\nInsufficient Funds");

}else

{

amount=amount-withdraw;

printf("\n\nPlease collect cash");

printf("\nYour balance is %lu",amount);

}

break;

case 3:

printf("\nEnter amount to deposit");

scanf("%lu",&deposit);

amount=amount+deposit;

printf("Your balance is %lu",amount);

break;

case 4:

printf("\nThank you for using ATM");

break;

default:

printf("\nInvalid Choice");

}

printf("\n\n\nDo you want another transaction?(y/n): ");

fflush(stdin);

scanf("%c",&another);

if(another=='n'||another=='N')

k=1;

}while(!k);

printf("\n\nHave a nice day");

getch();

}

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

Page 11: 70893566 C Language Programs

October 29, 2011 [C LANGUAGE PROGRAMS BY MR.K-MANIKANTA] For More @ www.stylewap.in

Manikanta Page 11 M.C.A

#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;

}

Count no. of students above,below and average students

#include<stdio.h>

#include<conio.h>

void main()

{

int a[10];

int aa=0,ba=0,ae=0,i;

clrscr();

printf("Enter the marks:\n");

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

{

scanf("%d",&a[i]);

if(a[i]>55)

aa++;

else if(a[i]<55)

ba++;

else

ae++;

Page 12: 70893566 C Language Programs

October 29, 2011 [C LANGUAGE PROGRAMS BY MR.K-MANIKANTA] For More @ www.stylewap.in

Manikanta Page 12 M.C.A

}

printf("No. OF AVG STUDENTS ARE:%d\n",ae);

printf("No. OF ABOVE AVERAGE STUDENTS:%d\n",aa);

printf("No. OF BELOW AVERAGE STUDENTS ARE:%d",ba);

getch();

}

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;

}

Bubble sort - linked list

Page 13: 70893566 C Language Programs

October 29, 2011 [C LANGUAGE PROGRAMS BY MR.K-MANIKANTA] For More @ www.stylewap.in

Manikanta Page 13 M.C.A

#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;

Page 14: 70893566 C Language Programs

October 29, 2011 [C LANGUAGE PROGRAMS BY MR.K-MANIKANTA] For More @ www.stylewap.in

Manikanta Page 14 M.C.A

} 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)

{

Page 15: 70893566 C Language Programs

October 29, 2011 [C LANGUAGE PROGRAMS BY MR.K-MANIKANTA] For More @ www.stylewap.in

Manikanta Page 15 M.C.A

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

#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");

Page 16: 70893566 C Language Programs

October 29, 2011 [C LANGUAGE PROGRAMS BY MR.K-MANIKANTA] For More @ www.stylewap.in

Manikanta Page 16 M.C.A

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 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)

Page 17: 70893566 C Language Programs

October 29, 2011 [C LANGUAGE PROGRAMS BY MR.K-MANIKANTA] For More @ www.stylewap.in

Manikanta Page 17 M.C.A

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;

}

C Program find Positive Negative with Switch Case Without

Conditional Operator

#include<stdio.h>

#include<conio.h>

void main(void)

{

char num;

clrscr();

printf("Enter a number +ve or -ve : ");

scanf("%c",&num);

switch(num)

{

case '-':

printf("Negative number");

Page 18: 70893566 C Language Programs

October 29, 2011 [C LANGUAGE PROGRAMS BY MR.K-MANIKANTA] For More @ www.stylewap.in

Manikanta Page 18 M.C.A

break;

default:

printf("Positive number");

}

getch();

}

C Program to calcuate interest and total amount at the end of

each year

Write a

c program

calculate interest and total amount at da end of each year

Note: Output is not in the form of table and rate is taken as

2%. It calculates amount of each year

#include <stdio.h>

#include <conio.h>

void main()

{

int t=1;

int r=2;

int y;

int y1=0;

long int p,a;

float i1;

double total;;

clrscr();

printf("enter starting amount&year");

scanf("%ld""%d",&p,&y);

while(y1<2009) i1="(p*r*t)/100;" total="i1+a+p;" p="p+a;"

style="color: rgb(255, 0, 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*/

Page 19: 70893566 C Language Programs

October 29, 2011 [C LANGUAGE PROGRAMS BY MR.K-MANIKANTA] For More @ www.stylewap.in

Manikanta Page 19 M.C.A

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;

}

Concatenate Two Strings

#include<stdio.h>

#include<conio.h>

#include<string.h>

void main()

{

char c[100];

char a[50];

char b[50];

clrscr();

printf("Enter a string1:");

gets(a);

printf("Enter a string2:");

gets(b);

strcat( a,b);

printf("%s",a);

getch();

}

Count no. of students above,below and average students

#include<stdio.h>

#include<conio.h>

void main()

{

int a[10];

int aa=0,ba=0,ae=0,i;

clrscr();

printf("Enter the marks:\n");

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

{

scanf("%d",&a[i]);

if(a[i]>55)

aa++;

else if(a[i]<55)

ba++;

else

ae++;

Page 20: 70893566 C Language Programs

October 29, 2011 [C LANGUAGE PROGRAMS BY MR.K-MANIKANTA] For More @ www.stylewap.in

Manikanta Page 20 M.C.A

}

printf("No. OF AVG STUDENTS ARE:%d\n",ae);

printf("No. OF ABOVE AVERAGE STUDENTS:%d\n",aa);

printf("No. OF BELOW AVERAGE STUDENTS ARE:%d",ba);

getch();

}

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);

}

count the array elements

#include<conio.h>

Page 21: 70893566 C Language Programs

October 29, 2011 [C LANGUAGE PROGRAMS BY MR.K-MANIKANTA] For More @ www.stylewap.in

Manikanta Page 21 M.C.A

#include<stdio.h>

void main()

{

int a[10],i,c=0;

clrscr();

printf("enter array elements=");

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

{

scanf("%d",&a[i]);

}

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

{

c++;

}

printf("counting elements of an array is:%d",c);

getch();

}

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);

Page 22: 70893566 C Language Programs

October 29, 2011 [C LANGUAGE PROGRAMS BY MR.K-MANIKANTA] For More @ www.stylewap.in

Manikanta Page 22 M.C.A

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);

}

Example of Using Strings in C

# include<stdio.h>

# include<conio.h>

# include<string.h>

void main()

{

char *a;

printf("Enter your name=");

gets(a);

printf("%s",a);

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? ");

Page 23: 70893566 C Language Programs

October 29, 2011 [C LANGUAGE PROGRAMS BY MR.K-MANIKANTA] For More @ www.stylewap.in

Manikanta Page 23 M.C.A

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);

}

Factorial of Number Using While Loop

#nclude <stdio.h>

#include <conio.h>

void main()

{

int i=1,n,fact=1;

clrscr();

printf("enter a number");

scanf("%d",&n);

while(i<=n)

{

fact=fact*i;

printf("%d",i);

i++;

}

printf("fact=%d",fact);

getch();

}

Factorial off a number using "do while" loop

#include<stdio.h>

#include<conio.h>

void main()

{

int n,f=1;

Page 24: 70893566 C Language Programs

October 29, 2011 [C LANGUAGE PROGRAMS BY MR.K-MANIKANTA] For More @ www.stylewap.in

Manikanta Page 24 M.C.A

clrscr();

printf("enter any number=");

scanf("%d",&n);

do

{

f=f*n;

n--;

}

while(n>0);

printf("factorial number is=%d",f);

getch();

}

find a given number is positive or negative number in C without

if statement, relational and conditional operator

# include <math.h>

# include <stdio.h>

# include <conio.h>

void main()

{

clrscr();

int p,n;

printf("Enter two no=");

scanf("%d",&amp;p);

switch(n=abs(p) +(-p))

{

case 0:

{

printf("Positiv e number");

break;

}

default:

{

printf("Negativ e number");

break;

}

}

getch();

}

Find address of char, string, integer

#include<stdio.h>

#include<conio.h>

main()

{

char *chp,*sp;

int i;

char ch,s[10];

int *ip;

Page 25: 70893566 C Language Programs

October 29, 2011 [C LANGUAGE PROGRAMS BY MR.K-MANIKANTA] For More @ www.stylewap.in

Manikanta Page 25 M.C.A

clrscr();

printf("Enter a char:");

scanf("%c",ch);

printf("Enter a string:");

scanf("%s",s);

printf("Enter a integer:");

scanf("%d",&i);

chp=&ch;

sp=s;

ip=&i;

printf("\nchar\tadd\tstring\t\tstringadd\tint\tint add\n");

printf("%c\t%u\t%s\t\t%u\t\t%d\t%u",ch,&chp,s,&s,i,&i);

printf("\nchar pointer value is:%u",chp);

printf("\nstring pointer value is:%u",sp);

printf("\nint pointer value is:%u",ip);

getch();

}

Find Inverse of a Given Matrix

#include<stdio.h>

#include<conio.h>

#include<stdlib.h>

#include<math.h>

//Read Matrix

void read_mat(float a[][10],int n)

{

int i,j;

printf("\n\nEnter %d X %d matrix below:\n",n,n);

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

for(j=0;j<n;j++)

scanf("%f",&a[i][j]);

}

//Write Matrix

void write_mat(float a[][10],int n)

{

int i,j;

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

{

for(j=0;j<n;j++)

printf("%10.2f",a[i][j]);

printf("\n");

}

}

Page 26: 70893566 C Language Programs

October 29, 2011 [C LANGUAGE PROGRAMS BY MR.K-MANIKANTA] For More @ www.stylewap.in

Manikanta Page 26 M.C.A

//Swap Rows

void swap_rows(float a[][10],int n,int i,int j)

{

int k,temp;

for(k=0;k<n;k++)

{

temp=a[i][k];

a[i][k]=a[j][k];

a[j][k]=temp;

}

}

//Multiplication of rows

void row_mult(float a[][10],int n,int i,float x)

{

int k;

for(k=0;k<n;k++)

a[i][k]*=x;

}

// Subtraction of rows

void row_sub(float a[][10],int n,int i,int j,float x)

{

int k;

for(k=0;k<n;k++)

a[j][k]-=x*a[i][k];

}

//Inverse of matrix

void inverse(float a[][10],float ia[][10],int n)

{

int i,j;

for(i=0;i1.0e-6)

break;

if(j==n)

{

printf("Inverse does not exist");

getch();

exit(0);

}

swap_rows(a,n,i,j);

swap_rows(ia,n,i,j);

}

row_mult(ia,n,i,1/a[i][i]);

row_mult(a,n,i,1/a[i][i]);

for(j=0;j<n;j++)

if(i!=j)

Page 27: 70893566 C Language Programs

October 29, 2011 [C LANGUAGE PROGRAMS BY MR.K-MANIKANTA] For More @ www.stylewap.in

Manikanta Page 27 M.C.A

{

row_sub(ia,n,i,j,a[j][i]);

row_sub(a,n,i,j,a[j][i]);

}

}

}

//main function

void main()

{

float a[10][10],b[10][10];

int n;

clrscr();

//Accept Matrix

printf("\n\nEnter order of the square matrix \n");

scanf("%d",&n);

read_mat(a,n);

//inverse the matrix

inverse(a,b,n);

printf("\n \nInverse of the given square matrix is : \n");

write_mat(b,n);

getch();

}

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;

Page 28: 70893566 C Language Programs

October 29, 2011 [C LANGUAGE PROGRAMS BY MR.K-MANIKANTA] For More @ www.stylewap.in

Manikanta Page 28 M.C.A

d3=0;

}

sum=d1+d2+d3;

printf("\n sum of 3 digit no is %d",sum);

getch();

}

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;

Page 29: 70893566 C Language Programs

October 29, 2011 [C LANGUAGE PROGRAMS BY MR.K-MANIKANTA] For More @ www.stylewap.in

Manikanta Page 29 M.C.A

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);

}

}

Program for demonstration of Tree Operations - INSERTION,

INORDER .

Page 30: 70893566 C Language Programs

October 29, 2011 [C LANGUAGE PROGRAMS BY MR.K-MANIKANTA] For More @ www.stylewap.in

Manikanta Page 30 M.C.A

#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();

}

Page 31: 70893566 C Language Programs

October 29, 2011 [C LANGUAGE PROGRAMS BY MR.K-MANIKANTA] For More @ www.stylewap.in

Manikanta Page 31 M.C.A

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;

Page 32: 70893566 C Language Programs

October 29, 2011 [C LANGUAGE PROGRAMS BY MR.K-MANIKANTA] For More @ www.stylewap.in

Manikanta Page 32 M.C.A

}

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;

}

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 *);

Page 33: 70893566 C Language Programs

October 29, 2011 [C LANGUAGE PROGRAMS BY MR.K-MANIKANTA] For More @ www.stylewap.in

Manikanta Page 33 M.C.A

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)

{

Page 34: 70893566 C Language Programs

October 29, 2011 [C LANGUAGE PROGRAMS BY MR.K-MANIKANTA] For More @ www.stylewap.in

Manikanta Page 34 M.C.A

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)

Page 35: 70893566 C Language Programs

October 29, 2011 [C LANGUAGE PROGRAMS BY MR.K-MANIKANTA] For More @ www.stylewap.in

Manikanta Page 35 M.C.A

{

if(p!=NULL)

{

postorder(p->left);

postorder(p->right);

printf("

Data :%d",p->data);

}

else

return;

}

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)

{

Page 36: 70893566 C Language Programs

October 29, 2011 [C LANGUAGE PROGRAMS BY MR.K-MANIKANTA] For More @ www.stylewap.in

Manikanta Page 36 M.C.A

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;

}

}

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 */

Page 37: 70893566 C Language Programs

October 29, 2011 [C LANGUAGE PROGRAMS BY MR.K-MANIKANTA] For More @ www.stylewap.in

Manikanta Page 37 M.C.A

/* 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);

}

Find address of char, string, integer

#include<stdio.h>

#include<conio.h>

main()

{

char *chp,*sp;

int i;

char ch,s[10];

int *ip;

clrscr();

printf("Enter a char:");

scanf("%c",ch);

printf("Enter a string:");

scanf("%s",s);

printf("Enter a integer:");

scanf("%d",&i);

Page 38: 70893566 C Language Programs

October 29, 2011 [C LANGUAGE PROGRAMS BY MR.K-MANIKANTA] For More @ www.stylewap.in

Manikanta Page 38 M.C.A

chp=&ch;

sp=s;

ip=&i;

printf("\nchar\tadd\tstring\t\tstringadd\tint\tint add\n");

printf("%c\t%u\t%s\t\t%u\t\t%d\t%u",ch,&chp,s,&s,i,&i);

printf("\nchar pointer value is:%u",chp);

printf("\nstring pointer value is:%u",sp);

printf("\nint pointer value is:%u",ip);

getch();

}

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;

Page 39: 70893566 C Language Programs

October 29, 2011 [C LANGUAGE PROGRAMS BY MR.K-MANIKANTA] For More @ www.stylewap.in

Manikanta Page 39 M.C.A

float sm;

for(s = 0; s < n - 1; s++)

{

w = fm(arr, s, n);

sm = arr[w];

arr[w] = arr[s];

arr[s] = sm;

}

}

UPPER, LOWER AND REVERSE

void main ()

{

char str [20];

clrscr ();

printf ("Enter your name: ");

gets (str);

printf("\nLength is : %d",strlen(str));

printf("\nUpper is : %s",strupr(str));

printf("\nLower is : %s",strlwr(str));

printf("\nReverese is : %s",strrev(str));

getch ();

}

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");

Page 40: 70893566 C Language Programs

October 29, 2011 [C LANGUAGE PROGRAMS BY MR.K-MANIKANTA] For More @ www.stylewap.in

Manikanta Page 40 M.C.A

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:

Page 41: 70893566 C Language Programs

October 29, 2011 [C LANGUAGE PROGRAMS BY MR.K-MANIKANTA] For More @ www.stylewap.in

Manikanta Page 41 M.C.A

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;

Page 42: 70893566 C Language Programs

October 29, 2011 [C LANGUAGE PROGRAMS BY MR.K-MANIKANTA] For More @ www.stylewap.in

Manikanta Page 42 M.C.A

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)

Page 43: 70893566 C Language Programs

October 29, 2011 [C LANGUAGE PROGRAMS BY MR.K-MANIKANTA] For More @ www.stylewap.in

Manikanta Page 43 M.C.A

{

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();

Page 44: 70893566 C Language Programs

October 29, 2011 [C LANGUAGE PROGRAMS BY MR.K-MANIKANTA] For More @ www.stylewap.in

Manikanta Page 44 M.C.A

}

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;

Page 45: 70893566 C Language Programs

October 29, 2011 [C LANGUAGE PROGRAMS BY MR.K-MANIKANTA] For More @ www.stylewap.in

Manikanta Page 45 M.C.A

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)

Page 46: 70893566 C Language Programs

October 29, 2011 [C LANGUAGE PROGRAMS BY MR.K-MANIKANTA] For More @ www.stylewap.in

Manikanta Page 46 M.C.A

{

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);

}

}

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);

Page 47: 70893566 C Language Programs

October 29, 2011 [C LANGUAGE PROGRAMS BY MR.K-MANIKANTA] For More @ www.stylewap.in

Manikanta Page 47 M.C.A

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)

{

Page 48: 70893566 C Language Programs

October 29, 2011 [C LANGUAGE PROGRAMS BY MR.K-MANIKANTA] For More @ www.stylewap.in

Manikanta Page 48 M.C.A

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));

Page 49: 70893566 C Language Programs

October 29, 2011 [C LANGUAGE PROGRAMS BY MR.K-MANIKANTA] For More @ www.stylewap.in

Manikanta Page 49 M.C.A

}

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;

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);

Page 50: 70893566 C Language Programs

October 29, 2011 [C LANGUAGE PROGRAMS BY MR.K-MANIKANTA] For More @ www.stylewap.in

Manikanta Page 50 M.C.A

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;

Page 51: 70893566 C Language Programs

October 29, 2011 [C LANGUAGE PROGRAMS BY MR.K-MANIKANTA] For More @ www.stylewap.in

Manikanta Page 51 M.C.A

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;

}

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++)

Page 52: 70893566 C Language Programs

October 29, 2011 [C LANGUAGE PROGRAMS BY MR.K-MANIKANTA] For More @ www.stylewap.in

Manikanta Page 52 M.C.A

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])

Page 53: 70893566 C Language Programs

October 29, 2011 [C LANGUAGE PROGRAMS BY MR.K-MANIKANTA] For More @ www.stylewap.in

Manikanta Page 53 M.C.A

a[i + low] = working[merge2++];

else

a[i + low] = working[merge1++];

else

a[i + low] = working[merge2++];

else

a[i + low] = working[merge1++];

}

}

Multiplication of Two Matrices

//Multiplication of Matrix

#include <stdio.h>

#include <conio.h>

int m1,n1,m2,n2,i,j,k,z[10][10]={0};

void value_sub(int a,int b,int arr[][10] )

{

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

{

for(j=0;j<b;j++)

{

printf(―Mat[%d%d] = ―,i+1,j+1);

scanf(―%d‖,&arr[i][j]);

fflush(stdin);

}

printf(―‖);

}

}

void mat_mul(int a,int b,int arr[][10],int brr[][10])

{

int k=0;

Page 54: 70893566 C Language Programs

October 29, 2011 [C LANGUAGE PROGRAMS BY MR.K-MANIKANTA] For More @ www.stylewap.in

Manikanta Page 54 M.C.A

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

{

for(j=0;j<b;j++)

{

for(k=0;k<a;k++)

z[i][j]+=arr[i][k]*brr[k][j];

printf(―%d\t‖,z[i][j]);

}

printf(―\n\n‖);

}

}

int main()

{

int A[10][10]={0},B[10][10]={0};

printf(―Enter the column and row of first matrix(m x n)\n‖);

scanf(―%d%d‖,&m1,&n1);

printf(―Enter the column and row of second matrix(m x n)\n‖);

scanf(―%d%d‖,&m2,&n2);

printf(―\n\n‖);

if (n1==m2)

{

value_sub(m1,n1,A);

printf(―\n\n‖);

value_sub(m2,n2,B);

Page 55: 70893566 C Language Programs

October 29, 2011 [C LANGUAGE PROGRAMS BY MR.K-MANIKANTA] For More @ www.stylewap.in

Manikanta Page 55 M.C.A

printf(―\n\n‖);

mat_mul(m1,n2,A,B);

}

else

printf(―Matrix multiplication cannot be done‖);

getch();

}

SUM,SUB,PRODUCT,DIVISION

#include

void main ()

{

int a,b,c,d,e,f;

clrscr();

printf ("Enter A: ");

scanf ("%d",&a);

printf ("Enter B: ");

scanf ("%d",&b);

c=a+b;

d=a-b;

e=a*b;

f=a/b;

printf ("\nSum is : %d",c);

printf ("\nSubtraction is : %d",d);

printf ("\nMultiplication is : %d",e);

printf ("\nDivision is : %d",f);

getch ();

}

Output

Page 56: 70893566 C Language Programs

October 29, 2011 [C LANGUAGE PROGRAMS BY MR.K-MANIKANTA] For More @ www.stylewap.in

Manikanta Page 56 M.C.A

Method #2

WAP TO SUM, SUBTRACT, MULTIPLY & DIVISION OF TWO NUMBERS (3

VARIABLES)

#include

void main ()

{

int a,b,c;

clrscr();

printf ("Enter A: ");

scanf ("%d",&a);

printf ("Enter B: ");

scanf ("%d",&b);

c=a+b;

printf ("\nSum is %d",c);

c=a-b;

printf ("\nSubtraction is %d",c);

c=a*b;

printf ("\nMultiplication is %d",c);

c=a/b;

printf ("\nDivision is %d",c);

getch ();

}

Output

Multiply and swap 2 nmbers using bitwise operators

#include<stdio.h>

#include<conio.h>

void main(void)

{

int a,b;

clrscr();

printf("Input value of A: ");

scanf("%d",&a);

Page 57: 70893566 C Language Programs

October 29, 2011 [C LANGUAGE PROGRAMS BY MR.K-MANIKANTA] For More @ www.stylewap.in

Manikanta Page 57 M.C.A

printf("Input value of B: ");

scanf("%d",&b);

a=a^b;

b=b^a;

a=b^a;

printf("After Swapping:\n");

printf("Value of A: %d",a);

printf("\nValue of B: %d",b);

getch();

}

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");

Page 58: 70893566 C Language Programs

October 29, 2011 [C LANGUAGE PROGRAMS BY MR.K-MANIKANTA] For More @ www.stylewap.in

Manikanta Page 58 M.C.A

scanf("%f",¤t);

result = voltage / current;

printf("The resistance is %0.2f ohms.\n",result);

break;

default :

exit(0);

break;

}

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();

}

Print Armstrong numbers Less Than 1000

#include<stdio.h>

#include<conio.h>

void main()

{

int q,a,b,c,z,x,n=1;

Page 59: 70893566 C Language Programs

October 29, 2011 [C LANGUAGE PROGRAMS BY MR.K-MANIKANTA] For More @ www.stylewap.in

Manikanta Page 59 M.C.A

clrscr();

printf(" OUTPUT :");

while(n<1000)

{

a=n%10;

q=n/10;

b=q%10;

c=q/10;

z=((c*100)+(b*10)+a);

x=(a*a*a)+(b*b*b)+(c*c*c);

if(x==z)

printf("\n\t%d",n);

n++;

}

getch();

}

Print Second Largest Among Given Three No.s

#include<stdio.h>

#include<conio.h>

void main()

{

int a,b,c;

clrscr();

printf(" OUTPUT :\n");

printf("Enter any three no.s:");

scanf("%d%d%d",&a,&b,&c);

if(a>b&&a>c)

if(b>c)

printf("%d is the second largest no.",b);

else

printf("%d is the second largest no.",c);

if(b>a&&b>c)

if(a>c)

printf("the second largest no. is % d",a);

else

printf(" the second largest no. is %d",c);

if(c>a&&c>b)

Page 60: 70893566 C Language Programs

October 29, 2011 [C LANGUAGE PROGRAMS BY MR.K-MANIKANTA] For More @ www.stylewap.in

Manikanta Page 60 M.C.A

if(b>a)

printf("second largest no. is %d",b);

else

printf("second largest no. is %d",a);

getch();

}

Print Second Largest Among Given Three No.S

#include<stdio.h>

#include<conio.h>

void main()

{

int a,b,c;

clrscr();

printf(" OUTPUT :\n");

printf("Enter any three no.s:");

scanf("%d%d%d",&a,&b,&c);

if(a>b&&a>c)

if(b>c)

printf("%d is the second largest no.",b);

else

printf("%d is the second largest no.",c);

if(b>a&&b>c)

if(a>c)

printf("the second largest no. is % d",a);

else

printf(" the second largest no. is %d",c);

if(c>a&&c>b)

if(b>a)

printf("second largest no. is %d",b);

else

printf("second largest no. is %d",a);

Page 61: 70893566 C Language Programs

October 29, 2011 [C LANGUAGE PROGRAMS BY MR.K-MANIKANTA] For More @ www.stylewap.in

Manikanta Page 61 M.C.A

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

Page 62: 70893566 C Language Programs

October 29, 2011 [C LANGUAGE PROGRAMS BY MR.K-MANIKANTA] For More @ www.stylewap.in

Manikanta Page 62 M.C.A

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;

}

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;

Page 63: 70893566 C Language Programs

October 29, 2011 [C LANGUAGE PROGRAMS BY MR.K-MANIKANTA] For More @ www.stylewap.in

Manikanta Page 63 M.C.A

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");

Page 64: 70893566 C Language Programs

October 29, 2011 [C LANGUAGE PROGRAMS BY MR.K-MANIKANTA] For More @ www.stylewap.in

Manikanta Page 64 M.C.A

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();

}

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.");

Page 65: 70893566 C Language Programs

October 29, 2011 [C LANGUAGE PROGRAMS BY MR.K-MANIKANTA] For More @ www.stylewap.in

Manikanta Page 65 M.C.A

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.");

Page 66: 70893566 C Language Programs

October 29, 2011 [C LANGUAGE PROGRAMS BY MR.K-MANIKANTA] For More @ www.stylewap.in

Manikanta Page 66 M.C.A

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);

}

Page 67: 70893566 C Language Programs

October 29, 2011 [C LANGUAGE PROGRAMS BY MR.K-MANIKANTA] For More @ www.stylewap.in

Manikanta Page 67 M.C.A

else

return;

}

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();

Page 68: 70893566 C Language Programs

October 29, 2011 [C LANGUAGE PROGRAMS BY MR.K-MANIKANTA] For More @ www.stylewap.in

Manikanta Page 68 M.C.A

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);

Page 69: 70893566 C Language Programs

October 29, 2011 [C LANGUAGE PROGRAMS BY MR.K-MANIKANTA] For More @ www.stylewap.in

Manikanta Page 69 M.C.A

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;

}

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");

Page 70: 70893566 C Language Programs

October 29, 2011 [C LANGUAGE PROGRAMS BY MR.K-MANIKANTA] For More @ www.stylewap.in

Manikanta Page 70 M.C.A

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++;

}

Page 71: 70893566 C Language Programs

October 29, 2011 [C LANGUAGE PROGRAMS BY MR.K-MANIKANTA] For More @ www.stylewap.in

Manikanta Page 71 M.C.A

}

} //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();

}

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);

Page 72: 70893566 C Language Programs

October 29, 2011 [C LANGUAGE PROGRAMS BY MR.K-MANIKANTA] For More @ www.stylewap.in

Manikanta Page 72 M.C.A

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;

}

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();

}

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);

Page 73: 70893566 C Language Programs

October 29, 2011 [C LANGUAGE PROGRAMS BY MR.K-MANIKANTA] For More @ www.stylewap.in

Manikanta Page 73 M.C.A

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 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

Page 74: 70893566 C Language Programs

October 29, 2011 [C LANGUAGE PROGRAMS BY MR.K-MANIKANTA] For More @ www.stylewap.in

Manikanta Page 74 M.C.A

{

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);

}

Program to construct a pyramid of any input numbers

Page 75: 70893566 C Language Programs

October 29, 2011 [C LANGUAGE PROGRAMS BY MR.K-MANIKANTA] For More @ www.stylewap.in

Manikanta Page 75 M.C.A

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();

}

Greatest of two numbers using relational conditional operators

#include<stdio.h>

#include<conio.h>

#include<math.h>

void main(void)

{

Page 76: 70893566 C Language Programs

October 29, 2011 [C LANGUAGE PROGRAMS BY MR.K-MANIKANTA] For More @ www.stylewap.in

Manikanta Page 76 M.C.A

int a,b;

clrscr();

printf("Enter a number: ");

scanf("%d",&a);

printf("Enter another number: ");

scanf("%d",&b);

printf("The greatest number is %d",(a+b+abs(a-b))/2);

getch();

}

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;

}

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;

}

Pyramid using nested for loops

#include<stdio.h>

#include<conio.h>

void main()

{

Page 77: 70893566 C Language Programs

October 29, 2011 [C LANGUAGE PROGRAMS BY MR.K-MANIKANTA] For More @ www.stylewap.in

Manikanta Page 77 M.C.A

int i,j;

for(i=5;i>=1;i--)

{

for(j=i;j>=1;j--)

{

printf("%d",j);

}

printf("\n");

}

}

OUTPUT

5 4 3 2 1

4 3 2 1

3 2 1

2 1

1

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: ");

Page 78: 70893566 C Language Programs

October 29, 2011 [C LANGUAGE PROGRAMS BY MR.K-MANIKANTA] For More @ www.stylewap.in

Manikanta Page 78 M.C.A

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"

Page 79: 70893566 C Language Programs

October 29, 2011 [C LANGUAGE PROGRAMS BY MR.K-MANIKANTA] For More @ www.stylewap.in

Manikanta Page 79 M.C.A

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);

}

Page 80: 70893566 C Language Programs

October 29, 2011 [C LANGUAGE PROGRAMS BY MR.K-MANIKANTA] For More @ www.stylewap.in

Manikanta Page 80 M.C.A

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++)

{

Page 81: 70893566 C Language Programs

October 29, 2011 [C LANGUAGE PROGRAMS BY MR.K-MANIKANTA] For More @ www.stylewap.in

Manikanta Page 81 M.C.A

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);

}

Print all permutations of a given string

# include <stdio.h>

# include <conio.h>

/* Function to swap values at two pointers */

void swap (char *x, char *y)

{

char temp;

temp = *x;

*x = *y;

*y = temp;

}

/* Function to print permutations of string

This function takes three parameters:

1. String

2. Starting index of the string

3. Ending index of the string. */

void permute(char *a, int i, int n)

{

int j;

if (i == n)

printf("%s\n", a);

else

Page 82: 70893566 C Language Programs

October 29, 2011 [C LANGUAGE PROGRAMS BY MR.K-MANIKANTA] For More @ www.stylewap.in

Manikanta Page 82 M.C.A

{

for (j = i; j <= n; j++)

{

swap((a+i), (a+j));

permute(a, i+1, n);

swap((a+i), (a+j)); //backtrack

}

}

}

/* Driver program to test above functions */

int main()

{

char a[] = "ABC";

permute(a, 0, 2);

getchar();

return 0;

}

Reverse words in a String C Program

#include

void reverse(char *begin, char *end);

void reverseWords(char *s)

{

char *word_begin = s;

char *temp = s;

while( *temp )

{

temp++;

if (*temp == '\0')

{

reverse(word_begin, temp-1);

}

else if(*temp == ' ')

{

reverse(word_begin, temp-1);

word_begin = temp+1;

}

}

reverse(s, temp-1);

}

void reverse(char *begin, char *end)

{

char temp;

while (begin < end)

{

Page 83: 70893566 C Language Programs

October 29, 2011 [C LANGUAGE PROGRAMS BY MR.K-MANIKANTA] For More @ www.stylewap.in

Manikanta Page 83 M.C.A

temp = *begin;

*begin++ = *end;

*end-- = temp;

}

}

int main( )

{

char s[] = "i like this program very much";

char *temp = s;

reverseWords(s);

printf("%s", s);

getchar();

return 0;

}

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:");

Page 84: 70893566 C Language Programs

October 29, 2011 [C LANGUAGE PROGRAMS BY MR.K-MANIKANTA] For More @ www.stylewap.in

Manikanta Page 84 M.C.A

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");

}

Search an array

#include

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

int search_arr(int myArray[], int elements, int number);

Page 85: 70893566 C Language Programs

October 29, 2011 [C LANGUAGE PROGRAMS BY MR.K-MANIKANTA] For More @ www.stylewap.in

Manikanta Page 85 M.C.A

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);

}

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);

Page 86: 70893566 C Language Programs

October 29, 2011 [C LANGUAGE PROGRAMS BY MR.K-MANIKANTA] For More @ www.stylewap.in

Manikanta Page 86 M.C.A

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;

}

Page 87: 70893566 C Language Programs

October 29, 2011 [C LANGUAGE PROGRAMS BY MR.K-MANIKANTA] For More @ www.stylewap.in

Manikanta Page 87 M.C.A

return n;

}

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();

}

Page 88: 70893566 C Language Programs

October 29, 2011 [C LANGUAGE PROGRAMS BY MR.K-MANIKANTA] For More @ www.stylewap.in

Manikanta Page 88 M.C.A

}

}

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;

}

}

Selection sort linked list

Page 89: 70893566 C Language Programs

October 29, 2011 [C LANGUAGE PROGRAMS BY MR.K-MANIKANTA] For More @ www.stylewap.in

Manikanta Page 89 M.C.A

#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 */

Page 90: 70893566 C Language Programs

October 29, 2011 [C LANGUAGE PROGRAMS BY MR.K-MANIKANTA] For More @ www.stylewap.in

Manikanta Page 90 M.C.A

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)

{

Page 91: 70893566 C Language Programs

October 29, 2011 [C LANGUAGE PROGRAMS BY MR.K-MANIKANTA] For More @ www.stylewap.in

Manikanta Page 91 M.C.A

/* 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;

Page 92: 70893566 C Language Programs

October 29, 2011 [C LANGUAGE PROGRAMS BY MR.K-MANIKANTA] For More @ www.stylewap.in

Manikanta Page 92 M.C.A

b = tmp;

d = b;

b = b->next;

}

}

} else

{

d = b;

b = b->next;

}

}

c = a;

a = a->next;

}

}

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]);

}

Page 93: 70893566 C Language Programs

October 29, 2011 [C LANGUAGE PROGRAMS BY MR.K-MANIKANTA] For More @ www.stylewap.in

Manikanta Page 93 M.C.A

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;

}

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);

Page 94: 70893566 C Language Programs

October 29, 2011 [C LANGUAGE PROGRAMS BY MR.K-MANIKANTA] For More @ www.stylewap.in

Manikanta Page 94 M.C.A

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();

}

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++)

Page 95: 70893566 C Language Programs

October 29, 2011 [C LANGUAGE PROGRAMS BY MR.K-MANIKANTA] For More @ www.stylewap.in

Manikanta Page 95 M.C.A

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);

}

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: ");

Page 96: 70893566 C Language Programs

October 29, 2011 [C LANGUAGE PROGRAMS BY MR.K-MANIKANTA] For More @ www.stylewap.in

Manikanta Page 96 M.C.A

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;

}

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;

Page 97: 70893566 C Language Programs

October 29, 2011 [C LANGUAGE PROGRAMS BY MR.K-MANIKANTA] For More @ www.stylewap.in

Manikanta Page 97 M.C.A

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);

}

SUM,SUB,PRODUCT,DIVISION

#include

void main ()

{

int a,b,c,d,e,f;

clrscr();

printf ("Enter A: ");

scanf ("%d",&a);

printf ("Enter B: ");

scanf ("%d",&b);

c=a+b;

d=a-b;

e=a*b;

f=a/b;

printf ("\nSum is : %d",c);

printf ("\nSubtraction is : %d",d);

printf ("\nMultiplication is : %d",e);

printf ("\nDivision is : %d",f);

getch ();

}

Output

Page 98: 70893566 C Language Programs

October 29, 2011 [C LANGUAGE PROGRAMS BY MR.K-MANIKANTA] For More @ www.stylewap.in

Manikanta Page 98 M.C.A

Method #2

WAP TO SUM, SUBTRACT, MULTIPLY & DIVISION OF TWO NUMBERS (3

VARIABLES)

#include

void main ()

{

int a,b,c;

clrscr();

printf ("Enter A: ");

scanf ("%d",&a);

printf ("Enter B: ");

scanf ("%d",&b);

c=a+b;

printf ("\nSum is %d",c);

c=a-b;

printf ("\nSubtraction is %d",c);

c=a*b;

printf ("\nMultiplication is %d",c);

c=a/b;

printf ("\nDivision is %d",c);

getch ();

}

Output

Page 99: 70893566 C Language Programs

October 29, 2011 [C LANGUAGE PROGRAMS BY MR.K-MANIKANTA] For More @ www.stylewap.in

Manikanta Page 99 M.C.A

Subtraction of Two Matrices

#include<stdio.h>

#include<conio.h>

//Read Matrix

void read_mat(float a[][10],int m,int n)

{

int i,j;

printf("\n\nEnter %d X %d matrix below:\n",m,n);

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

for(j=0;j<n;j++)

scanf("%f",&a[i][j]);

}

//Write Matrix

void write_mat(float a[][10],int m,int n)

{

int i,j;

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

{

for(j=0;j<n;j++)

printf("%10.2f",a[i][j]);

printf("\n");

}

}

//Subtract matrices

void sub_mat(float a[][10],float b[][10],float c[][10],int m,int

n)

{

int i,j;

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

for(j=0;j<n;j++)

c[i][j]=a[i][j] - b[i][j];

}

Page 100: 70893566 C Language Programs

October 29, 2011 [C LANGUAGE PROGRAMS BY MR.K-MANIKANTA] For More @ www.stylewap.in

Manikanta Page 100 M.C.A

//main function

void main()

{

float x[10][10],y[10][10],z[10][10];

int m,n;

clrscr();

//Accept two matrices

printf("\n\nEnter order of matrix \n");

scanf("%d %d",&m,&n);

read_mat(x,m,n);

read_mat(y,m,n);

//call sub_mat() function

sub_mat(x,y,z,m,n);

printf("\n\nSUBTRACTION OF THE GIVEN MATRICES IS:\n");

write_mat(z,m,n);

getch();

}

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;

}

To delete n Characters from a given position in a given string

Page 101: 70893566 C Language Programs

October 29, 2011 [C LANGUAGE PROGRAMS BY MR.K-MANIKANTA] For More @ www.stylewap.in

Manikanta Page 101 M.C.A

#include <stdio.h>

#include <conio.h>

#include <string.h>

void delchar(char *x,int a, int b);

void main()

{

char string[10];

int n,pos,p;

clrscr();

puts(―Enter the string‖);

gets(string);

printf(―Enter the position from where to delete‖);

scanf(―%d‖,&pos);

printf(―Enter the number of characters to be deleted‖);

scanf(―%d‖,&n);

delchar(string, n,pos);

getch();

}

// Function to delete n characters

void delchar(char *x,int a, int b)

{

if ((a+b-1) <= strlen(x))

{

strcpy(&x[b-1],&x[a+b-1]);

puts(x);

}

}

Total Number of Consonants in a String

#include<stdio.h>

#include<conio.h>

#include<string.h>

void main()

{

int c=0,i,l,p;

char a[10];

clrscr();

printf("program that gives total number of consonants in a

string");

printf("\n\n\n\t\t------------INPUT-------------");

printf("\n\nenter any string");//taking input from the user

scanf("%s",&a);

l=strlen(a);

for(i=0;i<l;i++)< div="">

{

if(a[i]=='a' || a[i]=='e' || a[i]=='o' || a[i]=='i' ||

a[i]=='u')

Page 102: 70893566 C Language Programs

October 29, 2011 [C LANGUAGE PROGRAMS BY MR.K-MANIKANTA] For More @ www.stylewap.in

Manikanta Page 102 M.C.A

c++;

}

p=l-c;

printf("\n\n\n\t\t------------OUTPUT------------");

printf("\n\nthe total no. of consonants in the string

are=%d",p);//printing output

getch();

}

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)

Page 103: 70893566 C Language Programs

October 29, 2011 [C LANGUAGE PROGRAMS BY MR.K-MANIKANTA] For More @ www.stylewap.in

Manikanta Page 103 M.C.A

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);

}

}

Use of Strlen() Function

#include<stdio.h>

#include<string.h>

void main(void)

{

char str[31];

int len;

printf("\nEnter any String");

gets(str);

len=strlen(str);

printf("\nNumber of Character in%s=%d\n",str,len);

}

INCREMENTAL / DECREMENTAL

WAP TO ADD 1 & SUBTRACT 1 FROM VALUE OF A & B

(INCREMENTAL & DECREMENTAL OPERATORS)

void main ()

{

int a,b;

clrscr();

printf ("Enter A: ");

scanf ("%d",&a);

printf ("Enter B: ");

scanf ("%d",&b);

a++;

b--;

printf ("\nA is %d",a);

printf ("\nB is %d",b);

getch ();

}

Page 104: 70893566 C Language Programs

October 29, 2011 [C LANGUAGE PROGRAMS BY MR.K-MANIKANTA] For More @ www.stylewap.in

Manikanta Page 104 M.C.A

ADD ENTERED DIGITS

void main ()

{

int no,r,res;

clrscr ();

printf ("Enter any value: ");

scanf ("%d",&no);

r=res=0;

while (no>0)

{

r=no%10;

no=no/10;

res=(res+r);

}

printf ("Sum is %d",res);

getch ();

}

ADD TWO VARIABLES

#include

void main ()

{

int a,b,c;

clrscr();

printf ("Enter A: ");

scanf ("%d",&a);

printf ("Enter B: ");

scanf ("%d",&b);

c=a+b;

printf ("\nSum is %d",c);

getch ();

}

Page 105: 70893566 C Language Programs

October 29, 2011 [C LANGUAGE PROGRAMS BY MR.K-MANIKANTA] For More @ www.stylewap.in

Manikanta Page 105 M.C.A

Output

CONCATENATE TWO STRINGS

void main ()

{

char *str,*str1;

clrscr ();

printf("Enter your name: ");

gets (str);

str1="jeet";

strcat(str,str1);

printf("\n %s",str);

getch ();

}

CELCIUS TO FAHRENHEIT

void main ()

{

float c,f;

clrscr ();

printf ("Enter the value of celcius: ");

scanf ("%f",&c);

f=(float) 9/5*c+32;

printf ("\nFahrenheit is %.2f",f);

getch ();

}

MATRIX 2 X 3

WAP TO CREATE DOUBLE DIMENSION ARRAY OF 2X3 MATRIX AND DISPLAY

ITS ELEMENTS

void main ()

{

int a[2][3],i,j;

clrscr ();

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

{

Page 106: 70893566 C Language Programs

October 29, 2011 [C LANGUAGE PROGRAMS BY MR.K-MANIKANTA] For More @ www.stylewap.in

Manikanta Page 106 M.C.A

for (j=0;j<3;j++)

{

printf ("\nEnter element: ");

scanf ("%d",&a[i][j]);

}

}

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

{

for (j=0;j<3;j++)

{

printf ("%d\t",a[i][j]);

}

printf ("\n");

}

getch ();

}

GETCH ( ) FUNCTION

#include

void main ()

{

char ch;

clrscr ();

printf ("Enter any character: ");

ch=getch();

printf ("You have pressed %c",ch);

getch ();

}

RECORDS ENTRY

WAP TO ENTER RECORDS AND ALSO REPEAT THE STEP IF USER WANTS TO

CONTINUE

void main ()

{

char nm [20],cls[10];

int rollno,m1,m2,m3,tm;

float per;

char ch;

clrscr ();

do

{

printf ("\nEnter Marks of Hindi: ");

scanf ("%d",&m1);

printf ("Enter Marks of Pbi : ");

scanf ("%d",&m2);

printf ("Enter Marks of Math : ");

scanf ("%d",&m3);

tm=m1+m2+m3;

per=(tm*100)/300;

Page 107: 70893566 C Language Programs

October 29, 2011 [C LANGUAGE PROGRAMS BY MR.K-MANIKANTA] For More @ www.stylewap.in

Manikanta Page 107 M.C.A

printf ("\nTotal Marks are %d",tm);

printf ("\nPercentage is %.2f",per);

printf ("\nDo you want to continue Y/N: ");

fflush(stdin);

scanf ("%c",&ch);

}

while (ch=='y' || ch=='Y');

getch ();

}

SWITCH CASE

WAP TO FIND AMOUNT OF GIVEN QUANTITY OF ANY COMPANY WITH 10%

DISCOUNT USING SWITCH CASE

void main()

{

int,ch qty;

long tb,dis,nb;

clrscr();

printf("1.BPL\n2.Onida\n3.Sony\n4.Samsung\n5.LG\n");

printf("\nEnter Your Choice: ");

fflush(stdin);

scanf("%d",&ch);

printf("Enter Qty: ");

scanf("%d",&qty);

switch(ch)

{

case 1:tb=(long)qty*10000;

printf("\nBPL is %ld",tb);

break;

case 2:tb=(long)qty*12000;

printf("\nOnida is %ld",tb);

break;

case 3:tb=(long)qty*11500;

Page 108: 70893566 C Language Programs

October 29, 2011 [C LANGUAGE PROGRAMS BY MR.K-MANIKANTA] For More @ www.stylewap.in

Manikanta Page 108 M.C.A

printf("\nSony is %ld ",tb);

break;

case 4:tb=(long)qty*11000;

printf("\nSamsung is %ld ",tb);

break;

case 5:tb=(long)qty*13000;

printf("\nLG is %ld ",tb);

break;

Default:

printf("Wrong Choice...");

}

dis=(tb*10)/100;

nb=tb-dis;

printf("\nDiscount is %ld",dis);

printf("

\nNet bill is %ld",nb);

getch();

}

GOOD ELSE BAD

void main ()

{

int a;

clrscr ();

printf ("Enter any Number: ");

scanf ("%d",&a);

if (a==10)

goto g;

else

goto b;

g:

printf ("Good");

goto end;

b:

printf ("Bad");

goto end;

end:

getch ();

}

BIGGER & EQUAL

WAP TO FIND OUT BIGGER & EQUAL NUMBER FROM THREE NUMBERS

(TERNARY OPERATORS)

void main ()

{

int a,b,c;

clrscr ();

printf ("Enter the value of A: ");

scanf("%d",&a);

Page 109: 70893566 C Language Programs

October 29, 2011 [C LANGUAGE PROGRAMS BY MR.K-MANIKANTA] For More @ www.stylewap.in

Manikanta Page 109 M.C.A

printf ("Enter the value of B: ");

scanf ("%d",&b);

printf ("Enter the value of C: ");

scanf ("%d",&c);

(a>b && a>c)?printf("A is Big"):(b>a && b>c)?printf("B is

Big"):(a==b && b==c)?printf("All are Eqaul"):printf("C is Big");

getch ();

}

BIGGER NUMBER

WAP TO FIND OUT BIGGER NUMBER FROM TWO NUMBERS

(TERNARY OPERATORS)

void main ()

{

int a,b;

clrscr ();

printf ("Enter the value of A: ");

scanf("%d",&a);

printf ("Enter the value of B: ");

scanf ("%d",&b);

(a>b)? printf ("A is Big"):printf("B is Big");

getch ();

}

EVEN OR ODD

void main ()

{

int a;

clrscr ();

printf ("Enter the value of A: ");

scanf("%d",&a);

if (a%2==0)

{

printf ("\nNumber is Even");

}

else

{

printf("\nNumber is Odd");

}

getch ();

}

Page 110: 70893566 C Language Programs

October 29, 2011 [C LANGUAGE PROGRAMS BY MR.K-MANIKANTA] For More @ www.stylewap.in

Manikanta Page 110 M.C.A

POSITIVE / NEGATIVE

void main ()

{

int a;

clrscr ();

printf ("Enter the value of A: ");

scanf("%d",&a);

if (a>=0)

{

printf ("Number is Positive");

}

else

{

printf("Number is Negative");

}

getch ();

}

POWER OF VARIABLE

void main ()

{

double no,r,res;

clrscr ();

printf ("Enter Number : ");

scanf ("%lf",&no);

printf ("Enter raised : ");

scanf ("%lf",&r);

res=pow(no,r);

printf ("\nResult is %.2lf", res);

getch ();

}

QUARDRATIC EQUATION

void main ()

{

int a,b,c,d;

clrscr ();

printf ("Enter A: ");

scanf ("%d",&a);

printf ("Enter B: ");

scanf ("%d",&b);

printf ("Enter C: ");

scanf ("%d",&c);

d= (b*b)-(4*a*c);

printf ("\nAnswer is %d",d);

getch ();

}

Page 111: 70893566 C Language Programs

October 29, 2011 [C LANGUAGE PROGRAMS BY MR.K-MANIKANTA] For More @ www.stylewap.in

Manikanta Page 111 M.C.A

SQUARE ROOT

void main ()

{

int no, a;

clrscr ();

printf ("Enter Number : ");

scanf ("%d",&no);

a=sqrt(no);

printf ("\nResult is %d", a);

getch ();

}

TOTAL BILL/ DISCOUNT A

void main ()

{

int b,dis,n;

clrscr ();

printf ("Enter Bill: ");

scanf ("%d",&b);

dis=(b<500)?(b*.10):(b>=500 &&amp; b<1000)?(b*.15):(b>=1000 &&

b<=2000)?(b*.20):(b*.25);

n=b-dis;

printf ("Net Bill is %d",n);

getch();

}

%AGE OF TOTAL MARKS

void main ()

{

int m1,m2,m3;

float tm,per;

clrscr ();

printf ("Enter M1: ");

scanf ("%d",&m1);

printf ("Enter M2: ");

scanf ("%d",&m2);

printf ("Enter M3: ");

scanf ("%d",&m3);

tm=m1+m2+m3;

per=(tm/300*100);

printf ("\nTotal Marks are %.2f",tm);

printf ("\nPercentage is %.2f",per);

getch ();

Page 112: 70893566 C Language Programs

October 29, 2011 [C LANGUAGE PROGRAMS BY MR.K-MANIKANTA] For More @ www.stylewap.in

Manikanta Page 112 M.C.A

}

CALCULATE TOTAL MARKS

void main ()

{

int m1,m2,m3,tm;

clrscr ();

printf ("Enter M1: ");

scanf ("%d",&m1);

printf ("Enter M2: ");

scanf ("%d",&m2);

printf ("Enter M3: ");

scanf ("%d",&m3);

tm=m1+m2+m3;

printf ("\nTotal Marks are %d",tm);

getch ();

}

TOTAL SALARY

void main ()

{

long int sal,hra,ta,ts;

clrscr ();

printf ("Enter Salary: ");

scanf("%ld",&sal);

if (sal>=5000)

{

hra=sal*.10;

ta=sal*.07;

}

else

{

hra=sal*.08;

ta=sal*.05;

}

ts=sal+hra+ta;

printf ("\n\nTotal Salary is Rs.%ld", ts);

getch ();

}

Page 113: 70893566 C Language Programs

October 29, 2011 [C LANGUAGE PROGRAMS BY MR.K-MANIKANTA] For More @ www.stylewap.in

Manikanta Page 113 M.C.A

LEAP YEAR

WAP TO FIND OUT YEAR IS LEAP OR NOT (IF-ELSE)

void main ()

{

int a;

clrscr ();

printf ("Enter the Year: ");

scanf("%d",&a);

if (a%4==0)

{

printf ("\nYear is Leap");

}

else

{

printf("\nYear is not Leap");

}

getch ();

}

SIZE OF VARIABLE

void main()

{

int a;

float b;

double c;

char ch;

long d;

char nm[10];

clrscr();

printf("\nInt size is \t:%d", sizeof (a));

printf("\nFloat size is \t:%d", sizeof (b));

printf("\nDouble size is \t:%d", sizeof (c));

printf("\nChar size is \t:%d", sizeof (ch));

printf("\nLong size is \t:%d", sizeof (d));

printf("\nString size is \t:%d", sizeof (nm));

getch ();

}

FIND STRING

Page 114: 70893566 C Language Programs

October 29, 2011 [C LANGUAGE PROGRAMS BY MR.K-MANIKANTA] For More @ www.stylewap.in

Manikanta Page 114 M.C.A

void main ()

{

char *k="Borland International", *g, *p;

clrscr ();

printf ("Enter string to find: ");

gets (g);

p=strstr(k,g);

printf ("%s",p);

getch ();

}

PALANDROM NUMBER

WAP TO FIND THAT NUMBER IS PALANDROM OR NOT (121=121)

#include

void main ()

{

int no,r,res,temp=0;

clrscr ();

printf ("Enter Number: ");

scanf ("%d",&no);

r=res=0;

temp=no;

while (no>0)

{

r=no%10;

no=no/10;

res=(res*10)+r;

}

if (temp==res)

printf("Number is Palandrom");

else

printf("Number is not Palandrom");

getch ();

}

PRIME NUMBER

void main ()

{

int no,i=2;

clrscr ();

printf ("Enter Number: ");

scanf ("%d",&no);

while (i<=no)

{

if (no%i==0)

break;

i++;

}

if (i==no)

Page 115: 70893566 C Language Programs

October 29, 2011 [C LANGUAGE PROGRAMS BY MR.K-MANIKANTA] For More @ www.stylewap.in

Manikanta Page 115 M.C.A

printf ("Number is Prime");

else

printf ("Number is not Prime");

getch ();

}

FACTORIAL NUMBER

WAP TO FIND THE FACTORIAL OF THE NUMBER (1X2X3X4)

void main ()

{

int fact=1,no;

clrscr();

printf ("Enter any number: ");

scanf ("%d",&no);

do

{

fact=fact*no;

no--;

}

while (no>0);

printf ("\nFactorial is %d",fact);

getch ();

}

WAP To find the GCD (greatest common divisor) of two given

integers

#include<stdio.h>

#include<conio.h>

#include<math.h>

unsigned int GcdRecursive(unsigned m, unsigned n);

unsigned int GcdNonRecursive(unsigned p,unsigned q);

int main(void)

{

int a,b,iGcd;

clrscr();

printf(―Enter the two numbers whose GCD is to be found: ―);

scanf(―%d%d‖,&a,&b);

Page 116: 70893566 C Language Programs

October 29, 2011 [C LANGUAGE PROGRAMS BY MR.K-MANIKANTA] For More @ www.stylewap.in

Manikanta Page 116 M.C.A

printf(―GCD of %d and %d Using Recursive Function is

%d\n‖,a,b,GcdRecursive(a,b));

printf(―GCD of %d and %d Using Non-Recursive Function is

%d\n‖,a,b,GcdNonRecursive(a,b));

getch();

}

/* Recursive Function*/

unsigned int GcdRecursive(unsigned m, unsigned n)

{

if(n>m)

return GcdRecursive(n,m);

if(n==0)

return m;

else

return GcdRecursive(n,m%n);

}

/* Non-Recursive Function*/

unsigned int GcdNonRecursive(unsigned p,unsigned q)

{

unsigned remainder;

remainder = p-(p/q*q);

if(remainder==0)

return q;

else

GcdRecursive(q,remainder);

}

Page 117: 70893566 C Language Programs

October 29, 2011 [C LANGUAGE PROGRAMS BY MR.K-MANIKANTA] For More @ www.stylewap.in

Manikanta Page 117 M.C.A

STRING LENGTH

void main ()

{

char ch [20];

int l;

clrscr ();

printf ("Enter String: ");

gets (ch);

l=strlen(ch);

printf ("Length of string is %d",l);

getch ();

}

PRINT NAME 10 TIMES

void main ()

{

int a=1;

clrscr();

do

{

printf ("Jagjeet\n");

a++;

}

while (a<=10);

getch ();

}

ASCII CODE - 0 TO 255

WAP TO PRINT ASCII CODE FROM 0 TO 255

void main ()

{

int i=0,count=0;

clrscr ();

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

{

if (count>24)

{count=0;getch();}

else

{printf("%d=%c\n",i,i);

count++;}

}

getch ();

}

Page 118: 70893566 C Language Programs

October 29, 2011 [C LANGUAGE PROGRAMS BY MR.K-MANIKANTA] For More @ www.stylewap.in

Manikanta Page 118 M.C.A

ODD SERIES 20 TO 1

void main ()

{

int a;

clrscr ();

a=20;

while (a>=1)

{

if (a%2==0)

printf ("%d\t",a);

else

printf ("%d\n",a);

a--;

}

getch ();

}

FABBONIC SERIES

void main ()

{

int a,b,c;

clrscr ();

a=0;b=1;c=1;

printf ("%d\n%d",a,b);

while (c<55)

{

Page 119: 70893566 C Language Programs

October 29, 2011 [C LANGUAGE PROGRAMS BY MR.K-MANIKANTA] For More @ www.stylewap.in

Manikanta Page 119 M.C.A

c=a+b;

printf ("\n%d",c);

a=b;

b=c;

}

getch ();

}

ENTER & DISPLAY

void main ()

{

char name [15];

clrscr ();

printf ("Enter your name: ");

gets (name);

printf ("\nme is: %s",name);

getch ();

}

NUMBERS DIVIDED BY 7

void main ()

{

int a;

clrscr ();

a=1;

while (a<=50)

{

if (a%7==0)

printf ("%d\n",a);

a++;

}

getch ();

}

SERIES BREAK ON 5

void main ()

{

int a;

clrscr ();

for (a=1;a<=10;a++)

{

if (a==5)

break;

printf ("%d\n",a);

}

getch ();

}

SQUARE & CUBE

void main ()

{

Page 120: 70893566 C Language Programs

October 29, 2011 [C LANGUAGE PROGRAMS BY MR.K-MANIKANTA] For More @ www.stylewap.in

Manikanta Page 120 M.C.A

int a=1,sqr=0,cube=0;

clrscr ();

while (a<=10)

{

sqr=pow(a,2);

cube=pow(a,3);

printf ("%d\t %d\t %d\n",a,sqr,cube);

a++;

}

getch ();

}

SKIP 5 & 7

void main ()

{

int a;

clrscr ();

for (a=1;a<=10;a++)

{

if (a==5 || a==7)

continue;

printf ("%d\n",a);

}

getch ();

}

PRINT SERIES (VARIABLE)

void main ()

{

int a,b;

clrscr ();

printf ("Enter Start: ");

Page 121: 70893566 C Language Programs

October 29, 2011 [C LANGUAGE PROGRAMS BY MR.K-MANIKANTA] For More @ www.stylewap.in

Manikanta Page 121 M.C.A

scanf ("%d",&a);

printf ("Enter End: ");

scanf ("%d",&b);

do

{

printf ("%d\n",a);

a++;

}

while (a<=b);

getch ();

}

PRINT STARS

void main ()

{

int i,j;

clrscr();

for (j=1;j<4;j++)

{

for (i=1;i<=5;i++)

{

printf ("*");

}

printf ("\n");

}

getch ();

}

TABLE OF 5

void main ()

{

int a,tab;

clrscr ();

a=1,tab=0;

while (a<=10)

{

tab=5*a;

a++;

printf ("%d\n",tab);

}

getch ();

}

PASSWORD VERIFICATION

WAP TO PRINT THE DETAIL OF THE PROGRAMMER

IF THE GIVEN NUMBER IS 464

void main ()

{

int pass;

Page 122: 70893566 C Language Programs

October 29, 2011 [C LANGUAGE PROGRAMS BY MR.K-MANIKANTA] For More @ www.stylewap.in

Manikanta Page 122 M.C.A

clrscr();

do

{

printf ("Enter Password to see the detail of programmer:\n");

scanf ("%d",&pass);

}

while (pass!=464);

printf ("\nJagjeet Singh");

printf ("\nB.Sc. (I.T.)\nPunjab Technical University");

getch ();

}

VALUE OF DATA TYPE

void main ()

{

int a=10;

float d=40.50;

char ch='A';

double dbl=78.9786;

long lng=7897711;

char nm [10]="JIMMY";

clrscr ();

printf ("\nInteger value is %d",a);

printf ("\nFloat value is %.2f",d);

printf ("\nCharacter value is %c",ch);

printf ("\nDouble value is %.4lf",dbl);

printf ("\nLong value is %ld",lng);

printf ("\nString value is %s",nm);

getch ();

}

Page 123: 70893566 C Language Programs

October 29, 2011 [C LANGUAGE PROGRAMS BY MR.K-MANIKANTA] For More @ www.stylewap.in

Manikanta Page 123 M.C.A

REVERSE NUMBER

void main ()

{

int no,r,res;

clrscr ();

printf ("Enter any value: ");

scanf ("%d",&no);

r=res=0;

while (no>0)

{

r=no%10;

no=no/10;

res=(res*10)+r;

}

printf ("\nReverse is %d",res);

getch ();

}

Wap to reverse words

#include <stdio.h>

#include <conio.h>

#include <string.h>

const int opt = 50;

int main(){

int i=0,j=0,c=0,vali[opt] = {0};

char str1[opt],str2[opt]="",str3[opt]="",ch;

printf("please enter your sentence :");

gets(str1);

for(i=0;str1[i] != '\0';i++){

if(str1[i] == ' '){

vali[j+1] = i;

j++;

}

}

c = j;

for(i=0;i<=j && c!=0;i++){

strcat(str2,&str1[vali[c]+1]);

strcpy(&str1[vali[c]],str3);

strcat(str2," ");

Page 124: 70893566 C Language Programs

October 29, 2011 [C LANGUAGE PROGRAMS BY MR.K-MANIKANTA] For More @ www.stylewap.in

Manikanta Page 124 M.C.A

c--;

}

strcat(str2,str1);

printf("\nyour reversed sentence is :");

puts(str2);

getch();

return 0;

}

SUM OF ARRAY

void main ()

{

int no[5],i,sum;

clrscr ();

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

{

printf ("Enter Element: ");

scanf ("%d",&no[i]);

}

sum=no[0]+no[1]+no[2]+no[3]+no[4];

printf ("\nSum of five Elements: %d",sum);

getch ();

}

SWAP NUMBERS

void main ()

{

int b,r,n,r1,r2;

clrscr ();

printf ("Enter the Value: ");

scanf ("%d",&n);

r=n%10;

n=n/10;

r1=n%10;

n=n/10;

r2=n%10;

n=n/10;

b=(r*100)*(r2*10)+(r2);

printf ("%d%d%d",r,r1,r2);

getch ();

}

Whether the given no. is armstrong or not

#include<stdio.h>

#include<conio.h>

void main()

{

int n,r,t,sum=0;

clrscr();

printf(" OUTPUT :\n");

Page 125: 70893566 C Language Programs

October 29, 2011 [C LANGUAGE PROGRAMS BY MR.K-MANIKANTA] For More @ www.stylewap.in

Manikanta Page 125 M.C.A

printf("\tEnter a no.");

scanf("%d",&n);

t=n;

while(n!=0)

{

r=n%10;

sum=sum+r*r*r;

n=n/10;

}

if(sum==t)

printf("The no. %d is armstrong",t);

else

printf("The no. %d is not an armstrong",t);

getch();

}

Whether the given no. Is palindrome or not

#include<stdio.h>

#include<conio.h>

void main()

{

int n,r,t,sum=0;

clrscr();

printf(" OUTPUT:\n");

printf("\tEnter a no.");

scanf("%d",&n);

t=n;

while(n!=0)

{

r=n%10;

sum=sum*10+r;

n=n/10;

}

if(sum==t)

printf("\tThe no. %d is a pallindrome",t);

else

printf("\tThe no. %d is not a pallindrome",t);

getch();

}

Write a C program to find both the largest and smallest number

in a list of integers

main( )

{

float largest(float a[ ], int n);

float value[4] = {2.5,-4.75,1.2,3.67};

Page 126: 70893566 C Language Programs

October 29, 2011 [C LANGUAGE PROGRAMS BY MR.K-MANIKANTA] For More @ www.stylewap.in

Manikanta Page 126 M.C.A

printf(―%f\n‖, largest(value,4));

}

float largest(float a[], int n)

{

int i;

float max;

max = a[0];

for(i = 1; i < n; i++)

if(max < a[i])

max = a[i];

return(max);

}

write a c program to find prime numbers

main()

{

int i,j=2,ch=0;

clrscr();

printf("\nENTER ANY NUMBER");

scanf("%d",&i);

while(j<=i/2)

{

if(i%j==0)

{

printf("%d IS NOT PRIME",i);

ch=1;

break;

}

else

{

j++;

}

}

if(ch==0)

{

printf("%d IS PRIME",i);

Page 127: 70893566 C Language Programs

October 29, 2011 [C LANGUAGE PROGRAMS BY MR.K-MANIKANTA] For More @ www.stylewap.in

Manikanta Page 127 M.C.A

}

}

Write a C program to find the sum of individual digits of a

positive integer

#include<stdio.h>

#include<conio.h>

void main()

{

int num, k=1, sum=0;

clrscr();

printf(―Enter the number whose digits are to be added:‖);

scanf(―%d‖,&num);

while(num!=0)

{

k=num%10;

sum=sum+k;

k=num/10;

num=k;

}

printf(―Sum of the digits:%d‖,sum);

getch();

}

Write a C program to generate all the prime numbers between 1

and n

#include <stdio.h>

void main()

{

Page 128: 70893566 C Language Programs

October 29, 2011 [C LANGUAGE PROGRAMS BY MR.K-MANIKANTA] For More @ www.stylewap.in

Manikanta Page 128 M.C.A

int no,counter,counter1,check;

clrscr();

printf(―<———————–PRIME NO. SERIES————————>‖);

printf(―\n\n\n\t\t\tINPUT THE VALUE OF N: ―);

scanf(―%d‖,&no);

printf(―\n\nTHE PRIME NO. SERIES B/W 1 TO %d : \n\n‖,no);

for(counter = 1; counter <= no; counter++)

{

check = 0;

//THIS LOOP WILL CHECK A NO TO BE PRIME NO. OR NOT.

for(counter1 = counter-1; counter1 > 1 ; counter1–)

if(counter%counter1 == 0)

{

check++; // INCREMENT CHECK IF NO. IS NOT A PRIME NO.

break;

}

if(check == 0)

printf(―%d\t‖,counter);

}

getch();

}

Write a C program to print all permutations of a given string

# include

# include

/* Function to swap values at two pointers */

void swap (char *x, char *y)

{

Page 129: 70893566 C Language Programs

October 29, 2011 [C LANGUAGE PROGRAMS BY MR.K-MANIKANTA] For More @ www.stylewap.in

Manikanta Page 129 M.C.A

char temp;

temp = *x;

*x = *y;

*y = temp;

}

/* Function to print permutations of string

This function takes three parameters:

1. String

2. Starting index of the string

3. Ending index of the string. */

void permute(char *a, int i, int n)

{

int j;

if (i == n)

printf("%s\n", a);

else

{

for (j = i; j <= n; j++)

{

swap((a+i), (a+j));

permute(a, i+1, n);

swap((a+i), (a+j)); //backtrack

}

}

}

/* Driver program to test above functions */

int main()

{

char a[] = "ABC";

permute(a, 0, 2);

getchar();

return 0;

}

Write a C program to reverse the words in a sentence in place.

#include

void rev(char *l, char *r);

int main(int argc, char *argv[])

{

char buf[] = "the world will go on forever";

char *end, *x, *y;

// Reverse the whole sentence first..

for(end=buf; *end; end++);

rev(buf,end-1);

Page 130: 70893566 C Language Programs

October 29, 2011 [C LANGUAGE PROGRAMS BY MR.K-MANIKANTA] For More @ www.stylewap.in

Manikanta Page 130 M.C.A

// Now swap each word within sentence...

x = buf-1;

y = buf;

while(x++ < end)

{

if(*x == '' || *x == ' ')

{

rev(y,x-1);

y = x+1;

}

}

// Now print the final string....

printf("%s\n",buf);

return(0);

}

// Function to reverse a string in place...

void rev(char *l,char *r)

{

char t;

while(l<r)

{

t = *l;

*l++ = *r;

*r-- = t;

}

} </r)

write c program to find the roots of a quadratic equation

#include<stdio.h>

#include<conio.h>

#include<math.h>

#include<process.h>

void main()

{

float a,b,c,x1,x2,disc;

Page 131: 70893566 C Language Programs

October 29, 2011 [C LANGUAGE PROGRAMS BY MR.K-MANIKANTA] For More @ www.stylewap.in

Manikanta Page 131 M.C.A

clrscr();

printf("Enter the co-efficients\n");

scanf("%f%f%f",&a,&b,&c);

disc=b*b-4*a*c;/*to find discriminant*/

if(disc>0)/*distinct roots*/

{

x1=(-b+sqrt(disc))/(2*a);

x2=(-b-sqrt(disc))/(2*a);

printf("The roots are distinct\n");

exit(0);

}

if(disc==0)/*Equal roots*/

{

x1=x2=-b/(2*a);

printf("The roots are equal\n");

printf("x1=%f\nx2=%f\n",x1,x2);

exit(0);

}

x1=-b/(2*a);/*complex roots*/

x2=sqrt(fabs(disc))/(2*a);

printf("The roots are complex\n");

printf("The first root=%f+i%f\n",x1,x2);

printf("The second root=%f-i%f\n",x1,x2);

getch();

Page 132: 70893566 C Language Programs

October 29, 2011 [C LANGUAGE PROGRAMS BY MR.K-MANIKANTA] For More @ www.stylewap.in

Manikanta Page 132 M.C.A

}

Write C program to implement Simpson method

#include<stdio.h>

#include<conio.h>

#include<math.h>

char postfix[80];

float stack[80];

char stack1[80];

int top=-1,top1=-1;

float eval(char postfix[], float x1);

void infix_postfix(char infix[]);

main()

{

float x0, xn, h, s,e1,e2, e3;

char exp[80], arr[80];

int i,n,l=0;

clrscr();

printf(―\nEnter an expression: ―);

gets(exp);

puts(―Enter x0, xn and number of sub-intervals: ―);

scanf(―%f%f%d‖, &x0, &xn, &n);

h=(xn-x0)/n;

if(exp[0]==‘l'&& exp[1]==‘o'&& exp[2]==‘g')

{

Page 133: 70893566 C Language Programs

October 29, 2011 [C LANGUAGE PROGRAMS BY MR.K-MANIKANTA] For More @ www.stylewap.in

Manikanta Page 133 M.C.A

l=strlen(exp);

for(i=0;i<l-3; i++)

arr[0]=exp[i+3];

arr[i]=‖;

infix_postfix(arr);

e1=eval(postfix,x0);

e2=eval(postfix,xn);

e3=4*eval(postfix, x0+h);

s=log(e1)+log(e2)+log(e3);

for (i=3;i<=n-1;i+=2)

s+=4*eval(postfix,x0+i*h)+2*eval(postfix, x0+(i-1)*h);

}

else

{

infix_postfix(exp);

s=eval(postfix,x0)+eval(postfix,xn)+4*eval(postfix, x0+h);

for (i=3;i<=n-1;i+=2)

s+=4*eval(postfix,x0+i*h)+2*eval(postfix, x0+(i-1)*h);

}

printf(―The value of integral is %6.3f\n‖,(h/3)*s);

return(0);

}

/*Inserting the operands in a stack. */

void push(float item)

Page 134: 70893566 C Language Programs

October 29, 2011 [C LANGUAGE PROGRAMS BY MR.K-MANIKANTA] For More @ www.stylewap.in

Manikanta Page 134 M.C.A

{

if(top==99)

{

printf(―\n\tThe stack is full‖);

getch();

exit(0);

}

else

{

top++;

stack[top]=item;

}

return;

}

/*Removing the operands from a stack. */

float pop()

{

float item;

if(top==-1)

{

printf(―\n\tThe stack is empty\n\t‖);

getch();

}

Page 135: 70893566 C Language Programs

October 29, 2011 [C LANGUAGE PROGRAMS BY MR.K-MANIKANTA] For More @ www.stylewap.in

Manikanta Page 135 M.C.A

item=stack[top];

top–;

return (item);

}

void push1(char item)

{

if(top1==79)

{

printf(―\n\tThe stack is full‖);

getch();

exit(0);

}

else

{

top1++;

stack1[top1]=item;

}

return;

}

/*Removing the operands from a stack. */

char pop1()

{

char item;

if(top1==-1)

Page 136: 70893566 C Language Programs

October 29, 2011 [C LANGUAGE PROGRAMS BY MR.K-MANIKANTA] For More @ www.stylewap.in

Manikanta Page 136 M.C.A

{

printf(―\n\tThe stack1 is empty\n\t‖);

getch();

}

item=stack1[top1];

top1–;

return (item);

}

/*Converting an infix expression to a postfix expression. */

void infix_postfix(char infix[])

{

int i=0,j=0,k;

char ch;

char token;

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

postfix[i]=‘ ‗;

push1(‗?‘);

i=0;

token=infix[i];

while(token!=‖)

{

if(isalnum(token))

{

Page 137: 70893566 C Language Programs

October 29, 2011 [C LANGUAGE PROGRAMS BY MR.K-MANIKANTA] For More @ www.stylewap.in

Manikanta Page 137 M.C.A

postfix[j]=token;

j++;

}

else if(token==‘(‗)

{

push1(‗(‗);

}

else if(token==‘)')

{

while(stack1[top1]!=‘(‗)

{

ch=pop1();

postfix[j]=ch;

j++;

}

ch=pop1();

}

else

{

while(ISPriority(stack1[top1])>=ICP(token))

{

ch=pop1();

postfix[j]=ch;

j++;

Page 138: 70893566 C Language Programs

October 29, 2011 [C LANGUAGE PROGRAMS BY MR.K-MANIKANTA] For More @ www.stylewap.in

Manikanta Page 138 M.C.A

}

push1(token);

}

i++;

token=infix[i];

}

while(top1!=0)

{

ch=pop1();

postfix[j]=ch;

j++;

}

postfix[j]=‖;

}

/*Determining the priority of elements that are placed inside

the stack. */

int ISPriority(char token)

{

switch(token)

{

case ‗(‗:return (0);

case ‗)‘:return (9);

case ‗+‘:return (7);

case ‗-‘:return (7);

Page 139: 70893566 C Language Programs

October 29, 2011 [C LANGUAGE PROGRAMS BY MR.K-MANIKANTA] For More @ www.stylewap.in

Manikanta Page 139 M.C.A

case ‗*‘:return (8);

case ‗/‘:return (8);

case ‗?‘:return (0);

default: printf(―Invalid expression‖);

}

return 0;

}

/*Determining the priority of elements that are approaching

towards the stack. */

int ICP(char token)

{

switch(token)

{

case ‗(‗:return (10);

case ‗)‘:return (9);

case ‗+‘:return (7);

case ‗-‘:return (7);

case ‗*‘:return (8);

case ‗/‘:return (8);

case ‖:return (0);

default: printf(―Invalid expression‖);

}

return 0;

}

Page 140: 70893566 C Language Programs

October 29, 2011 [C LANGUAGE PROGRAMS BY MR.K-MANIKANTA] For More @ www.stylewap.in

Manikanta Page 140 M.C.A

/*Calculating the result of expression, which is converted in

postfix notation. */

float eval(char p[], float x1)

{

float t1,t2,k,r;

int i=0,l;

l=strlen(p);

while(i<l)

{

if(p[i]==‘x')

push(x1);

else

if(isdigit(p[i]))

{

k=p[i]-‘0′;

push(k);

}

else

{

t1=pop();

t2=pop();

switch(p[i])

{

case ‗+‘:k=t2+t1;

Page 141: 70893566 C Language Programs

October 29, 2011 [C LANGUAGE PROGRAMS BY MR.K-MANIKANTA] For More @ www.stylewap.in

Manikanta Page 141 M.C.A

break;

case ‗-‘:k=t2-t1;

break;

case ‗*‘:k=t2*t1;

break;

case ‗/‘:k=t2/t1;

break;

default: printf(―\n\tInvalid expression‖);

}

push(k);

}

i++;

}

if(top>0)

{

printf(―You have entered the operands more than the operators‖);

exit(0);

}

else

{

r=pop();

return (r);

}

Page 142: 70893566 C Language Programs

October 29, 2011 [C LANGUAGE PROGRAMS BY MR.K-MANIKANTA] For More @ www.stylewap.in

Manikanta Page 142 M.C.A

return 0;

}

Write C Program to Print a Triangle

#include<stdio.h>

#include<conio.h>

void main()

{

int n;

printf("enter the lines :");

scanf("%d",&n);

for(int i=1;i<=n/2;i++)

{

printf("*\n");

for(int j=1;j<=2*i;j++)

printf("%d",j);

printf("\n");

}

if(n%2!=0)

printf("\n*");

getch();

}

XOR list example

#include <stdio.h>

#include <conio.h>

#include

struct xnode

{

int data;

Page 143: 70893566 C Language Programs

October 29, 2011 [C LANGUAGE PROGRAMS BY MR.K-MANIKANTA] For More @ www.stylewap.in

Manikanta Page 143 M.C.A

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;

}

Page 144: 70893566 C Language Programs

October 29, 2011 [C LANGUAGE PROGRAMS BY MR.K-MANIKANTA] For More @ www.stylewap.in

Manikanta Page 144 M.C.A

printf("\n");

}