Professional Documents
Culture Documents
Program – 1 Program – 3
/* Swapping two numbers – Example for Simple if */ /* Biggest among two numbers – Example for if – else */
#include<stdio.h> #include<stdio.h>
#include<conio.h> #include<conio.h>
void main() void main()
{ {
int a, b, t; int a,b;
clrscr(); clrscr();
printf(“Enter two Numbers:”); printf(“Enter two numbers:”);
scanf(“%d%d”, &a, &b); scanf(“%d%d”, &a, &b);
if(a>b) if(a>b)
{ printf(“%d is Big”, a);
t = a; else
a = b; printf(“%d is Big”, b);
b = t; getch();
} }
printf(“The values of a = %d and b = %d”, a, b);
getch(); Output:
} Enter two numbers: 20 30
30 is Big
Output:
Enter two Numbers: 8 5
The values of a = 5 and b = 8
Program – 2 Program – 4
/* Leap Year – Example for if – else */ /* Display the types of character – Example for Nested
#include<stdio.h> if - else */
#include<conio.h> #include<stdio.h>
void main() #include<conio.h>
{ void main()
int year; {
clrscr(); char ch;
printf(“Enter a Year:”); clrscr();
scanf(“%d”, &year); printf(“Enter a character:”);
if(year % 4 == 0) scanf(“%c”, &ch);
{ if((ch >= ‘a’ && ch = ‘z’) || (ch >= ‘A’ && ch < = ‘Z’))
printf(“%d is a Leap Year”, year); printf(“%c is an Alphabet.\n”, ch);
} else if(ch >= ‘0’ && ch <= ‘9’)
else printf(“%c is a Digit.\n”, ch);
{ else
printf(“%d is not a Leap Year”, year); printf(“%c is a Special Character”, ch);
} getch();
getch(); }
}
Output:
Output: Enter a character: *
Enter a Year: 2000 * is a Special Character
2000 is a Leap Year
Roy Antony
Digitally signed by Roy Antony
Arnold G
DN: cn=Roy Antony Arnold G,
Arnold G
o=Photon Soft Solutions, ou,
email=arnoldindia@aol.in, c=IN
Date: 2009.08.11 19:33:05 -08'00'
Panimalar Engineering College MCA Dept. C Programs
Program – 5 Program – 6
/* Arithmetic Operations (Calculator) – Example for /* Prime Number Program – Example for goto label */
switch – case */ #include<stdio.h>
#include<stdio.h> #include<conio.h>
#include<conio.h> #include<math.h>
void main() void main()
{ {
char ch; int n, i, r;
int a, b, ans; clrscr();
clrscr(); printf(“Enter the positive integer value…”);
printf(“Calculator:\n + : Addition\n - : Subtract \n * : scanf(“%d”, &n);
Multiply\n / : Division\n % : Mod Division\n”); i=2;
printf(“Enter the code:”); step1:
scanf(“%c”, &ch); if(i<=sqrt(n))
printf(“\nEnter two values: “); {
scanf(“%d%d”, &a, &b); r=n%i;
switch(ch) if(r==0)
{ {
case ‘+’ : printf(“%d is not a prime”, n);
ans = a+b; goto end;
break; }
case ‘-‘ : }
ans = a-b; else
break; {
case ‘*’ : i++;
ans = a*b; goto step1;
break; }
case ‘/’ : printf(“%d is prime”, n);
ans = a/b; getch();
break; end:
case ‘%’ : printf(“ “);
ans = a%b;
break; }
default:
printf(“\nInvalid Operator”); Output:
ans = 0; Enter the positive integer value… 3
} 3 is prime
printf(“\nThe Result is %d”, ans);
getch();
}
Output:
Calculator:
+ : Addition
- : Subtract
* : Multiply
/ : Division
% : Mod Division
Enter the Code: *
Enter two values: 8 2
The Result is 16.
Compiled by – GRAA
2
Panimalar Engineering College MCA Dept. C Programs
/* Sum of numbers upto 10 – Example for while loop */ /* Sum of numbers upto 10–Example for do–while loop */
#include<stdio.h> #include<stdio.h>
#include<conio.h> #include<conio.h>
void main() void main()
{ { int sum=0, i =1;
int sum=0, i =1; clrscr();
clrscr(); do
while(i<=10) { sum = sum + i;
{ i++;
sum = sum + i; } while(i<=10);
i++; printf(“The sum of numbers upto 10 is %d”, sum);
} getch();
printf(“The sum of numbers upto 10 is %d”, sum); }
getch(); Output:
} The sum of numbers upto 10 is 55
Output:
Program – 4
The sum of numbers upto 10 is 55
/* Sum of numbers upto 10 – Example for ‘for’ loop */
Program – 2 #include<stdio.h>
#include<conio.h>
/* Reversing given number & Checking Palindrome – void main()
Example for while loop */ {
#include<stdio.h> int sum=0, i;
#include<conio.h> clrscr();
void main() for(i=1; i<=10; i++)
{ {
int rev=0, n, num, digit; sum = sum + i;
clrscr(); }
printf(“\nEnter the number: “); printf(“The sum of numbers upto 10 is %d”, sum);
scanf(“%d”, &n); getch();
num = n; }
while(n!=0) Output:
{ The sum of numbers upto 10 is 55
digit = n % 10;
rev = rev * 10 + digit;
n = n/10; Program – 5
}
printf(“\nThe Reversed Number is %d”, rev); /* Print numbers upto 5 – Example for break stat. */
if (rev == num) #include<stdio.h>
printf(“\nThe Number is Palindrome”); void main()
else { int i;
printf(“\nThe Number is not a Palindrome”); for(i=1; i<=10; i++)
getch(); { if (i==6)
} break;
printf(“%d \t”, i);
}
Output:
Enter the number : 1221 }
The Reversed Number is 1221 Output:
The Number is Palindrome 1 2 3 4 5
Compiled by – GRAA
3
Panimalar Engineering College MCA Dept. C Programs
Program – 6 Program – 8
/* Sum of +ve numbers – Example for continue stat. */ /* Generation of Fibonacci Series – Example for while
#include<stdio.h> loop */
void main() #include<stdio.h>
{ #include<conio.h>
int i, n, sum = 0; void main()
for(i=1; i<=5; i++) {
{ int f1 = -1, f2 = 1, n, newterm = 0;
printf(“\n Enter a number…”); clrscr();
scanf(“%d”, &n); printf(“\nEnter the final term of series : “);
if(n<0) scanf(“%d”, &n);
continue; while(newterm <= n)
else {
sum = sum + n; newterm = f1 + f2;
} f1 = f2;
printf(“\nThe Sum is … %d”, sum); f2 = newterm;
} printf(“%d\t”, newterm);
}
Output: /* Negative Numbers are neglected */ getch();
Enter a number … 10 }
Enter a number … -12
Enter a number … 12 Output:
Enter a number … -10 Enter the final term of series: 21
Enter a number … 5 0 1 1 2 3 5 8 13 21
The Sum is … 27
Program – 9
Program – 7
/* Sum of digits of a number – Example for while loop */
/* Printing Number Pyramid – Example for nested for #include<stdio.h>
loop */ #include<conio.h>
#include<stdio.h> void main()
#include<conio.h> {
void main() int sum=0, num, rem;
{ clrscr();
int i, j, n; printf(“\nEnter the number: “);
clrscr(); scanf(“%d”, &num);
printf(“\nEnter a number : “); while(num!=0)
scanf(“%d”, &n); {
for(i=1; i<=n; i++) rem = num % 10;
{ sum = sum + rem;
for(j=i; j<=n; j++) num = num/10;
printf(“%d\t”, j); }
printf(“\nThe Sum of digits is %d”, sum);
printf(“\n”); getch();
} }
getch();
} Output:
Enter the number : 1221
Output: The Sum of digits is 6
Enter a number : 5
1 2 3 4 5
2 3 4 5
3 4 5
4 5
5
Compiled by – GRAA
4
Panimalar Engineering College MCA Dept. C Programs
/* Sum of two numbers – Example for global variable */ /* Sum of two numbers – Example for Function with
#include<stdio.h> arguments and no return value – Call by value */
#include<conio.h> #include<stdio.h>
int a, b; #include<conio.h>
void main() void main()
{ {
void sum(void); void sum(int, int);
clrscr(); int a, b;
printf(“\nEnter two numbers: “); clrscr();
scanf(“%d%d”, &a, &b); printf(“\nEnter two numbers: “);
sum(); scanf(“%d%d”, &a, &b);
getch(); sum(a, b); /* Function Call */
} getch();
}
void sum (void)
{ void sum (int a, int b)
printf(“The Sum is %d”, a+b); {
} printf(“The Sum is %d”, a+b);
}
Output:
Enter two numbers : 12 21 Output:
The Sum is 33 Enter two numbers : 10 21
The Sum is 31
Program – 2
Program – 4
/* Sum of two numbers – Example for Function with no
arguments and no return value */ /* Sum of two numbers – Example for Function with
#include<stdio.h> arguments and with return value – Call by value */
#include<conio.h> #include<stdio.h>
void main() #include<conio.h>
{ void main()
void sum(void); {
clrscr(); int sum(int, int); /*Function Prototype */
sum(); /* Function Call */ int a, b;
getch(); clrscr();
} printf(“\nEnter two numbers: “);
scanf(“%d%d”, &a, &b);
void sum (void) printf(“The Sum is %d”, sum(a, b)); /* Call by value */
{ getch();
int a, b; }
printf(“\nEnter two numbers: “);
scanf(“%d%d”, &a, &b); int sum (int a, int b)
printf(“The Sum is %d”, a+b); {
} return a+b;
}
Output:
Enter two numbers : 10 21 Output:
The Sum is 31 Enter two numbers : 10 21
The Sum is 31
Compiled by – GRAA
5
Panimalar Engineering College MCA Dept. C Programs
Program – 5 Program – 7
/* Sum of two numbers – Example for Function with no /*Sum of two numbers–Example for Pointer Function*/
arguments and with return value */ #include<stdio.h>
#include<stdio.h> #include<conio.h>
#include<conio.h> void main()
void main() {
{ int* sum(int*, int*); /*Function Prototype */
int sum(void); int *s;
clrscr(); clrscr();
printf(“The Sum is %d”, sum()); /* Function Call */ printf(“\nEnter two numbers: “);
getch(); scanf(“%d%d”, &a, &b);
} s = sum(&a, &b);
printf(“The Sum is %d”, *s);
int sum (void) getch();
{ }
int a, b;
printf(“\nEnter two numbers: “); int* sum (int *x, int *y)
scanf(“%d%d”, &a, &b); {
return a+b; int *z;
} z = *x + *y;
return (&z); /* Returns only address not value */
Output: }
Enter two numbers : 10 21
The Sum is 31 Output:
Enter two numbers : 10 21
The Sum is 31
Program – 6
Output:
Enter a numbers : 5
The Factorial is 120
Compiled by – GRAA
6
Panimalar Engineering College MCA Dept. C Programs
Program – 9 Program – 11
/* Finding Factorial – Example for Recursive Function */ /* Tower of Honoi – Example for Recursive Function */
#include<stdio.h> #include<stdio.h>
#include<conio.h> #include<conio.h>
void main() void main()
{ {
int fact(int); void honoi(int, char, char, char);
int n; int n;
clrscr(); clrscr();
printf(“\nEnter a number: “); printf(“\nHow many disks: “);
scanf(“%d”, &n); scanf(“%d”, &n);
printf(“The Factorial is %d”, fact(n)); honoi(n, ‘L’, ‘R’, ‘C’);
getch(); getch();
} }
int fact (int num) void honoi (int n, char from, char to, char temp)
{ {
int f; if(n > 0)
if (num == 1) {
return 1; honoi(n-1, from, temp, to);
else printf(“Move Disk %d from %c to %c\n”, n, from, to);
f = num * fact(num – 1); / * Recursive call */ honoi(n-1, temp, to, from);
return f; }
} }
Output: Output:
Enter a numbers : 5 How many disks: 3
The Factorial is 120 Move Disk 1 from L to R
Move Disk 2 from L to C
Move Disk 1 from R to C
Program – 10
Move Disk 3 from L to R
Move Disk 1 from C to L
/* Reversing String – Example for Recursive Function */ Move Disk 2 from C to R
#include<stdio.h>
Move Disk 3 from L to R
#include<conio.h>
void main()
{ Program – 12
void reverse(void);
clrscr(); /* Example for static variable & empty for loop */
printf(“\nEnter the Text: “); #include<stdio.h>
reverse(); void main()
getch(); {
} void show(void);
for( ; ; )
void reverse (void) show();
{ }
char c;
if(( c = getchar( ) ) != ‘\n’) void show (void)
reverse(); /* Recursive call */ {
purchar(c); int static n;
} printf(“%d\t”, ++n);
if(n == 5)
exit(1);
Output:
}
Enter the Text : ARUNAI
IANURA
Output:
1 2 3 4 5
Compiled by – GRAA
7
Panimalar Engineering College MCA Dept. C Programs
Compiled by – GRAA
8
Panimalar Engineering College MCA Dept. C Programs
Program – 4 Program – 6
Compiled by – GRAA
9
Panimalar Engineering College MCA Dept. C Programs
/* Print address and value of variable – Simple Example /* Sum of N numbers – Example for Using pointers with
for pointer */ Arrays */
#include<stdio.h> #include<stdio.h>
void main() void main()
{ int a=10, *b; { int *p, sum=0, a[5];
b = &a; printf(“\nEnter five numbers”);
printf(“\nThe Value of a = %d”, *b); for(p = a; p < a+5; p++)
printf(“\nAddress of a = %u”, b); scanf(“%d”, p);
} for(p = a; p < a+5; p++)
Output: sum = sum + *p;
The Value of a = 10 printf(“\nThe Sum is %d”, sum);
Address of a = 2028 }
Output:
Enter five numbers 10 20 30 40 50
Program – 2
The Sum is 150
/* Swapping values of variable – Simple Example for Call
by Reference (Using pointers with function) */ Program – 5
#include<stdio.h>
void main() /* Finding Biggest – Example for Function pointer
{ int a, b; (Function that returns the address not value)*/
void swap(int *, int *); #include<stdio.h>
printf(“\nEnter two number”); void main()
scanf(“%d%d”, &a, &b); { int *big, a, b;
printf(“\nBefore Exchange a = %d, b = %d”, a, b); int *biggest(int *, int *); /* function returns address */
swap(&a, &b); printf(“\nEnter two numbers”);
printf(“\nAfter Exchange a = %d, b = %d”, a, b); scanf(“%d%d”, &a, &b);
} big = biggest(&a, &b); /* address of biggest no. is stored in big*/
void swap(int *x, int *y) printf(“\nThe Biggest Number is %d”, *big);
{ int t; }
t = *x; int *biggest(int *m, int *n)
x = *y; { if(*m > *n) return (m); /*returns address stored in m */
y = t; else return (n); /* returns address stored in n */
} }
Output: Output:
Enter two number 10 23 Enter two numbers 30 50
Before Exchange a = 10, b = 23 The Biggest Number is 50
After Exchange a = 23, b = 10
Program – 6
Program – 3
/* Example for Array of pointers */
/* Printing Array Elements– Simple Example for using #include<stdio.h>
pointers with arrays */ void main()
#include<stdio.h> { int a = 4, b = 3, c = 2, i;
void main() int *p[3]={&a, &b, &c};
{ int *p, a[5] = {1, 2, 3, 4, 5}; printf(“\nThe Values are: “);
for(p = a; p < a+5; p++) for(i=0; i < 3; i++)
printf(“%d\t”, *p); printf(“%d\t”, *p[i]);
} }
Output: Output:
1 2 3 4 5 The Values are: 4 3 2
Compiled by – GRAA
10
Panimalar Engineering College MCA Dept. C Programs
Program – 7 Program – 9
/* Pointer Arithmetic – Example for Operations on /* Example for memory allocation functions usage */
Pointers*/ #include<stdio.h>
#include<stdio.h> #include<stdlib.h>
void main() #include<string.h>
{ void main()
int *p, a[5]; {
p = a; char *p;
printf(“\nAddress of a = %u”, p); p = (char *) malloc (7); /* allocates memory */
printf(“\nPostincrement of address = %u”, p++); strcpy(p, “MADRAS”);
printf(“\nPreincrement of address = %u”, ++p); printf(“\nMemory Contains: %s”, p);
printf(“\nPredecrement of address = %u”, --p); p = (char *) realloc(8); /* reallocates memory */
printf(“\nPostdecrement of address = %u”, p--); strcpy(p, “CHENNAI”);
printf(“\nAddress stored in p = %u”, p); printf(“\nMemory now Contains: %s”, p);
} free(p); /* Releases the memory */
}
Output:
Address of a = 3022 Output:
Postincrement of Address = 3022 Memory Contains: MADRAS
Preincrement of Address = 3026 Memory now Contains: CHENNAI
Predecrement of Address = 3024
Postdecrement of Address = 3024
Program – 10
Address stored in p = 3022
/* Sum of Array elements – Example for Dynamic
DYNAMIC MEMORY ALLOCATION Memory Allocation of Arrays */
#include<stdio.h>
#include<stdlib.h>
Program – 8 #include<conio.h>
void main()
/* Addition – Simple Example for Dynamic Memory {
Allocation */ int *a, sum = 0, n, i;
#include<stdio.h> clrscr();
#include<stdlib.h> printf(“\nHow many numbers: “);
#include<conio.h> scanf(“%d”, &n);
void main() a = (int *) malloc (n * sizeof(int));
{ printf(“\nEnter %d values \n”, n);
int *a, *b; for( i = 0; i < n; i++)
clrscr(); scanf(“%d’, a+i);
a = (int *) malloc (sizeof(int)); for( i = 0; i < n; i++)
b = (int *) malloc (sizeof(int)); sum = sum + *(a+i);
printf(“\nEnter value for a:”); printf(“\nThe Sum is %d”, sum);
scanf(“%d’, a); getch();
printf(“\nEnter value for b:”); }
scanf(“%d”, b);
printf(“\nThe Sum is %d”, *a + *b); Output:
getch(); How many numbers: 5
} Enter 5 values
53281
Output: The Sum is 19
Enter value for a: 3
Enter value for b: 8
The Sum is 11
Compiled by – GRAA
11
Panimalar Engineering College MCA Dept. C Programs
/* Complex Number Addition – Example for structures */ /*Student Structure–Example for Array of structures*/
#include<stdio.h> #include<stdio.h>
#include<conio.h> void main()
void main() { struct student
{ struct complex { char name[20];
{ int real, imag; char sex[2];
} c1, c2, c3; int age;
clrscr(); } s[2];
printf(“\nEnter first complex number: “); int i;
scanf(“%d%d”, &c1.real, &c1.imag); printf(“\nEnter Student Details (Name, sex, age): “);
printf(“\nEnter second complex number: “); for( i = 0; i<2; i++)
scanf(“%d%d”, &c2.real, &c2.imag); scanf(“%s%s%d”, s[i].name, s[i].sex, &s[i].age);
c3.real = c1.real + c2.real; printf(“\nThe Student Details are:\n”);
c3.imag = c1.imag + c2.imag; printf(“\n Name\t Sex \t Age\n”);
printf(“\nResultant complex number is %d+%d i ”, for( i = 0; i<2; i++)
c3.real, c3.imag); printf(“%s\t%s\t%d”, s[i].name, s[i].sex,
getch(); &s[i].age);
} }
Output: Output:
Enter first complex number: 3 4 Enter Student Details (Name, sex, age):
Enter second complex number: 4 2 Ram M 19
Resultant complex number is 7+ 6i Geetha F 19
The Student Details are:
Name Sex Age
Program – 2
Ram M 19
Geetha F 19
/*Student Structure–Example for Array of structures*/
#include<stdio.h>
void main() Program – 4
{ struct student
{ char name[20]; /*Example for structures with pointers*/
char sex[2]; #include<stdio.h>
int age; void main()
} s[2]; { struct student
int i; { char name[20];
printf(“\nEnter Student Details (Name, sex, age): “); char sex[2];
for( i = 0; i<2; i++) int age;
scanf(“%s%s%d”, s[i].name, s[i].sex, &s[i].age); } s, *sp;
printf(“\nThe Student Details are:\n”); sp = &s;
printf(“\n Name\t Sex \t Age\n”); printf(“\nEnter Student Details (Name, sex, age): “);
for( i = 0; i<2; i++) scanf(“%s%s%d”, sp->name, sp->sex, &sp->age);
printf(“%s\t%s\t%d”, s[i].name, s[i].sex, printf(“\nThe Student Details are:\n”);
&s[i].age); printf(“\n Name\t Sex \t Age\n”);
} printf(“%s\t%s\t%d”, sp->.name, sp->sex, sp->age);
Output: }
Enter Student Details (Name, sex, age): Output:
Ram M 19 Enter Student Details (Name, sex, age):
Geetha F 19 Ram M 19
The Student Details are: The Student Details are:
Name Sex Age Name Sex Age
Ram M 19 Ram M 19
Geetha F 19
Compiled by – GRAA
12
Panimalar Engineering College MCA Dept. C Programs
Program – 6 Program – 8
/*Complex Addition – Example Passing structures to /*Example for enum data type–Defaultly starts from 0 –
function */ Example for switch – case */
#include<stdio.h> #include<stdio.h>
#include<conio.h> void main()
void main() { enum country
{ typedef struct complex { INDIA = 91,
{ int real, imag; AMERICA = 1,
}; AUSTRALIA = 61,
complex c1, c2, c3; ITALY = 39
complex add(complex, complex); };
clrscr(); int code;
printf(“\nEnter first complex number: “); printf(“\nPlease Enter a Country Code:”);
scanf(“%d%d”, &c1.real, &c1.imag); scanf(“%d”, &code);
printf(“\nEnter second complex number: “); switch(code)
scanf(“%d%d”, &c2.real, &c2.imag); { case 91:
c3 = add(c1, c2); printf(“\nThis code is belongs to India”);
printf(“\nResultant complex number is %d+%d i ”, break;
c3.real, c3.imag); case 1:
getch(); printf(“\nThis code is belongs to America”);
} break;
case 61:
complex add(complex a, complex b) printf(“\nThis code is belongs to Australia”);
{ break;
complex c; case 39:
c.real = a.real + b.real; printf(“\nThis code is belongs to Italy”);
c.imag = a.imag + b.imag; default:
return (c); printf(“\nNo such code in database”);
} }
Output: }
Enter first complex number: 3 4 Output 1:
Enter second complex number: 4 2 Please Enter a Country Code: 39
Resultant complex number is 7+ 6i This code is belongs to Italy
Output 2:
Please Enter a Country Code: 121
No such code in database
Compiled by – GRAA
13
Panimalar Engineering College MCA Dept. C Programs
FILE HANDLING
Output:
Program – 1
Enter Student’s name and mark:
Raman 98
/* Writing text into Files – Example for fprintf, fputs,
Divya 90 ^Z
fputc functions */
Name Mark
#include<stdio.h>
-------------------
void main()
Raman 98
{ FILE *fp = fopen(“file1.txt”, “w”);
Divya 90
fprintf(fp, “India is my country”);
fputc(fp, ‘&’);
fputs(fp, “I Love my country”); Program – 4
fclose(fp);
} /* Program to Read and Write records from a file –
Output: /*The file file1.txt will have the following lines*/ Example for using structure in file and also for fwrite
India is my country and fread */
& #include<stdio.h>
I Love my country typedef struct student
{
unsigned int regno;
Program – 2
char name[20];
unsigned int mark;
/* Reading text from Files – Example for fscanf */
};
#include<stdio.h>
void main()
void main()
{ /* classrec is the file contains all the records */
{ int i; /* assume file2.txt contains only 30 */
student rec;
FILE* fp = fopen(“file2.txt”, “r”);
FILE* fp = fopen(“classrec”, “wb”);
fscanf(fp, “%d”, &i);
printf(“Enter Student’s Reg.No., name and mark:\n “);
printf(“\nThe integer in file2.txt is %d”, i);
printf(“Use Ctrl + Z to stop entry\n”);
fclose(fp);
while((scanf(“%u%s%u”, &rec.regno, rec.name,
}
&rec.mark)) != EOF)
Output: fwrite(&rec, sizeof(rec), 1, fp);
The integer in file2.txt is 30 fclose(fp);
printf(“\n”);
Program – 3 fp = fopen (“classrec”, “rb”);
printf(“Reg.No. \t Name \t Mark\n”);
/* Program to Read and Write to a file – Example for printf(“---------------------------------------------“);
file operations*/ while ((fread(&rec, sizeof(rec), 1, fp))
#include<stdio.h> printf(“%5u %-10s %3u\n”, rec.regno, rec.name,
void main() rec.mark);
{ /* studata is the file stored in secondary storage */ fclose(fp);
unsigned int mark; }
char name[20]; /* Opening file for write (append) */ Output:
FILE* fp = fopen(“studata”, “a”); Enter Student’s Reg.No., name and mark:
printf(“Enter Student’s name and mark:\n “); 345 Raman 98
printf(“Use Ctrl + Z to stop entry\n”); 321 Vel 88 Entered data will be stored in
sturec file. These data are
while((scanf(“%s%u”, name, &mark)) != EOF) 456 Divya 90
retrieved again for read
fprintf(fp, “%s %u”, name, mark); 210 Arul 30 ^Z operation
fclose(fp); /* Opening file for read */
fp = fopen (“studata”, “rt”); Reg.No. Name Mark
printf(“\t Name \t Mark\n”); --------------------------------
printf(“-------------------------“); 345 Raman 98
while ((fscanf(fp, “%s %u”, name, &mark)) != EOF) 321 Vel 88
printf(“%-10s %3u\n”, name, mark); 456 Divya 90
fclose(fp); 210 Arul 30
}
Compiled by – GRAA
14
Panimalar Engineering College MCA Dept. C Programs
Compiled by – GRAA
15
Panimalar Engineering College MCA Dept. C Programs
while(cur->next!=NULL) printf("\nhead->");
{
cur=cur->next; while(cur!=NULL)
} {
printf("%d->", cur->data);
temp->next=cur->next; cur = cur->next;
cur->next = temp; }
}
printf("NULL\n");
void delfirst() }
{
node *temp;
temp=head;
head = head->next;
free(temp);
}
void delmid()
{
int i=1, pos;
node *temp, *cur=head;
Compiled by – GRAA
16
Panimalar Engineering College MCA Dept. C Programs
Program – 2 }
void add()
/* Simple Linked List Operations */ {
int i=1, pos;
#include<stdio.h> node *cur=head->next , *temp;
#include<stdlib.h> temp = (node *) malloc(sizeof(node));
printf("\nEnter the Data for the node... ");
typedef struct node scanf("%d", &temp->data);
{ if (head == NULL)
int data; {
node *next; temp->next = head;
}; head = temp;
}
node *head=0; else
{
void main() temp->next = cur->next;
{ cur->next = temp;
int ch; }
void add(void); }
void del(void);
void display(void); void del()
int isempty(void); {
node *temp;
while(1) temp=head;
{ head = head->next;
printf("\n Single Linked List - Menu\n"); free(temp);
printf("\n 1. Add \n2.Delete\n 3.Exit\n"); }
int isempty(void)
{
return head==NULL;
Compiled by – GRAA
17
Panimalar Engineering College MCA Dept. C Programs
Program – 4
Program – 3
/* Queue Operations */
/* Stack Operations */ #include<stdio.h>
#include<stdio.h> #include<stdlib.h>
#include<stdlib.h> typedef struct node
typedef struct node { int data;
{ int data; node *next;
node *next; }; node *head=0;
}; node *head=0; void main()
void main() { int empty(void), ch;
{ int empty(void), ch; void enqueue(void);
void push(void); void dequeue(void);
void pop(void); void display(void);
void display(void); while(1)
while(1) { printf("\n Queue - Menu\n");
{ printf("\n Stack - Menu\n"); printf("\n 1. Enqueue\n 2.Dequeue\n 3.Exit\n");
printf("\n 1. Push \n 2. Pop \n 3.Exit\n"); printf("\nEnter your Choice... ");
printf("\nEnter your Choice... "); scanf("%d", &ch);
scanf("%d", &ch); switch(ch)
switch(ch) { case 1:
{ case 1: enqueue();
push(); display();
display(); break;
break; case 2:
case 2: if(! empty())
if(! empty()) { dequeue();
{ pop(); display(); }
display(); } else
else printf("\nList Empty");
printf("\nStack Empty"); break;
break; case 3:
case 3: exit(0);
exit(0); default:
default: printf("\nInvalid Choice\n");
printf("\nInvalid Choice\n"); } } }
} } } int empty(void)
int empty(void) { return head==NULL; }
{ return head==NULL; } void enqueue()
void push() { node *temp;
{ node *temp; temp = (node *) malloc(sizeof(node));
temp = (node *) malloc(sizeof(node)); printf("\nEnter the Data for the node... ");
printf("\nEnter the Data to push... "); scanf("%d", &temp->data);
scanf("%d", &temp->data); temp->next = head;
temp->next = head; head = temp;
head = temp; }
} void dequeue()
void pop() { node *temp, *cur=head->next;
{ node *temp; while(cur->next->next!=NULL)
temp=head; cur=cur->next;
head = head->next; temp=cur->next;
free(temp); cur->next=NULL;
} free(temp);
void display() }
{ node *cur=head->next; void display()
printf("\nhead->"); { node *cur=head->next;
while(cur!=NULL) printf("\nhead->");
{ printf("%d->", cur->data); while(cur!=NULL)
cur = cur->next; } { printf("%d->", cur->data);
printf("NULL\n"); cur = cur->next; }
Compiled by – GRAA
18
Panimalar Engineering College MCA Dept. C Programs
printf("NULL\n"); }
OTHER PROGRAMS
Area of the triangle is 9.921567
Program – 1
Compiled by – GRAA
19
Panimalar Engineering College MCA Dept. C Programs
Program – 6
Output 2:
Enter value for a, b and c 1 2 1
Discriminant = 0.00
Compiled by – GRAA
20