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 --
- Find area of a rectangle
- Find ASCII value of a character
- Convert Celsius to Fahrenheit
- Swap value of two variables
- Check the given number is odd or even
- Check whether a character is vowel or consonant
- Find largest among three numbers
- Leap year checking
- Positive negative checking
- Quadratic equation II. Programs using Loops --
- Factorial without using function
- Table of N and square of N
- Calculate x to the power y
- Multiplication table
- Sum of natural numbers
- Fibonacci starting from any two numbers
- Upper case to Lower case
- Lower to upper
- Pascal triangle
- LCM & GCD
- Prime numbers between two ranges
- Factors of a number
- Prime Factors
- Bin to dec and oct
- Count the number of digit in an integer
- Reverse the digits of given number
- Number palindrome
- Digit summation
- Amstrong checking
- Make simple calculator in C
- TO FIND SIN(X) USING SINE SERIES
- Exponent series
- Floyds Triangle III. Programs using Arrays --
- Fibonacci using array
- Largest among N numbers in an array
- Smallest among N numbers in an array
- Reverse the array elements
- Insert an element in an array
- Deleting an array element
- Transpose of a matrix
- Duplication removal
- Linear Search
- Binary search
- Split the sorted array
- Matrix addition
- Matrix multiplication
- Inverse of a 3X3 matrix IV. Programs using Functions --
- Factorial using function
- Min and Max of array
- Bubble Sort
- Convert :Bin to dec; dec to bin
- Bin to oct; oct to bin
- Dec to Hex
- Oct to dec; dec to oct
- Stack operation using function
- Factorial using recursive function
- Fibonacci using recursive function
- Sum of N numbers using recursion
- Reverse the sentence using recursion
- Power using recursion
- Towers of Hanoi
- Exponent using recursion
- GCD using recursion V. Programs using Structures --
- Student structure
- Players structure
- Add two polynomials using structures in function
- Add two distances using structures
- Add two complex numbers
- Calculate difference between two time period VI. Programs using Strings --
- Program to Count Blanks,Tabs and Newlines
- Palindrome checking
- convert a name into its ascii values.
- calculating string length without strlen function
- comparing 2 strings without strcmp function
- copying one string to another without using strcpy
- string concatenation without using strcat function
- Pattern replacement
- Finding vowels
- Sorting in alphabetical order
- Searching sub string in a string
- Find the frequency of a character in a string
- Remove characters in string except alphabets
- Reverse the given string VII. Programs using Pointers --
- Area of circle using pointers
- function pointers
- duplication removal using pointers
- Sorting integer array using pointers
- Sum of array using pointers
- Count number of words using pointers
- Length of a string using pointers
- Reverse the String Using Pointers
VIII. Programs using Files --
- Write a sentence into a file
- Employee file
- Employee-struct
- Copying the content of one file into another
- Convert the file contents in Upper-case & Write Contents in a output file
- Compare two text/data files in C Programming
- Reading & writing in files
- ODD-EVEN splitting
- Copy from one text file into another
- Display same source code as output
- Read a string of text from a file IX. Miscellaneous -
- Multiple files-Prime factors
- Multiple files-String sort & search
- Dec to Bin using bits
- Find Largest element element using dynamic memory allocation
- Matrix multiplication using dynamic memory allocation
- Add Digits of the Number Using Single Statement
- Reverse the digit without using % operator
- Addition without using +
- 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
- 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 ); }
- 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); }
- 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); }
- 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); }
- 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); }
- 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++; } }
- 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); }
- 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); }
- 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
- 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); }
- 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); } }
- 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; } } }
- 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); }
- 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
- 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]); }
- 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) }
- 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); }
- 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]); }
- 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]); }
- 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(); }
- 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]); }
- 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);} }
- 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; }
- 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)); }
- 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]); } }
- 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; }
- 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; }
- 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]); }
- 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)); }
- 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); }
- 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); }
- 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); } }
- 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;}
- 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');
- 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)); }
- 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
- 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); }
- 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
- 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); }
- 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++;} }
- 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(); }
- 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();
}
- 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); }
- 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(); }
- 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(); }
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 ) ; }
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]); }
- 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; }} }
- 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); }
- 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); }
- 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; }
- 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
- 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); }
- Files to maintain employee details #include #include void main() {FILE emp,empsal; int id,sal;