Sunteți pe pagina 1din 42

INDEX 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15. 16. 17. 18. 19. 20. 21.

Asterisks graph Print pattern Floyds triangle Program to check whether a number is binary or not Conversion of decimal no: to binary Conversion of binary no: to decimal Conversion of lowercase to uppercase Absolute value of a number Check whether a number is prime or not Prime numbers between a range(from x to y) Count and list of prime numbers inside a list Reversing an integer array Deleting duplicate elements from an array(avoiding repetition) Largest and smallest elements of an array Inserting an element into an array Deleting an element from an array Bubble sorting Linear search Binary Search Quick Sorting Menu driven program to find I. Length of the string II. Copy one string to another III. Concatenation of one string to another IV. Comparing one string with another 22. Achromatic string 23. String palindrome 24. Count the no: of Characters, white spaces, words, vowels and sentences in a string 25. Replace a string with another 26. Menu driven program to print I. Sum of two matrices II. Norm of the matrix III. Trace of the matrix(sum of diagonal elements, main & off diagonal) 27. Print lower left and upper right triangles of the matrix 28. Print the transpose of the matrix and check whether it is symmetric or not 29. Product of two matrices 30. Simple structure storing the details of a student 31. Structure that is using array within the structure to read 5 marks of the student 32. Structure having array variable as the structure variable storing details of a no: of students 33. Program to sort the student records according to student id 34. Program to find the largest & smallest elements of array using functions(Call by value) 35. Program to find the largest & smallest elements of array using functions & pointers(Call by reference) 36. Program to print palindromes numbers in a given list using functions 37. Fibonacci series using recursion 38. Reverse a string using recursion 39. Factorial using recursion 40. Program to print Armstrong numbers in a range(1-1000) using functions 41. Program to print the Armstrong numbers in a given list using functions and pointers 42. Swapping using pointers

43. Sorting using pointers 1. ASTRERISK'S GRAPH #include<stdio.h> #include<conio.h> void main() { int i,j,r; clrscr(); printf("Enter the no: of rows\n"); scanf("%d",&r); printf("\n\nASTERISK'S GRAPH\n"); for(i=0;i<r;i++) { for(j=0;j<=i;j++) { printf("* "); } printf("\n\n"); } getch(); } OUTPUT Enter the no: of rows 5 ASTERISK'S GRAPH * * * * * * * * * * * * * * * 2. PRINT PATTERN 1 01 010 1010 10101 #include<stdio.h> #include<conio.h> void main() { int i,j,r,t; clrscr(); printf("Enter the no: of rows\n"); scanf("%d",&r); printf("\n\nPATTERN IS\n"); t=0; for(i=0;i<=r;i++) { for(j=0;j<=i;j++)

{ if(t%2==0) printf("0 "); else printf("1 "); t++; } printf("\n\n"); } getch(); } OUTPUT Enter the no: of rows 5 PATTERN IS 0 10 101 0101 01010 101010 3. FLOYD'S TRIANGLE #include<stdio.h> #include<conio.h> void main() { int i,j,t,r; clrscr(); printf("Enter the no: of rows\n"); scanf("%d",&r); t=1; printf("\n\nfloyd's triangle\n"); for(i=0;i<r;i++) { for(j=0;j<=i;j++) { printf("%d\t",t); t++; } printf("\n\n"); } getch(); } OUTPUT Enter the no: of rows 6 Floyd's triangle 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21

4. CHECK WHETHER A NO: IS BINARY OR NOT #include<stdio.h> #include<conio.h> void main() { long num,flag; clrscr(); printf("A BINARY NUMBER PLZZZZ\n"); scanf("%ld",&num); while(num>0) { if(num%10==0||num%10==1) flag=1; else { flag=0; break; } num/=10; } if(flag==1) printf("THE NUMBER IS A BINARY NUMBER\n"); else printf("THE NUMBER IS NOT BINARY\n"); getch(); } OUTPUT A BINARY NUMBER PLZZZZ 123 THE NUMBER IS NOT BINARY A BINARY NUMBER PLZZZZ 1000010110 THE NUMBER IS A BINARY NUMBER 5. CONVERSION OF A DECIMAL NUMBER INTO BINARY #include<stdio.h> #include<conio.h> void main() { int num,a[20],i,j; clrscr(); printf("\n Enter a decimal integer\n"); scanf("%d",&num); i=0; while(num>0) { a[i]=num%2; i++; //increment the index of the array num=num/2; } /*print the array in reverse order */ Printf(The binary equivalent is\n\n) for(j=i-1;j>=0;j--) //starts at last index and decrementing to the zero index

printf("%d",a[j]); getch(); } OUTPUT Enter a decimal integer 4 The binary equivalent is 100 Enter a decimal integer 6 The binary equivalent is 110 6.CONVERSION OF BINARY INTO A DECIMAL NUMBER #include<stdio.h> #include<conio.h> #include<math.h> void main() { int i,j,r; long num; clrscr(); printf("\nEnter binary number\n"); scanf("%ld",&num); j=0; r=0; while(num>0) { i=num%10; r=r+(i*pow(2,j)); num=num/10; j++; } printf("Decimal equivalent is\n%d",r); getch(); } OUTPUT Enter binary number 111 Decimal equivalent is 7 Enter binary number 1100 Decimal equivalent is 12 7. PROGRAM TO CONVERT LOWERCASE TO UPPERCASE #include<stdio.h> #include<conio.h> void main() { char txt[40]; int count=0,i; clrscr(); printf("Enter a line of text\n\n");

while((txt[count]=getchar())!='\n') count++; i=0; while(i<count) { putchar(toupper(txt[i]));//toupper() is a built-in function to change the lower case to upper i++; //tolower() to change case to lower case } getch(); } OUTPUT Enter a line of text structure and arrays STRUCTURE AND ARRAYS 8. PROGRAM TO PRINT THE ABSOLUTE VALUE OF A NUMBER #include<stdio.h> #include<conio.h> void main() { int a; clrscr(); printf("Enter the no:\n"); scanf("%d",&a); if(a<0) a=-a; printf("The absolute value of the no: is %d\n",a); getch(); } OUTPUT Enter the no: -90 The absolute value of the no: is 90 9. PROGRAM TO CHECK WHETHER A NUMBER IS PRIME OR NOT #include<stdio.h> #include<conio.h> void main() { int p,i,flag=1; clrscr(); printf("Enter the number\n\n"); scanf("%d",&p); for(i=2;i<=p/2;i++) { if(p%i==0) { flag=0; break; } } if(flag==1) printf("\n\n%d is PRIME NUMBER\n",p); else

printf("\n\n%d is NOT a PRIME NUMBER\n",p); getch(); } OUTPUT Enter the number 23 23 is PRIME NUMBER Enter the number 45 45 is NOT a PRIME NUMBER 10. PRINT PRIME NUMBERS FROM X TO Y #include<stdio.h> #include<conio.h> void main() { int x,y,i,j,flag; clrscr(); printf("Enter the range X-Y\n\n"); scanf("%d%d",&x,&y); printf("The prime no:s are\n"); for(i=x;i<y;i++) { flag=1; for(j=2;j<=i/2;j++) { if(i%j==0) { flag=0; break; } } if(flag==1) printf("%d ",i); } getch(); } OUTPUT Enter the range X-Y 25 100 The prime no:s are 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97 11. COUNT THE NUMBER OF PRIME NUMBERS IN A GIVEN LIST ALSO PRINT THAT NUMBERS #include<stdio.h> #include<conio.h> void main() { int prm[40],i,j,n,c=0,flag; clrscr(); printf("Enter the limit\n\n"); scanf("%d",&n); printf("Enter the no:s of the array(numbers other than 1)\n");

for(i=0;i<n;i++) scanf("%d",&prm[i]); printf("\n\nTHE PRIME NUMBERS ARE\n"); for(i=0;i<n;i++) { flag=1; for(j=2;j<=prm[i]/2;j++) { if(prm[i]%j==0) { flag=0; break; } } if(flag==1) { printf("%d ",prm[i]); c++; } } printf("\n\nThe number of prime numbers is %d \n",c); getch(); } OUTPUT Enter the limit 5 Enter the no:s of the array 17 19 45 47 49 THE PRIME NUMBERS ARE 17 19 47 The number of prime numbers is 3 12. PRINTING ARRAY IN REVERSE ORDER #include<stdio.h> #include<conio.h> void main() { int a[10],n,i; clrscr(); printf("Enter the limit\n"); scanf("%d",&n); printf("Enter the no:s\n"); for(i=0;i<n;i++) scanf("%d",&a[i]); printf("\nElements of array a :\n"); for(i=0;i<n;i++) printf("%d\t",a[i]); printf("\n\nIN REVERSE ORDER\n"); for(i=n-1;i>=0;i--) printf("%d\t",a[i]); getch(); } OUTPUT

Enter the limit 5 Enter the no:s 12345 Elements of array a : 1 2 3 4 5 IN REVERSE ORDER 5 4 3 2 1 13..DELETION OF DUPLICATE ELEMENTS FROM AN ARRAY #include<stdio.h> #include<conio.h> void main() { int a[20],b[20],n,i,j; clrscr(); printf("Enter the limit :\n"); scanf("%d",&n); printf("Enter the elements of the array\n"); for(i=0;i<n;i++) scanf("%d",&a[i]); printf("The elements of the array\n"); for(i=0;i<n;i++) printf("%d\t",a[i]); for(i=0;i<n;i++) { for(j=i+1;j<n;j++) { if(a[i]==a[j]) a[j]=0; } } j=0; for(i=0;i<n;i++) { if(a[i]!=0) { b[j]=a[i]; j++; } } printf("\nThe elements of the array after deleting duplcate elements\n"); for(i=0;i<j;i++) printf("%d\t",b[i]); getch(); } OUTPUT Enter the limit : 9 Enter the elements of the array 123215482 The elements of the array 1 2 3 2 1 5 4 8 2

The elements of the array after deleting duplcate elements 1 2 3 5 4 8 14.LARGEST N SMALLEST ELEMENT OF AN ARRAY #include<stdio.h> #include<conio.h> void main() { int a[30],i,n,large,small; clrscr(); printf("Enter the limit\n"); scanf("%d",&n); printf("Enter the elements of the array\n\n"); for(i=0;i<n;i++) scanf("%d",&a[i]); large=a[0]; //assigning 0th index value of the array to large & small small=a[0]; for(i=1;i<n;i++) { if(a[i]>large) large=a[i]; else if(a[i]<small) small=a[i]; } printf("\n Largest value : %d\n",large); printf("\n Smallest value : %d\n",small); getch(); } OUTPUT Enter the limit 5 Enter the elements of the array 345 65 45 87 3 Largest value : 345 Smallest value : 3 15. INSERTING AN ELEMENT INTO AN ARRAY TO A DESIRED POSITION #include<stdio.h> #include<conio.h> void main() { int a[25],b[20],n,in,pos,i; clrscr(); printf("Enter limit\n"); scanf("%d",&n); printf("Enter the elements of array\n"); for(i=0;i<n;i++) scanf("%d",&a[i]); printf("Enter the element to be inserted\n"); scanf("%d",&in); printf("The position\n");

scanf("%d",&pos); for(i=0;i<pos;i++) { b[i]=a[i];} b[pos]=in; for(i=pos+1;i<n+1;i++) b[i]=a[i-1]; printf("ELEMENTS OF ARRAY AFTER INSERTION\n"); for(i=0;i<n+1;i++) printf("%d ",b[i]); getch(); } OUTPUT Enter limit 5 Enter the elements of array 1 2 3 4 5 Enter the element to be inserted 10 The position 3 ELEMENTS OF ARRAY AFTER INSERTION 1 2 3 10 4 5 16. DELETING AN ELEMENT FROM A PARTICULAR POSITION OF AN ARRAY #include<stdio.h> #include<conio.h> void main() { int a[20],b[20],n,i,pos; clrscr(); printf("ENTER LIMIT\n"); scanf("%d",&n); printf("ELEMENTS PLZZZ\n"); for(i=0;i<n;i++) scanf("%d",&a[i]); printf("Enter the position from which the element has to be deleted\n"); scanf("%d",&pos); for(i=0;i<pos;i++); for(i=pos;i<n-1;i++) { a[i]=a[i+1]; } printf("ARRAY\n"); for(i=0;i<n-1;i++) printf("%d ",a[i]); getch(); } OUTPUT

ENTER LIMIT 5 ELEMENTS PLZZZ 12 34 65 47 86 Enter the position from which the element has to be deleted 2 ARRAY 12 34 47 86 17. BUBBLE SORTING #include<stdio.h> #include<conio.h> void main() { int a[20],i,j,n,t; clrscr(); printf("Enter the limit\n"); scanf("%d",&n); printf("Enter the no:s\n"); for(i=0;i<n;i++) scanf("%d",&a[i]); for(i=0;i<n-1;i++) { for(j=0;j<n-i;j++) { if(a[j]>a[j+1]) { t=a[j]; a[j]=a[j+1]; a[j+1]=t; } } } printf("The elements of array in ascending order\n"); for(i=0;i<n;i++) printf("%d\t",a[i]); getch(); } OUTPUT Enter the limit 5 Enter the no:s 56 34 87 23 12 The elements of array in ascending order 12 23 34 56 87 18.LINEAR SEARCH

#include<stdio.h> #include<conio.h> void main() { int a[20],i,j,x,n,f,t; clrscr(); printf("Enter the limit\n"); scanf("%d",&n); printf("Enter the no:s\n"); for(i=0;i<n;i++) scanf("%d",&a[i]); for(i=0;i<n-1;i++) { for(j=0;j<n-i;j++) { if(a[j]>a[j+1]) { t=a[j]; a[j]=a[j+1]; a[j+1]=t; } } } printf("The elements of array in ascending order\n"); for(i=0;i<n;i++) printf("%d\t",a[i]); printf("\nEnter the no: to be searched\n"); scanf("%d",&x); for(i=0;i<n;i++) { if(x==a[i]) { printf("The searched element is at position %d\n",i+1); f=0; break; } } if(f!=0) printf("Search unsuccessful\n"); getch(); } OUTPUT Enter the limit 5 Enter the no:s 23 76 12 9 87 The elements of array in ascending order 9 12 23 76 87

Enter the no: to be searched 23 The searched element is at position 3 19. BINARY SEARCH #include<stdio.h> #include<conio.h> void main() { int a[20],i,j,x,n,f=1,t,low,high,mid; clrscr(); printf("Enter the limit\n"); scanf("%d",&n); printf("Enter the no:s\n"); for(i=0;i<n;i++) scanf("%d",&a[i]); for(i=0;i<n-1;i++) { for(j=0;j<n-i-1;j++) { if(a[j]>a[j+1]) { t=a[j]; a[j]=a[j+1]; a[j+1]=t; } } } printf("The elements of array in ascending order\n"); for(i=0;i<n;i++) printf("%d\t",a[i]); printf("\nEnter the no: to be searched\n"); scanf("%d",&x); low=0;high=n; while(low<=high) { mid=(low+high)/2; if(x<a[mid]) high=mid-1; else if(x>a[mid]) low=mid+1; else if(x==a[mid]) { printf("%d is located at location %d\n",x,mid+1); f=0; break; } } if(f!=0) printf("Search is unsuccessful\n"); getch(); } OUTPUT Enter the limit 6

Enter the no:s 12 52 54 23 3 95 The elements of array in ascending order 3 12 23 52 54 95 Enter the no: to be searched 52 52 is located at location 4 Enter the limit 3 Enter the no:s 123 52 45 The elements of array in ascending order 45 52 123 Enter the no: to be searched 23 Search is unsuccessful 20. PROGRAM TO PERFORM QUICK SORT #include<stdio.h> #include<conio.h> int n; void sort(int a[],int,int); void main() { int a[20],i,l,r; clrscr(); printf("Enter the no: of elements\n"); scanf("%d",&n); printf("Enter the elements\n"); for(i=0;i<n;i++) scanf("%d",&a[i]); l=0; r=n-1; sort(a,l,r); printf("After QUICK sort:"); for(i=0;i<n;i++) printf("%d\t",a[i]); getch(); } void sort(int b[],int left,int right) { int i,j,p,temp,k,f; //int b[10],left,right; if(right>left)

{ i=left; j=right; p=b[left]; f=0; while(!f) { do { i++; } while(b[i]<=p&&i<=right); while(b[j]>=p&&j>left) { --j; } if(j<i) f=1; else { temp=b[i]; b[i]=b[j]; b[j]=temp; } } temp=b[left]; b[left]=b[j]; b[j]=temp; sort(b,left,j-1); sort(b,i,right); } } OUTPUT Enter the no: of elements 4 Enter the elements 12 65 23 2 After QUICK sort:2 12 23 65 #include<stdio.h> #include<conio.h> void main() { int c,i,j; char str1[30],str2[30],ch; clrscr(); printf("Eneter the string\n\n"); scanf("%s",str1); do { printf("1:LENGTH OF STRING\n"); printf("2:CONCATENATE(IF STRINGS ARE NOT EQUAL)\n"); printf("3:COPY ONE STRING OVER ANOTHER\n"); printf("Eneter your choice among three\n");

scanf("%d",&c); switch(c) { case 1 : case 2 :

case 3 :

default :

for(i=0;str1[i]!='\0';i++); printf("Length of the string is %d",i); break; printf("Enter the second string\n"); scanf("%s",str2); for(i=0;str1[i]!='\0';i++); str1[i]=' '; i++; for(j=0;str2[j]!='\0';j++) { str1[i]=str2[j]; i++; } str1[i]='\0'; printf("The string after concatenation is %s",str1); break; printf("Enter the second string\n"); scanf("%s",str2); for(j=0,i=0;str2[j]!='\0';j++,i++) { str1[i]=str2[j]; //i++; } str1[i]='\0'; printf("String1=%s",str1); printf("String2=%s",str2); break; printf("Invalid choice\n"); break;

} printf("\n\nDo you want to continue(Yes\No)\n"); scanf(" %c",&ch); } while(ch=='y'); getch(); } OUTPUT Enter the string Smith 1:LENGTH OF STRING 2:CONCATENATE(IF STRINGS ARE NOT EQUAL) 3:COPY ONE STRING OVER ANOTHER Eneter your choice among three 1 Length of the string is 5 Do you want to continue(YesNo) y 1:LENGTH OF STRING 2:CONCATENATE(IF STRINGS ARE NOT EQUAL)

3:COPY ONE STRING OVER ANOTHER Eneter your choice among three 2 Enter the second string John The string after concatenation is Smith John Do you want to continue(YesNo) y 1:LENGTH OF STRING 2:CONCATENATE(IF STRINGS ARE NOT EQUAL) 3:COPY ONE STRING OVER ANOTHER Eneter your choice among three 3 Enter the second string rim String1=rimString2=rim Do you want to continue(YesNo) n #include<stdio.h> #include<conio.h> void main() { char str[50],acr[20]; int i,j; clrscr(); printf("\n\tEnter the string to which u have to find the achronime\n"); scanf("%[^\n]",str); acr[0]=toupper(str[0]); i=1; j=1; while(str[i]!='\0') { if(str[i]==' ') { acr[j]=toupper(str[i+1]); j++; } i++; } acr[j]='\0'; printf("The Achronime is %s",acr); getch(); } #include<stdio.h> #include<conio.h> void main() { char str[50],acr[20]; int i,j;

clrscr(); printf("\n\tEnter the string to which u have to find the achronime\n"); scanf("%[^\n]",str); acr[0]=toupper(str[0]); i=1; j=1; while(str[i]!='\0') { if(str[i]==' ') { acr[j]=toupper(str[i+1]); j++; } i++; } acr[j]='\0'; printf("The Achronime is %s",acr); getch(); } OUTPUT Enter the string to which u have to find the achronime kerala state road transport corporation The Achronime is KSRTC PROGRAM TO CONCATENATE TWO STRINGS IF THEY ARE NOT EQUAL #include<stdio.h> #include<conio.h> void main() { char s1[20],s2[20]; int i,j; clrscr(); printf("Enter the two strings\n"); scanf("%s%s",s1,s2); //COMPARE STRINGS i=0; while(s1[i]!='\0'&&s2[i]!='\0') { if(s1[i]==s2[i]) { i++; } else break; } if(s1[i]=='\0'&&s2[i]=='\0') printf("STRINGS ARE EQUAL\nNEED NOT TO CONCATENATE\n"); else { //CONCATENATION for(i=0;s1[i]!='\0';i++); s1[i]=' '; //place one space after the 1st string i++; for(j=0;s2[j]!='\0';j++)

{ s1[i]=s2[j]; i++; //increment the index value of source string } s1[i]='\0'; //put '\0' at the end of the string after appending the 2nd string printf("CONCATENATED STRING IS : %s",s1); } getch(); } OUTPUT Enter the two strings cap cap STRINGS ARE EQUAL NEED NOT TO CONCATENATE Enter the two strings cap map CONCATENATED STRING IS : cap map Enter the two strings cap caps CONCATENATED STRING IS : cap caps 21. MENU DRIVEN PROGRAM TO FIND I. LENGTH OF THE STRING II. COPY ONE STRING TO ANOTHER III. CONCATENATION OF ONE STRING TO ANOTHER #include<stdio.h> #include<conio.h> void main() { int c,i,j; char str1[30],str2[30],ch; clrscr(); printf("Eneter the string\n\n"); scanf("%s",str1); do { printf("1:LENGTH OF STRING\n"); printf("2:CONCATENATE(IF STRINGS ARE NOT EQUAL)\n"); printf("3:COPY ONE STRING OVER ANOTHER\n"); printf("Eneter your choice among three\n"); scanf("%d",&c); switch(c) { case 1 : for(i=0;str1[i]!='\0';i++); printf("Length of the string is %d",i); break; case 2 : printf("Enter the second string\n"); scanf("%s",str2);

case 3 :

default :

for(i=0;str1[i]!='\0';i++); str1[i]=' '; i++; for(j=0;str2[j]!='\0';j++) { str1[i]=str2[j]; i++; } str1[i]='\0'; printf("The string after concatenation is %s",str1); break; printf("Enter the second string\n"); scanf("%s",str2); for(j=0,i=0;str2[j]!='\0';j++,i++) { str1[i]=str2[j]; } str1[i]='\0'; printf("String1=%s",str1); printf("String2=%s",str2); break; printf("Invalid choice\n"); break;

} printf("\n\nDo you want to continue(Yes\No)\n"); scanf(" %c",&ch); } while(ch=='y'); getch(); } OUTPUT Eneter the string Smith 1:LENGTH OF STRING 2:CONCATENATE(IF STRINGS ARE NOT EQUAL) 3:COPY ONE STRING OVER ANOTHER Eneter your choice among three 1 Length of the string is 5 Do you want to continue(YesNo) y 1:LENGTH OF STRING 2:CONCATENATE(IF STRINGS ARE NOT EQUAL) 3:COPY ONE STRING OVER ANOTHER Eneter your choice among three 2 Enter the second string John The string after concatenation is Smith John Do you want to continue(YesNo) y 1:LENGTH OF STRING 2:CONCATENATE(IF STRINGS ARE NOT EQUAL)

3:COPY ONE STRING OVER ANOTHER Eneter your choice among three 3 Enter the second string rim String1=rimString2=rim Do you want to continue(YesNo) n 22. ACHROMATIC STRING #include<stdio.h> #include<conio.h> void main() { char str[50],acr[20]; int i,j; clrscr(); printf("\n\tEnter the string to which u have to find the achronime\n"); scanf("%[^\n]",str); acr[0]=toupper(str[0]); i=1; j=1; while(str[i]!='\0') { if(str[i]==' ') { acr[j]=toupper(str[i+1]); j++; } i++; } acr[j]='\0'; printf("The Achronime is %s",acr); getch(); } OUTPUT Enter the string to which u have to find the achronime kerala state road transport corporation The Achronime is KSRTC 23. PROGRAM TO CHECK WHETHER A STRING IS PALINDROME OR NOT #include<stdio.h> #include<conio.h> #include<string.h> void main() { int i,j,n,f=1,k=0; char str[50],rev[50]; clrscr(); printf("Enter the string to be checked : "); gets(str); for(i=0;str[i]!='\0';i++); i--; for(j=i;j>=0;j--)

{ rev[k]=str[j]; k++; } rev[k]='\0'; i=0; while(rev[i]!='\0'&&str[i]!='\0') { if(rev[i]==str[i]) i++; else { f=0; break; } } if(rev[i]=='\0'&&str[i]=='\0'&&f==1) printf("The string is a palindrome\n"); else printf("the string is not palindrome\n"); getch(); } OUTPUT Enter the string to be checked : amma The string is a palindrome 24. COUNT THE NUMBER OF CHARACTERS, WHITE SPACES, WORDS, VOWELS NAD SENTENCES IN A GIVEN STRING #include<stdio.h> #include<conio.h> void main() { int i,c=0,w=0,wrd=0,v=0,s=0; char str[100]; clrscr(); printf("Enter the string as a paragraph\n"); scanf("%[^\n]",str); i=0; // 0th index position of the string while(str[i]!='\0') { if((str[i]>='A'&&str[i]<='Z')||(str[i]>='a'&&str[i]<='z')) c++; if(str[i]=='a'||str[i]=='e'||str[i]=='i'||str[i]=='o'||str[i]=='u') v++; if(str[i]==' '||str[i]=='.') wrd++; if(str[i]==' ') w++; if(str[i]=='.') { s++; c++; } if(str[i]==','||str[i]==':'||str[i]==';')

c++; i++; } printf("\n\t printf("\n\t printf("\n\t printf("\n\t printf("\n\t getch(); SENTENCES = %d",s); WORDS = %d",wrd); WHITE SPACES = %d",w); VOWELS = %d",v); CHARACTERS = %d",c);

} OUTPUT Enter the string as a paragraph He is smart, intelligent, clever.He is efficient.But he is not talkative. SENTENCES = 3 WORDS = 13 WHITE SPACES = 10 VOWELS = 23 CHARACTERS = 63 25. REPLACE A STRING WITH ANOTHER #include<stdio.h> #include<conio.h> void main() { int i,j,k,pos,x; char s[50],s1[20],s2[20],test[20],nw[50]; clrscr(); printf("Enter the text : "); scanf("%[^\n]",s); printf("\nEnter the string to be replaced in the read text : "); scanf("%s",s1); printf("\nEnter the string that is replacing the string in the text :"); scanf("%s",s2); i=0; while(s[i]!='\0') { j=0; while((s[i]!=' ')&&(s[i]!='\0')) { test[j]=s[i]; i++; j++; } pos=i; test[j]='\0'; if(strcmp(test,s1)==0) { for(i=0;i<k;i++) nw[i]=s[i]; for(j=0;((s2[j]!='\0'));j++) { nw[i]=s2[j]; i++;

} x=pos; while(s[x]!='\0') { nw[i]=s[x]; x++; i++; } nw[i]='\0'; break; } i++; k=i; } printf("\nThe string after replacement is :"); puts(nw); getch(); } OUTPUT Enter the text : He is smart Enter the string to be replaced in the read text : smart Enter the string that is replacing the string in the text : intelligent The string after replacement is :He is intelligent Enter the text : This man is not systematic Enter the string to be replaced in the read text : is Enter the string that is replacing the string in the text : was The string after replacement is :This man was not systematic 26. PROGRAM TO COUNT THE NUMBER OF APPEARANCE OF A SINGLE WORD IN A STRING #include<stdio.h> #include<conio.h> void main() { int i,j,c=0; char s[50],s1[20],test[20]; clrscr(); printf("Enter the text : "); scanf("%[^\n]",s); printf("\nEnter the string to be checked : "); scanf("%s",s1); i=0; while(s[i]!='\0') { j=0; while((s[i]!=' ')&&(s[i]!='\0')&&(s[i]!='.'&&(s[i]!=','))) { test[j]=s[i]; i++; j++; } test[j]='\0'; if(strcmp(test,s1)==0)

{ c++; } i++; } printf("The word '%s' is repeated %d times in this string\n",s1,c); getch(); } OUTPUT Enter the text : she is cute she is mad she is weak she is strong Enter the string to be checked : she The word 'she' is repeated 4 times in this string 27. MENU DRIVEN PROGRAM TO PRINT i. SUM OF TWO MATRICES ii. LOWER LEFT TRIANGLE iii. UPPER RIGHT TRIANGLE iv. NORM OF THE MATRIX v. TRACE OF THE MATRIX(SUM OF DIAGONAL ELEMENTS, MAIN & OFF DIAGONAL #include<stdio.h> #include<conio.h> #include<math.h> void main() { int a[5][5],b[5][5],c[5][5]; int m,n,p,q,i,j,ch1,main,off,s; float norm; char ch; clrscr(); printf("Enter the order of matrix\n"); scanf("%d%d",&m,&n); printf("Enter the elements of the matrix\n"); for(i=0;i<m;i++) { for(j=0;j<n;j++) scanf("%d",&a[i][j]); } do { printf("1: Addition\n"); printf("2: Lower left triangle\n"); printf("3: Upper right triangle\n"); printf("4: Norm of the matrix\n"); printf("5: Trace of the matrix\n"); printf("Enter your choice\n"); scanf("%d",&ch1); switch(ch1) { case 1 : clrscr(); printf("The order of second matrix\n"); scanf("%d%d",&p,&q); printf("Enter the elements of the second matrix\n");

case 2 :

for(i=0;i<p;i++) { for(j=0;j<q;j++) scanf("%d",&b[i][j]); } if(m==p&&n==q) { for(i=0;i<m;i++) { for(j=0;j<n;j++) c[i][j]=a[i][j]+b[i][j]; } printf("The 1st matrix is\n\n"); for(i=0;i<p;i++) { for(j=0;j<q;j++) printf("%d\t",a[i][j]); printf("\n"); } printf("The 2nd matrix is\n\n"); for(i=0;i<p;i++) { for(j=0;j<q;j++) printf("%d\t",b[i][j]); printf("\n") ; } printf("The matrix sum is\n\n"); for(i=0;i<p;i++) { for(j=0;j<q;j++) printf("%d\t",c[i][j]); printf("\n"); } } else printf("Addition is not possibe\n\n"); break; if(m==n) { printf("The lower left triangle is:\n"); for(i=0;i<m;i++) { for(j=0;j<n;j++) { if(i<=j) printf("%d\t",a[i][j]); else printf("\t"); } printf("\n"); } } else

printf("Triangle cannot be printed\n"); case 3 : break; if(m==n) { printf("The upper right triangle is:\n"); for(i=0;i<m;i++) { for(j=0;j<n;j++) { if(i>=j) printf("%d\t",a[i][j]); else printf("\t"); } printf("\n"); } } else printf("Triangle cannot be printed\n"); case 4 : break; if(m==n) { s=0; for(i=0;i<m;i++) { for(j=0;j<n;j++) { s=s+a[i][j]*a[i][j]; } } norm=sqrt(s); printf("\n\tNORMAL = %f",norm); } else printf("Normal Cannot be found"); break; case 5 :if(m==n) { main=0; off=0; for(i=0;i<m;i++) { for(j=0;j<n;j++) { if(i==j) { main=main+a[i][j]; } if((i+j)==(m-1)) { off=off+a[i][j]; }

} } printf("\n\tTHE MAIN DIAGONAL SUM IS %d",main); printf("\n\t THE OFF DIAGONAL SUM IS %d",off); } else default : } printf("\nDo you want to continue :"); scanf(" %c",&ch); } while(ch=='y'); getch(); } OUTPUT Enter the order of matrix 22 Enter the elements of the matrix 1246 1: Addition 2: Lower left triangle 3: Upper right triangle 4: Norm of the matrix 5: Trace of the matrix Enter your choice 2 The lower left triangle is: 1 2 6 Do you want to continue :y 1: Addition 2: Lower left triangle 3: Upper right triangle 4: Norm of the matrix 5: Trace of the matrix Enter your choice 3 The upper right triangle is: 1 4 6 Do you want to continue :y 1: Addition 2: Lower left triangle 3: Upper right triangle 4: Norm of the matrix 5: Trace of the matrix Enter your choice 4 NORMAL = 7.549834 printf("Diagonal Sum cannot be found\n"); break; printf("Invalid choice\n"); break;

Do you want to continue :y 1: Addition 2: Lower left triangle 3: Upper right triangle 4: Norm of the matrix 5: Trace of the matrix Enter your choice 5 THE MAIN DIAGONAL SUM IS 7 THE OFF DIAGONAL SUM IS 6 Do you want to continue :n 28. PRINT THE TRANSPOSE OF THE MATRIX AND CHECK WHETHER IT IS SYMMETRIC OR NOT #include<stdio.h> #include<conio.h> void main() { int a[10][10],b[10][10],flag=0; int i,j,m,n; clrscr(); printf("Enter the order of the matrix\n"); scanf("%d%d",&m,&n); printf("enter the elements\n"); for(i=0;i<m;i++) { for(j=0;j<n;j++) scanf("%d",&a[i][j]); } printf("OUR MATRIX IS :\n"); for(i=0;i<m;i++) { printf("\n"); for(j=0;j<n;j++) printf("%d\t",a[i][j]); } for(i=0;i<n;i++) { for(j=0;j<m;j++) b[i][j]=a[j][i]; } printf("\nThe matrix transpose is\n"); for(i=0;i<n;i++) { printf("\n"); for(j=0;j<m;j++) printf("%d\t",b[i][j]); } if(m==n) { for(i=0;i<m;i++) {

for(j=0;j<n;j++) { if(b[i][j]==a[i][j]) flag=0; else { flag=1; break; } } } if(flag==0) printf("\nThe matrix is symmetric\n"); else printf("\nThe matrix is not symmetric\n"); } else printf("\nSymmetricity cannot be checked\n"); getch(); } OUTPUT Enter the order of the matrix 33 enter the elements 100 010 001 OUR MATRIX IS : 1 0 0 0 1 0 0 0 1 The matrix transpose is 1 0 0 0 1 0 0 0 1 The matrix is symmetric Enter the order of the matrix 23 enter the elements 123 456 OUR MATRIX IS : 1 2 3 4 5 6 The matrix transpose is 1 4 2 5 3 6 Symmetricity cannot be checked 29. PRODUCT OF TWO MATRICES Refer fair record

30. SIMPLE STRUCTURE STORING THE DETAILS OF A STUDENT #include<stdio.h> #include<conio.h> struct std { char name[20]; int rn; char brnch[10]; int total; }s; void main() { clrscr(); printf("\n\tENTER STUDENT INFORMATION"); printf("\n\tNAME : "); scanf("%s",s.name); printf("\n\tROLL NO : "); scanf("%d",&s.rn); printf("\n\tBRANCH : "); scanf("%s",s.brnch); printf("\n\tTOTAL : "); scanf("%d",&s.total); printf("\n\tSTUDENT RECORD"); printf("\n\tNAME\tROLL NO: BRANCH\tTOTAL\n"); printf("\t%s\t %d\t %s\t %d\n",s.name,s.rn,s.brnch,s.total); getch(); } OUTPUT ENTER STUDENT INFORMATION NAME : aaa ROLL NO : 12 BRANCH : cd TOTAL : 123 STUDENT RECORD NAME ROLL NO: BRANCH TOTAL aaa 12 cd 123 31. STRUCTURE THAT IS USING ARRAY WITHIN THE STRUCTURE TO READ 5 MARKS OF THE STUDENT #include<stdio.h> #include<conio.h> struct student { char name[10]; int m[6]; int total; float avg; }; void main() { struct student s; int j,n; clrscr(); printf("Enter the student information\n");

printf("\nName : "); scanf("%s",s.name); printf("\nEnter the 6 marks : "); s.total=0; for(j=0;j<5;j++) { scanf("%d",&s.m[j]); s.total=s.total+s.m[j]; } s.avg=s.total/5; printf("STUDENT RECORD\n\n"); printf("Name\t Mark1 Mark2 Mark3 Mark4 Mark5 printf("\n%s\t ",s.name); for(j=0;j<5;j++) { printf(" %d ",s.m[j]); } printf(" %d ",s.total); printf(" %f ",s.avg); getch(); } OUTPUT Enter the student information Name : aaa Enter the 6 marks : 76 87 56 79 96 STUDENT RECORD Name Mark1 Mark2 Mark3 Mark4 Mark5

Total

Average\n\n");

Total

Average

aaa 76 87 56 79 96 394 78.000000 32. STRUCTURE HAVING ARRAY VARIABLE AS THE STRUCTURE VARIABLE STORING DETAILS OF A NO: OF STUDENTS #include<stdio.h> #include<conio.h> struct student { char name[10]; int m[6]; int total; float avg; }; void main() { struct student s[10]; int i,j,n; clrscr(); printf("Enter the no: of students\n"); scanf("%d",&n); for(i=0;i<n;i++) {

printf("Enter the student %d information\n",i+1); printf("\nName : "); scanf("%s",s[i].name); printf("\nEnter the 6 marks : "); s[i].total=0; for(j=0;j<6;j++) { scanf("%d",&s[i].m[j]); s[i].total=s[i].total+s[i].m[j]; } s[i].avg=s[i].total/6; } printf("STUDENT RECORD\n\n"); printf("Name\t Mark1 Mark2 Mark3 Mark4 Mark5 Mark6 Total for(i=0;i<n;i++) { printf("\n%s\t ",s[i].name); for(j=0;j<6;j++) { printf(" %d ",s[i].m[j]); } printf(" %d ",s[i].total); printf(" %f ",s[i].avg); } getch(); } OUTPUT Enter the no: of students 3 Enter the student 1 information Name : aaa Enter the 6 marks : 45 56 59 86 65 69 Enter the student 2 information Name : bbb Enter the 6 marks : 59 67 49 81 76 56 Enter the student 3 information Name : ccc Enter the 6 marks : 58 46 68 79 86 59 Average\n\n");

STUDENT RECORD Name Mark1 Mark2 Mark3 Mark4 Mark5 Mark6 Total Average aaa 45 56 59 86 65 69 380 63.000000 bbb 59 67 49 81 76 56 388 64.000000 ccc 58 46 68 79 86 59 396 66.000000 33. PROGRAM TO SORT THE STUDENT RECORDS ACCORDING TO STUDENT ID #include<stdio.h> #include<conio.h> struct std { char name[20]; int id; char branch[10]; }; void main() { int i,j,n; struct std s[30],temp; clrscr(); printf("Enter the no: of students\n"); scanf("%d",&n); for(i=0;i<n;i++) { printf("Enter student %d information\n",i+1); printf("Student Name :"); scanf(" %s",s[i].name); printf("\nStudent ID :"); scanf("%d",&s[i].id); printf("\nBranch :"); scanf("%s",s[i].branch); } for(i=0;i<n;i++) { for(j=0;j<n-i-1;j++) { if(s[j].id>s[j+1].id) { temp=s[j]; s[j]=s[j+1]; s[j+1]=temp; } } } printf("\n\n\tSTUDENT RECORDS IN SORTED ORDER\n"); printf("\n\tSTUDENT ID\tNAME\tBRANCH\n\n"); for(i=0;i<n;i++) printf("\n\t\t%d\t%s\t%s\n",s[i].id,s[i].name,s[i].branch); getch(); } OUTPUT Enter the no: of students 3 Enter student 1 information

Student Name :abc Student ID :21 Branch :cs Enter student 2 information Student Name :pqr Student ID :3 Branch :cs Enter student 3 information Student Name :xyz Student ID :10 Branch :me STUDENT RECORDS IN SORTED ORDER STUDENT ID 3 10 NAME BRANCH pqr xyz cs me

21 abc cs 34. PROGRAM TO FIND THE LARGEST & SMALLEST ELEMENTS OF ARRAY USING FUNCTIONS(CALL BY VALUE METHOD) #include<stdio.h> #include<conio.h> int i; int smallest(int a[],int); int largest(int a[],int); void main() { int n,s,l,a[20]; clrscr(); printf("Enter the LIMIT :\n"); scanf("%d",&n); printf("Enter the numbres\n"); for(i=0;i<n;i++) scanf("%d",&a[i]); s=smallest(a,n); l=largest(a,n); printf("The LARGEST element = %d\n The SMALLEST element = %d\n",l,s); getch(); } int smallest(int x[],int d) { int small=x[0]; for(i=1;i<d;i++) { if(x[i]<small) small=x[i]; } return small; }

int largest(int x[],int d) { int large=x[0]; for(i=1;i<d;i++) { if(x[i]>large) large=x[i]; } return large; } OUTPUT Enter the LIMIT : 6 Enter the numbres 45 67 98 3 76 23 The LARGEST element = 98 The SMALLEST element = 3 35. PROGRAM TO FIND THE LARGEST & SMALLEST ELEMENTS OF ARRAY USING FUNCTIONS & POINTERS(CALL BY REFERENCE METHOD) #include<stdio.h> #include<conio.h> int i; int smallest(int*,int); int largest(int*,int); void main() { int n,s,l,a[20]; clrscr(); printf("Enter the LIMIT :\n"); scanf("%d",&n); printf("Enter the numbres\n"); for(i=0;i<n;i++) scanf("%d",&a[i]); s=smallest(a,n); l=largest(a,n); printf("The LARGEST element = %d\n The SMALLEST element = %d\n",l,s); getch(); } int smallest(int*p,int d) { int small=*p; //value at the 0th index position of the array for(i=1;i<d;i++) { if(*(p+i)<small) small=*(p+i); } return small; } int largest(int *p,int d) { int large=*p;

for(i=1;i<d;i++) { if(*(p+i)>large) large=*(p+i); } return large; } OUTPUT Enter the LIMIT : 3 Enter the numbres 34 76 2 The LARGEST element = 76 The SMALLEST element = 2 36. PROGRAM TO PRINT PALINDROME NUMBERS IN A GIVEN LIST USING FUNCTIONS #include<stdio.h> #include<conio.h> void pal(int a[],int); int i; void main() { int a[20],n; clrscr(); printf("LIMIT :"); scanf("%d",&n); printf("\nEnter the numbers :"); for(i=0;i<n;i++) scanf("%d",&a[i]); printf("PALINDROMES \n"); pal(a,n); getch(); } void pal(int x[],int d) { int s,temp; for(i=0;i<d;i++) { s=0; temp=x[i]; while(x[i]>0) { s=s*10+(x[i]%10); x[i]=x[i]/10; } if(temp==s) printf("%d ",s); } } OUTPUT LIMIT :5 Enter the numbers :2 575 44 890 111 PALINDROMES

2 575 44 111 37. PROGRAM TO REVERSE A STRING SING RECURSION #include<stdio.h> #include<conio.h> void reverse(); void main() { clrscr(); printf("Enter a string\n\n"); reverse(); } void reverse() { char c; if((c=getchar())!='\n') reverse(); putchar(c); getch(); } OUTPUT Enter a string program margorp 38. PROGRAM TO PRINT THE FIBONACCI SERIES USING RECURSION #include<stdio.h> #include<conio.h> void fib(int); //prototype declaration void main() { int n; clrscr(); printf("Enter the no: of terms u want 2 print\n"); scanf("%d",&n); printf("FIBONACCI SEQUENCE\n"); fib(n); //function call getch(); } void fib(int n) { static int a,b; int c; if(n<2) { a=0; b=1; } else { fib(n-1);

c=b; b=a+b; a=c; } printf("\t%d",a); getch(); } OUTPUT Enter the no: of terms u want 2 print 10 FIBONACCI SEQUENCE 0 1 1 2 3 5 8 13 21 34 39. FACTORIAL USING RECURSION Refer fair record 40. PROGRAM TO PRINT ARMSTRONG NUMBERS IN A RANGE(1-1000) USING FUNCTIONS #include<stdio.h> #include<conio.h> void arms(); void main() { clrscr(); printf("\nARMSTRONG NUMBERS : "); arms(); getch(); } void arms() { int rev,i,j,d,temp; for(i=1;i<1000;++i) { rev=0; temp=i; // to get the number after processing of while loop j=i; // to keep the loop variable i constant while(j>0) { d=j%10; rev=rev+(d*d*d); j=j/10; } if(temp==rev) printf("%d ",temp); } } OUTPUT ARMSTRONG NUMBERS : 1 153 370 371 407 41. PROGRAM TO PRINT THE ARMSTRONG NUMBERS IN A GIVEN LIST USING FUNCTIONS AND POINTERS #include<stdio.h> #include<conio.h> void arms(int *,int); void main() {

int a[20],n,i; int *p; clrscr(); printf("LIMIT :"); scanf("%d",&n); printf("\nELEMENTS :"); for(i=0;i<n;i++) scanf("%d",&a[i]); printf("\nARMSTRONG NUMBERS : "); arms(a,n); getch(); } void arms(int *p,int n) { int rev,i,d,temp; for(i=0;i<n;i++) { rev=0; temp=*(p+i); while(*(p+i)>0) { d=*(p+i)%10; rev=rev+(d*d*d); *(p+i)=*(p+i)/10; } if(temp==rev) printf("%d ",rev); } } OUTPUT LIMIT :3 ELEMENTS :1 234 153 ARMSTRONG NUMBERS : 1 153 42. SWAPPING USING POINTERS #include<stdio.h> #include<conio.h> void swap(int *,int *); void main() { int x,y; clrscr(); printf("Enter 2 nos\n\n"); scanf("%d%d",&x,&y); printf("Before exchange : x= %d y = %d\n\n",x,y); swap(&x,&y); printf("After exchange : x= %d y = %d\n\n",x,y); getch(); } void swap(int *p,int *q) { *p=*p+*q; *q=*p-*q; *p=*p-*q;

} OUTPUT Enter 2 nos 12 45 Before exchange : x= 12 y = 45 After exchange : x= 45 y = 12 43. SORTING USING POINTERS #include<stdio.h> #include<conio.h> void main() { int a[20],i,j,n,t; int *p; clrscr(); printf("Enter the limit\n"); scanf("%d",&n); p=&a[0]; printf("Enter the no:s\n"); for(i=0;i<n;i++) scanf("%d",p+i); for(i=0;i<n-1;i++) { for(j=0;j<n-i-1;j++) { if(*(p+j)>*(p+j+1)) { t=*(p+j); *(p+j)=*(p+j+1); *(p+j+1)=t; } } } printf("The elements of array in ascending order\n"); for(i=0;i<n;i++) printf("%d\t",*(p+i)); getch(); } OUTPUT Enter the limit 4 Enter the no:s 123 54 65 34 The elements of array in ascending order 34 54 65 123

S-ar putea să vă placă și