0% found this document useful (0 votes)
181 views

C Language Programs

The Basic PDF for starting your learning in C language.

Uploaded by

BalJIK
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
0% found this document useful (0 votes)
181 views

C Language Programs

The Basic PDF for starting your learning in C language.

Uploaded by

BalJIK
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
You are on page 1/ 46
C Language Programs GENERAL PROGRAMS 4.1 First C program #include +#include int main elrser0) printi("Hello world\n’); getchd; return 0; ) 2. I! Display the Entered Integer N #tinclude #include int main elrser(; int a; printf("Enter an integer scanf("%d", &a); printf("Integer that you have entered is %din*, a) getch; return 0; } 3. 11 Addition of 2 integer No. #include #include maind { clrser0) int a, b, ¢; printf(‘Enter two numbers to add\n"); scanf("Sd%d" 84,8); czar; printf("Sum of entered numbers = %d\ getchd; return 0; } 4. Il To Check - Odd or Even #include #include main elrser(); Prepared By V Ramasamy , Lecturer , Dept. of CSE , PARK Colleg int; scant("%d", 8): if ((nvay'2== ny print{("Even\n else printt("Odd\ getchd; return 0 y 5. / Arithmetic Operation #include #include int main { elise; int first, second, add, subtract, multiply; float divide: printf('Enter two integers\n’) scanf("%d%d", &first, &Second); ist * second first / (float)second; //typecasting printfc'Sum = %din",add); printf (Difference = %d\n" subtract); printf (Multiplication = %d\n", muttipiy); print{(’Division = %.2fn" divide); getcho, return 0; y 6. // To Check vowels Hinclude #include ‘main() clrser0; char ch; printf (Enter a characterin’ seanf("%c", &ch); if (ch == 'a" || ch =="A" || ch =="e' || ch =: Il.ch ==" || ch==0 | ch == "u"|j ch printf("%c is a vowel.\n", ch); ‘E'|Joh 5) ron else print(%c is not a vowel. in", ch); getchd; return 0; ) www .ramsinfodesk.webs.com e of Engg & Tech , Coimbatore. C Language Programs 7.1 To Check vowels #include #include main { elrser(); ter a characterin’); scanf("%c", &ch); sswiteh(ch) { case'a’ case's’ case'e’ case 'E' case case" case’o’ case 'O' case'u' case'U’ printf("%c is a vowel.\n", ch); break; default: printf("%0 is not a vowel.\n", ch). d getchd; return 0; } 8. Ii To Check vowels int check_vowel(char a) { ‘A aha a it azat+'a case orusea=a+ 32" Z) * Converting to lower it return 1; getcho; return 0; } 9. 1/To check leap year #include #include int main clrser): int year, printi(“Enter a year to check if itis a leap yearn"); scanf(""%d", &year); Prepared By V.Ramasamy , Lecturer , Dept. of CSE , PARK Colleg: If ( year%400 == 0) printf("ed is a leap year.\n", year): else if ( year%100 year) printf ("%ed is a leap year.\n", year); else printf('%ed is not a leap year.\n", year); getchd: return 0; y 10. // To find sum of digits #include #include main { clrser0; int n, sum = 0, remainder, printf('Enter an integer\n" scanf("2éd",&n); while(n '= 0) { remainder = n % 10; ‘sum = sum + remainder, n=n/10; y printf¢'Sum of digits of entered number = d\n" sum); getcho; return 0; y 11. To find sum of digits #include #include int add_digits(int); int main { clrserQ; int n, result; printf('s6din", result); getchd; return 0; www.ramsinfodesk.webs.com ¢ of Engg & Tech , Coimbatore. C Language Programs int add_digits(int n) { static int sum = 0; if(n==0) { getch); return 0; y ‘sum = n%10 + add_digits(n/10); retum sum; } 412. I To find sum of digits #include #include main { clrser() int n, sum = 0, remainder; printf(’Enter an integer\n"); scanf("%d",&n); while(n |= 0) { remainder = n % 10: sum = sum + remainder n= n/10; , rintf("Sum of digits of entered number Sedin*,sum); getchd; retumn 0 ) 413.1 To find factorial #include #include int main elrser() into, n, fact = 1; printt¢ factorial\n’) scanf("%éd", &n); inter a number to calculate it's for (¢= 1;¢.<=n; c++) fact = fact * ¢; printt(’Factorial of %d = 96d\n", n, fact): getchd; return 0; Prepared By V.Ramasamy , Lecturer , Dept. of CSE , PARK Colleg: 14, To find factorial #include #include long factorial(int) int main { clrserd int number long fact print(('Enter a number to calculate it's factorialn"); scanf("*ed", &number) printf('%éd! = %Id\n", number, factorial(number)): getchd, return 0; y long factorial(int n) { inte; long result return result; 418. // To find factorial #include #include long factorial(int) int main { clrserQ; int num; long f; printf('Enter a number to find factorial soanfi"%ed", &num); if (oum <0) printf("Negative numbers are not allowed \n"); else { 1 factorial(num); print (96d! = 961d\n", num, 9} ) getchd; return 0; ) www .ramsinfodesk.webs.com e of Engg & Tech , Coimbatore. C Language Programs long factorial(int n) { if(n==0) return 1; else return(n * factorial(n-1)) ) 46. //To find LOM #include +#include int main { clrser() int a, b,x, y, t, ged, lem; printf("Enter two integers\n"); scanf("%d%d", 8x, 8) aex bey, while (b I= 0) £ y god = a; lem = (y)/ged; printi("Greatest common divisor of %ed and Sd = %edin*, x, y, ged): printtc' Sbdin*, x, y, Jom): east common multiple of %d and %d getcho, retum 0; getchd; } 417. IITo find LOM #include #include Jong ged(long, long); int main { elrser() long x, y, hof, lem; printi("Enter two integers\n"); scanf("%ld%ld", &x, &y); hhof = goatx, y): tom = (ryyihef, Prepared By V-Ramasamy , Lecturer, Dept. of CSE , PARK College of En; 4 printf(’Greatest common divisor of 96ld and %id = Sbldin", x, y, het); printf(Least common multiple of Seld and %ld = Sbldin", x, y, lem); getcho) return 0; y long ged(tong a, long b) { if(@==0) retum a; retum god(b, a % b): 18. /[To find LOM Hinclude #include long ged(tong, long); int main { clrserd long x, y, het, lem; printf¢Enter two integers\n’); scanf("96ld%6Id", &x, &y); hof = god(x, y): lom = (xtyy/het; print((’Greatest common divisor of %6Id and %id = Sbldin", x, y, het); printf(’Least common multiple of S6ld and %ld = edn", x, y, lem); getcho; return 0; y long ged(tong x, long y) { if == 0) 4 retum y; } while (y != 0) if > yp { KEX-Yi » www.ramsinfodesk.webs.com & Tech , Coimbatore C Language Programs else y retum x ) 49. //To find Binary No. #include #include #include char *decimal_to_binary(int); main { clrser0) intn, ©, ks char “pointer; printf(Enter an integer in decimal number system\n’); seant('%d",&n) point print decimal_to_binary(n); ‘Binary string of %d is: %s\n", n, ©); free(pointer); getend: return 0; } char *decimal_to_binary(int n) { int c, d, count char “pointer; pointer = (char*)malloc(32#1); if (pointer == NULL) exit(EXIT_FAILURE); for(c=31;¢>=0;c~) { d=n>>o; (4&1) *(pointer+count) = 1+ else: *(pointer+count) = 0 +0" count++; } *(pointertcount) = retum pointer; ) Prepared By V-Ramasamy , Lecturer, Dept. of CSE , PARK College of En; 20. //To find Binary No. #include #include int main { alrserd int n, ok; printf(Enter an integer in decimal number system\n’) scanf("96d", &n); printf¢’%éd in binary number system is:\n", n); for (¢= 31; ¢>= 0; c~) 4 k=n>>e; kat) printt( else printt("0"); y printf¢\n"); getch() return 0; y 21. //To find NCR No. #include #include long factorial(int); long find_ner¢int, int); long find_npr(int, int); maing { clrserd; int, long ner, npr; printf(Enter the value of n and fin"); seanf("%ed%d", 8,81); ner = find_ner(n, 1); getcho, return 0; y www.ramsinfodesk.webs.com & Tech , Coimbatore C Language Programs Yong find_ner(int n, int») { long result; factorial(n)/(factorial(r)*factorial(n-r)); return result; ) Yong find_npri(int n, int 1) long result; result factorial(n)/factorial(n-r): return result; } Jong factorial(int n) into; long result = 1 for(e=1;¢<=n; cr) result = result*c; return (result ); } 22. i/ Sum of entered integers #include #include int maing { clrser0) int n, sum =, 6, value: printf(’Enter the number of integers you want to add\n printf(*Enter %d integers\n" for (c= 1; ¢ <= n; o++) { scanf("2%d",valuey; sum = sum'+ value: } printf('Sum of entered integers = %d\n",sum); getch): retum 0; ) 23. i! Sum of entered integers #include #include int main { Prepared By V-Ramasamy , Lecturer , Dept. of CSE . PARK Colleg: clrser0; int n, sum = 0, ©, array[100}; scant("%4d", &n): for (c= 0;¢ #include int main { clrser0; int x, y, temp; printf("Enter the value of x and y\ scanf("9d%6d", &x, &y); printf("Before Swapping\nx = %ed\ny = %din" x.y) temp = x, x=y: emp; printf("After Swapping\nx = %diny = 9d\n".xy); getcho; return 0; y 28. 1] Swap of 2 int Nos #include #include int main clrser0; int a, b; printf("Enter two integers to swap\n"); scanf("9d%d", &a, &b); at; www.ramsinfodesk.webs.com ¢ of Engg & Tech , Coimbatore. C Language Programs 26. 1! Swap of 2 int Nos #include #include int main { elrser(); intx, y, a, *B, temp; printf('Enter the value of x and yin" Scanf("%d%d", 8x, &y); printf(’Before Swapping\nx = Yd\ny = d\n", x. y): printf(“After Swapping\nx = 9%d\ny = %d\n", x, y); getch: return 0; } 27.11 Swap of 2 int Nos #include #include void swap(int’, int"); int main frser0 int, ys printf(’Enter the value of x and yin" printf("Before Swapping\nx = %d\ny = Sédin", x, y); swap(&x, &y); print(*After Swapping\nx = Yediny = %d\n", x, y); getchd: return 0; } void swaptint “a, int *b) { int temp; Prepared By V.Ramasamy , Lecturer , Dept. of CSE , PARK Colleg: 28. | Swap of 2 int Nos #include #include int main clrserQ; intx, y; scanf("%d%d", 8x, &y); printte'x, = %dlny = %dIN", x, y); print getcho; return 0; y 29. 1] Reverse of entered number #include #include maing { clrscrQ; int n, reverse = 0; inter a number to reverse\n") &n) ) reverse * 10, reverse + n%10; printt¢” scant("%6d" while (a { reverse 10; , printf('Reverse of entered number is = %d\ getchd: return 0; y 30. / To check palindrom #include #include main { clrscrd: int n, reverse = 0, temp; www.ramsinfodesk.webs.com ¢ of Engg & Tech , Coimbatore. C Language Programs printf('Enter a number to check ifit is a palindrome or notin’) scant( '%6d", 8); temp = n; while( temp I= 0) { reverse = reverse * 10; reverse = reverse + temp%10; temp = temp/t0; } if(n == reverse ) printf (“9d is a palindrome number.\n*, n); else printf (“96d is not a palindrome number.\n*, n): getchd: return 0; ) 31. // star display using loops #include #include main { elrser(: int row, ©, n, temp; printf('Enter the number of rows in pyramid of stars you wish to see"): scant("%6q',&n) temp = n; for ( row = 1 ; row <= nn ; row++) e #include Prepared By V-Ramasamy , Lecturer, Dept. of CSE , PARK College of En; maing clrser0; inte, ks printf(*Enter number of rows\n"); scanf("%d",8n): for(e=1;0<=nj;0H) for = 0; k++) printtc"* print” getcho) return 0; y 33. // star display using loops #include #include int main { clrserQ; inti, ca printt¢ printin scanfi"%d", &n); ter the number of rows of Floyd's triangle to for (i { for (C= 1:6 <= 1; o4) ice nj ie) printf(*%6d ",a); att y print y getcho; return 0; y 34, // star display using loops "n #include #include main « clrscrd; www.ramsinfodesk.webs.com & Tech , Coimbatore C Language Programs print{("Enter number of rows\n"); scanf("%d",8n) for(c=1;0<=n; c++) for (k= 1;k <=6; k++) printf("%c.", ch): printf'\n"); chet; y getchd; return 0; ) 36. I! star display using loops #tinclude #include main { elrser(): int n, ¢, k, space = 0; char ch, temp; scanf("%d", &n) ch temp for(k=n;k>=1;k-) © <= space; c++) printf("%c" temp); temp: } printfe\n"); ch- temp = ch; } getch): retum 0; ) 36. // pascal triangle #tinclude #include Jong factorial(int); Prepared By V Ramasamy , Lecturer , Dept. of CSE main { alrserd inti, nyc printf(’Enter the number of rows you wish to see in pascal triangle\n’); sscant("%6d",&n); for (i=O;i #include int main { clrserQ; int n, ¢, k, space = 1 printf("Enter number of rows\n"); seanf("%d", &n); space =n - 4 for (k= 1k <= ke) © <= space; c++) 0<= 2k; oF) www.ramsinfodesk.webs.com & Tech , Coimbatore C Language Programs printf("\n’ ) space for (k= 1; k <= n= 1; e+) { for (c= 1; ¢ <= space; c++) print"); space++; *(n-k)-1; oF+) getchd; return 0; } 38. // To check prime No. #include #include maing { clrserd: int n, i= 3, count, c; printf('Enter the number of prime numbers required\n"); scanf("2%6d",&n) Sed prime numbers are :\n",n); for (count = 2; count <=n; ) { for (c=2;0<= { if (i960 break; y if(c=i) print(c’%ecin",); count++; y } getchd) retum 0; } ot) ) Prepared By V.Ramasamy , Lectur 10 99. 11 To check prime No. #include #include maino { clrscr0; int, = 2; printf("Enter a number to check if tis prime\n"); scanf(°%d",&n); for(c=2;0<= -150e) { if (n%e == 0) printf(’96d is not prime.\n", n) break; y » if(e rintf("%d is prime.\n", n); getchd; return 0; } 40. 1! To check prime No. #include #include int check_prime(inty; main { clrser0; int n, result; print{("Enter an integer to check whether itis prime or not.\n") scanf("2%d".&n); result = check_prime(n); if (result == 1) printf("%d is prime.\n", n); else printf("%d is not prime.\n", n); getchd; return 0; y int check_prime(int a) { www.ramsinfodesk, webs.com Dept. of CSE , PARK College of Engg & Tech , Coimbatore. C Language Programs for(c=2:¢ a-tsott) it (a%e == 0) getch); return 0; } i(c==a) retum 1 , 41.1/ str palindrom #include #include #include main clrser0: char a[100}, b[100); printf('Enter the string to check if itis a palindrome\n’);, gets(a) stropy(b.a): stirev(b): if( stromp(a.b) == 0 ) printf(’Entered string is a palindrome.\n’ else printf('Entered string is not a palindrome.\n"); getchd; return 0; ) 42. 11 To check palindrom include #include main { clrser0; int n, reverse = 0, temp; printf('Enter a number to check if itis a palindrome or notin’ while( temp = 0) { reverse = reverse * 10; reverse = reverse + temp%10; temp = temp/10 } if (n == reverse) printf("éd is a palindrome number.\n", n) Prepared By V.Ramasamy , Lectur u else printf("%d is not a palindrome number.\n*, n); getchd, return 0; } 43. 11 To check palindrom #include #include #include main { clrserQ; char text{100}; int begin, middle, end, length = 0: gets(text); while (text[length] !: length++; end = length - 4; middle = length/2; for( begin = 0 ; begin < middle ; begin++ ) if (text{begin] != textfend] ) { printf¢’Not a palindrome.’ break; y end; } if( begit idle ) printf("Palindrome.\n"); getcho; return 0; y 44, 1! To check palindrom #include #include int is_palindrome(char’); void copy_string(char*, char*); void reverse_string(char’); int string_length(char‘); int compare_string(char*, char): main { clrser0; char string[100}; int result; print{("Enter a string\n"); www.ramsinfodesk, webs.com Dept. of CSE , PARK College of Engg & Tech , Coimbatore. C Language Programs gets(string) result = is_palindrome(string); if (result == 1) %s\" is a palindrome string.\n", string) Is not a palindrome string.\ string) int is_palindrome(char *string) int check, length; char *reverse; length = string_length(string): reverse = (char*)malloc(length+1); copy_string(reverse, string) reverse_string(reverse); check = compare_string(string, reverse); free(reverse); i (check == 0) return 1; else getch0; return 0; , int string_lengthchar *string) int length = 0: ‘while(’string) length++; string++; ) return lengthy , void copy_string(char “target, char *source) ‘while(*source) “target = *source; sourcet#: target++ } target , void reverse_string(char *string) int length, c: char *begin, “end, temp; Prepared By V.Ramasamy , Lecturer , Dept. of CSE , PARK Colles length = string_length(string) begin = string; end = string; for(c=0;0<(length-1); c++) end++; for (c= 0; 6 #include main { clrser0; intr; long number = 0, c, sum = 0, temp; print{("Enter the maximum range upto which you want to find armstrong numbers *) seanf("%ld", &number); print{("Following armstrong numbers are found from 1 to 6ld\n",number) for(¢= 1; ¢ <= number; c++) temp=c; ‘while( temp www.ramsinfodesk.webs.com gz & Tech , Coimbatore. C Language Programs 13, if(o== sum) printf’s6ld\n", c); sum = 0; ) getch0; getcho return 0; ) 46. /* Fibonacci Series c language */ include #include int Fibonacei(int); maing { clrser0; int n, i= 0,6; scanf("9ed", &n); printf(*Fibonacci series\n"); for(c=1;0<= os) { printf('%d\n", Fibonacci()); ) getch; return 0; y int Fibonacei(int n) { if(n==0) getch(; return 0; retum ( Fibonacci(n-1) + Fibonacci(n-2) ); , 47. 1] Matrix multiplication #include #include int main { clrser0; int m,n, p.q,c.d, k, sum = 0; int first[40][10}, second 0}(10}, multiply{10}(10}; printf("Enter the number of rows and columns of first matrixin’) scanf("%d%d", &, &n): printf("Enter the elements of first matrix\n"); Prepared By V.Ramasamy , Lectur for( c=0;c #include main { clrserQ; int first, second, *p, “a, sum; print{("Enter two integers to addin’); scanf("2%d%d", &first, &second); www.ramsinfodesk, webs.com Dept. of CSE , PARK College of Engg & Tech , Coimbatore. C Language Programs sum =*p+*q: printf¢'Sum of entered numbers = %d\n",sum); getchd: return 0; y 49. " Bubble sort code */ Hinclude #include int maing { clrser0: int array[100}, n, c,d, swap; print{("Enter number of elementsin’); scanf("%d", &n); printi("Enter %d integers\n*, n); for (c scant(’ for (¢=0;0<(n-1); c++) { for (d= 0;d array{d+1)) /* For decreasing order use < ” { swap = arrayld}; array[d] = arrayld+1}; arrayld+1] = swap: y y print{("Sorted list in ascending order: for (c=0;c #include void bubble_sort(long {], long); int main { clrserQ: Prepared By V.Ramasamy , Lectur long array(100}, n, ©, d, swap; number of elements\n"); . &n); printf (“Ent scant("96l printf ("Enter %ld longegers\n', n); for (¢= 0; stay swapping“ tld istld+ 1); t t list{d] list{d+1] } y y y 51.// Array maximum #include #include int main { clrscr0; int array[100}, maximum, size, ¢, location print{("Enter the number of elements in array\n"); scanf("%d", &size) printf("Enter %d integers\n*, size); for (c= 0; ¢ < size; c++) seanf("*ed", &arrayle)); maximum = array(0]; www.ramsinfodesk.webs.com . Dept. of CSE, PARK College of Engg & Tech , Coimbatore. C Language Programs for ( { 1:0 size; ct#) if (array{c] > maximum) locatio y print{(’Maximum element is present at location number 96d and it's value is %d.\n", location, maximum); getch() return 0; y 82.) Array maximum arraylc oH include #include int main { clrserQ; long array[100}, *maximum, size, ¢, locatio print{("Enter the number of elements in array\n scanf("%6ld", &size); printt¢ nter S6ld integersin’, size); for (c=0;¢ *maximum) { smaximum = *(array+c): location = c+ y y print{("Maximum element is present at location number eld and it's value is %ld.\n", location, *maximum); getchd; retum 0; } 83. / Array minimum Hinclude #include maing t clrserQ; int array[100}, minimum, size, ¢, locatior Prepared By V.Ramasamy , Lecturer , Dept. of CSE , PARK Colles printf('Enter the number of elements in array\n"); scanf("%d",&size) printf("Enter %d integers\n*, size); for (c=0;c #include main { clrscr0; int array[100), “minimum, size, o, location = 4 printf(’Enter the number of elements in array\n") scanf("%éd",&size); printf¢Enter %d integersin”, size); for (c= 0; ¢ #include main clrser0) int array[100], search, , number; printf(’Enter the number of elements in array\n’ scanf("9éd",&number); printf('Enter 9d numbers\n", number): for (c= 0; ¢ #include maing { clrserQ; int array(100), search, ¢, n, count printf(’Enter the number of elements in array\n’ Scanf("eed", &n); print{('Enter 9d numbers\n", n); for(c=0;c #include int linear_searchiint*, int, int); main { clrscrd; int array[100}, search, ¢, n, position; printf("Enter the number of elements in array\n scanf("%d",&n); printf("Enter %d numbers\n", n); for(c=0;c #include main { clrserQ; int ¢, first, last, middle, n, search, array[100}; printf(’Enter number of elements\n"); scanf("*6d",&n) printf('Enter %d integersin’, n); for(c=0;e last ) rintf(’Not found! 96d is not present in the list.\n", search); getcho; return 0; y 59. // Reversed array Hinclude #include int maing { clrser0; int n, ¢, d, aft00}, b{100}; printf scant("%6¢ Enter the number of elements in array\n" an); Prepared By V.Ramasamy , Lectur 7 printi("Enter the array elements\n"); * Copying reversed array into original * Here we are modifying original array, this is optional. 7 c #include int main { clrserd; int atray[100}, n, ¢, t, end; seanf("%d", &n); end=n-1 for (C= 0; e include #include void reverse_array(int, int); main clrsero: int n, 6, “pointer scant("%6« 8n); pointer = (int*)malloc(sizeof(int)*n) if( pointer == NULL ) exit(EXIT_FAILURE); reverse_array(pointer, n); printf(’Original array on reversal is\n"); O;c=0:c-,d++) *(Std) = *(pointerte); for(¢=0;c #include main { clrserQ: Prepared By V.Ramasamy , Lectur Dept. of CSE 18 int af5] = (1, 2, 3, 4, 5}, ¢, count = 4, bIS]; for(c=0;0 #include int main { clrscr0; int array[100}, position, ¢, n, value; printf("Enter number of elements in array\n scanf("%d", &n); printt(’Enter %d elements\n", n): for (0=0;0= position - 1; c~) array[c+1] = arraylc); array[position-1] = value; printf("Resultant array is\n’ for (= 0; ¢<=n; c++) printf("%d\n", arrayfc)); getcho; return 0; y 64, To delete ano. in Array #include #include www.ramsinfodesk, webs.com PARK College of Engg & Tech , Coimbatore. C Language Programs main { clrserQ; int array{100), position, ¢, n; printf(’Enter number of elements in array\n scanf("’éd", &n); printf('Enter %d elements\n’, n); for(c=0;c= n+1 ) printf(*Deletion not possible.\n"); else { for (c= position-1;c #include void merge(int (int, int int, int 0) int main clrserQ: int a[100}, b[100}, m, n, ¢, sorted{200}; print{("input number of elements in first arrayin") seanf("%d", &m); printf("Input %d integers\n", m); for (¢=0;c printf("input number of elements in second array\ scanf("%d", &n); printf("input %d integersin*, n); for (C= 0;c #include int main { clrscr; int array[100}, n, ¢, d, swap; printf ("Enter number of elements\n"); scanf("%ed", &n); print{("Enter %d integers\n*, n) www.ramsinfodesk, webs.com Dept. of CSE , PARK College of Engg & Tech , Coimbatore. C Language Programs for (¢=0;¢ array[d+1)) /* For decreasing order use < ‘ swap = arrayiah: array[d] = array[d+1]; arrayid+1] = swap: ? ) printf¢Sorted list in ascending order:\n"); for(c=0:c #include void bubble_sort(long {], long); int main clrserQ: long array[100}, n, ¢, d, swap; print{("Enter number of elementsin’);, seanf("%6ld", &n) print{("Enter %ld longegers\n’, n); for (¢= 0; 0 list{a+1)) ¢* Swapping * t lista t{d) = list{d+1}; list[d+1] = t; 68, * insertion sort ascending order */ #include #include int main { clrscrd; into, array{1000), ¢, d, t; printf(’Enter %d integers\n", n); for (c= 0;6 0 && arrayfa] < arrayld-1)) { t arraylal array(d) = arrayld-1]; array{d-1] = t an y y printf ("Sorted list in ascending order:\n"); for (¢=0;¢<=n- 1; 044) { printf(’d\n", arrayfe)); getcho, return 0; } 69. * Selection sort */ #include #include www.ramsinfodesk.webs.com . Dept. of CSE, PARK College of Engg & Tech , Coimbatore. C Language Programs main clrserQ; int array[100], n, ¢, d, position, swap; printf('Enter number of elements\n") scanf("9éd", &n): printf("Enter %d integers\n", n) arrayla] ) position = d; » if ( position t= ¢) { swap = array{c}; array{c] = arraylposition); arraylposition ) printf('Sorted list in ascending order:\n"); for(c=0;c #include maing clrsero) int m, n, c, d, first{10][10], second{t 0}{10}, sum{10}{10}; printf("Enter the number of rows and columns of matrix ‘Scanf("9d%d", &m, &1n): printf(‘Enter the elements of first matrixin") for(c=0;c #include int main { clrscr0; int m,n, ©, d, first{10]{10}, second|10}{10}, differencefi0}{10}; print{(“Enter the number of rows and columns of matrixin"); ‘seanf("%6d%d", &m, &n); printf"Enter the elements of first matrixin” c #include maing { clrser0: int m, n, 6, d, matrix{10]{10}, transpose[1 O][10}; printf("Enter the number of rows and columns of matrix ‘scanf("9d%d",&m,&n); printf(‘Enter the elements of matrix \n ea cae) for(d=0;d #include int maing { clrser0: int m,n, p,q, 6, d, k, sum = 0: int first[10][10], second1 0][10}, multiply{t0]{10}; Prepared By V.Ramasamy , Lectur 22 print((Enter the number of rows and columns of first matrixin"); scant("S6d%d", &m, &n); print{(‘Enter the elements of frst matrixin” for( c=0;¢ #include maino { olrser0; char array[20] = "Hello World”; www.ramsinfodesk.webs.com . Dept. of CSE, PARK College of Engg & Tech , Coimbatore. C Language Programs printf¢'%6s\n",array); getcho; return 0; y 7. 1] String Operation #include #include main clrserQ: ‘char array[100}; printf("Enter a string\n"); scanf("%6s", array); printf¢’You entered the string %s\n getcho; return 0: y 76. 1! String Operation include #include main) { elrser0; char af@0}; gets(a); printf("%s\n", a); getch0; return 0 ) 77.11 String Operation include #include main { clrser); char str{20}; scanf("%s",&str); Print{("96s" str) getch) return 0; y Prepared By V.Ramasamy , Lectur . Dept. of CSE 23 78. 1! String Operation #include #include main { clrscrd; char a[100);, seant("%I*in}", a) print("%sin", a) getchd, return 0; } 79. 1! String Operation #include #include #include maind { clrser0; char a[100}; int length; print("Enter a string to calculate it's length gets(ay, length = strlen(ay; printf"Length of entered string is = 9%d\n" length); getcho; return 0; } 80. /! String Operation #include #include #include main { clrscr; char {100} int length; printf("Enter a string to calculate i's lengthy gets(a, length = strlen(ay, print("Length of entered string is = d\n" length); www.ramsinfodesk.webs.com PARK College of Engg & Tech , Coimbatore. C Language Programs getcho) retum 0; y 81.// String Operation Hinclude #include #include int string_length(char *s) int c= 0; whilec(s+o)) or: return c; , 82. // String Operation #include #include #include maing t clrser0; char a[100}, b[100}; printf(’Enter the first string\n" gets(a); printf('Enter the second string\ gets(b) it( stremp(a,b) printf('Entered strings are equal.\n else printf('Entered strings are not equal.\n"); getch0; return 0; ) 83. 1 String Operation include #include int compare_string(char*, char main { clrser0: char first{100], second{100}, result; printfCEnter first string\v gets(first) Prepared By V Ramasamy , Lecturer , Dept. of CSE PARK Colles 24 printf("Enter second string\n"); gets(second); result = compare_string(first, second). if (result == 0) printf(*Both strings are same.n’); else printf('Entered strings are not equal.\n"); getch): return 0; y int compare_string(char *first, char *second) no") firste; second++; ) if( “first getch0; retum 0; else return -1; 84, /! String Operation #include #include #include main { clrscrQ; char sourcel] = °C program": char destination(50} stropy(destination, source); print{("Source string: %s\n", source); print{("Destination string: %s\n", destination); getcho; return 0; ¥ 85. 1! String Operation #include #include void copy_string(char*, char’); main { clrscrQ; www.ramsinfodesk.webs.com 1g & Tech , Coimbatore. C Language Programs char source[100], target[100}; printf¢’Enter source string\n" gets(source); copy_string(target, source); printf¢"Target string is "%s\"n", target); getchd; return 0: } void copy_string(char “target, char *source) while(’source) “target = “source: sourcet+: target++: ) “target = "0"; y 86. // String Operation #include #include #include main { clrser0: char a[100}, b[100}; printf(’Enter the first string\n" gets(a) printf(’Enter the second string\r gets(b); streat(a.b); printf('String obtained on concatenation is %s\n",a); getchd; getch() retum 0; } 87. 1 String Operation Hinclude #include void concatenate_string(char*, char’) maing { clrserQ; Prepared By V.Ramasamy , Lecturer , Dept. of CSE , PARK Colles 25 char original{100}, add{ 100} printf('Enter source string\n"); gets(original); printf("Enter string to concatenate\ gets(add); concatenate_string(original, add); printf¢'String after concatenation is \"%sV'\n", original); getch): return 0; y void concatenate_string(char “original, char “add) { whileCoriginal) original: whileCadd) { “original = “add; add++; original: } “original = "0"; y 88. /! String Operation #include #include #include main { clrser0; char arr{100}; print{(’Enter a string to reverse\n" gets(arr); strrevian); printf(Reverse of entered string is \n%s\n",arr) getcho; return 0; ¥ 89. /! String Operation #include #include int string_length(char‘); void reverse(char’) www.ramsinfodesk.webs.com gz & Tech , Coimbatore. C Language Programs maing { clrserQ; char string[100}; printf(’Enter a string\n"); gets(string); reverse(string) printf(’Reverse of entered string is "%st".\n", string); getch) retum 0; } void reverse(char “string) { int length, o; char *begin, *end, temp; length = string_length(string); for(¢=0;¢<(length- 1); oc) end++; for (c= 0; ¢= end) retum; c= *Gerbeg); “ex+beg) = "Ox+end); *(xtend) = c; reverse(x, ++beg, y 91.1! String Operation end); #include #include #include cchar* substring(char*, int, int); maid { clrscr; char string[100}, “pointer; int position, length print("Enter a string\n") gets(string); printf("Enter the position and length of substring scanf("2%d%d" position, &length); pointer = substring( string, position, length); printi("Required substring is \"%s\"\n", pointer) free(pointer); getchd; return 0; y °C substring function: It returns a pointer to the substring ” char *substring(char *string, int position, int length) { char “pointer; inte; www.ramsinfodesk, webs.com Dept. of CSE , PARK College of Engg & Tech , Coimbatore. C Language Programs pointer = malloc(length+1): if (pointer == NULL) printf("Unable to allocate memory.\n" exit(EXIT_FAILURE): ) for (c= 0; ¢ < postion -1 ; c++) stringt+; for (C= 0; ¢ #include #include #include cchar* substring(char’, int, int); maing { clrser0; char string[100}, “pointer; int position = 1, length . temp, string_length; printf(’Enter a string\n"); gets(string); temp = string_length = strien(string), print ‘Substring of '%s\" are\ string); while (position <= string_length) while (length <= temp) { pointer = substring(string, position, length); print{¢’%6s\n", pointer) {free(pointer) length++; } temp—; position++; length = 4 } getch0; return 0; > Prepared By V.Ramasamy , Lectur 27 /* Use substring function given in above ¢ program’? 93, ! String Operation #include #include Ainclude #include void sort_string(char’); maing { olrser0; char string[100}; printf("Enter some text\n"); gets(string);, sort_string(string), prinii("%sin", string); getcho; return 0; y void sort_string(char *s) € inte, d= 0, tength: char “pointer, *result, ch; length = strlen(s); result (char*)malloc(length+1): pointer = for (ch =a" ; ch <="z'; ch++) { for (c= 0; ¢< length ; c++) it (*pointer == ch) “(result+d) = “pointer; det y pointer; ¥ pointer = } *(resulted) = "0: stropy(s, result); free(result); y 94, ! String Operation #include #include www.ramsinfodesk.webs.com . Dept. of CSE, PARK College of Engg & Tech , Coimbatore. C Language Programs int maing { clrserQ; char text{100}, blank{100}; inte =0,d printf(’Enter some text\n"); gets(tex!); while (text{c] "oy { if ((text{c] =="* && text[o+1] blank{d] = text(c}; det; } oH; ) blankid printf("Text after removing blanks\n%s\n", blank); getcho; return 0; y 95. // String Operation #include #include #include #include define SPACE" main { clrser0; char string[100}, “blank, ‘start; int length, ¢ = 0, d = 0; printf"Enter a string\n"); gets(string); length = strien(string); blank = string; start = (char*)malloc(length+1); if (start == NULL) exit(EXIT_FAILURE); ‘while(*(blank+o)) { if (*(blank+c) == SPACE && “(blank+c+1) 0 else *(started) = *(blank+c); d+; Prepared By V.Ramasamy , Lectur ot } *(started)="0'; printf("%s\n", start); free(start geichy; return 0; y 96. /1 String Operation #include #include #include main() { cirserd; char stringl) trlwe in C"; printf('%s\n"striwr(string)): getchd), return 0; y 97. /! String Operation #include #include #include maing { clrscr0; char stringl] = "strupr in c” printf('%s\n",strupr(string)): getcho; return 0; } 98, /! String Operation #include #include Void upper_string(char’; main clrsor(; char string[100); print{("Enter a string to convert it into upper case\n’); www.ramsinfodesk, webs.com Dept. of CSE , PARK College of Engg & Tech , Coimbatore. C Language Programs gets(string): upper_string(string); printf('Entered string in upper case is \"%s\"\n", string); getch) retum 0; } void upper_string(char ‘string) ‘while(’string) if ("string >= 'a' && *string <= 2") { *string = *string - 32; , string++; } , 99. // String Operation #include #include void lower_string(char"); maing { clrscrQ; char string[100}; printf("Enter a string to convert it into lower case\n"); gets(string); lower_string(string); printf("Entered string in lower case is \"%s\"\n", string): getchd; return 0; y void lower_string(char “string) while(’string) { if (string >="A" 88 *string < string = *string + 32; y string++; } » 100. 1! String Operation Hinclude #include #include #include Prepared By V.Ramasamy , Lectur main { clrser0; char first[100], second[100}, “temp; print{("Enter the first string "); gets (first); printf("Enter the second string "); gets(second); printf(\nBefore Swapping\n’); printt ("First string: %s\n" first); printi("Second string: %s\n\n" second); temp (char*)malloc(100); stropy(temp, first) stropy(first,second);, strepy(second, temp); print("After Swappingin" printf (First string: %s\n" first); print{("Second string: %s\n" second): getcho; return 0; y 101. 1/ String Operation #include #include #include maing { clrscr0; cchar string[100}, chy int = 0, count{26] = {0}; print("Enter a string\n") gets(string); while ( string[c] != "0') /* Considering characters from ‘a’ to'z' only */ if (stringle] >= 'a' && string[o} <= count(string{c}'a']++; Z) ot , for(c=0;0<26;ct+) if( count{c} |= 0 ) printf(’%c occurs %d times in the entered string.\n",c+'a',count(c)); } www.ramsinfodesk, webs.com Dept. of CSE , PARK College of Engg & Tech , Coimbatore. C Language Programs getchd; return 0; y 102. 1/ String Operation #include #include Int check_anagram(char ], char [D; int main { clrserQ; char a[100}, b[100}; int fag: printf("Enter first string\n") gets(a); printf(’Enter second string\n"); gets(b); flag = check_anagram(a, b); s\" and \"s6s\" are anagrams.\n", a, b); printf(""'%st" and \"%s\" are not anagrams.\n*, a, b); getchd; return 0; ) int check_anagram(char af], char bf) { int first[26] = {0}, second|26} 0}, 0= 0; while (a{¢] != XO’) { first{a[c}'a'}++; while (b{o] != NO) { second|b{c}a'}++; or; } for (c= 0; 0 < 26; c++) if Girst{c] != secondfe)) getch(); return 0; } retum 1; Prepared By V.Ramasamy , Lectur 103. // String Operation #include #include #include int check_vowel(char); int main { clrscr0 char s[100), t{100}; inti, j= 0) printf ("Enter a string to delete vowels\n" gets(s); 0's ie) £ 0){ snot a vowel stropy(s, 1); /We are changing initial string printf (*String after deleting vowels: %s\n", s); getchd) return 0; } int check_vowel(char c) ‘switch(c) { case ‘a’ case ‘A’ retum 1; default getch0; return 0; } 104. // String Operation #include #include #include #include #define TRUE 1 #define FALSE 0 www.ramsinfodesk, webs.com Dept. of CSE , PARK College of Engg & Tech , Coimbatore. C Language Programs int check_vowel(char); main { clrserQ: char string[100}, “temp, *pointer, ch, *start; printf("Enter a string\n"); gets(string); temp pointer tring (char*ymalloc(100) if( pointer NULL) printf(*Unabie to allocate memory.\n" exit(EXIT_FAILURE), } start = pointer; whilec’temp) £ oy = temp; if (Icheck_vowel(ch) ) pointer pointer++; } temp++; } “pointer hi i pointer = start; strepy(string, pointer); /* If you wish to convert original string "7 free(pointer); printf("String after removing vowel is \"%s\"\n", string); getch0; relum 0; , int check_vowel(char a) t(azs'a'yja retum TRUE: retum FALSE; » 108. 1! copy File Hinclude #include #include Prepared By V.Ramasamy , Lectur main { clrser0; char ch, source_file[20), target_fle[20}; FILE “source, “target; print{("Enter name of file to copy\n gets(source_file); source = fopen(source_file, if( source == NULL) printf(’Press any key to exit..\n"); exit(EXIT_FAILURE); , print{("Enter name of target file\n") getsttarget_file);, target = fopentarget_file, "w"); if( target == NULL) folose(source); printf("Press any key to exit..\ exit(EXIT_FAILURE); , while(( ch = {getc(source) ) != EOF ) fputc(ch, target); print{("File copied successfully \ {elose(source); felose(target); getcho; return 0; y 106. // Delete File #include #include main { clrserd; int status; char fle_name[25]; printf("Enter the name of file you wish to delete\n"); gets(file_name); status = remove(file_name); if( statu ) print(("%s fle deleted successfully.\n" fle_name); www.ramsinfodesk, webs.com Dept. of CSE , PARK College of Engg & Tech , Coimbatore. C Language Programs else { printf('Unable to delete the file\n"); perror("Esror’) } getch) retum 0; } 107. // List File include #include #include #include maing { clrser0; int done; struct ffbik a; s any key to view the files in the current indfrst("."", 8,0); ‘while(/done) printf(’96s\n",a.ff_name); done = findnext(&a); ) getch0; getchd retum 0; ) 108. // Merge file Hinclude Hinclude #include main { clrser0; FILE "fst, *f52, *f char ch, file {20}, file2{20}, file3{20} printf(’Enter name of first fil gets(filet); printf('Enter name of second file ") gets(file2): Prepared By V.Ramasamy , Lectur 32 print{("Enter name of fle which will store contents of two files"); gets(fles); Printf('Press any key to exit..\ getch0; exit(EXIT_FAILURE); y t= fopenc{files,"w' itt { perror(‘Error *) rintf(’Press any key to exit..\ exit(EXIT_FAILURE); y while( (ch = fgete(ts1) ) fputoch, tt); ULL) OF) while(( ch = fgete(ts2) ) fputc(ch,f); OF) printt("Two fil suecessfully.\n" were merged into %s fle le); felose(ts1); felose(ts2): felose(t); getchd), return 0; } 108. // Read file #include #include #include main { clrserd; char ch, fle_name[25]; FILE “fp; printf("Enter the name of fle you wish to see gets(file_name); {fp = fopen(file_name,"r’); // read mode if( fp { error("Error while opening the file.\n");, NULL) www.ramsinfodesk, webs.com Dept. of CSE , PARK College of Engg & Tech , Coimbatore. C Language Programs exit(EXIT_FAILURE); } printf("The contents of %s file are : \n\ file_name) while( (ch = fgete(fp) ) != EOF ) printf(*6c",ch);, fclose(tp); getch() retum 0; } 110. / Random Nos #include #include Hinclude int maing { clrser0; into, m; printf(’Ten random numbers in [1,100}\n for (0= 1; ¢<= 10; c++) { n= randQ%100 + 1; printfc'%éd\n*, n); getchd; retum 0; } 111.) Random Nos #include #include #include main { clrser0; int n, max, num, ¢; printf('Enter the number of random numbers you want ") scanf("%éd",&n) printf('Enter the maximum value of random number"); scanf("¥%éd",&max); printf("%d random numbers from 0 to %d are = \n',n,max); randomize(; for(c=1;0<=n;ct+) { num = random(max); printf(*%d\n",num); Prepared By V.Ramasamy , Lectur getcho; return 0; } 112. 1/ Sum of two complex numbers #include #include struct complex int real, img; x maid { clrscr0 struct complex a, b, ¢; printf(’Enter a and b where a + ib is the first complex number.\n"); printi(’a ="); scanf("%d", &a.real); print{("b ="): seanf("%d", &a.img); printf("Enter ¢ and d where c + id is the second complex number.\n" ” Cd", “&b.real); real + b.real; img + b.ima; if (c.img >= 0) printf(’Sum of two complex numbers = %d + %ai\n" ¢.real.c.img); else printf("Sum of two complex numbers = %d %adi\n" c.real.c.img); getchd), return 0; 113. // Current system date #include #include #include main { clrserQ; struct date d; getdate(&d): www.ramsinfodesk, webs.com Dept. of CSE , PARK College of Engg & Tech , Coimbatore. C Language Programs 34 printf("Current system date is getchd: ed/%d/%d" d.da_day,d.da_mon,d.da_yeary; return 0; } getcho, return 0; 117. 11 System Shutdown } Ainclude 114. /Hipeontig #include #include int main { main) clrser0; { ‘system('shutdown -P now"); clrser0; getchd; return 0; system("C:\Windows\\System32\\ipconfig" } system("pause’); MATH.H getcho return 0; } 118. 1/ Absolute Value 148. // System Shutdown Ainclude #include #include #include #include #include #include void main main { cirscr0; lrserd; float n; char oh; float result; clrser0; printf("Enter an integer to calculate it's absolute value\n") scanf("%f", &n); result = abs(n); it(eh printf("Absolute value of %f = %6f\n", n, result) system("C:WINDOWS\\System32\\shutdown -s getcho; ) getcho) return 0; 119. 1/ cos value } 116. // System Shutdown include #include #include main { clrserQ; char ch; printf(’Do you want to shutdown your computer now (yin) scanf("96c", &ch); if (oh == 'y'| oh ==") system("C:\\WINDOWS\System32\\shutdown -5"); Prepared By V Ramasamy , Lecturer , Dept. of CSE, PARK Coll #include #include #include #include void main { clrserd; double result; float x; clrscrQ; printf(\n\n Enter value scant("96f" 8) result = cos(x): 05(%.Aif) = 96.2If\n", x, result); getcho, } www.ramsinfodesk, webs.com eye of Engg & Tech , Coimbatore. C Language Programs 35 120. // Round off void main ‘ #include double a, b, result; include clrscr0; #include printf("inin Enter a and b to calculate a%b :"); scani("%iMiP", 8a, &b); void mainof result = pow(a, b); double n, result,resultt; printf(nin %.2If raised to %.2= %.2fin", a, b, lrsero; result); print{("inin Enter a number to round it down : " getchd; ) resultt=ceil(n); printfC’nin Original number = %.2it", n); 124, 1/ Power 10 value printf(wn\n Number rounded down using floor method = %.2i", result); #include #include printi(inin Number rounded down using ceil #include method = %.21f, result); void mainO{ getchy; int ) double result; ctrser0; 421.//1og value result = pow10(x); printf(\nin Ten raised to %d is %Ifin, x, result); #include #include getcho; #include } void maing) { double n, result; clrser0: printf("n\n Enter a number to calculate it's natural log (base is e) :") ‘scant("%I", &n); printfe\n\n Natural log of %.2If = %ifin",n, result); getcho, } 122. 1] log 10 value #include #include #include void mainot double n, result; clrser); printf("n\n Enter a number to calculate it's log(base is 10) seanf("*éif", &n); printf("\n\n Common log of %.2If= %.2Ifin", n, result); getcho; y 4123. // Power value #include #include #include Prepared By V Ramasamy , Lecturer , Dept. of CSE, PARK Coll 128. // Round off #include #include #include int main { clrscr; double n, result; printt¢ scanf(" ;nter a number to round it up\n"); I, &n); ceili); " result) 126. // sin value #include #include #include void maint double result, x = M_PI/6; in); www.ramsinfodesk, webs.com eye of Engg & Tech , Coimbatore. C Language Programs 127. 1] square root value #include #include Hinclude void maind) { double n, result; clrserQ; printf¢'n\n Enter a number to calculate it's square root: "); seanf("*éit", &n); result = sqrt(n);, printfe"n\n Square root of % 21 = %.2INn", n, result); getchd) y DOS.H 128. // delay 2 #include #include void maing) { inte=t.4 clrser0: printf¢’This c program will exit in 10 seconds. \n for (C= 1; ¢ <= 30000; c+*) { d <= 30000; d++) y getcho; y 4129. 1/ User Screen exit with delay #include #include #include void maing) { clrser0; printf ("This c program will exit in 10 seconds. \n delay(10000);, getchd, } 130. // Get Date Hinclude #include #include void maind) { struct date d; clrserQ: Prepared By V.Ramasamy , Lectur getdate(&a); rintf(’\n\n Current system date is, 4diMed/%d\n" d.da_day,d.da_mon,d.da_year); getchd; } 131. // SOUND #include main { clrscr; ssound(400); delay(1000); nosound(; getchd; return 0; } 132. i Set System Date #include #include #include void main( struct date d; clrscr0; printf¢'\n\n Enter the new date (day, month and year ) as integers "); scanf("d%ed%d",&d.da_day,&d.da_mon,&d.da_y ear) setdate(8d); printf(’\n\n Current system date is %diMed/M%din"d.da_day.d.da_mon,d.da_year) getcho; } 133. // Screen Sleep #include #include #include void main { clrserd; printf(’Wait for 5 seconds to exit.\n"); sleep(); y 134, 1/ Sound #include #include www.ramsinfodesk, webs.com Dept. of CSE , PARK College of Engg & Tech , Coimbatore. C Language Programs void maing) { int a; clrser); for (a = 200; a <= 1000;a=a+20) { sound(a); delay(25); y nosound); , 4135. 1/ Sound #include main { clrscr); int a; for (a= 200; a <= 1000;a=a+20) { soundia); delay(25); osoundd; getch0; retum 0; ) GRAPHICS.H 136. 1/ Arc #include #include void main() { clrserQ: int gd = DETECT, gm; initgraph(&ad, &gm, "C:ANTCNBGI); are(100, 100, 0, 135, 50); getcho; closegraph0; } 137. 1] Square Bar #include #include void main() { clrser0: Int gd = DETECT, gm; initgraph(&ad, 29m, "CATCNBGI): bar(100, 100, 200, 200); Prepared By V.Ramasamy , Lectur getcho), closegraph0; y 138, // 3D Bar #include #include void main { clrser0; int gd = DETECT, gm; initgraph(&gd, &gm, "C\TCNBGI bar3d(100, 100, 200, 200, 20, 1); getchd; closegraph0; } 139, // Bar Chart, #include #include void main « clrscrQ; int gd = DETECT, gm; initgraph(&gd, 8m, "C:NTCNBGN") setcolor( YELLOW); rectangle(0,30,639,450): settexistyle(SANS_SERIF_FONT.HORIZ_DIR,2); setcolor(WHITE): outtextxy(275,0,"Bar Chart”); setlinestyle(SOLID_LINE,0,2); line(100,420,100,60); line(100,420,600,420) line(90,70,100,60); line(110,70,100,60); line (580,410,600,420) line($80,430,600,420); outtextxy(95,35,"Y") outtextxy(610,405, outtextxy(85,415,’ tg setfilstyle(LINE_FILL,BLUE), bar(150,100,200,419); settilstyle(XHATCH _FILL,RED); bbar(225,150,275,419); setfilstyle(WIDE_DOT_FILL,GREEN) bbar(300,200,350,419); setflstyle(INTERLEAVE_FILL.MAGENTA); bar(375,125,425,419); www.ramsinfodesk, webs.com Dept. of CSE , PARK College of Engg & Tech , Coimbatore. C Language Programs setfilstyle(HATCH_FILL,BROWN) bar(450,175,500,479); getcho; y 140. 1! Moving Car #include #include #include void maing) { clrser0; int i,j = 0, gd = DETECT, gm; initgraph(&gd,&gm, \TCNBGI" settextstyle(DEFAULT_FONT,HORIZ_DIR,2) outtextxy(25,240, "Press any key to view the moving car"); getchd), setviewport(0,0,639,440,1); for( i= 0; 1 <= 420; { +10, jer) rectangle(50+i,275,150+i,400); rectangle(150+i,350,200+i,400); circle(75+i,410,10); circle(175+i,410,10): setcolor(): delay(100): (i == 420) break; clearviewportd; y getcho; closegraphd; , 141. 1/ Clear Screen #include #include void maing) { clrser0; int gd = DETECT, gm; initgraph(&gd, &gm, "CATCNBGI); outtext(‘Press any key to clear the screen.”); getcho; cleardeviced); outtext(*Press any key to exit. getcho; closegrapho; , Prepared By V.Ramasamy , Lectur 142, // Close Graphics Mode #include #include void main { clrserQ; int gd = DETECT, gm; initgraphi&gd, 29m, "C:NTCNBGN"); outtext("Press any key to close the graphics mode...") getchd; closegraph0; y 143. 1/ Count Down #include #include #include void main() { clrscr0; int gd = DETECT, gm, i; char af]: initgraph( &gd, &gm, "CA\TCNBGI") settextjustify( CENTER_TEXT, CENTER_TEXT ); settextstyle(DEFAULT_FONT,HORIZ_DIR,3) setcolor(RED); for(i= 30 ;1>=0; { sprintt(a."%d".): outtextxy(getmarx()/2, getmaxy(/2, a): delay(1000), if(i==0) break; cleardeviced; } getcho; closegraphd) y 144. 1/ Circle #include #include void main() clrscr0; int gd = DETECT, g initgraph(&ga, &gm, *CNTCNBGI") circle(100, 100, 50); www.ramsinfodesk, webs.com Dept. of CSE , PARK College of Engg & Tech , Coimbatore.

You might also like