C programming in first year, Study notes of C programming

All programs in c programming language

Typology: Study notes

2025/2026

Uploaded on 02/20/2026

mylavaram-geethika
mylavaram-geethika 🇮🇳

2 documents

1 / 25

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Compiled By Dept of ICT/SOC Page 1
SASTRA University
C-Programs
I-Btech
I. Simple Programs --03
1. Find area of a rectangle
2. Find ASCII value of a character
3. Convert Celsius to Fahrenheit
4. Swap value of two variables
5. Check the given number is odd or even
6. Check whether a character is vowel or consonant
7. Find largest among three numbers
8. Leap year checking
9. Positive negative checking
10. Quadratic equation
II. Programs using Loops --04
11. Factorial without using function
12. Table of N and square of N
13. Calculate x to the power y
14. Multiplication table
15. Sum of natural numbers
16. Fibonacci starting from any two numbers
17. Upper case to Lower case
18. Lower to upper
19. Pascal triangle
20. LCM & GCD
21. Prime numbers between two ranges
22. Factors of a number
23. Prime Factors
24. Bin to dec and oct
25. Count the number of digit in an integer
26. Reverse the digits of given number
27. Number palindrome
28. Digit summation
29. Amstrong checking
30. Make simple calculator in C
31. TO FIND SIN(X) USING SINE SERIES
32. Exponent series
33. Floyds Triangle
III. Programs using Arrays --07
34. Fibonacci using array
35. Largest among N numbers in an array
36. Smallest among N numbers in an array
37. Reverse the array elements
38. Insert an element in an array
39. Deleting an array element
40. Transpose of a matrix
41. Duplication removal
42. Linear Search
43. Binary search
44. Split the sorted array
45. Matrix addition
46. Matrix multiplication
47. Inverse of a 3X3 matrix
IV. Programs using Functions --10
48. Factorial using function
49. Min and Max of array
50. Bubble Sort
51. Convert :Bin to dec; dec to bin
52. Bin to oct; oct to bin
53. Dec to Hex
54. Oct to dec; dec to oct
55. Stack operation using function
56. Factorial using recursive function
57. Fibonacci using recursive function
58. Sum of N numbers using recursion
59. Reverse the sentence using recursion
60. Power using recursion
61. Towers of Hanoi
62. Exponent using recursion
63. GCD using recursion
V. Programs using Structures --14
64. Student structure
65. Players structure
66. Add two polynomials using structures in function
67. Add two distances using structures
68. Add two complex numbers
69. Calculate difference between two time period
VI. Programs using Strings --16
70. Program to Count Blanks,Tabs and Newlines
71. Palindrome checking
72. convert a name into its ascii values.
73. calculating string length without strlen function
74. comparing 2 strings without strcmp function
75. copying one string to another without using strcpy
76. string concatenation without using strcat function
77. Pattern replacement
78. Finding vowels
79. Sorting in alphabetical order
80. Searching sub string in a string
81. Find the frequency of a character in a string
82. Remove characters in string except alphabets
83. Reverse the given string
VII. Programs using Pointers --19
84. Area of circle using pointers
85. function pointers
86. duplication removal using pointers
87. Sorting integer array using pointers
88. Sum of array using pointers
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19

Partial preview of the text

Download C programming in first year and more Study notes C programming in PDF only on Docsity!

SASTRA University

C-Programs

I-Btech

I. Simple Programs --

  1. Find area of a rectangle
  2. Find ASCII value of a character
  3. Convert Celsius to Fahrenheit
  4. Swap value of two variables
  5. Check the given number is odd or even
  6. Check whether a character is vowel or consonant
  7. Find largest among three numbers
  8. Leap year checking
  9. Positive negative checking
  10. Quadratic equation II. Programs using Loops --
  11. Factorial without using function
  12. Table of N and square of N
  13. Calculate x to the power y
  14. Multiplication table
  15. Sum of natural numbers
  16. Fibonacci starting from any two numbers
  17. Upper case to Lower case
  18. Lower to upper
  19. Pascal triangle
  20. LCM & GCD
  21. Prime numbers between two ranges
  22. Factors of a number
  23. Prime Factors
  24. Bin to dec and oct
  25. Count the number of digit in an integer
  26. Reverse the digits of given number
  27. Number palindrome
  28. Digit summation
  29. Amstrong checking
  30. Make simple calculator in C
  31. TO FIND SIN(X) USING SINE SERIES
  32. Exponent series
  33. Floyds Triangle III. Programs using Arrays --
  34. Fibonacci using array
  35. Largest among N numbers in an array
  36. Smallest among N numbers in an array
  37. Reverse the array elements
  38. Insert an element in an array
  39. Deleting an array element
  40. Transpose of a matrix
  41. Duplication removal
  42. Linear Search
  43. Binary search
  44. Split the sorted array
  45. Matrix addition
  46. Matrix multiplication
  47. Inverse of a 3X3 matrix IV. Programs using Functions --
  48. Factorial using function
  49. Min and Max of array
  50. Bubble Sort
  51. Convert :Bin to dec; dec to bin
  52. Bin to oct; oct to bin
  53. Dec to Hex
  54. Oct to dec; dec to oct
  55. Stack operation using function
  56. Factorial using recursive function
  57. Fibonacci using recursive function
  58. Sum of N numbers using recursion
  59. Reverse the sentence using recursion
  60. Power using recursion
  61. Towers of Hanoi
  62. Exponent using recursion
  63. GCD using recursion V. Programs using Structures --
  64. Student structure
  65. Players structure
  66. Add two polynomials using structures in function
  67. Add two distances using structures
  68. Add two complex numbers
  69. Calculate difference between two time period VI. Programs using Strings --
  70. Program to Count Blanks,Tabs and Newlines
  71. Palindrome checking
  72. convert a name into its ascii values.
  73. calculating string length without strlen function
  74. comparing 2 strings without strcmp function
  75. copying one string to another without using strcpy
  76. string concatenation without using strcat function
  77. Pattern replacement
  78. Finding vowels
  79. Sorting in alphabetical order
  80. Searching sub string in a string
  81. Find the frequency of a character in a string
  82. Remove characters in string except alphabets
  83. Reverse the given string VII. Programs using Pointers --
  84. Area of circle using pointers
  85. function pointers
  86. duplication removal using pointers
  87. Sorting integer array using pointers
  88. Sum of array using pointers
  1. Count number of words using pointers
  2. Length of a string using pointers
  3. Reverse the String Using Pointers

VIII. Programs using Files --

  1. Write a sentence into a file
  2. Employee file
  3. Employee-struct
  4. Copying the content of one file into another
  5. Convert the file contents in Upper-case & Write Contents in a output file
  6. Compare two text/data files in C Programming
  7. Reading & writing in files
  8. ODD-EVEN splitting
  9. Copy from one text file into another
  10. Display same source code as output
  11. Read a string of text from a file IX. Miscellaneous -
  12. Multiple files-Prime factors
  13. Multiple files-String sort & search
  14. Dec to Bin using bits
  15. Find Largest element element using dynamic memory allocation
  16. Matrix multiplication using dynamic memory allocation
  17. Add Digits of the Number Using Single Statement
  18. Reverse the digit without using % operator
  19. Addition without using +
  20. Addition without using arithmetic operators

#include #include main() {int a,b,c; float d,p,q; clrscr(); printf("valus of a,b,c?"); scanf("%d%d%d",&a,&b,&c); d=((bb)-(4ac)); if(d>0) {printf("real"); p=-b+sqrt(d)/2a; q=-b-sqrt(d)/2a; printf("the roots are %f %f",p,q);} else if(d<0) printf("imaginary"); else {printf("real and equal"); p=((-b)+sqrt(d))/(2a)); q=((-b)-sqrt(d))/(2*a)); printf("the roots are %f %f",p,q);} }

II. Programs using Loops

  1. Factorial without using function #include void main() {int i,number,factorial; printf(“\nEnter the number : "); scanf("%d",&n); factorial = 1; for(i=1;i<=n;i++) factorial = factorial * i; printf(“\nFactorial of %d is %d",n,factorial ); }
  2. Table of N and square of N #include void main() { int n; printf(“\not Squaren"); printf("-----------------n"); for(n=1;n <=10;n++) printf("%d\t%d\n",n,n*n); }
  3. Calculate x to the power y #include void main() { int base, exp; long long int value=1; printf("Enter base number and exponent "); scanf("%d%d", &base, &exp); while (exp!=0) { value*=base; --exp; }

printf("Answer = %d", value); }

  1. Multiplication table #include void main() { int n, i; printf("Enter an integer to find multiplication table: "); scanf("%d",&n); for(i=1;i<=20;++i) printf("%d * %d = %d\n", n, i, n*i); }
  2. Sum of natural numbers #include void main() { int n, count, sum=0; printf("Enter an integer: "); scanf("%d",&n); count=1; while(count<=n) { sum+=count; ++count; } printf("Sum = %d",sum); }
  3. Fibonacci starting from any two numbers #include void main() {int first,second,sum,num,counter=0; printf("Enter the term : "); scanf("%d",&num); printf(“\nEnter First Number : "); scanf("%d",&first); printf(“\nEnter Second Number : "); scanf("%d",&second); printf(“\nFibonacci Series : %d %d ",first,second); while(counter< num) { sum=first+second; printf("%d ",sum); first=second; second=sum; counter++; } }
  4. Uppercase to Lower case #include #include 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 lower case is->%s",str); }

  1. Lower to Upper #include 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]>=97&&str[i]<=122) str[i]=str[i]-32; } printf("\nThe string in lowercase is->%s",str); }
  2. Pascal triangle #include void main() {int bin=1,p,q=0,r,x; printf("Rows you want to input:"); scanf("%d",&r); printf("\nPascal's Triangle:\n"); while(q0;--p) printf(" "); for(x=0;x<=q;++x) {if((x==0)||(q==0)) bin=1; else bin=(bin*(q-x+1))/x; printf("%6d",bin); } printf("\n"); ++q;} } O/P: For r=4: 1 1 1 1 2 1 1 3 3 1
  3. LCM & GCD

    include

    include

    void main() {int n1, n2, prod, gcd, lcm,m,i ; printf("Enter the two numbers : ") ; scanf("%d %d", &n1, &n2) ; prod = n1 * n2 ; if(n1>n2 ) m=n2; else m=n1; for(i=m;i>=1;i--){

if(n1%i==0 && n2%i==0){ gcd = i ; break; } } lcm = prod / gcd ; printf("\nThe GCD is : %d", gcd) ; printf("\n\nThe LCM is : %d", lcm); }

  1. Prime numbers between two ranges #include void main() { int n1, n2, i, j, flag; printf("Enter two numbers(intevals): "); scanf("%d %d", &n1, &n2); printf("Prime nos in range %d - %d are: ", n1, n2); for(i=n1+1; i void main() { int n,i; printf("Enter a positive integer: "); scanf("%d",&n); printf("Factors of %d are: ", n); for(i=1;i<=n;++i) { if(n%i==0) printf("%d ",i); } }
  2. Prime Factors #include void main() { int n,i; printf("Enter a Number:"); scanf("%d",&n); printf("\n\nPrime Factors of %d is: ",n); for(i=2;i<=n;i++) { if(n%i==0) { printf("%d,",i); n=n/i; i--; if(n==1) break; } } }
  3. Bin to dec and oct #include void main() {long int decNum,rem,quotient,binNum=0,pos=1,octnum=0,quot;

{float sum,term,xd,x; int i; printf("Enter x in degree:"); scanf("%f",&xd); x=(xd3.141552654)/180.0; sum=0; term=x; for(i=2;fabs(term)>0.000001;i++) {sum+=term; term=-termxx/((2i-1)(2i-2));} printf("Sin (%f)=%f",xd,sum); }

  1. Exponent series #include #define ACCURACY 0. void main() { int n, count; float x, term, sum; printf("Enter value of x:"); scanf("%f", &x); n = term = sum = count = 1; while (n <= 100) { term = term * x/n; sum = sum + term; count = count + 1; if (term < ACCURACY) n = 999; else n = n + 1; } printf("Terms = %d Sum = %f\n", count, sum); }

33. FLOYD'S TRIANGLE

#include void main() { int i,j,k=1; int range; printf("Enter the range: "); scanf("%d",&range); printf("FLOYD'S TRIANGLE : n \n”); for(i=1;i<=range;i++) { for(j=1;j<=i;j++,k++) printf("%d ",k); printf(“\n"); } } FLOYD'S TRIANGLE : for range= 1 2 3 4 5 6 7 8 9 10

III. Programs using Arrays

  1. Fibonacci using array #include main()

{int n,fib[25]; scanf("%d",&n); fib[0]=0; fib[1]=1; for(i=2;i<=n;i++) fib[i]=fib[i-2]+fib[i-1]; for(i=0;i<=n;i++) printf("%d\n",fib[i]); }

  1. Largest among N numbers in an array #include void main() { int a[30],i,n,largest; printf(“\n Enter no of elements :"); scanf("%d",&n); for(i=0 ; i < n ; i++) scanf("%d",&a[i]); largest = a[0]; for(i = 0;i largest ) largest = a[i]; } printf(“\nLargest Element : %d",largest) }
  2. Smallest among N numbers in an array #include void main() { int a[30],i,n,smallest; printf(“\n Enter no of elements :"); scanf("%d",&n); for(i=0 ; i < n ; i++) scanf("%d",&a[i]); smallest = a[0]; for(i = 0 ;i < n ; i++) { if ( a[i] < smallest ) smallest = a[i]; } printf(“\nSmallest Element : %d",smallest); }
  3. Reverse the array elements #include void main() { int a[30],i,j,n,temp; printf(“\n Enter no of elements :"); scanf("%d",&n); for(i=0 ; i < n ; i++) scanf("%d",&a[i]); j = i-1; // j will Point to last Element i = 0; // i will be pointing to first element while(i < j) { temp = a[i]; a[i] = a[j]; a[j] = temp; i++; // increment i and decrement j j--; }

for(i = 0 ;i< n ;i++) printf(“\n %d",a[i]); }

  1. Insert an element in an array #include void main() { int arr[30],element,num,i,location; printf(“\n Enter no of elements :"); scanf("%d",&num); for(i=0 ; i < num ; i++) scanf("%d",&arr[i]); printf(“\n Enter the element to be inserted :"); scanf("%d",&element); printf(“\n Enter the location"); scanf("%d",&location); for(i = num ;i >= location ; i--) arr[i] = arr[i-1]; num++; arr[location-1] = element; for(i = 0 ;i < num ;i++) printf("\n %d",arr[i]); }
  2. Deleting an array element #include void main() { int a[30],n,i,j; printf("\n Enter no of elements :"); scanf("%d",&n); printf("\n Enter %d elements :",n); for(i=0;i < n;i++) scanf("%d",&a[i]); printf("\n location of the element to be deleted :"); scanf("%d",&j); while(j < n) { a[j-1]=a[j]; j++; } n--; for(i=0;i < n;i++) printf(“\n %d",a[i]); getch(); }
  3. Transpose of a matrix #include void main() { int a[10][10],m,i,j,temp; printf(“\n Enter the size of matrix :"); scanf("%d",&m); printf(“\n Enter the values a:"); for(i=0;i void main() { int a[50], int i,j,k,size,n,t; printf("\nEnter size of the array: "); scanf("%d",&n); printf("\nEnter %d elements into the array: ",n); for(i=0;ia[j]) {t=a[i]; a[i]=a[j]; a[j]=t; } } } printf("\nThe array after removing duplicates is: "); for(i=0;i < size;i++) printf(" %d ",a[i]); }
  4. Linear Search #include void main() { int a[30],x,n,i; printf(“\nEnter no of elements :"); scanf("%d",&n); printf(“\nEnter the values :"); for(i=0;i < n;i++) scanf("%d",&a[i]); printf(“\nEnter the elements to be searched");

scanf("%d",&b[i][j]); for(i=0;i<=2;i++) for(j=0;j<=2;j++) { sum = 0; for(k=0;k<=2;k++) sum = sum + a[i][k] * b[k][j]; c[i][j]=sum; } printf(“\nMultiplication Of Two Matrices : \n”); for(i=0;i<3;i++) { for(j=0;j<3;j++) printf(" %d ",c[i][j]); printf(“\n);} }

  1. Inverse of a 3X3 matrix #include void reduction(float a[][6],int size,int pivot ,int col) {int i,j; float factor; factor=a[pivot][col]; for(i=0;i<2*size;i++) a[pivot][i]/=factor; for(i=0;i int findFactorial(int); int main() { int i,factorial,num;

printf("Enter a number: "); scanf("%d",&num); factorial = findFactorial(num); printf("Factorial of %d is: %d",num,factorial); return 0; } int findFactorial(int num) { int i,f=1; for(i=1;i<=num;i++) f=f*i; return f; }

  1. Find minimum number in an array #include int minimum (int values[], int numberOfElements) {int minValue, i; minValue = values[0]; for ( i = 1; i < numberOfElements; ++i ) if ( values[i] < minValue ) minValue = values[i]; return minValue; } int main (void) {int array1[5] = { 157, -28, -37, 26, 10 }; int array2[7] = { 12, 45, 1, 10, 5, 3, 22 }; int minimum (int values[], int numberOfElements); printf ("array1 minimum: %i\n", minimum (array1, 5)); printf ("array2 minimum: %i\n", minimum (array2, 7)); }
  2. Bubble Sort #include void bubble_sort(int [],int); void main() { int a[30],n,i; printf(“\nEnter no of elements :"); scanf("%d",&n); printf(“\nEnter array elements :"); for(i=0;ia[j+1]) { temp=a[j]; a[j]=a[j+1]; a[j+1]=temp; } printf(“\nAfter pass %d : ",i);

for(k=0;k< n;k++) printf("%5d",a[k]); } }

  1. Convert :Bin to dec; dec to bin #include #include int binary_decimal(int n); int decimal_binary(int n); void main() { int n; char c; printf("1. Enter alphabet 'd' to convert binary to decimal.\n"); printf("2. Enter alphabet 'b' to convert decimal to binary.\n"); scanf("%c",&c); if (c =='d' || c == 'D') { printf("Enter a binary number: "); scanf("%d", &n); printf("%d in binary = %d in decimal", n, binary_decimal(n)); } if (c =='b' || c == 'B') { printf("Enter a decimal number: "); scanf("%d", &n); printf("%d in decimal = %d in binary", n, decimal_binary(n)); } } int decimal_binary(int n) { int rem, i=1, binary=0; while (n!=0) { rem=n%2; n/=2; binary+=remi; i=10; } return binary; } int binary_decimal(int n) { int decimal=0, i=0, rem; while (n!=0) { rem = n%10; n/=10; decimal += rem*pow(2,i); ++i; } return decimal; }
  2. Bin to oct; oct to bin #include #include int binary_octal(int n); int octal_binary(int n); void main() { int n; char c; printf("Instructions:\n"); printf("Enter alphabet 'o' to convert binary to octal.\n"); printf("2. Enter alphabet 'b' to convert octal to binary.\n"); scanf("%c",&c);

if ( c=='o' || c=='O') { printf("Enter a binary number: "); scanf("%d",&n); printf("%d in binary = %d in octal", n, binary_octal(n)); } if ( c=='b' || c=='B') { printf("Enter a octal number: "); scanf("%d",&n); printf("%d in octal = %d in binary",n, octal_binary(n)); } } int binary_octal(int n) { int octal=0, decimal=0, i=0; while(n!=0) { decimal+=(n%10)pow(2,i); ++i; n/=10; } i=1; while (decimal!=0) { octal+=(decimal%8)i; decimal/=8; i=10; } return octal; } int octal_binary(int n) { int decimal=0, binary=0, i=0; while (n!=0) { decimal+=(n%10)pow(8,i); ++i; n/=10; } i=1; while(decimal!=0) { binary+=(decimal%2)i; decimal/=2; i=10; } return binary; }

  1. Dec to Hex #include #include void dec_hex(long int num) // Function Definition {long int rem[50],i=0,length=0; while(num>0) { rem[i]=num%16; num=num/16; i++; length++; } printf("Hexadecimal number : "); for(i=length-1;i>=0;i--) { switch(rem[i]) { case 10: printf("A"); break; case 11: printf("B"); break; case 12: printf("C"); break; case 13:

printf("\n%d",st[i]);} } int peep(int st[]) {if (top== -1) {printf("stack is empty"); return (-1);} else return(st[top]); }

  1. Factorial using recursive function #include void main() {int n,x,i,a; int factorial(int); printf("any number\n"); scanf("%d",&n); x=factorial(n); printf("the factorial of %d is %d",n,x); } int factorial(int n) {if(n==1) return (1); else return(n*factorial(n-1)); }
  2. Fibonacci using recursive function #include fib(int,int,int); void main() {int n; scanf("%d",&n); fib(n,0,1); } fib(int n,int a,int b) {int c; c=a+b; printf("%d",c); n--; if(n==1) return; fib(n,b,c); }
  3. Sum of N numbers using recursion #include int add(int n); void main() { int n; printf("Enter an positive integer: "); scanf("%d",&n); printf("Sum = %d",add(n)); } int add(int n) { if(n!=0) return n+add(n-1); }
    1. Reverse the sentence using recursion #include void Reverse(); void main() { printf("Enter a sentence: "); Reverse(); } void Reverse() { char c; scanf("%c",&c); if( c != '\n') { Reverse(); printf("%c",c); } }
    2. Power using recursion #include int power(int n1,int n2); void main() { int base, exp; printf("Enter base number: "); scanf("%d",&base); printf("Enter power number(positive integer): "); scanf("%d",&exp); printf("%d^%d = %d", base, exp, power(base, exp)); } int power(int base,int exp) { if ( exp!=1 ) return (base*power(base,exp-1)); else return base;}
    3. Towers of Hanoi #include void towers(int,char,char,char); void towers(int n,char frompeg,char topeg,char auxpeg) { /* If only 1 disk, make the move and return / if(n==1) { printf("\nMove disk 1 from peg %c to peg %c",frompeg,topeg); return; } / Move top n-1 disks from A to B, using C as auxiliary / towers(n-1,frompeg,auxpeg,topeg); / Move remaining disks from A to C / printf("\nMove disk %d from peg %c to peg %c",n,frompeg,topeg); / Move n-1 disks from B to C using A as auxiliary */ towers(n-1,auxpeg,topeg,frompeg); } main() { int n; printf("Enter the number of disks : "); scanf("%d",&n); printf("The Tower of Hanoi involves the moves :\n\n"); towers(n,'A','C','B');
  1. Exponent using recursion #include int exp_rec(int,int); main() {int n1,n2,res; scanf(“%d%d”,&n1,&n2); res=exp_rec(n1,n2); } int exp_rec(int x, int y); {if(y==0) return 1; else return(x*exp_rec(x,y-1)); }
  2. GCD using recursion int GCD(int,int); void main() {scanf(“%d%d”,&n1,&n2); res=GCD(n1,n2); printf(“gcd=%d”,res); } int GCD(int x,int y); {int rem; rem=x%y; if(rem==0) return y; else return(GCD(y,rem)); } V. Programs using Structures
  3. Student details -structure #include struct stu {char name[25]; int rno; int m[5]; struct date { int d,m,y; } dob; }s[20]; void main() { int total,tot,n,i,j; float avg,avgs; clrscr(); printf("\nenter the no of student "); scanf("%d",&n); for(i=0;i struct play {char name[25]; int age; int nmatch; int run; float avgrun; }cri[100]; void main() { int n,i; float d; clrscr(); printf("\nenter the no of players "); scanf("%d",&n); for(i=0;icri[i+1].avgrun) d=cri[i+1].avgrun; cri[i+1].avgrun=cri[i].avgrun; cri[i].avgrun=d; } printf("\ndetails inascending order\n"); printf("\nName\tage\tmatches\truns\tavg_run"); for(i=0;i #define MAX 20 struct addpolynomial { int exp, coef; }; //function to read polynomial int read_addpolynomial(struct addpolynomial p[]) {

temp.real=n1.real+n2.real; temp.imag=n1.imag+n2.imag; return(temp); }

  1. Calculate difference between two time periods #include struct TIME { int seconds; int minutes; int hours; }; void Difference(struct TIME t1, struct TIME t2, struct TIME *diff); void main() { struct TIME t1,t2,diff; printf("Enter start time: \n"); printf("Enter hours, minutes and seconds respectively: "); scanf("%d%d%d",&t1.hours,&t1.minutes,&t1.seconds); printf("Enter stop time: \n"); printf("Enter hours, minutes and seconds respectively: "); scanf("%d%d%d",&t2.hours,&t2.minutes,&t2.seconds); Difference(t1,t2,&diff); printf("\nTIME DIFFERENCE: %d:%d:%d - ",t1.hours,t1.minutes,t1.seconds); printf("%d:%d:%d ",t2.hours,t2.minutes,t2.seconds); printf("= %d:%d:%d\n",diff.hours,diff.minutes,diff.seconds); } void Difference(struct TIME t1, struct TIME t2, struct TIME *differ) { if(t2.seconds>t1.seconds) { --t1.minutes; t1.seconds+=60; } differ->seconds=t1.seconds-t2.seconds; if(t2.minutes>t1.minutes) { --t1.hours; t1.minutes+=60; } differ->minutes=t1.minutes-t2.minutes; differ->hours=t1.hours-t2.hours; }

VI. Programs using strings

  1. Program to Count Blanks,Tabs and Newlines #include int main(void) { int nb,nt,nl,c; nb=nt=nl=0; while((c=getchar())!=’*’) { if(c==' ') ++nb; if(c=='\t') ++nt; if(c=='\n') ++nl; } printf(“no. of Blanks is %d,No. of Tabs is %d and No. of Newlines is %d",nb,nt,nl); }
  2. Palindrome checking #include

void main() { int j,i,k,c=0; char a[80]; clrscr(); printf("\nEnter main string:-\n"); gets(a); k=strlen(a); for(i=0,j=k-1;i void main() {char a[25]; int i; printf("enter your name\n"); scanf(“%s”,a); while(a[i]!='\0') {printf("%c=%d\n",a[i],a[i]); i++;} }

  1. calculating string length without strlen function #include void main() {int i=1; char a[25]; printf("any number\n"); while((a[i]=getchar())!='\n') i++; printf("lenght is %d",i-1); getch(); }
  2. comparing 2 strings without strcmp function #include

void main() { int i,j,k=0,l,ls; char a[80],b[80]; clrscr(); printf("\nEnter string1:-\n"); gets(a); printf("\nEnter string2:-\n"); gets(b); l=strlen(b); ls=strlen(a); for(i=0,j=0;(i

if(a[i]==b[j]) k=1; if(a[i]!=b[j]) {k=0; break; } } if (k==1) printf("strings are equal\n"); else {if(k==0) printf("\n\nstrings are not equal.");} getch();

}

  1. copying one string to another without using strcpy #include void main() { int i,j,l,ls; char a[80],b[80]; clrscr(); printf("\nEnter main string:-\n"); gets(b); ls=strlen(b); for(i=0,j=0;j<=ls-1;i++,j++) a[i]=b[j]; printf("\n\ncopied string is %s ",a); }
  2. string concatenation without using strcat function #include void main() { int i,j,l,ls; char a[80],b[80]; printf("\nEnter main string:-\n"); gets(a); printf("enter the string to be concatinated\n"); gets(b); l=strlen(a); ls=strlen(b); for(i=l,j=0;j<=ls;i++,j++) a[i]=b[j]; printf("\n\nconcatinated string is "); puts(a); getch(); }
  3. Pattern replacement #include void main() {char str[200],pat[20],new_str[200],rep_pat[100]; int i=0,j=0,k,n=0,rep=0; printf(“enter source string”); gets(str);

printf(“enter string to be replaced”); gets(pat); printf("\n enter new string to replace pattern"); gets(rep_pat); while(str[i]!='\0') { j=0;k=i;rep=0; while(str[k]==pat[j] && pat[j]!='\0') { k++; j++; } if(pat[j]=='\0') { i=k; while(rep_pat[rep]!='\0') { new_str[n]=rep_pat[rep]; rep++; n++; } } new_str[n]=str[i]; i++; n++; } printf("The String is "); puts(new_str); getch(); }

  1. Finding vowels #include void main() { int n,i,f=0,k=0; char a[80]; clrscr(); printf("\nEnter main string:-\n"); gets(a); n=strlen(a); for(i=0;i areaperi ( radius, &area, &perimeter ) ; printf ( "\nArea = %f", area ) ; printf ( "\nPerimeter = %f", perimeter ) ; }

  2. function pointers #include void isprime(int); void (fprime)(int); void main() {int n,i,j,c=0,k=1; fprime=isprime; scanf("%d",&n); (fprime)(n); getchar(); } void isprime(int a) {int i,fg=0; for(i=2;i void main(){ int arr[50]; int p; int i,j,k,size,t; printf("\nEnter size of the array: "); scanf("%d",& size); printf("\nEnter %d elements into the array: ",n); for(i=0;i< size;i++) scanf("%d",&arr[i]); p=arr; for(i=0;i(p+j))

{ t=*(p+i); (p+i)=(p+j); *(p+j)=t;} } } printf("\nThe array after removing duplicates is: "); for(i=0;i < size;i++) printf(" %d ",arr[i]); }

  1. Sorting integer array using pointers #include void sort(int size,int p); void main() {int i,a[8]={11,2,34,57,890,44,33,22}; sort(8,a); for(i=0;i<8;i++) printf("\n%d",a[i]); } void sort(int size,int p) {int j,t,i; for(i=0;i(p+j)) {t=(p+i); (p+i)=(p+j); *(p+j)=t; }} }
  2. Sum of array using pointers #include void main() { int a[10], int i,sum=0; int ptr; printf("Enter 10 elements: \n”); for(i=0;i<10;i++) scanf("%d",&a[i]); ptr = a; / a=&a[0] */ for(i=0;i<10;i++) { sum = sum + ptr; //p=content pointed by 'ptr' ptr++; } printf("The sum of array elements is %d",sum); }
  3. Count number of space,words,digits,numbers using pointers #include #include #include /low implies that position of pointer is within a word/

#define low 1 /high implies that position of pointer is out of word./ #define high 0 void main() {int nob,now,nod,nov,nos,pos=high; char s; nob=now=nod=nov=nos=0; printf("Enter any string:"); gets(s); while(s!='') { if(s==' ') / counting number of blank spaces. / { pos=high; ++nob; } else if(pos==high) / counting number of words. / { pos=low; ++now; } if(isdigit(s)) /* counting number of digits. / ++nod; if(isalpha(s)) /* counting number of vowels / switch(s) { case 'a': case 'e': case 'i': case 'o': case 'u': case 'A': case 'E': case 'I': case 'O': case 'U': ++nov; break; } /* counting number of special characters / if(!isdigit(s)&&!isalpha(*s)) ++nos; s++;} printf(“\nNumber of words %d",now); printf(“\nNumber of spaces %d",nob); printf(“\nNumber of vowels %d",nov); printf(“\nNumber of digits %d",nod); printf(“\nNumber of special characters %d",nos); }

  1. Length of a string using pointer

#include int string_ln(char*); void main() { char str[20];

int l; printf("Enter any string: \n”); gets(str); l=string_ln(str); printf("The length of the given string %s is : %d",str,l); } int string_ln(charp) / p=&str[0] / { int count=0; while(p!='\0') { count++; p++; } return count; }

  1. Reverse the String Using Pointers #include void main() { char str[50], rev[50]; char *sptr = str, rptr = rev; int i=-1; printf("Enter any string : "); scanf("%s",str); while(sptr){ sptr++; i++; } while(i>=0) { sptr--; *rptr = *sptr; rptr++; --i; } *rptr='\0'; printf("Reverse of string is : %s",rev); } VIII. Programs using Files
  2. Write a sentence into a file #include #include/* For exit() function */ void main() { char c[1000]; FILE *fptr; fptr=fopen("program.txt","w"); if(fptr==NULL) { printf("Error!"); exit(1); } printf("Enter a sentence:\n"); gets(c); fprintf(fptr,"%s",c); fclose(fptr); }
  3. Files to maintain employee details #include #include void main() {FILE emp,empsal; int id,sal;