Вы находитесь на странице: 1из 59

1. Program to calculate the factorial of a number 2. Program to calculate the sum digits of a number 3. Program to reverse a number 4.

Program to check the number is strong number or not. 5. Program to calculate the prime factors of a numbers 6. Program to check given number is armstrong or not 7. Program to check given number is palendrome or not 8. Program to add between any two numbers using loop 9. Program to calculate daily expenditure if monthely expenditure is given using loop. 10. Program to count number of bits are set to 1 in an integer. 11. Program to calculate G.C.D of any two numbers 12. Program to calculate L.C.M of two numbers. 13. Program to calculate fibonacci series 14. Program to calculate string palindrome 15. Program to check the number is Prime number or not 16. Program to find largest number in an array 17. Program to find Second largest number in an array 18. Program to remove duplicate elements in an array 19. Program to convert decimal to binary 20. Program to convert binary to decimal 21. Program to check the number is perfect number or not. 22. Program to find generic root of a number. 23. Program to check a year is leap year or not. 24. Program to revese a string 25. Program to add a sub-string in a string 26. Program to traverse a string in reverse order 27. Program to count number of vowels , digits,characters and word present in string. 28. Program to add between two matrix 29. Program to multiplication between two matrix 30. Program to transpose a matrix 31. Program to check a matrix is sparse matrix or not 32. Program to calculate Amicable pairs from 1 to 1000 33. Program to calculate Sum of the series 1+2+3+---------+n 34. Program to find area triangle 35. Program for Bubble sort 36. Program for Selection sort 37. Program for insertion sort 38. Program for Quick Sort 39. Program for Merge Sort 40. Program for Sequential search using array 41. Program for Sequential search using linked list 42. Program for Binary search using array 43. Program for Binary search using linked list 44. Program to implement stack using linked list 45. Program to convert infix to prefix notation 46. Program to Evaluate postfix notation 47. Program to implement Queue using linked list 48. Program for Dqueue 49. Program for Priority Queue 50. Program to traverse linked list in reverse order.

51. Program to display the contents of a file using command line argument 52. Caclulate the age of a person after giving the date of birth 53. Cacluate the average marks of student looking at the following table 54. Cacluate the amount to be paid after giving the total time. 55. Program to convert upper case to lower case 56. Program to calculate the power of a number 57. INSERT AN ELMENT IN AN ARRAY AT DESIRED POSITION 58. program to calculate the sum of the following series 1 - x + x2/2! + x3/3! --------to nth term 59. find the weekday of a particular date(ex-6 sep 2010 (monday) 60.Write a program to convert given ip address 192.168.3.35 into 192.168.003.035 61.Caclulate the net salary looking at the following table 62. Write a program to display : * *** ***** ******* ********* ****** *** * 63. Write a program which will store roll name and age in student structure and address telephone no. In address structure. Implement the nested structure which will store information of ten students and their address and organize data in descending order by their roll no. 64. Write a program to store the information of student such as roll, name and course to a file using read & write system call. 65. Write a program to count no. of paragraphs, lines, words and characters present in a file. 66. Write a C program that will generate a table of values for the equation; f(X,Y) = 2ex3 + (23 + Y) x Where 1 < x < 5 with an increment 0.5 and 1 < y < 5 with an increment 0.25. 67. Write a C program using a do while loop, to calculate the sum of every third integer, beginning with I = 2, for all values of I that are less than 100. 68. Write a C program to solve a quadratic equation using switch statement. 69. Write a C program that accepts a character as input data and determine. If the character is an uppercase letter. An uppercase letter is any character that is greater than or equal to A and

less than or equal to Z. If the uppercase letter, display the message character is an uppercase letter. uppercase, display the message The is not an uppercase letter.

entered character is an The character just entered If the entered character is not character just entered character

70. To find the longest word and its length in a give string. 71. Write program : To create data for 50 students ( roll, name, mark 1, mark 2, mark 3, termmark ) and then find the total for each student and average mark of all students. 72. Write Programs : To round afloating point number to an indicate decimal place e. g. 17.457 will be rounded to 17.46 to two decimal places. 73. Write a C program to create a file for RD account in a bank assuming the file structure as RD account no, instalment amount and number of instalments. 74. Write a c program to open a file named BPUT and write a line of text in it by reading the text from the key board. 75. Declare a 2-dimensional array a of integers of size 4 x 4.. Treat it as matrix. Write a C program to test if this matrix is symmetric. 76. Write C program to read the contents of file, f and paste the contents at the beginning of another file f without taking help of any extra file 77. To find the longest elements and its location in 2-d array assuming elements of the array are distinet. 78. 79. 80. 81. 82. 83. 84. Program for DFS Program for BFS Program to search an element using hash table Program to search a string from hast table Program to search an element using hash table using chaning Shortest path using warshal's algorith Shortest path using dijkstra's algorith

85. Find the path matrix of Graph 86. Program to read the current date and time 87. Program to read all files from current directory -----------------------------------------------------1. Program to calculate the factorial of a number main() { int x,n; printf("Enter a number :"); scanf("%d",&n); x=fact(n);

printf("%d",x); } int fact(int n) { int f=1; while(n>0) { f=f*n; n--; } return f; } 2. Program to calculate the sum digits of a number main() { int x,n; printf("Enter a number :"); scanf("%d",&n); x=sum_digit(n); printf("%d",x); } int sum_digit(int n) { int s=0; while(n>0) { s=s + n%10; n=n/10; } return s; } 3. Program to reverse a number main() { int x,n; printf("Enter a number :"); scanf("%d",&n); x=reverse(n); printf("%d",x); } int reverse(int n) { int s=0; while(n>0) { s=s *10 + n%10; n=n/10; } return s;

} 4. Program to check the number is strong number or not. main() { int x,n; printf("Enter a number :"); scanf("%d",&n); x=strong(n); if(x==n) printf("Strong"); else printf("Not strong"); } int strong(int n) { int s=0,r,f; while(n>0) { r=n%10; f=fact(r); s=s+f; n=n/10; } return s; } int fact(int n) { int f=1; while(n>0) { f=f*n; n--; } return f; } 5. Program to calculate the prime factors of a numbers main() { int x,n; printf("Enter a number :"); scanf("%d",&n); prime_factors(n); } int prime_factors(int n) { int i=1,k; while(i<=n) { if(n%i==0) { k=check_prime(i);

if(k!=0) printf("%d ",k); } i++; } } int check_prime(int n) { int i=1; int c=0; while(i<=n) { if(n%i==0) c++; i++; } if(c==2) return n; else return 0; } 6. Program to check given number is armstrong or not main() { int n,x; printf("Enter a number:"); scanf("%d",&n); x=armstrong(n); if(x==n) printf("Arm strong"); else printf("Not arm strong"); } int armstrong(int num) { int sum=0,r; while(num!=0) { r=num%10; num=num/10; sum=sum+(r*r*r); } return sum; } 7. Program to check given number is palendrome or not main() { int n,x; printf("Enter a number:"); scanf("%d",&n); x=palendrome(n);

if(x==n) printf("Palendrome "); else printf("Not palendrome "); } int palendrome(int num) { int r=0; while(num>0) { r=r* 10 + num%10; num=num/10; } return r; } 8. Program to add between any two numbers using loop main() { int x; int a,b; printf("Enter any two numbers :"); scanf("%d%d",&a,&b); x=add(a,b); printf("%d ",x); } int add(int a,int b) { while(a>0) { b++; a--; } return b; } 9. Program to calculate daily expenditure if monthely expenditure is given using loop. main() { int x,n; printf("Enter monthely expenditure :"); scanf("%d",&n); x=daily_exp(n); printf("%d ",x); } int daily_exp(int n) { int c=0; while(n>0) { c++;

n=n-30; } return c; } 10. Program to count number of bits are set to 1 in an integer. main() { int x,n; printf("Enter a number :"); scanf("%d",&n); x=bit_count(n); printf("%d ",x); } int bit_count(int n) { int c=0; while(n>0) { c++; n=n&n-1; } return c; } 11. Program to calculate G.C.D of any two numbers main() { int n1,n2,x; printf("Enter two numbers:"); scanf("%d%d",&n1,&n2); x=gcd(n1,n2); printf("%d ",x); } int gcd(int n1,int n2) { while(n1!=n2) { if(n1>n2) n1=n1-n2; else n2=n2-n1; } return n1; } 12. Program to calculate L.C.M of two numbers. main() {

int n1,n2,x; printf("Enter two numbers:"); scanf("%d%d",&n1,&n2); x=lcm(n1,n2); printf("%d ",x); } int lcm(int n1,int n2) { int x,y; x=n1,y=n2; while(n1!=n2) { if(n1>n2) n1=n1-n2; else n2=n2-n1; } return x*y/n1; } 13. Program to calculate fibonacci series main() { int n; printf("Enter the number range:"); scanf("%d",&n); fibo(n); } int fibo(int n) { int i=0,j=1,k=2,r,f; printf("%d %d ", i,j); while(k<n) { f=i+j; i=j; j=f; printf(" %d",j); k++; } } 14. Program to calculate string palindrome main() { char x[100],y[100]; printf("Enter a string :"); scanf("%s",x); strcpy(y,x); check_palindrome(x); if(strcmp(x,y)==0) printf("Palindrome"); else

printf("Not Palindrome"); } int check_palindrome(char *x) { int len=strlen(x); int i; char temp; for(i=0;i<len/2;i++) { temp=x[i]; x[i]=x[len-i-1]; x[len-i-1]=temp; } } 15. Program to check the number is Prime number or not main() { int n,k; printf("Enter a number:"); scanf("%d",&n); k=check_prime(n); if(k==2) printf("Prime"); else printf("Not prime"); } int check_prime(int n) { int i=1,c=0; while(i<=n) { if(n%i==0) c++; i++; } return c; } 16. Program to find largest number in an array main() { int a[]={15,67,25,90,40}; int k; k=large_number(a); printf("%d ",k); } int large_number(int a[5]) { int i,big; big=a[0]; for(i=1;i<5;i++) { if(big<a[i])

big=a[i]; } return big; } 17. Program to find Second largest number in an array main() { int a[]={15,67,25,90,40}; int k; k=large_number(a); printf("%d ",k); } int large_number(int un[5]) { int big1,big2; int i; big1 = un[0]; for ( i=1;i<5;++i ) if ( big1 < un[i] ) big1 = un[i]; if ( big1!=un[0] ) big2=un[0]; else big2=un[1]; for(i=1; i<5;++i ) if (big1!=un[i] && big2 < un[i] ) big2=un[i]; return big2; } 18. Program to remove duplicate elements in an array main() { int i,k; int x[10]={5,7,2,8,9,3,3,6,7,20}; k=remove_duplicate(x); for(i=0;i<k;i++) { printf(" %d",x[i]); } } int remove_duplicate(int p[10]) { int size=10,i,j,k; for(i=0;i<size;i++) { for(j=0;j<size;j++) { if(i==j) { continue;

} else if(*(p+i)==*(p+j)) { k=j; size--; while(k < size) { *(p+k)=*(p+k+1); k++; } j=0; } } } return size; } 19. Program to convert from decimal to binary main() { int n; printf("Enter a number :"); scanf("%d",&n); decimal_binary(n); } int decimal_binary(int n) { int m,no=0,a=1,rem; m=n; while(n!=0) { rem=n%2; no=no+rem*a; n=n/2; a=a*10; } printf("%d",no); } 20. Program to convert binary to decimal main() { int n; printf("Enter data in binary format :"); scanf("%d",&n); binary_decimal(n); } int binary_decimal(int n) { int j=1,rem,n1=0;

while(n!=0) { rem=n%10; n1=n1+rem*j; j=j*2; n=n/10; } printf("%d",n1); } 21. Program to check the number is perfect number or not. main() { int n,x; printf("Enter a number:"); scanf("%d",&n); x=check_perfect(n); if(x==n) printf("Perfect number :"); else printf("Not a perfect number :"); } int check_perfect(int n) { int s=0,i=1; while(i<n) { if(n%i==0) s=s+i; i++; } return s; } 22.Program to find generic root of a number. main() { int n,k; printf("Enter a number"); scanf("%d",&n); k=generic(n); printf("%d",k); } int generic(int n) { int sum,r; if(n<10) return n; while(n>10) { sum=0; while(n!=0)

{ r=n%10; n=n/10; sum+=r; } if(sum>10) n=sum; else break; } return sum; }

23. Program to check a year is leap year or not. main() { int year; printf("Enter the year :\n"); scanf("%d",&year); if((year % 400==0 )|| ((year % 4==0)&& (year %100!=0))) printf("Leap Year "); else printf("Not leap year"); } 24. Program to revese a string main() { char x[100]; printf("Enter a string :"); gets(x); strrev(x); printf("%s",x); } int strrev(char *x) { int len=strlen(x); int i; char temp; for(i=0;i<len/2;i++) { temp=x[i]; x[i]=x[len-i-1]; x[len-i-1]=temp; } } 25. Program to add a sub-string in a string main()

{ char x[100],y[20]; int pos; printf("Enter string :"); gets(x); printf("Enter substring:"); scanf("%s",y); printf("Enter position:"); scanf("%d",&pos); add_substring(x,y,pos); } int add_substring(char *x,char *y,int pos) { char z[100]; int i=0,j=0,k; memset(z,0,sizeof(z)); while(i<pos) { z[i]=*x; x++; i++; } while(*y!=0) { z[i]=*y; i++; y++; } z[i]=' '; i++; while(*x!=0) { z[i]=*x; i++; x++; } z[i]=0; printf("%s",z); } 26. Program to traverse a string in reverse order main() { char x[100]; printf("Enter a string :"); gets(x); int len=strlen(x)-1; while(len>=0) { printf("%c",x[len]); len--; } }

27. Program to count number of words,vowels,digits and space present in string. main() { int word=0,space=0,digit=0,vowels=0,i; char x[100]; printf("Enter a string :"); gets(x); for(i=0;i<strlen(x);i++) { if(x[i]=='a' || x[i]=='A' || x[i]=='e' || x[i]=='E' || x[i]=='i' || x[i]=='I' || x[i]=='o' || x[i]=='O' || x[i]=='u' || x[i]=='U') vowels++; if(x[i]>=48 && x[i]<=57) digit++; if(x[i]==32) //space space++; } word=space+1; printf("%d %d %d %d\n",word,space,digit,vowels); }

28. Program to add between two matrix main() { int a[3][3]={ 1,2,1, 1,2,2, 3,2,1 }; int b[3][3]={ 2,2,1, 1,1,1, 2,3,1 }; int c[3][3]; int i,j,k,sum=0; 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++)

{ for(j=0;j<3;j++) { printf("%d ",c[i][j]); } printf("\n"); } } 29. Program for multiplication between two matrix main() { int a[3][3]={ 1,2,1, 1,2,2, 3,2,1 }; int b[3][3]={ 2,2,1, 1,1,1, 2,3,1 }; int c[3][3]; int i,j,k,sum=0; for(i=0;i<3;i++) { for(j=0;j<3;j++) { c[i][j]=0; for(k=0;k<3;k++) { c[i][j]=c[i][j]+a[i][k]*b[k][j]; } } } for(i=0;i<3;i++) { for(j=0;j<3;j++) { printf("%d ",c[i][j]); } printf("\n"); } } 30. Program to transpose a matrix main() { int a[3][5]={ 1,4,5,6,7, 2,3,4,1,5, 9,5,3,1,4 };

int b[5][3]; int i,j; for(i=0;i<3;i++) { for(j=0;j<5;j++) { b[j][i]=a[i][j]; } } for(i=0;i<5;i++) { for(j=0;j<3;j++) { printf("%d",b[i][j]); } printf("\n"); } } 31. Program to check a matrix is sparse matrix or not

main() { int mat[3][5]={ 1,0,2,3,0, 0,1,0,1,0, 0,0,0,1,0 }; int i,j,nzero=0,zero=0; for(i=0;i<3;i++) { for(j=0;j<5;j++) { if(mat[i][j]!=0) nzero++; else zero++; } } if(zero>nzero) printf("The matrix is sparse matrix"); else printf("Not a sparse matrix"); } 32. Program to calculate Amicable pairs from 1 to 1000 #include "stdio.h" main()

{ int n,k; int i=1,s1=0,s2=0; for(k=1;k<=1000;k++) { n=k; while(i<n) { if(n%i==0) s1=s1+i; i++; } i=1; if(s1==n) continue; while(i<s1) { if(s1%i==0) s2=s2+i; i++; } if(n==s2) printf("%d \n",n); s1=0; s2=0; } } 33. Program to calculate Sum of the series 1+2+3+---------+n main() { int r; printf("Enter the number range: "); scanf("%d",&r); printf("%d",(r*(r+1))/2); } 34. Program to find area triangle #include "math.h" int main() { double a,b,c,s; double area; printf("Enter the size of triangle :"); scanf("%lf%lf%lf",&a,&b,&c); s = (a + b + c)/2; area =sqrt (s*(s-a)*(s-b)*(s-c));

printf("%lf",area); } 35. Program for Bubble sort main() { int a[5]={4,9,40,2,25}; int i; bubble_sort(a); for(i=0;i<5;i++) printf("%d ",a[i]); } int bubble_sort(int a[5]) { int i,j,temp; for(i=0;i<5;i++) { for(j=0;j<5-i;j++) { if(a[j]>a[j+1]) { temp=a[j]; a[j]=a[j+1]; a[j+1]=temp; } } } } 36. Program for Selection sort main() { int a[5]={4,9,40,2,25}; int i; selection_sort(a); for(i=0;i<5;i++) printf("%d ",a[i]); } int selection_sort(int a[5]) { int i,j,temp; for(i=0;i<5;i++) { for(j=i+1;j<5;j++) { if(a[i]>a[j]) { temp=a[i]; a[i]=a[j]; a[j]=temp; } }

} } 37. Program main() {

for Insertion sort

int a[5]={4,9,40,2,25}; int i; insert_sort(a); for(i=0;i<5;i++) printf("%d ",a[i]); } int insert_sort(int a[5]) { int i,j,k,temp; for(i=1;i<5;i++) { for(j=0;j<i;j++) { if(a[i]<a[j]) { temp=a[i]; for(k=i;k>j;k--) { a[k]=a[k-1]; } a[j]=temp; } } } } 38. Program main() { int x[5]={5,9,2,20,6}; int i; quick_sort(x,0,4); for(i=0;i<5;i++) printf("%d ",x[i]); } quick_sort(int x[10],int first,int last) { int pivot,j,temp,i; if(first<last) { pivot=first; i=first; j=last; while(i<j) { for quick sort

while(x[i]<=x[pivot]&&i<last) i++; while(x[j]>x[pivot]) j--; if(i<j) { temp=x[i]; x[i]=x[j]; x[j]=temp; } } temp=x[pivot]; x[pivot]=x[j]; x[j]=temp; quick_sort(x,first,j-1); quick_sort(x,j+1,last); } } 39. Program for Merge Sort

40. Program for sequential search main() { int a[]={6,5,3,20,55}; int i,flag=0,n; printf("Enter number to search :"); scanf("%d",&n); for(i=0;i<5;i++) { if(a[i]==n) { flag=1; break; } } if(flag==1) printf("Found\n"); else printf("Not found\n"); } 41. Program for Sequential search struct xxx { int data; struct xxx *ad; }; struct xxx *create_linkedlist(); main() { int k; struct xxx *b; using linked list

b=create_linkedlist(); k=search(b); if(k==1) printf("Found "); else printf("Not found\n"); } struct xxx *create_linkedlist() { char ch[10]; struct xxx *p,*q,*r; p=malloc(sizeof(struct xxx)); printf("Enter data :"); scanf("%d",&p->data); r=p; while(1) { printf("Do u continue yes/no :"); scanf("%s",ch); if(strcmp(ch,"no")==0) break; q=malloc(sizeof(struct xxx)); p->ad=q; p=q; printf("Enter data :"); scanf("%d",&p->data); } p->ad=0; return r; } int search(struct xxx *p) { int n,flag=0; printf("Enter number to search :"); scanf("%d",&n); while(p!=0) { if(p->data==n) { flag=1; break; } p=p->ad; } return flag; } 42. Binary Search using an Array void main() {

int a[10],i,n,m,c=0,l,u,mid; printf("Enter the size of an array->"); scanf("%d",&n); printf("\nEnter the elements of the array->"); for(i=0;i<n;i++) { scanf("%d",&a[i]); } printf("\nThe elements of an array are->"); for(i=0;i<n;i++) { printf(" %d",a[i]); } printf("\nEnter the number to be search->"); scanf("%d",&m); l=0,u=n-1; while(l<=u) { mid=(l+u)/2; if(m==a[mid]) { c=1; break; } else if(m<a[mid]) { u=mid-1; } else l=mid+1; } if(c==0) printf("\nThe number is not in the list"); else printf("\nThe number is found"); } 43. Program for Binary search using linked list struct xxx { int roll; struct xxx *ad; }; struct xxx *create(); main() { int status; int u,n; struct xxx *b; b=create(); visit(b);

selection_sort(b); printf("\n"); visit(b); u=count(b); printf("Enter number to search :"); scanf("%d",&n); status=binary_search(b,1,u,n); if(status==1) printf("Found\n"); else printf("Not found\n"); } int binary_search(struct xxx *p,int l,int u,int n) { struct xxx *r=p; int mid,i,f=0; while(l<=u && r!=0) { mid=(l+u)/2; for(i=1;i<mid;i++) { r=r->ad; } if(n==r->roll) { f=1; break; } if(n<r->roll) { u=mid-1; } if(n>r->roll) { l=mid+1; } r=p; } return f; } int count(struct xxx *p) { int c=0; while(p!=0) { c++; p=p->ad; } return c; } int selection_sort(struct xxx *p) {

struct xxx *q=p->ad; int temp; while(p->ad!=0) { while(q!=0) { if(p->roll>q->roll) { temp=p->roll; p->roll=q->roll; q->roll=temp; } q=q->ad; } p=p->ad; q=p->ad; } } int visit(struct xxx *p) { while(p!=0) { printf("%d ",p->roll); p=p->ad; } } struct xxx *create() { struct xxx *p,*q,*r; char ch[10]; p=malloc(sizeof(struct xxx)); r=p; while(1) { printf("Enter roll :"); scanf("%d",&p->roll); printf("Do u continue yes/No:"); scanf("%s",ch); if(strcmp(ch,"no")==0) break; q=malloc(sizeof(struct xxx)); p->ad=q; p=q; } p->ad=0; return r; } 44. Program to implement stack using linked list struct xxx {

int roll; struct xxx *ad; }; struct xxx *p=0; main() { int x; do { printf("1 for push\n"); printf("2 for pop\n"); printf("0 for stop\n"); printf("Enter choice :"); scanf("%d",&x); if(x==1) { push(); } else if(x==2) { pop(); } }while(x!=0); } int pop() { if(p==0) { printf("Stack is empty\n"); return; } else { printf("%d \n",p->roll); p=p->ad; } } int push() { struct xxx *q; if(p==0) { p=malloc(sizeof(struct xxx)); printf("Enter roll :"); scanf("%d",&p->roll); p->ad=0; } else {

q=malloc(sizeof(struct xxx)); q->ad=p; p=q; printf("Enter roll :"); scanf("%d",&p->roll); } }

45. Program to convert infix to prefix notation struct xxx { char data; struct xxx *ad; }; main() { int len; struct xxx *opd=0,*opr=0,*q; char x[50]; char y[50]; int i=0; printf("Enter Infix expression :"); scanf("%s",x); char *p=x+strlen(x)-1; int k1,k2; for(i=strlen(x)-1;i>=0;i--) { if(*p>=48 && *p<=57) { if(opd==0) { opd=malloc(sizeof(struct xxx)); opd->data=*p; opd->ad=0; } else { q=malloc(sizeof(struct xxx)); q->ad=opd; opd=q; opd->data=*p; } } else { if(opr==0) { opr=malloc(sizeof(struct xxx));

opr->data=*p; opr->ad=0; } else { k1=check_precedence(*p); k2=check_precedence(opr->data); if(k1<=k2) { while(k1<=k2 && opr!=0) { q=malloc(sizeof(struct xxx)); q->ad=opd; opd=q; opd->data=opr->data; opr=opr->ad; if(opr==0) { opr=malloc(sizeof(struct xxx)); opr->ad=0; break; } k2=check_precedence(opr->data); } opr->data=*p; } else { q=malloc(sizeof(struct xxx)); q->ad=opr; opr=q; opr->data=*p; } }//else } //else p--; } //for while(opr!=0) { q=malloc(sizeof(struct xxx)); q->ad=opd; opd=q; opd->data=opr->data; opr=opr->ad; } memset(y,0,sizeof(y)); i=0;

while(opd!=0) { y[i]=opd->data; i++; opd=opd->ad; } printf("%s ",y); } int check_precedence(int m) { switch(m) { case '+': case '-': return 1; case '/': case '*': return 2; } } 46. Program to Evaluate postfix notation struct xxx { unsigned char data; struct xxx *ad; }; main() { unsigned char a,b,c; struct xxx *st=0,*q; char x[100]; printf("Enter Post Expression:"); scanf("%s",x); char *p=x; while(*p!=0) { if(*p>=48 && *p<=57) { if(st==0) { st=malloc(sizeof(struct xxx)); st->data=*p; st->ad=0; } else { q=malloc(sizeof(struct xxx)); q->ad=st; st=q; st->data=*p;

} } else { b=st->data-48; st=st->ad; a=st->data-48; if(*p=='*') { c=a*b; } else if(*p=='/') { c=a/b; } else if(*p=='+') { c=a+b; } else if(*p=='-') { c=a-b; } st->data=c+48; } p++; } printf("%d ", st->data-48); } 47. Program to implement Queue using linked list #include<stdio.h> #include<malloc.h> struct node { int info; struct node *next; } ; struct node *front, *rear; void enqueue(int elt); int dequeue(); void display(); void main() { int ch, elt;

rear = NULL; front = NULL; while (1) { printf("1 Insert\n"); printf("2 Delete\n"); printf("3 Display\n"); printf("4 Exit\n"); printf("Enter your choice : "); scanf("%d", &ch); switch (ch) { case 1: printf("Enter The Element Value\n"); scanf("%d", &elt); enqueue(elt); break; case 2: elt = dequeue(); printf("The deleted element = %d\n", elt); break; case 3: display(); break; default: exit(0); } } } void enqueue(int elt) { struct node *p; p = (struct node*)malloc(sizeof(struct node)); p->info = elt; p->next = NULL; if (rear == NULL || front == NULL) front = p; else rear->next = p; rear = p; } int dequeue() { struct node *p; int elt; if (front == NULL || rear == NULL) { printf("\nUnder Flow"); exit(0); } else {

p = front; elt = p->info; front = front->next; free(p); } return (elt); } void display() { struct node *t; t = front; while (front == NULL || rear == NULL) { printf("\nQueue is empty"); exit(0); } while (t != NULL) { printf("->%d", t->info); t = t->next; } } 49. Program for Priority Queue struct xxx { int pr; struct xxx *ad; }; struct xxx *p=0; main() { int x; do { printf("1 for add\n"); printf("2 for dele\n"); printf("3 for traverse\n"); printf("0 for stop\n"); printf("Enter choice :"); scanf("%d",&x); if(x==1) add(); else if(x==2) dele(); else if(x==3) trav(); }while(x!=0);

} int trav() { struct xxx *r=p; while(r!=0) { printf("%d ",r->pr); r=r->ad; } } int add() { struct xxx *q,*r,*m; if(p==0) { p=malloc(sizeof(struct xxx)); printf("Enter priority :"); scanf("%d",&p->pr); p->ad=0; } else { q=malloc(sizeof(struct xxx)); printf("Enter priority :"); scanf("%d",&q->pr); if(q->pr <= p->pr) { q->ad=p; p=q; } else { r=p; while(q->pr > r->pr) { m=r; if(r->ad==0) { r->ad=q; q->ad=0; return; } r=r->ad; } m->ad=q; q->ad=r; } } } int dele()

{ struct xxx *r,*m; r=p; if(r==0) { printf("Stack is empty\n"); exit(0); } while(r->ad!=0) { m=r; r=r->ad; } free(r); m->ad=0; } 50. Traverse a linked list in reverse order #include <stdio.h> #define NODES 4 typedef struct list_head { struct node* head; struct node* tail; } list_head; typedef struct node { struct node* next; int value; } node; node n[NODES]; list_head init_empty_list() { list_head h; h.head = 0; h.tail = 0; return h; } list_head init_list() { int i; list_head h; for (i=0; i<NODES-1; ++i) { n[i].next = &n[i+1]; n[i].value = i+1; } n[NODES-1].next = 0; n[NODES-1].value = NODES; h.head = &n[0];

h.tail = &n[NODES-1]; return h; } void print_list(list_head h) { node* p = h.head; while (p) { printf("%d ", p->value); p = p->next; } printf("\n"); } list_head reverse_list(list_head h) { list_head nh; if ((h.head == 0) || (h.head->next ==0)) { return h; } nh.head = h.head->next; nh.tail = h.tail; nh = reverse_list(nh); h.head->next = 0; nh.tail->next = h.head; nh.tail = h.head; return nh; } int main() { list_head head; head = init_list(); print_list(head); head = reverse_list(head); print_list(head); return 0; }

51. Program to display the contents of a file using command line argument #include "fcntl.h" main(int x, char *y[], char *z[]) { int i; char ch; if(x<2) {

printf("Too few parameters\n"); exit(0); } for(i=1;i<x;i++) { int k=open(y[i],O_RDONLY); if(k==-1) { printf("File not found\n"); break; } while(read(k,&ch,1)) printf("%c",ch); close(k); } } 52. Calculate the age of a person after giving the date of the birth. #include "time.h" main() { char x[100],y[100]; int dd,mm,yy; int bd,bm,by; int nd,nm,ny; unsigned int t; struct tm *mytime; t=time(0); mytime=localtime(&t); dd=mytime->tm_mday; mm=mytime->tm_mon+1; yy=mytime->tm_year+1900; printf("Enter birtd day,mon and year :"); scanf("%d%d%d",&bd,&bm,&by); if(dd>=bd) nd=dd-bd; else if(mm==1 || mm==3 || mm==5 || mm==7 || mm==8 || mm==10 || mm==12) { dd=dd+31; nd=dd-bd; } else if(mm==4 || mm==6 || mm==9 || mm==11) { dd=dd+30; nd=dd-bd;

} else if(mm==2) { dd=dd+28; nd=dd-bd; } if(mm>=bm) { nm=mm-bm; } else { mm=mm+12; yy=yy-1; nm=mm-bm; } ny=yy-by; printf("%d %d %d \n",ny,nm,nd); } 53. Cacluate the average marks of student looking at the following table p1 p2 p3 average >=60% >=50 >=40 Result first second third

avg=(p1+p2+p3)/3; where p1,p2 and p3 >=30 main() { int p1,p2,p3,avg; printf("Enter Marks for p1 p2 & p3 : "); scanf("%d%d%d",&p1,&p2,&p3); avg=(p1+p2+p3)/3; if(p1>=30 && p2>=30 && p3>=30) { if(avg>=60) printf("First\n"); else if(avg>=50) printf("Second\n"); else if(avg>=40) printf("Third\n"); else printf("Failed\n");

} else printf("Failed\n"); } 54. Cacluate the amount to be paid after giving the total time. Time Amount 8 hours Next 4 hours Next 4 hours Next 4 hours 100/20/- ph 30/- ph 40/- ph

main() { int time,amt; printf("Enter Total Time :"); scanf("%d",&time); if(time==8) amt=100; else if(time>8 && time<=12) amt=100+(time-8)*20; else if(time>12 && time<=16) amt=180+(time-12)*30; else if(time>16 && time<=20) amt=300+(time-16)*40; else { printf("Invalid entry\n"); exit(0); } printf("%d", amt); } 55. Program to convert upper case to lower case void main() { char str[20]; int i; printf("Enter any string->"); scanf("%s",str); printf("The string is->%s",str); for(i=0;i<=strlen(str);i++) { if(str[i]>=65&&str[i]<=90) str[i]=str[i]+32;

} printf("\nThe string in uppercase is->%s",str); } 56. Program to calculate the power of a number void main() { int pow,num,i=1; long int sum=1; printf("\nEnter a number: "); scanf("%d",&num); printf("\nEnter power: "); scanf("%d",&pow); while(i<=pow) { sum=sum*num; i++; } printf("\n%d to the power %d is: %ld",num,pow,sum); 57. INSERT AN ELMENT IN AN ARRAY AT DESIRED POSITION void main() { int a[50],size,num,i,pos,temp; printf("\nEnter size of the array: "); scanf("%d",&size); printf("\nEnter %d elements in to the array: ",size); for(i=0;i<size;i++) scanf("%d",&a[i]); printf("\nEnter position and number to insert: "); scanf("%d %d",&pos,&num); i=0; while(i!=pos-1) i++; temp=size++; while(i<temp) { a[temp]=a[temp-1]; temp--; } a[i]=num; for(i=0;i<size;i++) printf(" %d",a[i]); } } 58. program to calculate the sum of the following series 1 - x + x2/2! + x3/3! --------to nth term main() { int serise,square,fact=1,loop=1,loop1;

float sum; float result=0; printf("Enter The serise "); scanf("%d",&serise); while(loop<=serise) { square=pow(loop,2); for(loop1=1;loop1<=loop;loop1++) { fact = fact * loop1; } sum=(float)square/fact; if(loop%2!=0) result = result + sum; else result = result - sum; fact=1; loop++; } printf("The summation Of the serise is %f\n",result); } 59. Find the weekday of a particular date(ex-6 sep 2010 (monday) #include "time.h" #include "stdio.h" main() { char daybuf[20]; int dd,mm,yy; unsigned int t; struct tm *mytime; t=time(0); mytime=localtime(&t); dd=mytime->tm_mday; mm=mytime->tm_mon+1; yy=mytime->tm_year+1900; mytime->tm_hour = 0; mytime->tm_min = 0; mytime->tm_sec = 1; mytime->tm_isdst = -1; printf("Enter the day,mon and year :"); scanf("%d%d%d",&dd,&mm,&yy); if(mktime(mytime) == -1) fprintf(stderr, "Unkown -\n"); else strftime(daybuf, sizeof(daybuf), "%A", mytime); printf("It was a %s\n", daybuf);

} 60.Write a program to convert given ip address 192.168.3.35 into 192.168.003.035 #include<stdio.h> int main() { char input[16]; char p[4][4]; int i,a,b,len; int j,c; printf("pl enter any ip address\n"); scanf("%s",input); a=0;b=0; for(i=0;input[i];i++) { if(input[i]!='.') p[a][b++]=input[i]; else { p[a][b]=0; a++; b=0; } } p[a][b]=0; for(a=0;a<4;a++) { len=strlen(p[a]); for(j=len-1;j>=0;j--) p[a][j+3-len]=p[a][j]; for(j=0;j<(3-len);p[a][j]='0',j++); p[a][3]=0; } input[0]=0; for(i=0;i<4;i++) { strcat(input,p[i]); strcat(input,"."); } input[15]=0; puts(input); return 0; } 61.Caclulate the net salary looking at the following table Basic TA >=20000 >=10000 >=500 100 DA 500 200 100 PF 200 300 100 NET 200 100 ? ? ?

main() { int basic,ta,da,pf,net; printf("Enter basic Salary :"); scanf("%d",&basic); if(basic>=20000) { ta=500; da=200; pf=200; } else if(basic>=10000) { ta=200; da=300; pf=100; } else if(basic>=5000) { ta=100; da=100; pf=100; } else { printf("Invalid entry"); exit(0); } net=(basic+ta+da)-pf; printf("%d", net); } 62. Write a * *** ***** ******* ********* ****** *** main() { int line,star=1,space,i,j,k; printf("Enter the no of line :\n"); scanf("%d",&line); printf("Enter the no of space\n"); scanf("%d",&space); for(i=0;i<line;i++) { program to display :

for(j=0;j<space;j++) { printf(" "); } for(k=0;k<star;k++) { printf("*"); } if(i<line/2) { space=space-1; star=star+2; } else { star=star-2; space=space+1; } printf("\n"); } } 63. Write a program which will store roll name and age in student structure and address telephone no. In address structure. Implement the nested structure which will store information of ten students and their address and organize data in descending order by their roll no. #include "stdio.h" #include "stdlib.h" #include "fcntl.h" #define SIZE 3 struct address { char addr[30]; long int phoneno; }; struct student { char name[20]; int roll; int age; struct address p; }__attribute__((__packed__)); main() { struct student details[SIZE],temp; int loop,outer,inner; for(loop = 0;loop<SIZE;loop++) { printf("Enter name :"); scanf("%s",details[loop].name); printf("Enter roll :");

scanf("%d",&details[loop].roll); printf("Entere Age: "); scanf("%d",&details[loop].age); printf("Enter address: "); scanf("%s",details[loop].p.addr); printf("Enter Phone number:"); scanf("%d",&details[loop].p.phoneno); } printf("Before Sorting\n"); for(loop = 0;loop < SIZE;loop++) { printf("%s\t%d\t%d\t%s\t%d\n",details[loop].name,details[loop].ro ll,details[loop].age,details[loop].p.addr,details[loop].p.phoneno); } printf("\n"); for(outer = 0;outer < SIZE;outer++) { for(inner = outer + 1;inner < SIZE;inner++) { if(details[outer].roll > details[inner].roll) { temp = details[outer]; details[outer] = details[inner]; details[inner] = temp; } } } printf("After Sorting\n"); for(loop = 0;loop < SIZE;loop++) { printf("%s\t%d\t%d\t%s\t%d\n",details[loop].name,details[loop].ro ll,details[loop].age,details[loop].p.addr,details[loop].p.phoneno); } printf("\n"); } 64. Write a program to store the information of student such as roll, name and course to a file using read & write system call. #include "fcntl.h" #include "stdio.h" struct student { char name[30]; int roll; int age; }__attribute__((__packed__)); main()

{ int handlerStructure, numberOfStudent, loop; handlerStructure = open("studentDetails.txt",O_RDWR); printf("HOW MANY STUDENT'S INFORMATION :"); scanf("%d",&numberOfStudent); struct student details[numberOfStudent]; for(loop = 0;loop < numberOfStudent;loop++) { write(1,"ENTER NAME OF STUDENT :",strlen("ENTER NAME OF STUDENT :")); memset(details[loop].name,0,sizeof(details[loop].name)); read(0,details[loop].name,sizeof(details[loop].name)); write(1,"ENTER THE ROLL NO. :",strlen("ENTER THE ROLL NO. :")); read(0,&details[loop].roll,sizeof(details[loop].roll)); write(1,"ENTER THE STUDENT'S AGE :",strlen("ENTER THE STUDENT'S AGE :")); read(0,&details[loop].age,sizeof(details[loop].age)); } //write(handlerStructure,&details,sizeof(struct student)); for(loop = 0;loop < numberOfStudent;loop++) { write(handlerStructure,&details[loop].name,strlen(details[loop].n ame)); write(handlerStructure,&details[loop].roll,sizeof(details[loop].r oll)); write(handlerStructure,&details[loop].age,sizeof(details[loop].ag e)); } loop = 0; while(read(handlerStructure,&details[loop],sizeof(struct student))) { write(1,&details[loop].name,strlen(details[loop].name)); write(1,&details[loop].roll,sizeof(details[loop].roll)); write(1,&details[loop].age,sizeof(details[loop].age)); loop++; } } 65. Write a program to count no. of paragraphs, lines, words and characters present in a file.

78. Program for DFS struct xxx { int node; int total; int status; struct xxx *ad; }; struct xxx p[5]; main() { struct xxx *q=0,*r,*m; int a[5][5]={ 0,1,1,1,0, 0,0,0,0,1, 0,0,0,1,0, 0,1,0,0,1, 0,0,0,0,0 }; int i,j,k; int result[5]; memset(p,0,sizeof(p)); //create adjacence list for(i=0;i<5;i++) { p[i].node=i+1; for(j=0;j<5;j++) { if(a[i][j]==1) p[i].total++; } for(k=1;k<=p[i].total;k++) { printf("Enter adjacence node for %d\n",p[i].node); if(q==0) { q=malloc(sizeof(struct xxx)); printf("Enter adjacence node :"); scanf("%d",&q->node); q->ad=0; p[i].ad=q; } else { r=malloc(sizeof(struct xxx)); printf("Enter adjacence node :"); scanf("%d",&r->node); q->ad=r; q=r; r->ad=0; }

} q=0; p[i].total=0; } //traverse adjacence list for(i=0;i<5;i++) { printf("%d : ",p[i].node); if(p[i].ad!=0) { r=p[i].ad; while(r!=0) { printf("%d -> ",r->node); r=r->ad; } printf("\n"); } } q=malloc(sizeof(struct xxx)); printf("Enter source :"); scanf("%d",&q->node); set_status(q->node); p[q->node-1].status=1; q->ad=0; while(1) { r=p[q->node-1].ad; if(r==0) break; p[q->node-1].status=1; while(r!=0) { if(r->status==0) { m=malloc(sizeof(struct xxx)); m->node=r->node; set_status(r->node); m->ad=q; q=m; } r=r->ad; } } while(q!=0) { printf("%d ",q->node); q=q->ad; }

} set_status(int n) { struct xxx *r; int i; for(i=0;i<5;i++) { r=p[i].ad; while(r!=0) { if(r->node==n) r->status=1; r=r->ad; } } } 79. Program for BFS struct xxx { int node; int total; int status; struct xxx *ad; }; struct xxx p[5]; main() { struct xxx *q=0,*r,*m,*f; int a[5][5]={ 0,1,1,1,0, 0,0,0,0,1, 0,0,0,1,0, 0,1,0,0,1, 0,0,0,0,0 }; int i,j,k; int result[5]; memset(p,0,sizeof(p)); //create adjacence list for(i=0;i<5;i++) { p[i].node=i+1; for(j=0;j<5;j++) { if(a[i][j]==1) p[i].total++; } for(k=1;k<=p[i].total;k++)

{ printf("Enter adjacence node for %d\n",p[i].node); if(q==0) { q=malloc(sizeof(struct xxx)); printf("Enter adjacence node :"); scanf("%d",&q->node); q->ad=0; p[i].ad=q; } else { r=malloc(sizeof(struct xxx)); printf("Enter adjacence node :"); scanf("%d",&r->node); q->ad=r; q=r; r->ad=0; } } q=0; p[i].total=0; } //traverse adjacence list for(i=0;i<5;i++) { printf("%d : ",p[i].node); if(p[i].ad!=0) { r=p[i].ad; while(r!=0) { printf("%d -> ",r->node); r=r->ad; } printf("\n"); } } q=malloc(sizeof(struct xxx)); printf("Enter source :"); scanf("%d",&q->node); set_status(q->node); p[q->node-1].status=1; q->ad=0; f=q; while(1) { r=p[q->node-1].ad; if(r==0) break; p[q->node-1].status=1; while(r!=0)

{ if(r->status==0) { m=malloc(sizeof(struct xxx)); m->node=r->node; set_status(r->node); q->ad=m; q=m; } r=r->ad; } } while(f!=0) { printf("%d ",f->node); f=f->ad; } } set_status(int n) { struct xxx *r; int i; for(i=0;i<5;i++) { r=p[i].ad; while(r!=0) { if(r->node==n) r->status=1; r=r->ad; } } } 80. Program to search an element using hash table main() { int x[10]; int k,m=8,i=0,j=0,flag=0; int pos; memset(x,0,sizeof(x)); for(i=0;i<8;i++) { printf("Enter data :"); scanf("%d",&k); pos=(k%m+j)%m;

if(x[pos]==0) x[pos]=k; else { while(x[pos]!=0) { j++; pos=(k%m+j)%m; } x[pos]=k; j=0; } } j=0; printf("Enter number to search :"); scanf("%d",&k); pos=(k%m+j)%m; if(k==x[pos]) flag=1; else { while(pos<m) { pos++; if(x[pos]==k) { flag=1; break; } } } if(flag==1) printf("Found\n"); else printf("Not found\n"); } 81. Program to search a string from hast table main() { char x[8][20]; int i,k,pos,m=8,j=0,f=0; char p[20]; memset(x,0,sizeof(x)); for(i=0;i<8;i++) { printf("Enter string :"); scanf("%s",p); k=p[0]; pos=(k%m+j)%m;

if(strlen(x[pos])==0) strcpy(x[pos],p); else { while(j<m) { j++; pos=(k%m+j)%m; if(strlen(x[pos])==0) break; } strcpy(x[pos],p); j=0; } } j=0; printf("Enter search string :"); scanf("%s",p); k=p[0]; pos=(k%m+j)%m; if(strcmp(x[pos],p)==0) f=1; else while(j<m) { j++; pos=(k%m+j)%m; if(strcmp(x[pos],p)==0) { f=1; break; } } if(f==1) printf("Found"); else printf("Not found"); } 82. Program to search an element using hash table using chaning struct xxx { int data; struct xxx *ad; }; main() { struct xxx p[10],*q,*r; int i,k,m=10,pos,flag=0; memset(p,0,sizeof(p));

for(i=0;i<10;i++) { printf("Enter element :"); scanf("%d",&k); pos=k%m; if(p[pos].data==0) { p[pos].data=k; } else { if(p[pos].ad==0) { q=malloc(sizeof(struct xxx)); p[pos].ad=q; q->data=k; q->ad=0; } else { r=p[pos].ad; while(r->ad!=0) { r=r->ad; } q=malloc(sizeof(struct xxx)); q->data=k; q->ad=0; r->ad=q; } } } printf("Enter number to search:"); scanf("%d",&k); pos=k%m; if(k==p[pos].data) flag=1; else { r=p[pos].ad; while(r!=0) { if(k==r->data) { flag=1; break; } r=r->ad; } } if(flag==1)

printf("Found\n"); else printf("Not found\n"); } 83. Shortmest path using warshal's algorith main() { int a[6][6]={ 0,5,3,99,1,99, 99,0,99,5,99,99, 99,99,0,99,2,2, 99,99,3,0,99,3, 99,2,99,1,0,99, 99,99,99,99,99,0 }; int i,j,k; int p[6][6]; for(i=0;i<6;i++) { for(j=0;j<6;j++) { if(a[i][j]==0) p[i][j]=0; else p[i][j]=a[i][j]; } } //find shortest path for(k=0;k<6;k++) { for(i=0;i<6;i++) { for(j=0;j<6;j++) { if(p[i][j]<=p[i][k] + p[k][j]) p[i][j]=p[i][j]; else p[i][j]=p[i][k]+p[k][j]; } } } //traverse shortest path for(i=0;i<6;i++) { for(j=0;j<6;j++) { printf("%d ",p[i][j]); } printf("\n"); }

} 84. Shortest path using dijkstra's algorith main() { int int int int

x[8]; y[8]; i,j,label,s,c=1; flag[8];

int a[8][8]={ 0,100,3,100,100,100,3,100, 100,0,100,100,100,100,2,5, 100,100,0,3,100,100,100,100, 100,100,100,0,100,1,100,100, 100,2,100,100,0,2,100,2, 100,100,100,100,100,0,6,1, 100,2,4,4,1,100,0,100, 100,100,100,5,100,100,100,0 }; memset(flag,0,sizeof(flag)); printf("Enter source:"); scanf("%d",&label); flag[label]=1; for(i=0;i<8;i++) { x[i]=a[label][i]; } while(c<8) { label=smallest(x,flag); s=x[label]; for(i=0;i<8;i++) { y[i]=a[label][i]+s; if(x[i]>y[i]) x[i]=y[i]; } flag[label]=1; c++; } for(i=0;i<8;i++) { printf("%d ",x[i]); } } int smallest(int x[],int flag[]) {

int i,j,k; for(i=0;i<8;i++) { if(flag[i]!=1) { k=x[i]; break; } } for(i=0;i<8;i++) { if(flag[i]!=1) if(x[i]<k) { k=x[i]; j=i; } } return j; } 85. Find the path matrix of Graph main() { int a[6][6]={ 0,1,1,0,1,0, 0,0,0,1,0,0, 0,0,0,0,1,1, 0,0,1,0,0,1, 0,1,0,1,0,0, 0,0,0,0,0,0 }; int i,j,k; int p[6][6]; for(i=0;i<6;i++) { for(j=0;j<6;j++) { if(a[i][j]==0) p[i][j]=0; else p[i][j]=1; } } //calculate path matrix for(k=0;k<6;k++) { for(i=0;i<6;i++) {

for(j=0;j<6;j++) { p[i][j]=p[i][j]|(p[i][k] & p[k][j]); } } } //visit path matrix for(i=0;i<6;i++) { for(j=0;j<6;j++) { printf("%d ",p[i][j]); } printf("\n"); } } 86. Program to read the current date and time #include<time.h> main() { struct tm *k; unsigned long int t; int h,m,s; int dd,mm,yy; t = time(0); k = localtime(&t); h = k->tm_hour; m = k->tm_min; s = k->tm_sec; dd = k->tm_mday; mm = k->tm_mon+1; yy = k->tm_year+1900; printf("Time : %d : %d : %d\n",h,m,s); printf("Date : %d : %d : %d\n",dd,mm,yy); } 87. Program to read all files from current directory #include "dirent.h" main() { DIR *p; struct dirent *q; p = opendir("/home/satyam/Desktop/demo");

while((q=readdir(p)) != 0) { if(q->d_name[0] != '.') printf("%s\n",q->d_name); } }

Вам также может понравиться