Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 70

/* A program to calculate the simple and compound intrest.*/ #include"stdio.h" #include"math.

h" void main() { int p; float t, r, si=0, ci=0; clrscr(); printf("\n\n\tEnter profit, time duration, rate of interest: "); scanf("%d %f %f",&p, &t, &r); si=(p*t*r)/100; // formula to calculate Simple Intrest ci=p+(p*pow(r,t)/100); // formula to calculate Compound Intrest printf("\n\n\tThe Simple Interest is: %.3f",si); printf("\n\n\tThe Compound Interest is: %.3f",ci); getch(); } /* OK */ /* A program to find the grade of a student with five subjects.*/ #include"stdio.h" void main() { int regno, marks[5], i, sum=0; float avg; char grade; clrscr(); printf("\n\n\tEnter a register number of a student: "); scanf("%d",&regno); printf("\n\n\tEnter the marks secured in five subjects: \n"); for(i=0;i<5;i++) { scanf("%d",&marks[i]); } for(i=0;i<5;i++) { sum=sum+marks[i]; } avg=(float)(sum/5); if(avg>75) { printf("\n\n\tPassed with DISTINCTION."); } else if(avg>60 && avg<74) { printf("\n\n\tPassed with A grade."); } else

if(avg>40 && avg<59) { printf("\n\n\tPassed with B grade."); } else printf("\n\n\tFailed."); getch(); } /* OK */ /* To calculate the sum of first n natural numbers using formula and using for loop*/ #include"stdio.h" void main() { int num, i, lsum=0, n; clrscr(); printf("\n\n\tHow many natural numbers? "); scanf("%d",&num); if(num > 0) { n=(num*(num+1))/2; printf("\n\n\tThe sum of %d natural numbers using formula is: %d",num,n); printf("\n\n\tThe natural numbers between 1 and %d are: ",num); for(i=1;i<=num;i++) { printf("%d ",i); lsum=lsum+i; } printf("\n\n\tBy 'for' loop the sum of natural numbers is: %d",lsum); } else printf("\n\tInvalid Number."); getch(); } /* OK */ /* To check whether the given integer is odd or even */ #include<stdio.h> void main() { int num; clrscr(); printf("\n\tEnter a number: "); scanf("%d",&num); if(num%2 == 0) printf("\n\tThe given number is an EVEN number."); else printf("\n\tThe given number is an ODD number."); getch(); } /* OK */

/* To check whether the given integer is positive or negative */ #include<stdio.h> void main() { int num; clrscr(); printf("\n\tEnter a number: "); scanf("%d",&num); if(num > 0) printf("\n\tThe given number is a POSITIVE number."); else printf("\n\tThe given number is a NEGATIVE number."); getch(); } /* OK */ /* To determine the biggest of the given 3 numbers */ #include<stdio.h> void main() { int a, b, c, big; clrscr(); printf("\n\tEnter three numbers: "); scanf("%d %d %d",&a, &b, &c); if(a > b) big = a; else big = b; if(c > big) big = c; printf("\n\tThe biggest of %d %d %d is: %d",a, b, c, big); getch(); } /* To find the reverse of the given number and finding it is palindrome or not*/ #include"stdio.h" void main() { int num, pnum, r, rev; clrscr(); printf("\n\tEnter a number: "); scanf("%d",&num); pnum=num; rev=0; while(num>0) { r=num%10; num=num/10; rev=(rev*10)+r; } printf("\n\tThe reverse of %d is: %d",pnum,rev);

if(pnum==rev) printf("\n\n\tThe given number is a PALINDROME number."); else printf("\n\n\tThe given number is not a PALINDROME number."); getch(); } /* OK */ /* Prime number */ #include<stdio.h> void main() { int num, i=2, flag = 0; clrscr(); printf("\n\tEnter a number: "); scanf("%d",&num); while(i < num) { if(num%i == 0) flag = 1; i++; } if(flag == 1) printf("\n\tNot a PRIME Number."); else printf("\n\tPRIME Number"); getch(); } /* Prime numbers in the given range */ #include<stdio.h> void main() { int num, i=2, j=2, flag = 0; clrscr(); printf("\n\tEnter a number: "); scanf("%d",&num); if(num > 0) { printf("\n\tThe PRIME Numbers are: \n\n\t"); while(i <= num) { j = 2; flag = 0; while(j < i) { if(i%j == 0) flag = 1; j++; }

if(flag==0) printf(" %d",i); i++; } } else printf("\n\tInvalid Range"); getch(); } /* OK */ /* To display some range of numbers in a different formats */ #include"stdio.h" void main() { int i,j,k=1; clrscr(); printf("\n\n\t"); for(i=1;i<=5;i++) { for(j=1;j<=i;j++) { printf(" %d ",k); k++; } printf("\n\t"); } printf("\n\n\t"); for(i=1;i<=5;i++) { k=0; for(j=1;j<=i;j++) { k++; printf(" %d ",k); } printf("\n\t"); } k=0; printf("\n\n\t"); for(i=1;i<=5;i++) { k++; for(j=1;j<=i;j++) { printf(" %d ",k); } printf("\n\t");

} k=6; printf("\n\n\t"); for(i=5;i>=0;i--) { --k; for(j=1;j<=i;j++) { printf(" %d ",k); } printf("\n\t"); } printf("\n\n\t"); for(i=5;i>=1;i--) { for(j=0;j<i;j++) { printf(" %d ",i-j); } printf("\n\t"); } getch(); } /* OK */ /* To find the sum of the given negative number and positive numbers.*/ #include"stdio.h" #include"conio.h" void main() { int num[10], psum=0, nsum=0, i, n; float avg; clrscr(); printf("\n\tHow many numbers? "); scanf("%d",&n); printf("\n\n\tEnter those %d numbers ",n); for(i=0;i<n;i++) { printf("\n\n\tEnter %d element: ",i+1); scanf("%d",&num[i]); } for(i=0;i<n;i++) { if(num[i]>=0) { psum=psum+num[i]; }

else { nsum=nsum+num[i]; } } printf("\n\n\tThe sum of positive numbers is: %d ",psum); printf("\n\n\tThe sum of negative numbers is: %d ",nsum); avg=(float) (psum+nsum)/n; printf("\n\n\tThe average of positive & negative numbers is: %2f ",avg); getch(); } /* A program to find the rank and position of an array element in the given array list.*/ #include"stdio.h" void main() { int a[10]; int i, n, element; clrscr(); printf("\n\n\tEnter the array size: "); scanf("%d",&n); printf("\n\n\tEnter those %d elements.\n",n); for(i=0;i<n;i++) scanf("%d",&a[i]); printf("\n\n\tEnter the element to find position & rank: "); scanf("%d",&element); for(i=0;i<n;i++) { if(element==a[i]) { printf("\n\n\tThe rank of the element \"%d\" is: %d",element,i); printf("\n\n\tThe element \"%d\" is found at %d position.",element,i+1); } } printf("\n\n\tpress any key to continue...."); getch(); } /* OK */ /* GCD and LCM of any two given integers*/ #include"stdio.h" int gcd(int, int); void main() { int a, b,lcm, g, p; clrscr(); printf("\n\n\tEnter any two integers: "); scanf("%d %d",&a, &b); if(a==0 && b==0) printf("\n\n\tGCD and LCM cannot be determined."); else

{ p=abs(a*b); if(a==0) { p=b; printf("\n\n\tThe GCD and LCMis: %d %d",abs(b), p); } else if(b==0) { p=a; printf("\n\n\tThe GCD and LCMis: %d %d",abs(a), p); } else g=gcd(a, b); lcm=p/g; printf("\n\n\tThe GCD and LCMis: %d %d",g, lcm); } getch(); } int gcd(int m, int n) { int x=abs(m), y=abs(n); if(x>y) { if(x%y==0) return(y); else gcd(y, x%y); } else if(y>x) { if(y%x==0) return(x); else gcd(x, y%x); } } /* OK */ /* To determine the sin(x) value */ #include"stdio.h" int pow(int num, int x); int fact(int num); void main() { int n, value=0, y; clrscr();

printf("\n\n\tEnter the x value: "); scanf("%d",&y); // printf("\n\n\tEnter the exponent value: "); // scanf("%d",&n); n=y; printf("\n\n\tThe value of sin(x) is: "); value=1+(pow(n, y)/fact(n)); printf("%d",value); getch(); } /* A function to calculate power */ int pow(int num, int x) { int p; if(num>=0) { p=x*pow(num-2, x); } return(p); } int fact(int num) { int f=1; if(num>=0) { f=num*fact(num-2); } return(f); } /* Second Largest & Second Smallest*/ #include"stdio.h" void main() { int a[10], i, n, temp, j; clrscr(); printf("\n\n\tHow many array elements? "); scanf("%d",&n); printf("\n\n\tEnter those array elements."); for(i=0;i<n;i++) scanf("%d",&a[i]); /* loop to find second largest and second smallest element */ for(i=0;i<n;i++) for(j=0;j<n-i;j++) { if(a[j]>a[j+1]) { temp=a[j];

a[j]=a[j+1]; a[j+1]=temp; } } i=n-2; printf("\n\n\tSecond Largest Element: %d ",a[i]); i=1; printf("\n\n\tSecond Smallest Element: %d",a[i]); getch(); } /* OK */ /* To delet the repeated occurences of and make the array elements unique.*/ #include"stdio.h" #include"stdlib.h" void main() { int a[10]; int n, i, j, temp; clrscr(); printf("\n\n\tEnter the array range: "); scanf("%d",&n); printf("\n\n\tEnter those %d array elements,\n\t",n); for(i=0;i<n;i++) { scanf("%d",&a[i]); } for(i=0;i<n;i++) for(j=0;j<i;j++) { if(a[j]>a[j+1]) { temp=a[j]; a[j]=a[j+1]; a[j+1]=temp; } } for(i=0;i<n;i++) { for(j=1;j<=n;j++) { if(a[i]==a[j]) { a[i]=' '; } } } printf("\n\n\tThe array elements are: "); for(i=0;i<n;i++)

printf(" %d ",a[i]); getch(); } /* To display the number of occurences and positions of occurence of an element in an array*/ #include"stdio.h" void main() { int a[10]; int n, i, ele, count=0, flag=0; clrscr(); printf("\n\n\tEnter the array range: "); scanf("%d",&n); printf("\n\n\tEnter those %d array elements,\n\t",n); for(i=0;i<n;i++) scanf("%d",&a[i]); printf("\n\tEnter an element to be find: "); scanf("%d",&ele); printf("\n\n\tThe given element %d is found at :",ele,"\tposition."); for(i=0;i<n;i++) { if(ele==a[i]) { flag=1; count++; printf("%d ",i+1); } else flag=0; } if(flag==0) printf("\n\n\tThe given element %d is NOT found.",ele); else printf("\n\n\tThe given element %d is occured %d times.", ele, count); getch(); } /* OK */ /*Transpose of a given matrix*/ #include"stdio.h" #include"conio.h" void main() { int a[4][4],b[4][4]; int i,j,n; clrscr(); printf("\n\tTranspose of a given matrix."); printf("\n\n\tEnter the order of the matrix: "); scanf("%d",&n); printf("\n\tEnter all the elements of the matrix.\n");

for(i=0;i<n;i++) for(j=0;j<n;j++) { scanf("%2d",&a[i][j]); } printf("\n\tThe transpose of the given matrix is,\n "); for(i=0;i<n;i++) for(j=0;j<n;j++) { b[i][j]=a[j][i]; } for(i=0;i<n;i++) { for(j=0;j<n;j++) { printf("%2d ",b[i][j]); } printf(" \n "); } printf("\t"); textcolor(161); cprintf("\n\npress any key to continue...."); getch(); } /* OK */ /* Scalar Matrix Unit Matrix */ #include"stdio.h" #include"stdlib.h" void main() { int i, j, a[4][4], sum1=0, sum2=0; clrscr(); printf("\n\n\tEnter the matrix elements: \n"); for(i=0;i<3;i++) for(j=0;j<3;j++) scanf("%d",abs(a[i][j])); for(i=0;i<3;i++) for(j=0;j<3;j++) { sum1=sum1+a[i][j]; } for(i=0;i<3;i++) for(j=0;j<3;j++) { if(a[i]==a[j]) // diagonal addition. sum2=sum2+a[i][j]; }

if(sum1==0) printf("\n\n\tThe given matrix is a null matrix."); else { if(sum1==sum2) { if(sum1==3) printf("\n\n\tThe given matrix is a Unit Matrix."); } if(sum1==sum2) printf("\n\n\tThe given matrix is a Scalar Matrix."); else printf("\n\n\tThe given matrix is not a Scalar Matrix."); } getch(); } /* A matrix problem to find row vice, column vice, and sum of all elements in the given matrix.*/ #include"stdio.h" void main() { int a[5][5], c=0; int i, j, m, n; clrscr(); printf("\n\tEnter order of a matrix: "); scanf("%d %d",&m, &n); printf("\n\n\tEnter the A matrix elements:\n"); /* A loop to accept matrix elements */ for(i=0;i<m;i++) for(j=0;j<n;j++) { scanf("%d",&a[i][j]); } printf("\n\n\tThe row vice addition is: \n"); /* loop to add elements in row vice order */ for(i=0;i<m;i++) { c=0; for(j=0;j<n;j++) { c=c+a[i][j]; } printf("\n\n\tIn %d row: %d",i+1, c); } printf("\n\n\n\tThe column vice addition is: \n"); /* loop to add elements in column vice */ for(i=0;i<m;i++) { c=0; for(j=0;j<n;j++)

{ c=c+a[j][i]; } printf("\n\n\tIn %d column: %d",i+1, c); } printf("\n\n\n\tThe sum of all elements is: "); /* loop to add all elements */ c=0; for(i=0;i<m;i++) { for(j=0;j<n;j++) { c=c+a[i][j]; } } printf("%d",c); getch(); } /* OK */ /* A program to check whether the given string is PALINDROME or NOT #include<stdio.h> #include<string.h> void main() { int flag=0, i, count = 0, j = -1, cou= 1; char str[20], b[20]; clrscr(); printf("\n\n\tEnter a string: "); scanf("%s",str); count = strlen(str); for(i=count; i>0; i--) { j++; b[j] = str[i]; } j = 0; for(i=0; i<count; i++) { ++j; if(b[j]==str[i]) cou++; } if(cou == count) flag = 1; if(flag == 1) printf("\n\n\tThe given string is a \"PALINDROME\"."); else printf("\n\n\tThe given string is \"NOT PALINDROME\".");

*/

getch(); } /* OK */ #include"stdio.h" void main() { int count=0; char ch, *st1, *st2; clrscr(); printf("\n\n\tEnter a string: "); scanf("%s",st1); while(*st1!='\0') { count++; st1++; } printf("\n\n\tThe length of the string is: %d",count); getch(); } /* OK */ #include"stdio.h" #include"string.h" #include<conio.h> void main() { char *st2, *st1, *st3; clrscr(); printf("\n\n\tEnter a string: "); scanf("%s",st1); fflush(stdin); st3=st1; printf("\n\tThe reverse of the given string is: "); while((*st2++=*st1++)!='\0'); while(*st3--!='\0') { *st2--; printf("%s",st2); } getch(); }

#include"stdio.h" #include"string.h" void main() { int i; char st[50], str[50], *st1, *st2, *st3;

clrscr(); printf("\n\n\tEnter a string: "); scanf("%s",st); i=strlen(st); st1=st; st2=str; while((*st2++=*st1++)!='\0'); printf("\n\n\tThe copied string is: %s",str); fflush(stdin); while(i>=0) { st1--; *st3=*st1; st3++; i--; } printf("\n\n\tThe reverse of the given string is: %s",st3); getch(); } /* Convertion from uppercase to lowercase & viceversa. */ #include"stdio.h" #include"ctype.h" void main() { char a[25], i, j; clrscr(); printf("\n\n\tEnter a sentence: \n\n\t"); for(i=0;i<='\n';i++) scanf("%c",&a[i]); flushall(); printf("\n\n\tThe converted sentence is: \n\n\t"); for(i=0;i<='\n';i++) { if(islower(a[i])) { printf("%c",a[i]-=32); } else { if(isupper(a[i])) printf("%c",a[i]+=32); } } getch(); } /* OK */ /* Read a sentence and count the number of vowells and consonent present in it*/ #include"stdio.h"

#include"ctype.h" void main() { int vcou=0, ccou=0; char *str, i; clrscr(); printf("\n\n\tEnter a sentence.\n"); scanf("%[^\n]",str); while(*str!='\0') { switch(tolower(*str)) { case 'a': case 'e': case 'i': case 'o': case 'u': vcou++; break; default : ccou++; break; } str++; } printf("\n\n\tThe number of vowels are: %d",vcou); printf("\n\n\tThe number of consonent are: %d",ccou); getch(); } /* OK */ /* Arranging N names in ascending order. */ #include"stdio.h" #include<string.h> void main() { int i, j, n; char a[15][15], *temp; clrscr(); printf("\n\tHow many names? "); scanf("%d",&n); printf("\n\n\tEnter those %d names: \n",n); for(i=0;i<n;i++) scanf("%s",a[i]); printf("\n\n\tThe sorted order is: "); for(i=0;i<n;i++) for(j=0;j<i;j++) { if(strcmp(a[i], a[j]) < 0) { strcpy(temp, a[i]); strcpy(a[i], a[j]);

strcpy(a[j], temp); } } for(i=0;i<n;i++) printf("\n%s",a[i]); getch(); } /* Structure implementation on program num : 9 */ #include<stdio.h> struct Student { int reg_no; int marks; }stu[10]; void main() { int i, n; int per=0; clrscr(); printf("\n\tHow many Students? "); scanf("%d", &n); printf("\n\tEnter those %d students register number & marks of five subjects marks",n); for(i=0; i<n; i++) { printf("\n\tEnter register number: "); scanf("%d", &stu[i].reg_no); printf("\n\tEnter their marks: "); scanf("%d", &stu[i].marks); } printf("\n\tRegister number \tMarks\t\tGrade:\n "); for(i=0; i<n; i++) { per = stu[i].marks/5; printf("\n\t%d", stu[i].reg_no); printf("\t\t\t%d", stu[i].marks); if(per >= 75) printf("\t\tDistinction\n"); else { if(per >= 60 && per <= 74) printf("\t\tA\n"); else { if(per >= 40 && per <= 59) printf("\t\tB\n"); else { if(per < 39) printf("\t\tFail\n");

} } } } getch(); } /* Swaping using Call by reference method */ #include<stdio.h> void swap(int *, int *); void main() { int a, b; clrscr(); printf("\n\tEnter two numbers: "); scanf("%d %d",&a, &b); printf("\n\ta= %d",a); printf("\n\tb= %d",b); swap(&a, &b); printf("\n\n\n\ta= %d",a); printf("\n\tb= %d",b); getch(); } void swap(int *x, int *y) { int t; t = *x; *x = *y; *y = t; } /* A program to swap two elements without using a temporary variebles.*/ #include"stdio.h" void main() { int a, b; clrscr(); printf("\n\n\tEnter the value of A: "); scanf("%d",&a); printf("\n\n\tEnter the value of B: "); scanf("%d",&b); b=b-a; a=a+b; b=a-b; printf("\n\n\tAfter swaping the value of A: %d, and B: %d",a,b); getch(); } /* To find NCR using recursive functions*/ #include"stdio.h"

int factn(int x); int factr(int y); void main() { int r, n, ncr, f1, f2, f3; clrscr(); printf("\n\n\tEnter the value for N and R: "); scanf("%d %d",&n, &r); f1=factn(n); f2=factn(n-r); f3=factr(r); ncr=f1/(f2*f3); printf("\n\n\tThe nCr value of %d and %d is: %d",n, r, ncr); getch(); } int factn(int x) { int m=1; if(x>0) { m=x*factn(x-1); } return(m); } int factr(int y) { int n=1; if(y>0) { n=y*factr(y-1); } return(n); } /* A program to find the x^y by making use of recursion function.*/ #include"stdio.h" int power(int, int); void main() { int x,y,p,i; clrscr(); printf("\n\tEnter mantisa and radix: "); scanf("%d %d",&x,&y); /* p=x; for(i=1;i<y;i++) { p=p*x;

} */ p=power(x,y); printf("\n\t%d raised to %d is: %d",x,y,p); getch(); } int power(int m, int n) { int k=m; if(n>1) { k=k*power(m, n-1); } return(k); } /* Largest of three numbers using conditional operators */ #include"stdio.h" #include"stdlib.h" #include"conio.h" void main() { int num1,num2,num3,l1,l,large; clrscr(); printf("\n\tEnter three numbers: "); scanf("%d %d %d",&num1,&num2,&num3); /* if(num1>num2) l1=num1; else l1=num2; if(num3>l1) l=num3; else l=l1; */ l1=num1>num2? num1:num2; l=num3>l1? num3:l1; printf("\n\n\tThe largest of %d, %d, & %d, is: %d",num1,num2,num3,l); getch(); } /* A program to find the maximum and minimum array elements using pointer to array concept.*/ #include"stdio.h" void main() { int *a[10], n, i, j, max, min; clrscr(); printf("\n\n\tHow many elements? "); scanf("%d",&n); printf("\n\n\tEnter those array elements: \n");

for(i=0;i<n;i++) { scanf("%d",a[i]); } for(i=0;i<n;i++) { max=*a[i]; for(j=0;j<n;j++) { if(*a[j]>max) max=*a[j]; } } for(i=0;i<n;i++) { min=*a[i]; for(j=0;j<n;j++) { if(*a[j]<min) min=*a[j]; } } printf("\n\n\tThe maximum array element in the given array is: %d",max); printf("\n\n\tThe minimum array element in the given array is: %d",min); getch(); } /* Symmetric Matrix */ #include<stdio.h> void main() { int Matrix[4][4]; int i, j, Sum=0, Sum1=0; clrscr(); printf("\n\tEnter a 3*3 matrix elements:\n"); for(i=0; i<3; i++) for(j=0; j<3; j++) { scanf("%d",&Matrix[i][j]); } for(i=0; i<3; i++) for(j=0; j<3; j++) { if(i != j) { if(i > j) Sum += Matrix[i][j]; else Sum1 += Matrix[i][j]; }

} if(Sum == Sum1) printf("\n\tSymmetric Matrix"); else printf("\n\tNot a Symmetric Matrix"); printf("\n\n\tpress any key to continue...."); getch(); } /* Skew-Symmetric Matrix */ /* CREATED : 28th Mar :06 */ #include<stdio.h> void main() { int Matrix[4][4]; int i, j, Sum=0, Sum1=0; clrscr(); printf("\n\tEnter a 3*3 matrix elements:\n"); for(i=0; i<3; i++) for(j=0; j<3; j++) { scanf("%d",&Matrix[i][j]); } for(i=0; i<3; i++) { for(j=0; j<3; j++) { if((i==0 && j!=2) || (i==1 && j!=1) || (i==2 && j!=0)) { if(i > j) Sum += Matrix[i][j]; else Sum1 += Matrix[i][j]; } } } if(Sum == Sum1) printf("\n\tSkew-Symmetric Matrix"); else printf("\n\tNot a Skew-Symmetric Matrix"); printf("\n\n\tpress any key to continue...."); getch(); } /* A program of file handling using fgetc and fputc function */ #include<stdio.h> void main() { FILE *fp; char ch;

clrscr(); fp = fopen("file.dat","a"); while((ch = getchar()) != '\n') { fputc(ch, fp); } fclose(fp); getch(); } /* A program of file handling using fgetc and fputc function */ #include<stdio.h> void main() { FILE *fp; char ch; clrscr(); fp = fopen("file.dat","r"); while(!feof(fp)) { ch = fgetc(fp); printf("%c", ch); } fclose(fp); getch(); } /* A program of file handling using fwrite and fread function */ #include<stdio.h> struct Employee { int id; char name[15]; char ch1; } emp; void main() { FILE *fp, *fp1; char ch; clrscr(); fp = fopen("file1.dat","r"); do { // scanf("\n\t%c", &emp.ch1); printf("\n\n\tEnter identity number: "); scanf("%d", &emp.id); printf("\n\tEnter name: "); scanf("%s", &emp.name); printf("\n\tDo you want to continue? y/n "); } while(getch() != 'n'); fwrite(&emp, sizeof(emp), 1, fp);

fp1 = fopen("file2.dat","r"); while(!feof(fp)) { ch = fread(&emp, sizeof(emp), 1, fp); fprintf(fp1,"%c",ch); } fclose(fp); fclose(fp1); getch(); } /* A program of file handling using fgets and fputs function */ #include<stdio.h> void main() { FILE *fp; char ch[50]; clrscr(); fp = fopen("file11.dat","r"); printf("\n\tEnter a string: "); gets(ch); fputs(ch, fp); /* fputs Function */ fclose(fp); getch(); } /* A program of file handling using fgets and fputs function */ #include<stdio.h> void main() { FILE *fp; char ch[50]; clrscr(); fp = fopen("file11.dat","r"); while(!eof(fp)) { printf("%s", fgets(ch, fp)); } fclose(fp); getch(); } /* Creation of Singly linked list and display the same. */ #include"stdio.h" #include"stdlib.h" #include"alloc.h" struct LinkedList { int empid; char empname[25]; struct LinkedList* link; };

typedef struct LinkedList node; void createlist(node*); void display(node*); void main() { node* start; clrscr(); start= (node*)malloc(sizeof(node)); createlist(start); printf("\n\n\tThe contents of the list is:\n\n\t"); display(start); getch(); } void createlist(node* n) { char choice; printf("\n\tEnter the employee ID: "); scanf("%d",&n->empid); fflush(stdin); printf("\n\tEnter the employee name: "); scanf("%s",&n->empname); fflush(stdin); printf("\n\tDo you want to continue?"); scanf("%c",&choice); if(choice=='y') { n->link=(node*)malloc(sizeof(node)); createlist(n->link); } else { n->link=NULL; } } void display(node* n) { if(n!=NULL) { if(n->link!=NULL) printf("%d %s->",n->empid, n->empname); else printf("%d %s->NULL",n->empid, n->empname); display(n->link); } }

#include"stdio.h"

#include"string.h" void main() { char str[15], str1[15], str2[15]; clrscr(); printf("\n\tEnter first string: "); scanf("%s",str); printf("\n\tEnter second string: "); scanf("%s",str1); printf("\n\n\tSTRING OPERATIONS.\n\t-----------------"); printf("\n\n\t1. The length of the string '%s' is : %d",str,strlen(str)); printf("\n\n\t2. The comparision of two given strings is: %d",strcmp(str, str1)); printf("\n\n\t3. The concatnation of the given two strings is: %s",strcat(str, str1)); printf("\n\n\t4. The uppercase of the given string is: %s",strupr(str)); printf("\n\n\t5. The lowercase of the given string is: %s",strlwr(str)); printf("\n\n\t6. The reverse of the given two strings is: %s",strrev(str)); printf("\n\n\t7. The copy of the given two strings is: %s",strcpy(str, str1)); printf("\n\n\t8. The strstr function yields: %s",strstr(str, str1)); printf("\n\n\t9. Character where strings intersect is at position %d\n",strcspn(str,str1)); printf("\n\t10. The transformation of some part of the string is: %d",strxfrm(str2,str1, 3)); getch(); }

#include"stdio.h" #include"math.h" void main() { int num1, num2; float i=5.6; clrscr(); printf("\n\n\tEnter two numbers: "); scanf("%d %d",&num1, &num2); printf("\n\n\tMATHMATICAL OPERATIONS.\n\t----------------------"); printf("\n\n\t1. The number %d raised to %d is: %f",num1,num2,pow(num1,num2)); printf("\n\n\t2. The square root of %d is: %f",num1,sqrt(num1)); printf("\n\n\t3. The logarithmic value of %d is: %f",num2,log(num2)); printf("\n\n\t4. The sin value of %d is: %f",num1,sin(num1)); printf("\n\n\t5. The cos value of %d is: %f",num2,cos(num2)); printf("\n\n\t6. The exponential value of %d is: %f",num1,exp(num1)); printf("\n\n\t7. The absolute value of %2f is: %d",i,abs(i)); printf("\n\n\t8. The floor value of %2f is: %2f",i,floor(i)); i=5.2; printf("\n\n\t9. The ceil value of %2f is: %2f",i,ceil(i)); i=1; printf("\n\n\t10. The tangent value of %f is: %f",i,tan(i)); getch(); }

/* 10 Character Functions*/ #include<stdio.h> #include<ctype.h> void main() { int num; char ch1, ch2, ch3; clrscr(); printf("\n\tEnter number: "); scanf("%d",&num); if(isdigit(num)) printf("\n\tYou entered a number.\n"); printf("\n\tEnter a character: "); scanf("%c",&ch1); if(isalpha(ch1)) printf("\n\tYou entered an alphabet.\n"); if(islower(ch1)) printf("\n\tThe respective convertion is: %c",toupper(ch1)); else if(isupper(ch1)) printf("\n\tThe respective convertion is: %c",tolower(ch1)); printf("\n\tThe respective convertion to ascii is: %d",toascii(ch1)); getch(); } /* To reverse the stored array elements./ #include"stdio.h" void main() { int a[10], n, i; clrscr(); printf("\n\n\tEnter the array range: "); scanf("%d",&n); printf("\n\tEnter those %d array elements.\n\n\t",n); for(i=0;i<n;i++) { scanf("%d",&a[i]); } printf("\n\n\tThe given array elements in reverse order is: \n\n\t"); for(i=n-1;i>=0;i--) { printf("%d ",a[i]); } getch(); } /* Sparce Matrix Representation. */ #include"stdio.h" void main() {

int a[10][10], i, j, n; clrscr(); printf("\n\tEnter the size of the array: "); scanf("%d", &n); printf("\n\tEnter the elemnts of a SPARSE matrix:\n"); for(i=0; i<n; i++) for(j=0; j<n; j++) { scanf("%d",&a[i][j]); } printf("\n\tRows\tColumns\tValue\n"); for(i=0; i<n; i++) for(j=0; j<n; j++) { if(a[i][j] > 0 && i != i-1) { printf("\n\n\t%d\t%d\t%d", i+1, j+1, a[i][j]); } } getch(); } /* A program to convert the given DECIMAL numbers into respective BINARYdigits./

#include"stdio.h" void main() { int num, num1, a[10], n=0, i, nu; clrscr(); printf("\n\n\tEnter a decimal number: "); scanf("%d",&num); nu=num; while(num>=1) { n++; num1=num%2; if(num1==1) { a[n]=1; // printf("%d",a[n]); } else { a[n]=0; // printf("%d",a[n]); } num/=2; }

printf("\n\tThe Binary digits of %d is: ",nu); for(i=n;i>0;i--) printf("%d",a[i]); getch(); } /* A program to convert the given binary digits into equivalent decimal number*/ #include"stdio.h" #include"math.h" void main() { int bd, bd1, i=-1, dn=0, n; clrscr(); printf("\n\n\tEnter a Binary Digits: "); scanf("%d",&bd); bd1=bd; while(bd>0) { n=bd%10; printf("\n\t%d",bd); if(n==1) { i++; dn=dn+pow(2,i); } else { i++; } bd/=10; } printf("\n\n\tThe Decimal equivalent of \"%d\" is: \"%d\"",bd1,dn); getch(); } /* A program to check whether the given number is a PERFECT number or not.*/ #include"stdio.h" void main() { int num1, num2, r1, r2, n1=0, n2=0; clrscr(); printf("\n\n\tEnter a number: "); scanf("%d",&num1); num2=num1; while(num1>0) { r1=num1%10; num1=num1/10; n1=n1+r1; }

while(num2>0) { r2=num2%10; num2=num2/10; n2=n2+(r2*r2); } if(n1==n2) printf("\n\n\tThe entered number is a PERFECT Number."); else printf("\n\n\tThe entered number is NOT a PERFECT Number."); printf("\n\n\tpress any key to continue...."); getch(); } /* To check whether the given number is an armstrong number or not.*/ #include"stdio.h" void main() { int num, n1, n2=0, n, n3=0; clrscr(); printf("\n\n\tEnter a number: "); scanf("%d",&num); n=num; do { n3=n%10; n2=(n2+(n3*n3*n3)); printf("\n\tIn n2 the value is: %d ",n2); n/=10; } while(n>=1); if(num==n2) printf("\n\n\tThe given number \"%d\" is an \"ARMSTRONG\" Number.",num); else printf("\n\n\tThe given numbr \"%d\" is NOT an \"ARMSTRONG\" Number.",num); getch(); } /* Tower's of Hanoi Problem */ #include"stdio.h" int towers(int, char, char , char); void main() { int d=3, count=0; char A, B, C; clrscr(); count=towers(d, 'A', 'B', 'C'); printf("\n\n\n\tThe number of steps involved is: %d",count); printf("\n\n\tpress any key to continue..."); getch(); }

/* Recursive Function int towers(int m, char x, char y, char z) { static int count=0; if(m>0) { count++; towers(m-1, x, z, y); printf("\n\n\tThe disk %d is moved from peg %c to peg %c", m, x, y); towers(m-1, z, y, x); } return(count); } /* Sum of digits of a given number */ #include"stdio.h" void main() { int num,num1,r=0; clrscr(); printf("\n\tEnter a number: "); scanf("%d",&num); num1=num; while(num>0) { r=r+num%10; num=num/10; } printf("\n\tThe sum of digits of %d is: %d",num1,r); getch(); } /* A program to condence a given number into a single digit.*/ #include"stdio.h" void main() { long int num, c=0, r, r1, c1=0, num1; clrscr(); printf("\n\n\tEnter a number: "); scanf("%ld",&num); num1=num; while(num>0) { r=num%10; num=num/10; c=c+r; } if(c>10) {

*/

while(c>0) { r1=c%10; c=c/10; c1=c1+r1; } printf("\n\n\tThe condenced form of the given number %ld is: %ld",num1,c1); } else printf("\n\n\tThe condenced form of the given number %ld is: %ld",num1,c); printf("\n\n\tpress any key to continue...."); getch(); } /* A program to find the determinent of the given 3*3 matrix elements.*/ #include"stdio.h" void main() { int a[4][4], i, j, det=0, det1=0, det2=0, det3=0; clrscr(); printf("\n\n\tEnter a matrix elements: \n"); for(i=0;i<3;i++) for(j=0;j<3;j++) { scanf("%d",&a[i][j]); } det1=a[0][0]*((a[2][2]*a[1][1]-a[2][1]*a[1][2])); det2=a[0][1]*((a[2][2]*a[1][0]-a[2][0]*a[1][2])); det3=a[0][2]*((a[2][1]*a[1][0]-a[2][0]*a[1][1])); det=det1-det2+det3; printf("\n\n\tThe determinent of the given matrix is: %d",det); printf("\n\n\tpress any key to continue...",getch()); } /* A program to illustrate the pointers to array. */ #include<stdio.h> void main() { int *sp[3][3], i, j; clrscr(); printf("\n\tEnter a matrix elements: \n"); for(i=0; i<3; i++) for(j=0; j<3; j++) scanf("%d", &sp[i][j]); printf("\n\tYour matrix elements are: \n"); for(i=0; i<3; i++) { for(j=0; j<3; j++) { printf(" %d", sp[i][j]); }

printf("\n"); } getch(); } /* A program to print upper right and lower left triangle elements of a matrix./ #include<stdio.h> void main() { int a[4][4], i, j; clrscr(); printf("\n\tEnter a matrix elements: \n"); for(i=0; i<3; i++) for(j=0; j<3; j++) { scanf("%d", &a[i][j]); } printf("\n\tThe upper right elements are: \n"); for(i=0; i<3; i++) for(j=0; j<3; j++) { if(j > i) { printf(" %d", a[i][j]); } } printf("\n\n\n\tThe lower left elements are: \n"); for(i=0; i<3; i++) for(j=0; j<3; j++) { if(i > j) { printf(" %d", a[i][j]); } } getch(); } /* A program to illustrate the array of pointers */ #include<stdio.h> void main() { int *a[10], i, n; clrscr(); printf("\n\tEnter the range of the array: "); scanf("%d", &n); printf("\n\tEnter those array elements:\n"); for(i=0; i<n; i++)

{ scanf("%d", &a[i]); } printf("\n\tYour array elements are:\n"); for(i=0; i<n; i++) { printf(" %d ", a[i]); } getch(); } /* Salary Slip */ #include<stdio.h> void main() { FILE *fp; long int salary, div; char name[15], br[15], eid[50]; clrscr(); fp = fopen("sal.dat","w"); fflush(stdin); do { printf("\n\t\tEnter the name of an employee: "); scanf("%s",name); printf("\n\t\tEnter his branch: "); scanf("%s",br); printf("\n\t\tEnter division number: "); scanf("%ld",&div); printf("\n\t\tEnter Salary amount: "); scanf("%ld",&salary); printf("\n\t\tEnter his e-main id: "); scanf("%s",eid); printf("\n\n\t\t\tTOYOTA PVT.CO"); printf("\n\t\t\t#124, St.Peter Street,\n\t\t\tAUSTRALIA."); printf("\n\t\t\t\t\tFone: 21545 225455478"); printf("\n\t\t\t\t\tFax : 454547871127872"); printf("\n\n\tName :\t%s",name); printf("\n\n\tBranch :\t%s",br); printf("\n\n\tDivision :\t%ld",div); printf("\n\n\tPayment :\t%ld",salary); printf("\n\n\tE-mail id :\t%s",eid); printf("\n\n\n\tSignature "); printf("\t\t\t\tSeal & Sign of GMgr"); printf("\n\n\tDo you want to add this to file? y/n "); fflush(stdin); if(getchar() == 'y'); { fprintf(fp,"\n\n\t\t\tTOYOTA PVT.CO"); fprintf(fp,"\n\t\t\t#124, St.Peter Street,\n\t\t\tAUSTRALIA.");

fprintf(fp,"\n\t\t\t\t\tFone: 21545 225455478"); fprintf(fp,"\n\t\t\t\t\tFax : 454547871127872"); fprintf(fp,"\n\n\tName :\t%s",name); fprintf(fp,"\n\n\tBranch :\t%s",br); fprintf(fp,"\n\n\tDivision :\t%ld",div); fprintf(fp,"\n\n\tPayment :\t%ld",salary); fprintf(fp,"\n\n\tE-mail id :\t%s",eid); fprintf(fp,"\n\n\n\tSignature "); fprintf(fp,"\t\t\t\tSeal & Sign of GMgr"); }

printf("\n\n\tDo you want to continue? y/n "); fflush(stdin); } while(getchar()!='n'); fclose(fp); } /* OK */ /* Finding Depreciation of a product. */ #include<stdio.h> void main() { int time, i_cost, f_cost = 0, dep = 0; float rate; clrscr(); printf("\n\tEnter initial cost: "); scanf("%d",&i_cost); printf("\n\tEnter the rate: "); scanf("%f",&rate); printf("\n\tEnter the number of years: "); scanf("%d",&time); dep = (float)(((i_cost * rate)/100) *12) *time; f_cost = i_cost - dep; printf("\n\n\tThe final cost after %d years is: %d",time,f_cost); printf("\n\n\tThe depreciation amount is: %d",dep); getch(); } /* Sales bill generation in Big Bazar using files concepts. */ #include<stdio.h> #include<dos.h> struct dosdate_t d; void main() {

FILE *fp; int items; float amount; char name[25]; clrscr(); fp = fopen("bill.dat","r"); _dos_getdate(&d); printf("\n\tEnter the name of the custmer: "); gets(name); printf("\n\tEnter the number of items purchased: "); scanf("%d",&items); printf("\n\tEnter the total amount: "); scanf("%f",&amount); fprintf(fp,"\n\t\t********************************"); fprintf(fp,"\n\n\t\t\tBig Bazar\n\t\t\tShankarpuram"); fprintf(fp,"\n\t\t\tBANGALORE-560 004\n"); fprintf(fp,"\n\t\t********************************\n"); fprintf(fp,"\n\t\t\t\t\t%d/%d/%d",d.day,d.month,d.year); fprintf(fp,"\n\n\t\tName :\t"); fputs(name,fp); fprintf(fp,"\n\t\tNo.Items :\t%d",items); fprintf(fp,"\n\t\tTotal amount:\t%4.2f",amount); fprintf(fp,"\n\n\n\t\tSignature of Manager:"); printf("\n\n\n\tThese information is currently is written to the file."); fclose(fp); getch(); }

/* Illustration of storage classes.*/ #include"stdio.h" extern int a=5, b=10; void f(); void fs(int ); void main() { static int m=5; clrscr(); printf("\n\n\tA= %d and B= %d",a,b); f(); a=10; b=5; printf("\n\n\tA= %d and B= %d",a,b);

printf("\n\n\tThe value of M in function is: %d",m-=1); fs(m); getch(); } void f() { int x=a, y=b; printf("\n\n\tA=%d, B=%d",x,y); } void fs(int n) { printf("\n\n\tThe value of static M in function is: %d",n); } /* Convertion from uppercase to lowercase & viceversa. */ #include"stdio.h" #include"ctype.h" void main() { char i, ch[15], n; clrscr(); printf("\n\n\tEnter a string\n"); for(i=0;i<='\n';i++) scanf("%c",&ch[i]); printf("\n\tThe converted case is: \n\n"); for(i=0;i<='\n';i++) { if(ch[i]!='\0') { if(islower(ch[i])) printf("%c",toupper(ch[i])); else { if(isupper(ch[i])); printf("%c",tolower(ch[i])); } } } getch(); } /*C PROGRAM TO SEARCH AN ELEMENT IN A LIST OF ELEMENTS USING BINARY SEARCH */ #include<stdio.h> void main() { int list[' ']; int beg,end,mid,i,n,item,found;

clrscr(); printf("ENTER NUMBER OF ELEMENTS IN THE ARRAY :"); scanf("%d",&n); printf("ENTER THE ELEMENTS IN ASCENDING ORDER:"); for(i=0;i<n;i++) scanf("%d",&list[i]); printf("ENTER THE ELEMENT TO BE SEARCHED :"); scanf("%d",&item); beg=0; end=n-1; found=0; while(beg<=end && !found) { mid=(beg+end)/2; if(item==list[mid]) found=1; else if(item<list[mid]) end=mid-1; else beg=mid+1; } if(found) printf("ITEM EXISTS IN THE POSITION %d",mid+1); else printf("ITEM DOES NOT APPEAR"); getch(); } /*ENTER NUMBER OF ELEMENTS IN THE ARRAY :5 ENTER THE ELEMENTS :3 17 29 46 200 ENTER THE ELEMENT TO BE SEARCHED :29 ITEM EXISTS IN THE POSITION 3 ENTER NUMBER OF ELEMENTS IN THE ARRAY :10 ENTER THE ELEMENTS :-78 -10 5 19 22 45 66 110 237 591 ENTER THE ELEMENT TO BE SEARCHED :3 ITEM DOES NOT APPEAR*/

/* C PROGRAM TO CHECK WHETHER A GIVEN INTEGER IS ODD OR EVEN */ #include<stdio.h> void main() { int num; clrscr(); printf("ENTER A NUMBER :"); scanf("%d",&num); if(num%2==0) printf("%d IS AN EVEN NUMBER ",num); else

printf("%d IS AN ODD NUMBER",num); getch(); } /*ENTER A NUMBER :345 345 IS AN ODD NUMBER ENTER A NUMBER :980 980 IS AN EVEN NUMBER*/ /* C PROGRAM TO CONCATENATE TWO STRINGS */ #include<stdio.h> #include<string.h> void main() { char str1[10],str2[10]; clrscr(); printf("ENTER FIRST STRING :"); scanf("%s",str1); printf("ENTER SECOND STRING :"); scanf("%s",str2); printf("CONCATENATED STRING IS %s",strcat(str1,str2)); getch(); } /*ENTER FIRST STRING : GOOD ENTER SECOND STRING : MORNING CONCATENATED STRING IS GOODMORNING ENTER FIRST STRING :LAND /* C PROGRAM TO FIND THE FACTORIAL OF A NUMBER */ #include<stdio.h> void main() { long fact=1; int i,num; clrscr(); printf("ENTER A NUMBER :"); scanf("%d",&num); for(i=1;i<=num;i++) fact*=i; printf("FACTORIAL OF %d=%ld",num,fact); getch(); } /*ENTER A NUMBER :4 FACTORIAL OF 4=24 ENTER A NUMBER :10 FACTORIAL OF 10=3628800*/

/* C PROGRAM TO COPY THE CONTENTS OF ONE FILE TO ANOTHER */ #include<stdio.h> void main() { FILE *fsource,*ftarget; char ch; clrscr(); /*ENTER DATA INTO INPUT*/ printf("ENTER DATA INTO FILE \n"); fsource=fopen("INPUT","w"); while((ch=getchar())!=EOF) putc(ch,fsource); fclose(fsource); fsource=fopen("INPUT","r"); if(fsource==NULL) { printf("CANNOT OPEN FILE"); getch(); exit(); } ftarget=fopen("TEMP","w"); if(ftarget==NULL) { printf("CANNOT OPEN FILE"); getch(); exit(); } /*COPY THE CONTENTS OF INPUT TO TEMP*/ while((ch=getc(fsource))!=EOF) putc(ch,ftarget); fclose(ftarget); printf("\nTHE CONTENTS OF THE FILE COPIED ARE\n\n"); ftarget=fopen("TEMP","r"); while((ch=getc(ftarget))!=EOF) printf("%c",ch); fclose(ftarget); fclose(fsource); getch(); } /* ENTER DATA INTO FILE EVEN THE GREATEST CREATIONS START FROM SMALL SEEDS^Z THE CONTENTS OF THE FILE COPIED ARE EVEN THE GREATEST CREATIONS START FROM SMALL SEEDS

ENTER DATA INTO FILE NOTHING IS PERMANENT BUT CHANGE^Z THE CONTENTS OF THE FILE COPIED ARE NOTHING IS PERMANENT BUT CHANGE*/ /* C PROGRAM TO FIND GCD OF THREE NUMBERS USING USER DEFINED FUNCTIONS*/ #include<stdio.h> void main() { int a,b,c,temp,gcd; clrscr(); printf("ENTER THREE NUMBERS :"); scanf("%d %d %d",&a,&b,&c); temp=gcdf(a,b); gcd=gcdf(temp,c); printf("GCD OF %d %d AND %d IS %d",a,b,c,gcd); getch(); } int gcdf(int x,int y) { while(x!=y) { if(x>y) x-=y; else y-=x; } return(x); } /* ENTER THREE NUMBERS :45 15 120 GCD OF 45 15 AND 120 IS 15 ENTER THREE NUMBERS :67 12 45 GCD OF 67 12 AND 45 IS 1 */ /* C PROGRAM TO FIND GCD AND LCM OF GIVEN TWO NUMBERS */ #include<stdio.h> void main() { int x,y,gcd,lcm,prod; clrscr(); printf("ENTER TWO NUMBERS :");

scanf("%d %d",&x,&y); prod=x*y; while(x!=y) { if(x>y) x-=y; else y-=x; } gcd=x; lcm=prod/gcd; printf("LCM = %d\nGCD = %d",lcm,gcd); getch(); } /*ENTER TWO NUMBERS :13 52 LCM = 52 GCD=13 ENTER TWO NUMBERS :91 42 LCM = 546 GCD=7*/ /* C PROGRAM TO ADD TWO MATRICES */ #include<stdio.h> void main() { int a[' '][' '],b[' '][' '],c[' '][' '],i,j,m,n; clrscr(); printf("ENTER ORDER OF THE MATRIX(m*n) :"); scanf("%d %d",&m,&n); printf("ENTER MATRIX A ELEMENTS :"); for(i=0;i<m;i++) for(j=0;j<n;j++) scanf("%d",&a[i][j]); printf("ENTER MATRIX B ELEMENTS :"); for(i=0;i<m;i++) for(j=0;j<n;j++) { scanf("%d",&b[i][j]); c[i][j]=a[i][j]+b[i][j]; } printf("MATRIX A \n"); for(i=0;i<m;i++) { for(j=0;j<n;j++) printf("\t%d\t",a[i][j]); printf("\n"); }

printf("\nMATRIX B \n"); for(i=0;i<m;i++) { for(j=0;j<n;j++) printf("\t%d\t",b[i][j]); printf("\n"); } printf("\nSUM MATRIX \n"); for(i=0;i<m;i++) { for(j=0;j<n;j++) printf("\t%d\t",c[i][j]); printf("\n"); } getch(); } /* ENTER ORDER OF THE MATRIX(m*n) :2 2 ENTER MATRIX A ELEMENTS :6 4 3 7 ENTER MATRIX B ELEMENTS :9 3 2 1 MATRIX A 6 4 3 7 MATRIX B 9 3 2 1 SUM MATRIX 15 7 5 8 ENTER ORDER OF THE MATRIX(m*n) :2 3 ENTER MATRIX A ELEMENTS :1 4 6 5 4 3 ENTER MATRIX B ELEMENTS :-3 5 4 5 -1 2 MATRIX A 1 4 6 5 4 3 MATRIX B -3 5 4 5 -1 2 SUM MATRIX -2 9 10 10 3 5 */ /* C PROGRAM TO MULTIPLY TWO MATRICES */

#include<stdio.h> void main() { int a[' '][' '],b[' '][' '],c[' '][' '],i,j,k; int arows,acols,brows,bcols,crows,ccols; clrscr(); printf("ENTER ORDER OF FIRST MATRIX (m*n) :"); scanf("%d %d",&arows,&acols); printf("ENTER ORDER OF SECOND MATRIX (n*l) :"); scanf("%d %d",&brows,&bcols); if(acols!=brows) { printf("INVALID MATRICIES "); getch(); exit(); } printf("ENTER ELEMENTS OF FIRST MATRIX \n"); for(i=0;i<arows;i++) for(j=0;j<acols;j++) scanf("%d",&a[i][j]); printf("ENTER ELEMENTS OF SECOND MATRIX \n"); for(i=0;i<brows;i++) for(j=0;j<bcols;j++) scanf("%d",&b[i][j]); crows=arows; ccols=bcols; /*MULTIPLICATION LOOP*/ for(k=0;k<crows;k++) for(i=0;i<ccols;i++) { c[k][i]=0; for(j=0;j<acols;j++) c[k][i]+=a[k][j]*b[j][i]; } clrscr(); printf("MATRIX A\n"); for(i=0;i<arows;i++) { for(j=0;j<acols;j++) printf("%d\t",a[i][j]); printf("\n"); } printf("MATRIX B\n"); for(i=0;i<brows;i++) { for(j=0;j<bcols;j++) printf("%d\t",b[i][j]);

printf("\n"); } printf("PRODUCT MATRIX \n"); for(i=0;i<crows;i++) { for(j=0;j<ccols;j++) printf("%d\t",c[i][j]); printf("\n"); } getch(); } /* ENTER ORDER OF FIRST MATRIX (m*n) :3 3 ENTER ORDER OF SECOND MATRIX (n*l) :2 2 INVALID MATRICIES ORDER OF FIRST MATRIX (m*n) :3 3 ENTER ORDER OF SECOND MATRIX (n*l) :3 2 ENTER ELEMENTS OF FIRST MATRIX 9 3 2 6 5 4 2 8 3 ENTER ELEMENTS OF SECOND MATRIX 4 3 8 2 1 4 MATRIX A 9 3 2 6 5 4 2 8 3 MATRIX B 4 3 8 2 1 4 PRODUCT MATRIX 62 41 68 44 75 34 */ /* C PROGRAM TO ILLUSTRATE FILE HANDLING WITH MIXED DATA TYPE */ #include<stdio.h> void main() { FILE *fbank; char accname[20],ch; int accno; float balance; fbank=fopen("BANK1","w"); if(fbank==NULL) { printf("FILE CANNOT BE OPENED"); getch(); exit(); }

else { do { clrscr(); printf("ENTER ACCOUNT NUMBER :"); scanf("%d",&accno); printf("ENTER NAME :"); scanf("%s",accname); printf("ENTER ACCOUNT BALANCE :"); scanf("%f",&balance); fprintf(fbank,"%8d %25s %10.4f",accno,accname,balance); printf("DO YOU WISH TO CONYINUE (Y/N)?"); ch=getche(); }while(ch=='Y'||ch=='y'); fclose(fbank); } if((fbank=fopen("BANK1","r"))!=NULL) { clrscr(); printf("-----------------------------------------------------------------------------\n"); printf(" BANK ACCOUNT DETAILS \n"); printf("-----------------------------------------------------------------------------\n"); printf("ACCOUNT NO NAME BALANCE\n"); while((fscanf(fbank,"%d %s %f",&accno,accname,&balance))!=EOF) printf("%10d %30s %15.2f\n",accno,accname,balance); printf("-----------------------------------------------------------------------------\n"); } fclose(fbank); getch(); } /* ENTER ACCOUNT NUMBER :1 ENTER NAME :GIRISH ENTER ACCOUNT BALANCE :9000.00 DO YOU WISH TO CONYINUE (Y/N)?Y ENTER ACCOUNT NUMBER :2 ENTER NAME :SURESH ENTER ACCOUNT BALANCE :500.00YOU WISH TO CONYINUE (Y/N)?Y ENTER ACCOUNT NUMBER :3 ENTER NAME :PALLAVI ENTER ACCOUNT BALANCE :3500.00 DO YOU WISH TO CONYINUE (Y/N)?N ----------------------------------------------------------------------------BANK ACCOUNT DETAILS ----------------------------------------------------------------------------ACCOUNT NO NAME BALANCE 1 GIRISH 9000.00

2 SURESH 500.00 3 PALLAVI 3500.00 ----------------------------------------------------------------------------*/ /*C PROGRAM TO FIND THE NORM OF A MATRIX */ #include<stdio.h> #include<math.h> void main() { int a[' '][' '],i,j,m,n; float norm=0.0; clrscr(); printf("ENTER THE ORDER OF THE MATRIX(m*n) :"); scanf("%d %d",&m,&n); printf("ENTER THE MATRIX ELEMENTS :"); for(i=0;i<m;i++) for(j=0;j<n;j++) { scanf("%d",&a[i][j]); norm+=a[i][j]*a[i][j]; } printf("\nTHE ENTERED MATRIX IS\n"); for(i=0;i<m;i++) { for(j=0;j<n;j++) printf("\t%d",a[i][j]); printf("\n"); } norm=sqrt(norm); printf("\nNORM OF MATRIX IS %f",norm); getch(); } /*ENTER THE ORDER OF THE MATRIX(m*n) :2 3 ENTER THE MATRIX ELEMENTS :5 6 10 21 8 16 THE ENTERED MATRIX IS 5 6 10 21 8 16 NORM OF MATRIX IS 30.364452 ENTER THE ORDER OF THE MATRIX(m*n) :3 3 ENTER THE MATRIX ELEMENTS :6 -1 4 -13 5 6 7 9 1 THE ENTERED MATRIX IS 6 -1 4 -13 5 6 7 9 1 NORM OF MATRIX IS 20.346991 */

/* C PROGRAM TO CHECK WHETHER A STRING IS A PALINDROME */ #include<stdio.h> #include<string.h> void main() { char str1[20],str2[10]; clrscr(); printf("ENTER A STRING :"); scanf("%s",str1); strcpy(str2,str1); strrev(str2); if(strcmp(str1,str2)==0) printf("\"%s\" IS A PALINDROME ",str1); else printf("\"%s\" IS NOT A PALINDROME ",str1); getch(); } /*ENTER A STRING :MALAYALAM "MALAYALAM" IS A PALINDROME ENTER A STRING :BASEBALL "BASEBALL" IS NOT A PALINDROME*/ /* C PROGRAM TO ADD A SET OF NUMBERS STORED IN AN ARRAY USING POINTERS */ #include<stdio.h> void main() { int n,arr[' '],i,sum=0,*ptr; clrscr(); printf("ENTER THE NUMBER OF ELEMENTS IN ARRAY :"); scanf("%d",&n); printf("ENTER THE ELEMENTS :"); for(i=0;i<n;i++) scanf("%d",&arr[i]); ptr=arr; for(i=0;i<n;i++) { sum=sum+(*ptr); ptr++; } printf("SUM OF THE NUMBERS IS %d",sum); getch(); } /* ENTER THE NUMBER OF ELEMENTS IN ARRAY :10 ENTER THE ELEMENTS :1 2 3 4 5 6 7 8 9 10

SUM OF THE NUMBERS IS 55 ENTER THE NUMBER OF ELEMENTS IN ARRAY :8 ENTER THE ELEMENTS :45 23 44 55 900 478 33 -90 SUM OF THE NUMBERS IS 1488 */ /* C PROGRAM TO GENERATE PRIME NUMBERS */ #include<stdio.h> void main() { int n,p,prime,i,r; clrscr(); printf("ENTER THE UPPER LIMIT FOR GENERATION OF PRIME NUMBERS :"); scanf("%d",&n); printf("\nTHE PRIME NUMBERS ARE\n"); for(p=1;p<=n;p++) { prime=1; /*ASSUME THE NUMBER IS PRIME*/ i=2; while(i<=p-1 && prime==1) { r=p%i; if(r==0) prime=0; i++; } if(prime==1) printf("%d ",p); } getch(); } /*ENTER THE UPPER LIMIT FOR GENERATION OF PRIME NUMBERS :25 THE PRIME NUMBERS ARE 1 2 3 5 7 11 13 17 19 23 ENTER THE UPPER LIMIT FOR GENERATION OF PRIME NUMBERS :60 THE PRIME NUMBERS ARE 1 2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59*/ /* C PROGRAM TO FIND ALL ROOTS OF A QUADRATIC EQUATION */ #include<stdio.h> #include<math.h> void main() { float a,b,c,disc,x,x1,x2,xr,xi,deno;

clrscr(); printf("ENTER THE CO-ORDINATES :"); scanf("%f %f %f",&a,&b,&c); disc=b*b-4*a*c; /* CALCULATING DISCRIMINANT */ deno=2*a; if(disc>0) { disc=sqrt(disc); printf("ROOTS ARE REAL AND DISTINCT\n"); x1=(-b+disc)/deno; x2=(-b-disc)/deno; printf("ROOTS ARE X1=%.4f,X2=%.4f",x1,x2); } else if(disc<0) { disc=sqrt(abs(disc)); xr=-b/deno; xi=disc/deno; printf(" FIRST COMPLEX ROOT IS %.4f+i%.4f",xr,xi); printf("\n SECOND COMPLEX ROOT IS %.4f-i%.4f",xr,xi); } else printf("ROOTS ARE REPEATED X=%.4f",-b/deno); getch(); } /* ENTER THE CO-ORDINATES :3 5 2 ROOTS ARE REAL AND DISTINCT ROOTS ARE X1=-0.6667,X2=-1.0000*/ /* ENTER THE CO-ORDINATES :3 3 5 FIRST COMPLEX ROOT IS -0.5000+i1.1902 SECOND COMPLEX ROOT IS -0.5000-i1.1902*/ /* ENTER THE CO-ORDINATES :1 2 1 ROOTS ARE REPEATED X=-1.0000*/ /* C PROGRAM TO WRITE TO AND READ FROM A FILE */ #include<stdio.h> void main() { FILE *fp; char ch; clrscr(); printf("ENTER DATA INTO FILE \n"); fp=fopen("INPUT","w"); while((ch=getchar())!=EOF) putc(ch,fp); fclose(fp);

printf("\n\nDATA IN THE FILE \n"); fp=fopen("INPUT","r"); while((ch=getc(fp))!=EOF) printf("%c",ch); fclose(fp); getch(); } /* ENTER DATA INTO FILE SMOKING IS INJURIOUS TO HEALTH^Z

DATA IN THE FILE SMOKING IS INJURIOUS TO HEALTH ENTER DATA INTO FILE AN APPLE A DAY KEEPS THE DOCTOR AWAY^Z

DATA IN THE FILE AN APPLE A DAY KEEPS THE DOCTOR AWAY*/

/* C PROGRAM TO REVERSE AN INTEGER NUMBER */ #include<stdio.h> void main() { int num,r,rev=0; clrscr(); printf("ENTER A NUMBER NOT ENDING WITH ZERO scanf("%d",&num); while(num!=0) { r=num%10; rev=rev*10+r; num=num/10; } printf("REVERSE OF THE NUMBER IS %d",rev); getch(); } /* ENTER A NUMBER NOT ENDING WITH ZERO :3546 REVERSE OF THE NUMBER IS 6453 ENTER A NUMBER NOT ENDING WITH ZERO :452 REVERSE OF THE NUMBER IS 254*/

:");

/* C PROGRAM TO CALCULATE ROW SUM AND COLUMN SUM */ #include<stdio.h> int m,n; void main() { int i,j,a[' '][' '],rsum[' '],csum[' ']; clrscr(); printf("ENTER THE ORDER OF THE MATRIX (m*n):"); scanf("%d %d",&m,&n); printf("ENTER THE MATRIX ELEMENTS :"); for(i=0;i<m;i++) for(j=0;j<n;j++) scanf("%d",&a[i][j]); for(i=0;i<m;i++) rsum[i]=rowsum(a,i); for(j=0;j<n;j++) csum[j]=colsum(a,j); printf("ENTERED MATRIX WITH ROW SUMS AND COLUMN SUMS\n"); for(i=0;i<m;i++) { for(j=0;j<n;j++) printf("\t%d",a[i][j]); printf("\t| %d",rsum[i]); printf("\n"); } printf("---------------------------------------------------------------\n"); for(i=0;i<n;i++) printf("\t%d",csum[i]); getch(); } int rowsum(int x[' '][' '],int rnum) { int i,j,rsum=0; for(i=0;i<m;i++) for(j=0;j<n;j++) if(i==rnum) rsum+=x[i][j]; return(rsum); } int colsum(int x[' '][' '],int cnum) { int i,j,csum=0; for(i=0;i<m;i++)

for(j=0;j<n;j++) if(j==cnum) csum+=x[i][j]; return(csum); } /* ENTER THE ORDER OF THE MATRIX (m*n):2 2 ENTER THE MATRIX ELEMENTS :5 6 7 -3 ENTERED MATRIX WITH ROW SUMS AND COLUMN SUMS 5 6 | 11 7 -3 | 4 --------------------------------------------------------------12 3 ENTER THE ORDER OF THE MATRIX (m*n):4 2 ENTER THE MATRIX ELEMENTS :5 9 0 -1 2 -6 8 -3 ENTERED MATRIX WITH ROW SUMS AND COLUMN SUMS 5 9 | 14 0 -1 | -1 2 -6 | -4 8 -3 | 5 --------------------------------------------------------------15 -1 */ /* C PROGRAM TO COMPUTE SIMPLE INTEREST GIVEN PRINCIPLE,RATE AND TIME*/ #include<stdio.h> void main() { float principle,time,rate,sinterest; clrscr(); printf("ENTER PRICIPLE :"); scanf("%f",&principle); printf("ENTER TIME :"); scanf("%f",&time); printf("ENTER RATE OF INTEREST :"); scanf("%f",&rate); sinterest=principle*time*rate/100.0; printf("SIMPLE INTEREST IS %f",sinterest); getch(); } /*ENTER PRICIPLE :3000 ENTER TIME :2 ENTER RATE OF INTEREST :5 SIMPLE INTEREST IS 300.000000 ENTER PRICIPLE :5450 ENTER TIME :3

ENTER RATE OF INTEREST :13 SIMPLE INTEREST IS 2125.500000*/

/* C PROGRAM TO SEARCH AN ELEMENT IN A LIST OF ELEMENTS USING LINEAR SEARCH*/ #include<stdio.h> void main() { int list[25],i,n,found=0,loc=0,item; clrscr(); printf("ENTER NUMBER OF ELEMENTS :"); scanf("%d",&n); printf("ENTER THE ELEMENTS \n"); for(i=0;i<n;i++) scanf("%d",&list[i]); printf("ENTER THE NUMBER TO BE SEARCHED :"); scanf("%d",&item); /*SEARCH LOOP*/ for(i=0;i<n && !found;i++) if(item==list[i]) { found=1; loc=i+1; } if(found) printf("SUCCESSFUL SEARCH\n\nELEMENT FOUND AT LOCATION %d",loc); else printf("UNSUCCESSFUL SEARCH"); getch(); } /*ENTER NUMBER OF ELEMENTS :5THE ELEMENTS 13 76 4 59 10 ENTER THE NUMBER TO BE SEARCHED :4 SUCCESSFUL SEARCH ELEMENT FOUND AT LOCATION 3 ENTER NUMBER OF ELEMENTS :8 ENTER THE ELEMENTS 34 55 28 7894 66 77 3 4 ENTER THE NUMBER TO BE SEARCHED :60 UNSUCCESSFUL SEARCH*/ /* C PROGRAM TO FIND THE VALUE OF SIN(X) USING THE SERIES EXPANSION */ #include<stdio.h> #include<math.h> void main() {

float x,t,sine,sum; int degree,i,deno; clrscr(); printf("ENTER THE VALUE FOR DEGREE :"); scanf("%d",&degree); x=degree*3.14159/180.0; sine=sin(x); printf("VALUE OF SIN(%d)=%f USING BUILT-IN FUNCTION",degree,sine); sum=x; t=x; i=1; while(i<=9) { deno=2*i*(2*i+1); t=-t*x*x/deno; sum+=t; i++; } printf("\nVALUE OF SIN(%d)=%f USING SERIES EXPANSION",degree,sum); getch(); } /*ENTER THE VALUE FOR DEGREE :39 VALUE OF SIN(39)=0.629320 USING BUILT-IN FUNCTION VALUE OF SIN(39)=0.629320 USING SERIES EXPANSION ENTER THE VALUE FOR DEGREE :81 VALUE OF SIN(81)=0.987688 USING BUILT-IN FUNCTION VALUE OF SIN(81)=0.987688 USING SERIES EXPANSION*/ /* C PROGRAM TO SORT A SET OF NUMBERS USING BUBBLE SORT*/ #include<stdio.h> void main() { int num[25],i,j,n,temp; clrscr(); printf("ENTER NUMBER OF ELEMENTS TO BE SORTED :"); scanf("%d",&n); printf("ENTER THE ELEMENTS :"); for(i=0;i<n;i++) scanf("%d",&num[i]); clrscr(); printf("UNSORTED NUMBERS \n"); for(i=0;i<n;i++) printf("%d\t",num[i]); /*SORTING LOOP*/ // for(i=0;i<n;i++)

for(i=1;i<=n-1;i++) { // for(j=0;j<n-i;j++) for(j=0;j<n-i;j++) { if(num[j]>num[j+1]) { temp=num[j]; num[j]=num[j+1]; num[j+1]=temp; } } } printf("\nSORTED NUMBERS \n"); for(i=0;i<n;i++) printf("%d\t",num[i]); getch(); } /*ENTER NUMBER OF ELEMENTS TO BE SORTED :6 ENTER THE ELEMENTS :142 54 38 9 278 845 UNSORTED NUMBERS 142 54 38 9 278 845 SORTED NUMBERS 9 38 54 142 278 845 ENTER NUMBER OF ELEMENTS TO BE SORTED :8 ENTER THE ELEMENTS :73 44 2 458 -76 -9 6 44 UNSORTED NUMBERS 73 44 2 458 -76 -9 6 44 SORTED NUMBERS -76 -9 2 6 44 44 73 458*/ /* C PROGRAM TO SORT A SET OF NUMBERS USING SELECTION SORT */ #include<stdio.h> int n; void main() { void sort(int,int *); int a[' '],i,j; clrscr(); printf("ENTER THE NUMBER OF ELEMENTS :"); scanf("%d",&n); printf("ENTER THE ELEMENTS :"); for(i=0;i<n;i++) scanf("%d",&a[i]); for(i=0;i<n-1;i++) { sort(i,a); for(j=0;j<n;j++) printf("%d\t",a[j]);

printf("\n"); } printf("SORTED ARRAY IS \n"); for(i=0;i<n;i++) printf("%d\t",a[i]); getch(); } void sort(int x,int *y) { int k=x,loc,temp,*t,i,small; t=y; /*SAVE POINTER*/ y=y+x; /*MOVE POINTER TO STARTING LOC OF UNSORTED ARRAY*/ small=*y; /*ASSUME FIRST NUMBER TO BE SMALLEST*/ loc=x; /*AND LOC TO BE LOC OF FIRST NUMBER*/ while(k<n-1) { if(small>*(y+1)) { /*LOOP TO FIND THE SMALLEST AND ITS LOCATION*/ small=*(y+1); loc=k+1; } y++; k++; } /*SWAPPING THE SMALLEST ELEMENT WITH THE 1ST ELEMENT OF THE UNSORTED ARRAY*/ temp=*(t+x); *(t+x)=*(t+loc); *(t+loc)=temp; } /* ENTER THE NUMBER OF ELEMENTS :4 ENTER THE ELEMENTS : 356 4 69 1687 4 356 69 1687 4 69 356 1687 4 69 356 1687 SORTED ARRAY IS 4 69 356 1687 ENTER THE NUMBER OF ELEMENTS : ENTER THE ELEMENTS :78 -4 0 273 83 49 -50 -50 -4 0 273 83 49 78 -50 -4 0 273 83 49 78 -50 -4 0 273 83 49 78 -50 -4 0 49 83 273 78 -50 -4 0 49 78 273 83 -50 -4 0 49 78 83 273

SORTED ARRAY IS -50 -4 0 49 78 83 273*/ /*C PROGRAM TO FIND THE STANDARD DEVIATION */ #include<stdio.h> #include<math.h> #define MAX 25 void main() { int i,n; float value[MAX],dev,sum,sumsqr,mean,variance,stddev; /* dev=DEVIATION ,stddev=STANDARD DEVIATION */ clrscr(); sum=sumsqr=n=0; printf("INPUT VALUES : INPUT -1 TO END\n"); for(i=0;i<MAX;i++) { scanf("%f",&value[i]); if(value[i]==-1) break; sum+=value[i]; n+=1; } mean=sum/(float)n; for(i=0;i<n;i++) { dev=value[i]-mean; sumsqr+=dev*dev; } variance=sumsqr/(float)n; stddev=sqrt(variance); printf("\nNUMBER OF ITEMS = %d\n",n); printf("MEAN = %f\n",mean); printf("STANDARD DEVIATION = %f\n",stddev); getch(); } /* INPUT VALUES : INPUT -1 TO END 56 49 61 60 55 -1 NUMBER OF ITEMS = 5 MEAN = 56.200001 STANDARD DEVIATION = 4.261455 INPUT VALUES : INPUT -1 TO END 78 82 85 76.5 81.15 83.75 79.99 -1 NUMBER OF ITEMS = 7

MEAN = 80.912857 STANDARD DEVIATION = 2.798008*/ /* C PROGRAM TO COMPARE TWO STRINGS USING STRING FUNCTIONS */ #include<stdio.h> #include<string.h> void main() { char str1[20],str2[20]; clrscr(); printf("ENTER FIRST STRING :"); scanf("%s",str1); printf("ENTER SECOND STRING :"); scanf("%s",str2); if(strcmp(str1,str2)==0) printf("THE TWO STRINGS ARE SAME "); else printf("THE TWO STRINGS ARE DIFFERENT "); getch(); } /* ENTER FIRST STRING :ASTROLOGY ENTER SECOND STRING :ASTROLOGY THE TWO STRINGS ARE SAME ENTER FIRST STRING :TAROTCARD ENTER SECOND STRING :TAROTcARD THE TWO STRINGS ARE DIFFERENT*/ /* C PROGRAM TO COMPARE TWO STRINGS WITHOUT USING STRING FUNCTIONS */ #include<stdio.h> #include<conio.h> void main() { char str1[20],str2[20]; clrscr(); printf("ENTER FIRST STRING :"); scanf("%s",str1); printf("ENTER SECOND STRING :"); scanf("%s",str2); if(compare(str1,str2)==0) printf("THE TWO STRINGS ARE SAME "); else printf("THE TWO STRINGS ARE DIFFERENT "); getch(); } int compare(char s1[20],char s2[20])

{ int i=0,flag=0,diff; while(s1[i]!='\0' || s2[i]!='\0') { if(s1[i]!=s2[i]) { flag=1; diff=s1[i]-s2[i]; break; } i++; } if(flag) return(diff); else return(0); } /* ENTER FIRST STRING :MONOPOLY ENTER SECOND STRING :MONOPOLY THE TWO STRINGS ARE SAME ENTER FIRST STRING :HONEYbee ENTER SECOND STRING :HONEYBEE THE TWO STRINGS ARE DIFFERENT */ /* SIMPLE C PROGRAM ON STRUCTURES */ #include<stdio.h> struct complex { float real; float imag; }; void main() { void display(); struct complex x,y,z; char opr; float deno; clrscr(); printf("\n\n\tENTER REAL AND IMAGINARY PART OF FIRST COMPLEX NUMBER :"); scanf("%f %f",&x.real,&x.imag); printf("\n\n\tENTER REAL AND IMAGINARY PART OF SECOND COMPLEX NUMBER:"); scanf("%f %f",&y.real,&y.imag); printf("\n\tENTER OPERATOR(+,-,*,/) :"); opr=getche(); switch(opr) { case '+': printf("\n\tADDITION OF COMPLEX NUMBERS ");

z.real=x.real+y.real; z.imag=x.imag+y.imag; display(z); break; case '-': printf("\n\tSUBTRACTION OF COMPLEX NUMBERS "); z.real=x.real-y.real; z.imag=x.imag-y.imag; display(z); break; case '*': printf("\n\tMULTIPLICATION OF COMPLEX NUMBERS "); z.real=x.real*y.real-x.imag*y.imag; z.imag=x.real*y.imag+x.imag*y.real; display(z); break; case '/': printf("\n\tDIVISION OF COMPLEX NUMBERS "); deno=y.real*y.real+y.imag*y.imag; z.real=(x.real*y.real+x.imag*y.imag)/deno; z.imag=(x.imag*y.real-x.real*y.imag)/deno; display(z); break; default : printf("\n\tINVALID OPERATOR "); } getch(); } void display(struct complex c) { if(c.imag>=0) printf("%.4f+i%.4f",c.real,c.imag); else printf("%.4f-i%.4f",c.real,abs(c.imag)); } /* SIMPLE C PROGRAM ON STRUCTURES */ #include<stdio.h> struct student { char name[20]; int age,m1,m2,m3; float avg; }; void main() { struct student s[5]; int i; clrscr(); printf("ENTER THE DETAILS OF 5 STUDENTS \n"); for(i=0;i<5;i++)

{ printf("ENTER NAME OF THE STUDENT :"); scanf("%s",s[i].name); printf("ENTER AGE OF THE STUDENT :"); scanf("%d",&s[i].age); printf("ENTER MARKS IN THREE SUBJECTS(FOR 100) :"); scanf("%d %d %d",&s[i].m1,&s[i].m2,&s[i].m3); } clrscr(); printf("------------------------------------------------------------------------------\n"); printf(" STUDENT DETAILS\n"); printf("------------------------------------------------------------------------------\n"); for(i=0;i<5;i++) { s[i].avg=(s[i].m1+s[i].m2+s[i].m3)/3.0; if((s[i].m1<35) ||(s[i].m2<35)||(s[i].m3<35)) printf("\n%25s %3d %3d %3d %3d %3.2f FAIL",s[i].name,s[i].age,s[i].m1,s[i].m2,s[i].m3,s[i].avg); else printf("\n%25s %3d %3d %3d %3d %3.2f PASS",s[i].name,s[i].age,s[i].m1,s[i].m2,s[i].m3,s[i].avg); } printf("\n----------------------------------------------------------------------------"); getch(); } /* ENTER THE DETAILS OF 5 STUDENTS ENTER NAME OF THE STUDENT :RAMA ENTER AGE OF THE STUDENT :16 ENTER MARKS IN THREE SUBJECTS(FOR 100) :78 87 81 ENTER NAME OF THE STUDENT :KALA ENTER AGE OF THE STUDENT :17 ENTER MARKS IN THREE SUBJECTS(FOR 100) :65 56 60 ENTER NAME OF THE STUDENT :PRIYA ENTER AGE OF THE STUDENT :18 ENTER MARKS IN THREE SUBJECTS(FOR 100) :34 45 51 ENTER NAME OF THE STUDENT :GEETHA ENTER AGE OF THE STUDENT :16 ENTER MARKS IN THREE SUBJECTS(FOR 100) :72 67 27 ENTER NAME OF THE STUDENT :ANTHONY ENTER AGE OF THE STUDENT :17 ENTER MARKS IN THREE SUBJECTS(FOR 100) :67 75 73 -----------------------------------------------------------------------------STUDENT DETAILS -----------------------------------------------------------------------------RAMA 16 78 87 81 82.00 PASS KALA 17 65 56 60 60.33 PASS PRIYA 18 34 45 51 43.33 FAIL GEETHA 16 72 67 27 55.33 FAIL ANTHONY 17 67 75 73 71.67 PASS

------------------------------------------------------------------------------*/ /* C PROGRAM TO FIND THE SUM OF N NATURAL NUMBERS*/ #include<stdio.h> void main() { int num,i; long int sum=0; clrscr(); printf("ENTER A NUMBER :"); scanf("%d",&num); for(i=1;i<=num;i++) sum+=i; printf("SUM OF %d NATURAL NUMBERS IS %ld",num,sum); getch(); } /*ENTER A NUMBER :35 SUM OF 35 NATURAL NUMBERS IS 630 ENTER A NUMBER :500 SUM OF 500 NATURAL NUMBERS IS 125250 */ /* C PROGRAM TO SWAP THE CONTENTS OF TWO VARIABLES USING POINTERS */ #include<stdio.h> void main() { void swap(int *,int *); int a,b; clrscr(); printf("ENTER TWO NUMBERS :"); scanf("%d %d",&a,&b); clrscr(); printf("THE NUMBERS BEFORE SWAPPING ARE A=%d,B=%d",a,b); swap(&a,&b); printf("\nTHE NUMBERS AFTER SWAPPING ARE A=%d,B=%d",a,b); getch(); } void swap(int *x,int *y) { int temp; temp=*x; *x=*y; *y=temp; } /* ENTER TWO NUMBERS :67 14

THE NUMBERS BEFORE SWAPPING ARE A=67,B=14 THE NUMBERS AFTER SWAPPING ARE A=14,B=67 ENTER TWO NUMBERS :1345 9027 THE NUMBER BEFORE SWAPPING ARE A=1345,B=9027 THE NUMBER AFTER SWAPPING ARE A=9027,B=1345 */ /* SIMPLE C PROGRAM USING SWITCH STATEMENT */ #include<stdio.h> void main() { int per,n; clrscr(); printf("ENTER STUDENT'S PERCENTAGE :"); scanf("%d",&per); n=per/10; switch(n) { case 10: case 9 : case 8 : case 7 : printf("STUDENT HAS GOT A DISTINCTION"); break; case 6 : printf("STUDENT HAS GOT A FIRST CLASS"); break; case 5 : printf("STUDENT HAS GOT A SECOND CLASS"); break; case 4 : printf("STUDENT HAS GOT A PASS CLASS"); break; default: printf("STUDENT HAS FAILED"); } getch(); } /*ENTER STUDENT'S PERCENTAGE :89 STUDENT HAS GOT A DISTINCTION ENTER STUDENT'S PERCENTAGE :55 STUDENT HAS GOT A SECOND CLASS ENTER STUDENT'S PERCENTAGE :29 STUDENT HAS FAILED*/ /* C PROGRAM TO FIND THE AREA OF TRIANGLE , GIVEN THREE SIDES */ #include<stdio.h> #include<math.h> void main() { float side_a,side_b,side_c,s,area; clrscr(); printf("ENTER THE THREE SIDES OF THE TRIANGLE :");

scanf("%f %f %f",&side_a,&side_b,&side_c); s=(side_a+side_b+side_c)/2; if((s-side_a<=0)||(s-side_b<=0)||(s-side_c<=0)) printf("INVALID INPUTS"); else { area=sqrt(s*(s-side_a)*(s-side_b)*(s-side_c)); printf("AREA OF THE TRIANGLE IS %f",area); } getch(); } /*ENTER THE THREE SIDES OF THE TRIANGLE :4 5 7 AREA OF THE TRIANGLE IS 9.797959*/ /*ENTER THE THREE SIDES OF THE TRIANGLE :4.5 6.7 3 AREA OF THE TRIANGLE IS 5.502218*/ /*ENTER THE THREE SIDES OF THE TRIANGLE :3 4 8 INVALID INPUTS*/ /* C PROGRAM TO FIND THE TRACE OF A MATRIX */ #include<stdio.h> void main() { int a[' '][' '],i,j,trace=0,n; clrscr(); printf("ENTER THE ORDER OF THE SQUARE MATRIX :"); scanf("%d",&n); printf("ENTER THE MATRIX ELEMENTS :"); for(i=0;i<n;i++) for(j=0;j<n;j++) { scanf("%d",&a[i][j]); if(i==j) trace+=a[i][j]; } printf("\nTHE ENTERED MATRIX IS\n"); for(i=0;i<n;i++) { for(j=0;j<n;j++) printf("\t%d",a[i][j]); printf("\n"); } printf("\nTRACE OF MATRIX IS %d",trace); getch(); } /*ENTER THE ORDER OF THE SQUARE MATRIX :3 ENTER THE MATRIX ELEMENTS :4 5 7 8 9 3 4 5 4 THE ENTERED MATRIX IS 4 5 7

8 9 3 4 5 4 TRACE OF MATRIX IS 17 ENTER THE ORDER OF THE SQUARE MATRIX :2 ENTER THE MATRIX ELEMENTS :-78 5 6 80 THE ENTERED MATRIX IS -78 5 6 80 TRACE OF MATRIX IS 2 */ /* C PROGRAM TO FIND THE TRACE OF A MATRIX */ #include<stdio.h> void main() { int a[' '][' '],i,j,trace=0,n; clrscr(); printf("ENTER THE ORDER OF THE SQUARE MATRIX :"); scanf("%d",&n); printf("ENTER THE MATRIX ELEMENTS :"); for(i=0;i<n;i++) for(j=0;j<n;j++) { scanf("%d",&a[i][j]); if(i==j) trace+=a[i][j]; } printf("\nTHE ENTERED MATRIX IS\n"); for(i=0;i<n;i++) { for(j=0;j<n;j++) printf("\t%d",a[i][j]); printf("\n"); } printf("\nTRACE OF MATRIX IS %d",trace); getch(); } /* C PROGRAM TO FIND THE TRANSPOSE OF A MATRIX */ #include<stdio.h> void main() { int a[' '][' '],at[' '][' '],i,j,m,n; clrscr(); printf("ENTER ORDER OF MATRIX (m*n) :"); scanf("%d %d",&m,&n);

printf("ENTER THE MATRIX ELEMENTS \n"); for(i=0;i<m;i++) for(j=0;j<n;j++) { scanf("%d",&a[i][j]); at[j][i]=a[i][j]; } printf("\nMATRIX A \n"); for(i=0;i<m;i++) { for(j=0;j<n;j++) printf("\t%d\t",a[i][j]); printf("\n"); } printf("\nTRANSPOSE OF MATRIX A \n"); for(i=0;i<n;i++) { for(j=0;j<m;j++) printf("\t%d\t",at[i][j]); printf("\n"); } getch(); } /*ENTER ORDER OF MATRIX (m*n) :2 2 ENTER THE MATRIX ELEMENTS 4 7 6 5 MATRIX A 4 7 6 5 TRANSPOSE OF MATRIX A 4 6 7 5 ENTER ORDER OF MATRIX (m*n) : -8 7 5 -1 4 9 ENTER THE MATRIX ELEMENTS MATRIX A -8 7 5 -1 4 9 TRANSPOSE OF MATRIX A -8 5 4 7 -1 9 */ /* C PROGRAM TO CONVERT A STRING IN UPPERCASE TO LOWERCASE */ #include<stdio.h>

#include<string.h> void main() { int n,i; static char str[20],strlow[20]; clrscr(); printf("ENTER A STRING IN UPPER CASE :"); scanf("%s",str); n=strlen(str); for(i=0;i<n;i++) strlow[i]=tolower(str[i]); printf("STRING IN LOWER CASE IS %s",strlow); getch(); } /* ENTER A STRING IN UPPER CASE :SENSATIONAL STRING IN LOWER CASE IS sensational ENTER A STRING IN UPPER CASE :CREAtions STRING IN LOWER CASE IS creations */ /* C PROGRAM TO FIND THE LARGEST AMONG GIVEN THREE NUMBERS */ #include<stdio.h> void main() { int p,q,r,lar; clrscr(); printf("ENTER THREE NUMBERS :"); scanf("%d %d %d",&p,&q,&r); if(p>q) lar=p; else lar=q; if(r>lar) lar=r; printf("LARGEST OF %d %d AND %d IS %d ",p,q,r,lar); getch(); } /*ENTER THREE NUMBERS :26 567 13 LARGEST OF 26 567 AND 13 IS 567 ENTER THREE NUMBERS :9978 45 12786 LARGEST OF 9978 45 AND 12786 IS 12786*/ /* C PROGRAM TO COUNT THE NUMBER OF VOWELS IN A STRING */ #include<stdio.h> #include<string.h> void main() { char str[' '];

int i,n,count=0; clrscr(); printf("ENTER A STRING :"); scanf("%s",str); n=strlen(str); for(i=0;i<n;i++) switch(str[i]) { case 'A': case 'a': case 'E': case 'e': case 'I': case 'i': case 'O': case 'o': case 'U': case 'u': count++; } printf("%d VOWELS OCCUR IN %s",count,str); getch(); } /*ENTER A STRING :SPIRITUALITY 5 VOWELS OCCUR IN SPIRITUALITY ENTER A STRING : LORDGANESHA 4 VOWELS OCCUR IN LORDGANESHA*/

You might also like