C in Depth

/*P2.1 Program to find out the size and limits of data types*/
#include<stdio.h>
#include<limits.h>
#include<float.h>
int main(void)
{
printf("sizeof(char) = %u\n",sizeof(char));
printf("sizeof(short) = %u\n",sizeof(short));
printf("sizeof(int) = %u\n",sizeof(int));
printf("sizeof(long) = %u\n",sizeof(long));
printf("sizeof(float) = %u\n",sizeof(float));
printf("sizeof(double) = %u\n",sizeof(double));
printf("sizeof(long double) = %u\n",sizeof(long double));

printf("SCHAR_MIN = %d\n",SCHAR_MIN);
printf("SCHAR_MAX = %d\n",SCHAR_MAX);
printf("UCHAR_MAX = %d\n",UCHAR_MAX);

printf("SHRT_MIN = %d\n",SHRT_MIN);
printf("SHRT_MAX = %d\n",SHRT_MAX);
printf("USHRT_MAX = %u\n",USHRT_MAX);

printf("INT_MIN = %d\n",INT_MIN);
printf("INT_MAX = %d\n",INT_MAX);
printf("UINT_MAX = %u\n",UINT_MAX);

printf("LONG_MIN = %ld\n",LONG_MIN);
    printf("LONG_MAX = %ld\n",LONG_MAX);
    printf("ULONG_MAX = %lu\n",ULONG_MAX);
 
printf("FLT_MIN = %e\n",FLT_MIN);
printf("FLT_MAX = %e\n",FLT_MAX);

printf("DBL_MIN = %e\n",DBL_MIN);
printf("DBL_MAX = %e\n",DBL_MAX);

printf("LDBL_MIN = %e\n",LDBL_MIN);
printf("LDBL_MAX = %e\n",LDBL_MAX);

/*Number of digits of precision*/
printf("FLT_DIG = %d\n",FLT_DIG);
printf("DBL_DIG = %d\n",DBL_DIG);
printf("LDBL_DIG = %d\n",LDBL_DIG);
return 0;
}


/*P4.1 Integer arithmetic operations*/
#include<stdio.h>
int main(void)
{
    int a=17,b=4;
printf("Sum=%d\n",a+b);
printf("Difference=%d\n",a-b);
printf("Product=%d\n",a*b);
printf("Quotient=%d\n",a/b);
printf("Remainder=%d\n",a%b);
return 0;
}

/*P4.2 Program to understand the floating point arithmetic operation */
#include<stdio.h>
int main(void)
{
float a=12.4,b=3.8;
printf("Sum=%.2f\n",a+b);
printf("Difference=%.2f\n",a-b);
printf("Product=%.2f\n",a*b);
printf("a/b=%.2f\n",a/b);
return 0;
}

/*P4.3 Prefix increment/decrement*/
#include<stdio.h>
int main(void)
{
int x=8;
printf("x=%d\t",x);
printf("x=%d\t",++x); /*Prefix increment*/
printf("x=%d\t",x);
printf("x=%d\t",--x); /*Prefix decrement*/
printf("x=%d\n",x);
return 0;
}

/*P4.4 Program to understand the use of postfix increment/decrement*/
#include<stdio.h>
int main(void)
{
int x=8;
printf("x=%d\t",x);
printf("x=%d\t",x++); /*postfix increment*/
printf("x=%d\t",x);
printf("x=%d\t",x--); /*postfix decrement*/
printf("x=%d\n",x);
return 0;
}


/*P4.4 Program to understand the use of postfix increment/decrement*/
#include<stdio.h>
int main(void)
{
int x=8;
printf("x=%d\t",x);
printf("x=%d\t",x++); /*postfix increment*/
printf("x=%d\t",x);
printf("x=%d\t",x--); /*postfix decrement*/
printf("x=%d\n",x);
return 0;
}

/*P4.6 Program to print the larger of two numbers using conditional operator */
#include<stdio.h>
int main(void)
{
int a,b,max;
printf("Enter values for a and b :");  
scanf("%d%d",&a,&b);
max = a>b ? a : b; /*ternary operator*/
printf("Larger of %d and %d is %d\n",a,b,max);
return 0;
}
/*P4.7 Program to understand the use of comma operator*/
#include<stdio.h>
int main(void)
{
int a,b,c,sum;
sum = (a=8,b=7,c=9,a+b+c);
printf("Sum=%d\n",sum);
return 0;
}
/*P4.8 Program to interchange the value of two variables using comma operator*/
#include<stdio.h>
int main(void)
{
  int a=8,b=7,temp;
printf("a=%d, b=%d\n",a,b);
temp=a,a=b,b=temp;
printf("a=%d, b=%d\n",a,b);
return 0;
}
/*P4.9 Program to understand the sizeof operator*/
#include<stdio.h>
int main(void)
{
int var;
printf("Size of int=%u\n",sizeof(int));
printf("Size of float=%u\n",sizeof(float));
printf("Size of var=%u\n",sizeof(var));
printf("Size of an integer constant=%u\n",sizeof(45));  
return 0;
}

/*P4.10 Program to understand type conversion in assignment*/
#include<stdio.h>
int main(void)
{
char c1,c2;
int i1,i2;
float f1,f2;
c1='H';  
i1=80.56; /*float converted to int, only 80 assigned to i1  */
f1=12.6;
c2=i1; /*int converted to char */
i2=f1; /*float converted to int */
/*Now c2 has the character with ASCII value 80, i2 is assigned value 12 */ 
printf("c2=%c,  i2=%d\n",c2,i2);
f2=i1; /*int converted to float*/
i2=c1; /*char converted to int */
/*Now i2 contains ASCII value of character 'H' which is 72*/
printf("f2=%.2f,  i2=%d\n",f2,i2);
return 0;
}

/*P4.11 Program to illustrate the use of cast operator*/
#include<stdio.h>
int main(void)
{
int x=5,y=2;
float p,q;
p=x/y;
printf("p=%f\n",p);
q=(float)x/y;
printf("q=%f\n",q);
return 0;
}
/*P4.12 Program to evaluate some expressions*/
int main(void)
{
int a,b,c,d,e,f,g,h,k;
a=8, b=4, c=2, d=1, e=5, f=20;
printf("%d\t", a+b-(c+d)*3%e+f/9);

a=17, b=5, c=6, d=3, e=5;
printf("%d\t",a%6-b/2+(c*d-5)/e);

a=4, b=5, c=6, d=3, e=5, f=10;
printf("%d\t",a*b-c/d<e+f);

a=8, b=5, c=8, d=3, e=65, f=10, g=2, h=5, k=2;
printf("%d\t",a-b+c/d==e/f-g+h%k);

a=8, b=3, c=2, d=3, e=2, f=11;
printf("%d\n",a-b||(a-b*c)+d&&e-f%3);
return 0;
}
/*P5.1 Program to print the bigger number*/ #include<stdio.h> int main(void) { int a,b; printf("Enter two numbers : "); scanf("%d%d",&a,&b); if(a>b) printf("Bigger number=%d\n",a); else printf("Bigger number=%d\n",b); return 0; }

/*P5.2 Program to print whether a number is even or odd */ #include<stdio.h> int main(void) { int num; printf("Enter a number : "); scanf("%d",&num); if(num%2 == 0) /*test for even */ printf("Number is even\n"); else { printf("Number is odd\n"); num*=2; printf("Now the number is %d\n",num); } return 0; }
/*P5.3 Program to print a message if negative number is entered */ #include<stdio.h> int main(void) { int num; printf("Enter a number : "); scanf("%d",&num); if(num<0) { printf("Number entered is negative\n"); num=-num; } printf("Value of num is : %d\n", num); return 0; }


/*P5.4 Program to find quotient and remainder*/ #include<stdio.h> int main(void) { int x,y,quo,rem; printf("Enter two numbers : "); scanf("%d%d",&x,&y); if(y) /*if y is non-zero*/ { quo=x/y; rem=x%y; printf("Quotient=%d, Remainder=%d\n",quo,rem); } else printf("Divide by zero error\n"); return 0; }

/*P5.5 Program to find biggest number from three given numbers*/ #include<stdio.h> int main(void) { int a,b,c,big; printf("Enter three numbers : "); scanf("%d%d%d", &a, &b, &c); if(a>b) { if(a>c) big=a; else big=c; } else { if(b>c) big=b; else big=c; } printf("Biggest number is %d\n",big); return 0; }/*End of main()*/

/*P5.6 Program to find whether a year is leap or not*/ #include<stdio.h> int main(void) { int year; printf("Enter year : "); scanf("%d",&year); if(year%100 != 0) { if(year%4 == 0) printf("Leap year\n"); else printf("Not leap\n"); } else { if(year%400 == 0) printf("Leap year\n"); else printf("Not leap\n"); } return 0; }

/*P5.7 Program to find out the grade of a student when the marks of 4 subjects are given. The method of assigning grade is - percentage>=85 grade=A percentage<85 and percentage>=70 grade=B percentage<70 and percentage>=55 grade=C percentage<55 and percentage>=40 grade=D percentage<40 grade=E */ #include<stdio.h> int main(void) { float m1,m2,m3,m4,total,per; char grade; printf("Enter marks of 4 subjects : "); scanf("%f%f%f%f",&m1,&m2,&m3,&m4); total=m1+m2+m3+m4; per=total/4; if(per>=85) grade='A'; else if(per>=70) grade='B'; else if(per>=55) grade='C'; else if(per>=40) grade='D'; else grade='E'; printf("Percentage is %f,Grade is %c\n",per,grade); return 0; }
/*P5.8 Program to print numbers from 1 to 10 using while loop*/ #include<stdio.h> int main(void) { int i=1; while(i<=10) { printf("%d\t",i); i = i+1; /*Statement that changes the value of condition*/ } printf("\n"); return 0; }

/*P5.9 Program to print numbers in reverse order with a difference of 2*/ #include<stdio.h> int main(void) { int k=10; while(k>=2) { printf("%d\t",k); k=k-2; } printf("\n"); return 0; }
/*P5.10 Program to print the sum of digits of any number*/ #include<stdio.h> int main(void) { int n,sum=0,rem; printf("Enter a number : "); scanf("%d",&n); while(n>0) { rem = n%10; /*taking last digit of n*/ sum+=rem; n/=10; /*skipping last digit of n*/ } printf("Sum of digits=%d\n",sum); return 0; }
/*P5.11 Program to find the product of digits of any number*/ #include<stdio.h> int main(void) { int n,prod=1,rem; printf("Enter a number : "); scanf("%d",&n); while(n>0) { rem = n%10; /*taking last digit of n*/ prod*=rem; n/=10; /*skipping last digit of n*/ } printf("Product of digits = %d\n",prod); return 0; }
/*P5.12 Program to find the factorial of any number*/ #include<stdio.h> int main(void) { int n,num; long fact=1; printf("Enter a number : "); scanf("%d",&n); num=n; if(n<0) printf("No factorial of negative number\n"); else { while(n>1) { fact*=n; n--; } printf("Factorial of %d=%ld\n",num,fact); } return 0; }
/*P5.13 Program to convert a binary number to a decimal number*/ #include<stdio.h> int main(void) { int n,nsave,rem,d,j=1,dec=0; printf("Enter the number in binary : "); scanf("%d",&n); nsave=n; while(n>0) { rem=n%10; /*taking last digit of n*/ d=rem*j; dec+=d; j*=2; n/=10; /*skipping last digit of n*/ } printf("Binary number = %d, Decimal number = %d\n",nsave,dec); return 0; }
/*P5.14 Program to print numbers from 1 to 10 using a do-while loop*/ #include<stdio.h> int main(void) { int i=1; do { printf("%d\t",i); i = i+1; }while(i<=10); printf("\n"); return 0; }
/*P5.15 Program to count digits in a number */ #include<stdio.h> int main(void) { int n,count=0,rem; printf("Enter a number : "); scanf("%d",&n); do { n/=10; count++; }while(n>0); printf("Number of digits=%d\n",count); return 0; }
/*P5.16 Program to find the sum of numbers entered*/ #include<stdio.h> int main(void) { int num,sum=0; do { printf("Enter a number (enter 0 to stop) : "); scanf("%d",&num); sum+=num; }while(num!=0); printf("Sum is %d\n",sum); return 0; }
/*P5.17 Program to print numbers from 1 to 10 using for loop*/ #include<stdio.h> int main(void) { int i; for(i=1; i<=10; i++) printf("%d\t",i); printf("\n"); return 0; }
/*P5.18 Program to print numbers in reverse order with a difference of 2*/ #include<stdio.h> int main(void) { int k; for(k=10; k>=2; k-=2) printf("%d\t",k); printf("\n"); return 0; }
/*P5.19 Multiply two positive numbers without using * operator*/ #include<stdio.h> int main(void) { int a,b,i; int result=0; printf("Enter two numbers to be multiplied : "); scanf("%d%d",&a,&b); for(i=1; i<=b; i++) result=result+a; printf("%d * %d = %d\n",a,b,result); return 0; }

/*P5.20 Find the sum of this series upto n terms 1 + 2 + 4 + 7 + 11 + 16 + …………*/ #include<stdio.h> int main(void) { int i,n,sum=0,term=1; printf("Enter number of terms : "); scanf("%d",&n); for(i=1; i<=n; i++) { sum+=term; term=term+i; } printf("The sum of series upto %d terms is %d\n",n,sum); return 0; }

/*P5.21 Program to generate fibonacci series 1, 1, 2, 3, 5, 8, 13, 34, 55, 89................... In this series each number is a sum of the previous two numbers*/ #include<stdio.h> int main(void) { long x,y,z; int i,n; x=0; y=1; printf("Enter the number of terms : "); scanf("%d",&n); printf("%ld ",y); for(i=1; i<n; i++) { z=x+y; printf("%ld ",z); x=y; y=z; } printf("\n"); return 0; }
/*P5.22 Program to print the sum of digits of any number using for loop */ #include<stdio.h> int main(void) { int n,sum=0,rem; printf("Enter a number : "); scanf("%d",&n); for( ; n>0; n/=10) { rem=n%10; /*taking last digit of number*/ sum+=rem; } printf("Sum of digits=%d\n",sum); return 0; }
/*P5.23 Program to show the use of comma operator in for loop*/ #include<stdio.h> int main(void) { int i,j; for(i=0,j=10; i<=j; i++,j--) printf("i=%d j=%d\n",i,j); return 0; }
/*P5.24 Program to understand nesting in for loop*/ #include<stdio.h> int main(void) { int i,j; for(i=1; i<=3; i++) /*outer loop*/ { printf("i=%d\n",i); for(j=1; j<=4; j++) /*inner loop*/ printf("j=%d\t",j); printf("\n"); } return 0; }
/*P5.25 Program to print Armstrong numbers*/ #include<stdio.h> int main(void) { int num,n,cube,d,sum; printf("Armstrong numbers are :\n"); for(num=100; num<=999; num++) /*outer loop to generate numbers*/ { n=num; sum=0; while(n>0) /*inner loop to calculate sum of cube of digits*/ { d=n%10; n/=10; cube=d*d*d; sum=sum+cube; }/*End of while loop*/ if(num==sum) printf("%d\n",num); }/*End of for loop*/ return 0; }


/* P5.26 Program to find the sum of digits of a number until the sum is reduced to 1 digit. For example - 538769->38->11->2 */ #include<stdio.h> int main(void) { long num; int dig,sum; printf("Enter a number : "); scanf("%ld",&num); printf("%ld->",num); do { sum = 0; while(num!=0) { dig=num%10; sum+=dig; num/=10; } printf("%d\t",sum); num=sum; }while(num/10!=0); /*while num is more than one digit*/ return 0; }

/*P5.27 Program to understand the use of break*/ #include<stdio.h> int main(void) { int n; for(n=1; n<=5; n++) { if(n==3) break; printf("Number=%d\n",n); } printf("Out of for loop\n"); return 0; }
/*P5.28 Program to find whether a number is prime or not*/ #include<stdio.h> #include<math.h> int main(void) { int i,n; printf("Enter a number : "); scanf("%d",&n); for(i=2; i<=sqrt(n); i++) if(n%i==0) break; if(i>sqrt(n)) printf("%d is prime\n",n); else printf("%d is not prime\n",n); return 0; }
/*P5.29 Program to understand the use of continue statement*/ #include<stdio.h> int main(void) { int n; for(n=1; n<=5; n++) { if(n==3) continue; printf("Number=%d\n",n); } printf("Out of for loop\n"); return 0; }
/*P5.30 Program to find the sum and average of 10 positive integers*/ #include<stdio.h> int main(void) { int i=1,n,sum=0; float avg; printf("Enter 10 positive numbers : \n"); while(i<=10) { printf("Enter number %d : ",i); scanf("%d",&n); if(n<0) { printf("Enter only positive numbers\n"); continue; } sum+=n; i++; } avg=sum/10.0; printf("Sum=%d Avg=%f\n",sum,avg); return 0; }
/*P5.30 Program to find the sum and average of 10 positive integers*/ #include<stdio.h> int main(void) { int i=1,n,sum=0; float avg; printf("Enter 10 positive numbers : \n"); while(i<=10) { printf("Enter number %d : ",i); scanf("%d",&n); if(n<0) { printf("Enter only positive numbers\n"); continue; } sum+=n; i++; } avg=sum/10.0; printf("Sum=%d Avg=%f\n",sum,avg); return 0; }
/*P5.33 Program to understand the switch with break statement*/ #include<stdio.h> int main(void) { int choice; printf("Enter your choice : "); scanf("%d",&choice); switch(choice) { case 1: printf("First\n"); break; /*break statement in switch*/ case 2: printf("Second\n"); break; case 3: printf("Third\n"); break; default: printf("Wrong choice\n"); } return 0; }/*End of main()*/

/*P5.34 Program to perform arithmetic calculations on integers*/ #include<stdio.h> int main(void) { char op; int a,b; printf("Enter number,operator and another number : "); scanf("%d%c%d",&a,&op,&b); switch(op) { case '+': printf("Result = %d\n",a+b); break; case '-': printf("Result = %d\n",a-b); break; case '*': printf("Result = %d\n",a*b); break; case '/': printf("Result = %d\n",a/b); break; case '%': printf("Result = %d\n",a%b); break; default: printf("Enter valid operator\n"); }/*End of switch*/ return 0;
}/*End of main()*/

/*P5.35 Program to find whether the alphabet is a vowel or consonant*/ #include<stdio.h> int main(void) { char ch; printf("Enter an alphabet : "); scanf("%c",&ch); switch(ch) { case 'a': case 'e': case 'i': case 'o': case 'u': printf("Alphabet is a vowel\n"); break; default: printf("Alphabet is a consonant\n"); } return 0; }
/*P5.36 A menu driven program using infinite loop and switch*/ #include<stdio.h> int main(void) { int choice; while(1) { printf("1.Create database\n"); printf("2.Insert new record\n"); printf("3.Modify a record\n"); printf("4.Delete a record\n"); printf("5.Display all records\n"); printf("6.Exit\n"); printf("Enter your choice : "); scanf("%d",&choice); switch(choice) { case 1: printf("Database created.....\n\n"); break; case 2: printf("Record inserted.....\n\n"); break; case 3: printf("Record modified.....\n\n"); break; case 4: printf("Record deleted.....\n\n"); break; case 5: printf("Records displayed.....\n\n"); break; case 6: exit(1); default: printf("Wrong choice\n"); }/*End of switch*/ }/*End of while*/ return 0; }/*End of main( )*/

/*P5.37 Program to check whether a date is valid or not*/ #include<stdio.h> int main(void) { int d,m,y; int flag=1,isleap=0; printf("Enter date(dd/mm/yyyy): "); scanf("%d/%d/%d",&d,&m,&y); if(y%100!=0 && y%4==0 || y%400==0) isleap=1; if(y<1850 || y>2050 || m<1 || m>12 || d<1 || d>31) flag=0; else if(m==2) /*Check for number of days in February*/ { if(d==30 || d==31 || (d==29 && !isleap) ) flag=0; } else if(m==4 || m==6 || m==9 || m==11)/*Check days in April,June,Sept,Nov*/ { if(d==31) flag=0; } if(flag==0) printf("Not a valid date\n"); else printf("Valid Date \n"); return 0; }/*End of main()*/

/*P5.38 Program to get difference of two dates in years, months, days*/ #include<stdio.h> int main(void) { int d1,d2,d,m1,m2,m,y1,y2,y; printf("Enter first date(dd/mm/yyyy): "); scanf("%d/%d/%d",&d1,&m1,&y1); printf("Enter second date(dd/mm/yyyy): "); scanf("%d/%d/%d",&d2,&m2,&y2); if(d2<d1) { if(m2==3) { if(y2%100!=0 && y2%4==0 || y2%400==0) /*check for leap year*/ d2=d2+29; else d2=d2+28; } else if(m2==5 || m2==7 || m2==10 || m2==12) d2=d2+30; else d2=d2+31; m2=m2-1; } if(m2<m1) { y2=y2-1; m2=m2+12; } y=y2-y1; m=m2-m1; d=d2-d1; printf("Difference of the two dates is : "); printf("%d years %d months %d days\n",y,m,d); return 0; }/*End of main()*/

/*P5.39 Program to multiply two numbers by russian peasant method*/ #include<stdio.h> int main(void) { int a,b,x,y,s=0; printf("Enter two numbers to be multiplied : "); scanf("%d%d",&x,&y); a = x; b = y; while(a>=1) /*Loop till first number reduces to 1*/ { if(a%2!=0) /*If first number is odd*/ s=s+b; /*Add second number to s*/ a/=2; /*Divide first number by 2*/ b*=2; /*Multiply second number by 2*/ } printf("%d * %d = %d\n",x,y,s); return 0; }

/*P5.40 Program to find out the number of notes required for a given amount of money*/ #include<stdio.h> int main(void) { int n,choice,notes; printf("Enter the total amount in Rs : "); scanf("%d",&n); printf("Enter the value of note from which you want to begin : "); scanf("%d",&choice); switch(choice) { case 100: notes=n/100; printf("Number of 100 Rs notes = %d\n", notes); n=n%100; /*Fall thru*/ case 50: notes=n/50; printf("Number of 50 Rs notes = %d\n", notes); n=n%50; /*Fall thru*/ case 20: notes=n/20; printf("Number of 20 Rs notes = %d\n", notes); n=n%20; /*Fall thru*/ case 10: notes=n/10; printf("Number of 10 Rs notes = %d\n", notes); n=n%10; /*Fall thru*/ case 5: notes=n/5; printf("Number of 5 Rs notes = %d\n", notes); n=n%5; /*Fall thru*/ case 2: notes=n/2; printf("Number of 2 Rs notes = %d\n", notes); n=n%2; /*Fall thru*/ case 1: notes=n/1; printf("Number of 1 Rs notes = %d\n", notes); break; default: printf("Enter only valid values"); break; } printf("\n"); return 0; }
/*P5.41 Program to find day of week from a given date*/ #include<stdio.h> int main(void) { int d,m,y,j,f,h,fh,day; printf("Enter date(dd/mm/yyyy): "); scanf("%d/%d/%d",&d,&m,&y); j=d; switch(m-1) { case 11: j+=30; /*Fall thru in all cases*/ case 10: j+=31; case 9: j+=30; case 8: j+=31; case 7: j+=31; case 6: j+=30; case 5: j+=31; case 4: j+=30; case 3: j+=31; case 2: j+=28; case 1: j+=31; } if(y%100!=0 && y%4==0 || y%400==0) if(m>2) j=j+1; f=(y-1)/4; h=(y-1)/100; fh=(y-1)/400; day=(y+j+f-h+fh)%7; switch(day) { case 0: printf("Saturday\n"); break; case 1: printf("Sunday\n"); break; case 2: printf("Monday\n"); break; case 3: printf("Tuesday\n"); break; case 4: printf("Wednesday\n"); break; case 5: printf("Thursday\n"); break; case 6: printf("Friday\n"); break; } return 0; }/*End of main()*/

/*P5.42 Program to print triad numbers*/ #include<stdio.h> int main(void) { int m,n,p,num; int i,k,d1,d2,d3; for(num=100; num<=999/3; num++)/*Loop A*/ { for(i=num; i<=3*num; i+=num )/*loop B*/ { k=i; d1=k%10; k/=10; d2=k%10; k/=10; d3=k%10; k/=10; if(d1==d2 || d2==d3 || d3==d1) goto nextnum; }/*End of loop B*/ for(m=num; m>0; m/=10)/*Loop C*/ { d1=m%10; for(n=num*2; n>0; n/=10)/*Loop D*/ { d2=n%10; for(p=num*3; p>0; p/=10) /*Loop E*/ { d3=p%10; if(d1==d2 || d2==d3 || d1==d3) goto nextnum; }/*End of Loop E*/ }/*End of Loop D*/ }/*End of loop C*/ printf("%d %d %d\t",num,num*2,num*3); nextnum: ; }/*End of loop A*/ return 0; }/*End of main()*/

/*P5.43 Program to find the LCM and HCF of two numbers*/ #include<stdio.h> int main(void) { int x,y,a,b; printf("Enter two numbers : "); scanf("%d%d",&x,&y); a=x; b=y; while(a!=b) { if(a<b) a=a+x; else b=b+y; } printf("LCM of %d and %d is %d\n",x,y,a); a=x; b=y; while(a!=b ) { if(a>b) a=a-b; else b=b-a; } printf("HCF of %d and %d is %d\n",x,y,a); return 0; }


/*PYRAMIDS*/ #include<stdio.h> /*Pyramid (a)*/ int main(void) { int i,j,n; printf("Enter number of lines : "); scanf("%d",&n); for(i=1; i<=n; i++) { for(j=1; j<=i; j++) printf("* "); printf("\n"); } return 0; } /*Pyramid (b)*/ /* int main(void) { int i,j,n; printf("Enter number of lines : "); scanf("%d",&n); for(i=1; i<=n; i++) { for(j=1; j<=i; j++) printf("%2d",i); printf("\n"); } return 0; } */ /*Pyramid (c)*/ /* int main(void) { int i,j,n; printf("Enter number of lines : "); scanf("%d",&n); for(i=1; i<=n; i++) { for(j=1; j<=i; j++) printf("%2d",j); printf("\n"); } return 0; } */ /*Pyramid (d)*/ /* int main(void) { int i,j,n; printf("Enter number of lines : "); scanf("%d",&n); for(i=1; i<=n; i++) { for(j=1; j<=i; j++) printf("%3d",i+j); printf("\n"); } return 0; } */ /*Pyramid (e)*/ /* int main(void) { int i,j,n; printf("Enter number of lines : "); scanf("%d",&n); for(i=1; i<=n; i++) { for(j=1; j<=i; j++) printf("%d ",(i+j)%2==0 ? 1 : 0); printf("\n"); } return 0; } */ /*Pyramid (f)*/ /* int main(void) { int i,j,n,p=1; printf("Enter number of lines : "); scanf("%d",&n); for(i=1; i<=n; i++) { for(j=1; j<=i; j++) printf("%3d",p++); printf("\n"); } return 0; } */ /*Pyramid (g)*/ /* int main(void) { int i,j,n; printf("Enter number of lines : "); scanf("%d",&n); for(i=1; i<=n; i++) { for(j=1; j<=i; j++) printf("%2d",n+1-j); printf("\n"); } return 0; } */ /*Pyramid (h)*/ /* int main(void) { int i,j,n; printf("Enter number of lines : "); scanf("%d",&n); for(i=1; i<=n; i++) { for(j=1; j<=i; j++) printf("%2d",n+1-i); printf("\n"); } return 0; } */ /*Pyramid (i)*/ /* int main(void) { int i,j,n; printf("Enter number of lines : "); scanf("%d",&n); for(i=1; i<=n; i++) { for(j=1; j<=n+1-i; j++) printf("* "); printf("\n"); } return 0; } */ /*Pyramid (j)*/ /* int main(void) { int i,j,n; printf("Enter number of lines : "); scanf("%d",&n); for(i=1; i<=n; i++) { for(j=1; j<=n+1-i; j++) printf("%2d",i); printf("\n"); } return 0; } */ /*Pyramid (k)*/ /* int main(void) { int i,j,n; printf("Enter number of lines : "); scanf("%d",&n); for(i=1; i<=n; i++) { for(j=1; j<=n+1-i; j++) printf("%2d",j); printf("\n"); } return 0; } */ /*Pyramid (l)*/ /* int main(void) { int i,j,n; printf("Enter number of lines : "); scanf("%d",&n); for(i=1; i<=n; i++) { for(j=1; j<=n+1-i; j++) printf("%2d",n+1-i); printf("\n"); } return 0; } */ /*Pyramid (m)*/ /* int main(void) { int i,j,n; printf("Enter number of lines : "); scanf("%d",&n); for(i=1; i<=n; i++) { for(j=1; j<=n+1-i; j++) printf("%2d",n+1-j); printf("\n"); } return 0; } */ /*Pyramid (n)*/ /* int main(void) { int i,j,n; printf("Enter number of lines : "); scanf("%d",&n); for(i=1; i<=n; i++) { for( j=1; j<=n-i; j++) printf(" "); for(j=1; j<=i; j++) printf("* "); printf("\n"); } return 0; } */ /*Pyramid (o)*/ /* int main(void) { int i,j,n; printf("Enter number of lines : "); scanf("%d",&n); for(i=1; i<=n; i++) { for(j=1; j<=n-i; j++) printf(" "); for(j=1; j<=i; j++) printf("%2d",j); printf("\n"); } return 0; } */ /*Pyramid (p)*/ /* int main(void) { int i,j,n; printf("Enter number of lines : "); scanf("%d",&n); for(i=1; i<=n; i++) { for( j=1; j<=n-i; j++) printf(" "); for(j=1; j<=i; j++) printf("*"); printf("\n"); } return 0; } */ /*Pyramid (q)*/ /* int main(void) { int i,j,n; printf("Enter number of lines : "); scanf("%d",&n); for(i=1; i<=n; i++) { for(j=1; j<=n-i; j++) printf(" "); for(j=1; j<=i; j++) printf("%d",j); printf("\n"); } return 0; } */ /*Pyramid (r)*/ /* int main(void) { int i,j,n; printf("Enter number of lines : "); scanf("%d",&n); for(i=1; i<=n; i++) { for(j=1; j<=n-i; j++) printf(" "); for(j=1; j<=2*i-1; j++) printf("*"); printf("\n"); } return 0; } */ /*Pyramid (s)*/ /* int main(void) { int i,j,n; printf("Enter number of lines : "); scanf("%d",&n); for(i=1; i<=n; i++) { for(j=1; j<=n-i; j++) printf(" "); for(j=1; j<=2*i-1; j++) printf("%d",i); printf("\n"); } return 0; } */ /*Pyramid (t)*/ /* int main(void) { int i,j,n; printf("Enter number of lines : "); scanf("%d",&n); for(i=1; i<=n; i++) { for(j=1; j<=n-i; j++) printf(" "); for(j=1; j<=2*i-1; j++) printf("%d",j); printf("\n"); } return 0; } */ /*Pyramid (u)*/ /* int main(void) { int i,j,n; printf("Enter number of lines : "); scanf("%d",&n); for(i=1; i<=n; i++) { for(j=1; j<=2*(n-i); j++) printf(" "); for(j=1; j<=2*i-1; j++) printf("* "); printf("\n"); } return 0; } */ /*Pyramid (v)*/ /* int main(void) { int i,j,n,p; printf("Enter number of lines : "); scanf("%d",&n); for(i=1; i<=n; i++) { for(j=1; j<=n-i; j++) printf(" "); p=i; for(j=1; j<=i; j++) printf("%d",p++); p=p-2; for(j=1; j<=i-1; j++) printf("%d",p--); printf("\n"); } return 0; } */ /*Pyramid (w)*/ /* int main(void) { int i,j,n,p; printf("Enter number of lines : "); scanf("%d",&n); for(i=1; i<=n; i++) { for(j=1; j<=n-i; j++) printf(" "); p=n; for(j=1; j<=i; j++) printf("%d",p--); p=p+2; for(j=1; j<=i-1; j++) printf("%d",p++); printf("\n"); } return 0; } */ /*Pyramid (x)*/ /* int main(void) { int i,j,n; printf("Enter number of lines : "); scanf("%d",&n); for(i=1; i<=n; i++) { for(j=1; j<=i; j++) printf(" "); for(j=1; j<=2*(n-i)+1; j++) printf("*"); printf("\n"); } return 0; } */ /*Pyramid (y)*/ /* int main(void) { int i,j,n; printf("Enter number of lines : "); scanf("%d",&n); for(i=1; i<=n; i++) { for(j=1; j<=i; j++) printf(" "); for(j=1; j<=2*(n-i)+1; j++) printf("%d",n+1-i); printf("\n"); } return 0; } */ /*Pyramid (z)*/ /* int main(void) { int i,j,n; printf("Enter number of lines : "); scanf("%d",&n); for(i=1; i<=n; i++) { for(j=1; j<=i; j++) printf(" "); for(j=1; j<=2*(n-i)+1; j++) printf("%d",j); printf("\n"); } return 0; } */ /*Pyramid (z1)*/ /* int main(void) { int i,j,n,p; printf("Enter number of lines : "); scanf("%d",&n); for(i=1; i<=n; i++) { for(j=1; j<=i; j++) printf(" "); p=n+1-i; for(j=1; j<=n-i+1; j++) printf("%d",p++); p=p-2; for(j=1; j<=n-i; j++) printf("%d",p--); printf("\n"); } return 0; } */ /*Pyramid (z2)*/ /* int main(void) { int i,j,n,p; printf("Enter number of lines : "); scanf("%d",&n); for(i=1; i<=n; i++) { for(j=1; j<=i; j++) printf(" "); p=n; for(j=1; j<=n-i+1; j++) printf("%d",p--); p=p+2; for(j=1; j<=n-i; j++) printf("%d",p++); printf("\n"); } return 0; } */ /*Pyramid (z3)*/ /* int main(void) { int i,j,n; printf("Enter n : "); scanf("%d",&n); for(i=1; i<=n; i++) { for(j=1; j<=n-i; j++) printf(" "); for(j=1; j<=2*i-1; j++) printf("*"); printf("\n"); } n--; for(i=1; i<=n; i++) { for(j=1; j<=i; j++) printf(" "); for(j=1; j<=2*(n-i)+1; j++) printf("*"); printf("\n"); } return 0; } */ /*Pyramid (z4)*/ /* int main(void) { int i,j,n; printf("Enter n : "); scanf("%d",&n); for(i=1; i<=n; i++) { for(j=1; j<=n-i; j++) printf(" "); for(j=1; j<=2*i-1; j++) printf("%d",i); printf("\n"); } n--; for(i=1; i<=n; i++) { for(j=1; j<=i; j++) printf(" "); for(j=1; j<=2*(n-i)+1; j++) printf("%d",n+1-i); printf("\n"); } return 0; } */ /*Pyramid (z5)*/ /* int main(void) { int i,j,n; printf("Enter n : "); scanf("%d",&n); for(i=1; i<=n; i++) { for(j=1; j<=n-i; j++) printf(" "); for(j=1; j<=2*i-1; j++) printf("%d",j); printf("\n"); } n--; for(i=1; i<=n; i++) { for(j=1; j<=i; j++) printf(" "); for(j=1; j<=2*(n-i)+1; j++) printf("%d",j); printf("\n"); } return 0; } */ /*Pyramid (z6)*/ /* int main(void) { int i,j,n,p; printf("Enter n : "); scanf("%d",&n); for(i=1; i<=n; i++) { for(j=1; j<=n-i; j++) printf(" "); p=i; for(j=1; j<=i; j++) printf("%d",p++); p=p-2; for(j=1; j<=i-1; j++) printf("%d",p--); printf("\n"); } n--; for(i=1; i<=n; i++) { for(j=1; j<=i; j++) printf(" "); p=n+1-i; for(j=1; j<=n-i+1; j++) printf("%d",p++); p=p-2; for(j=1; j<=n-i; j++) printf("%d",p--); printf("\n"); } return 0; } */ /*Pyramid (z7)*/ /* int main(void) { int i,j,n,p; printf("Enter n : "); scanf("%d",&n); for(i=1; i<=n; i++) { for(j=1; j<=n-i; j++) printf(" "); p=n; for(j=1; j<=i; j++) printf("%d",p--); p=p+2; for(j=1; j<=i-1; j++) printf("%d",p++); printf("\n"); } n--; for(i=1; i<=n; i++) { for(j=1; j<=i; j++) printf(" "); p=n; for(j=1; j<=n-i+1; j++) printf("%d",p--); p=p+2; for(j=1; j<=n-i; j++) printf("%d",p++); printf("\n"); } return 0; }

/*P6.1 Program to find the square root of a number*/ #include<stdio.h> #include<math.h> int main(void) { double n,s; printf("Enter a number : "); scanf("%lf",&n); s=sqrt(n); printf("Square root of %.2lf is %.2lf\n",n,s); return 0; } /*P6.2 Program to draw a line*/ #include<stdio.h> void drawline(void); /*Function Declaration*/ int main(void) { drawline(); /*Function Call*/ return 0; } void drawline(void) /*Function Definition*/ { int i; for(i=1; i<=80; i++) printf("-"); } /*P6.3 Program to find the sum of two numbers*/ #include<stdio.h> int sum(int x,int y); /*Function declaration*/ int main(void) { int a,b,s; printf("Enter values for a and b : "); scanf("%d%d",&a,&b); s=sum(a,b); /*Function call*/ printf("Sum of %d and %d is %d\n",a,b,s); return 0; } int sum(int x,int y) /*Function definition*/ { int s; s=x+y; return s; } /*P6.4 Program to find the sum of two numbers*/ #include<stdio.h> int sum(int x,int y); /*Function declaration*/ int main(void) { int a=10,b=20,k; k=sum(a,b); /*Function call*/ printf("%d\n",k); k=sum(4,5); /*Function call*/ printf("%d\n",k); k=sum(a+b,b*2); /*Function call*/ printf("%d\n",k); return 0; } int sum(int x,int y) /*Function definition*/ { int s; s=x+y; return s; } /*P6.5 Program to understand the use of return statement*/ #include<stdio.h> void selection(int age,float ht); int main(void) { int age; float ht; printf("Enter age and height: "); scanf("%d %f",&age,&ht); selection(age,ht); return 0; } void selection(int age,float ht) { if(age>25) { printf("Age should be less than 25\n"); return; } if(ht<5) { printf("Height should be more than 5\n"); return; } printf("Selected\n"); } /*P6.6 Program to find the larger number*/ #include<stdio.h> int larger(int x,int y); int main(void) { int x, y; printf("Enter two numbers : "); scanf("%d%d",&x,&y); printf("Larger number=%d\n",larger(x,y)); return 0; } int larger(int x,int y) { return x>y?x:y; } /*P6.7*/ #include<stdio.h> void func(int a,int b); int main(void) { int x=10,y=20; func(x,y); printf("x=%d,y=%d\n",x,y); return 0; } void func(int a,int b) { a++; b--; printf("a=%d,b=%d\n",a,b); } /*P6.8 Program to find the sum of two numbers*/ #include<stdio.h> int sum(int x,int y) /*Function definition*/ { int s; s=x+y; return s; } int main(void) { int a,b,s; printf("Enter values for a and b : "); scanf("%d %d",&a,&b); s=sum(a,b); /*Function call*/ printf("Sum of %d and %d is %d\n",a,b,s); return 0; } /*P6.9 Program that finds whether a number is even or odd*/ #include<stdio.h> void find(int n); int main(void) { int num; printf("Enter a number : "); scanf("%d",&num); find(num); return 0; } void find(int n) { if(n%2==0) printf("%d is even\n",n); else printf("%d is odd\n",n); } /*P6.10 Program to find out the factorial of a number*/ #include<stdio.h> long int factorial(int n); int main(void) { int num; printf("Enter a number : "); scanf("%d",&num); if(num<0) printf("No factorial of negative number\n"); else printf("Factorial of %d is %ld\n",num,factorial(num)); return 0; } long int factorial(int n) { int i; long int fact=1; if(n==0) return 1; for(i=n; i>1; i--) fact*=i; return fact; } /*P6.11 Program that uses a function with no arguments and no return values*/ #include<stdio.h> void displaymenu(void); int main(void) { int choice; displaymenu(); printf("Enter your choice : "); scanf("%d",&choice); return 0; } void displaymenu(void) { printf("1.Create database\n"); printf("2.Insert new record\n"); printf("3.Modify a record\n"); printf("4.Delete a record\n"); printf("5.Display all records\n"); printf("6.Exit\n"); } /*P6.12*/ #include<stdio.h> int func(void); int main(void) { printf("%d\n",func()); return 0; } /*Returns the sum of squares of all odd numbers from 1 to 25*/ int func(void) { int num,s=0; for(num=1; num<=25; num++) { if(num%2!=0) s+=num*num; } return s; } /*P6.13 Program to find the type and area of a triangle*/ #include<stdio.h> #include<math.h> void type(float a,float b,float c); void area(float a,float b,float c); int main(void) { float a,b,c; printf("Enter the sides of triangle : "); scanf("%f%f%f",&a,&b,&c); if(a<b+c && b<c+a && c<a+b) { type(a,b,c); area(a,b,c); } else printf("No triangle possible with these sides\n"); return 0; } void type(float a, float b, float c) { if((a*a)+(b*b)==(c*c) || (b*b)+(c*c)==(a*a) || (c*c)+(a*a)==(b*b)) printf("Right angled triangle\n"); if(a==b && b==c) printf("Equilateral triangle\n"); else if(a== b || b==c || c==a) printf("Isosceles trianlge\n"); else printf("Scalene triangle\n"); } void area(float a,float b,float c) { float s, area; s=(a+b+c)/2; area=sqrt(s*(s-a)*(s-b)*(s-c)); printf("Area of triangle=%f\n",area); } /*P6.14 Program to find the sum of digits of any number*/ #include<stdio.h> int sum(int n); int main(void) { int num; printf("Enter a number : "); scanf("%d",&num); printf("Sum of digits of %d is %d\n",num,sum(num)); return 0; } int sum(int n) { int sum=0; while(n>0) { sum+=n%10; n/=10; /*Skip the last digit of number*/ } return sum; } /*P6.15 Program to understand the use of global variables*/ #include<stdio.h> void func1(void); void func2(void); int a,b=6; int main(void) { printf("Inside main() : a=%d,b=%d\n",a,b); func1(); func2(); return 0; } void func1(void) { printf("Inside func1() : a=%d,b=%d\n",a,b); } void func2(void) { int a=8; printf("Inside func2() : a=%d,b=%d\n",a,b); } /*P6.16 Program to understand the use of static variables*/ #include<stdio.h> void func(void); int main(void) { func(); func(); func(); return 0; } void func(void) { int a=10; static int b=10; printf("a=%d, b=%d\n",a,b); a++; b++; } /*P6.17*/ #include<stdio.h> int reverse(int n); int main(void) { int num; printf("Enter a number : "); scanf("%d",&num); printf("%d\n",reverse(num)); return 0; } int reverse(int n) { int rev=0; do { rev=rev*10+n%10; n/=10; }while(n>0); return rev; } /*P6.18*/ #include<stdio.h> long int reverse(long int n); int isPalindrome(long int num); int main(void) { long int num; printf("Enter a number : "); scanf("%ld",&num); if(isPalindrome(num)) printf("Number is a palindrome\n"); else printf("Number is not a palindrome\n"); return 0; } int isPalindrome(long int num) { if(num==reverse(num)) return 1; return 0; } long int reverse(long int n) { long int rev=0; do { rev=rev*10+n%10; n/=10; }while(n>0); return rev; } /*P6.19*/ #include<stdio.h> #include<math.h> int isprime(int n); int main(void) { int num; printf("Enter a number : "); scanf("%d",&num); if(isprime(num)) printf("Number is prime\n"); else printf("Number is not prime\n"); return 0; } int isprime(int n) { int i; for(i=2; i<=sqrt(n); i++) if(n%i==0) return 0; return 1; } /*P6.20*/ #include<stdio.h> #include<math.h> void printPrimes(int num1,int num2); int isprime(int n); int main(void) { int num1,num2; printf("Enter two numbers : "); scanf("%d%d",&num1,&num2); printf("Prime number between %d and %d are : ",num1,num2); printPrimes(num1,num2); return 0; } void printPrimes(int num1, int num2) { int i; for(i=num1; i<=num2; i++) if(isprime(i)) printf("%d ",i); } int isprime(int n) { int i; for(i=2; i<=sqrt(n); i++) if(n%i==0) return 0; return 1; } /*P6.21*/ #include<stdio.h> long int binary(int num); int main(void) { int num; printf("Enter a number : "); scanf("%d",&num); printf("Decimal=%d, Binary=%ld\n",num,binary(num)); return 0; } long int binary(int num) { long a=1,bin=0,rem; while(num>0) { rem=num%2; bin=bin+rem*a; num/=2; a*=10; } return bin; } /*P6.22*/ #include<stdio.h> #include<math.h> double power(double a,int n); int main(void) { double a; int n; printf("Enter base : "); scanf("%lf",&a); printf("Enter exponent : "); scanf("%d",&n); printf("%lf raised to power %d is %lf\n",a,n,power(a,n)); return 0; } double power(double a,int n) { int i; double p=1; if(n==0) return 1; else { for(i=1; i<=abs(n); i++) p=p*a; if(n>0) return p; else return 1/p; } } /*P6.23 Program to convert a binary or octal number to a decimal number*/ #include<stdio.h> func(int n,int base); int main(void) { int num, base, result; char choice; printf("Enter 'b' for binary or 'o' for octal : "); scanf("%c",&choice); printf("Enter a number : "); scanf("%d",&num); base=(choice=='b')? 2 : 8; result=func(num,base); printf("Decimal number is %d\n",result); return 0; } func(int n,int base) { int rem,d,j=1,dec=0; while(n>0) { rem=n%10; /*extract last digit*/ d=rem*j; dec+=d; j*=base; n/=10; /*skip last digit*/ } return dec; } /*P6.24 Program to find out permutations and combinations*/ #include<stdio.h> long factorial(int); long perm(int,int); long comb(int,int); int main(void) { int n,r; printf("Enter n and r: "); scanf("%d%d",&n,&r); printf("Total combinations are : %ld\n",comb(n,r)); printf("Total permutations are : %ld\n",perm(n,r)); return 0; } long comb(int n,int r) { long c; c=perm(n,r)/factorial(r); return c; } long perm(int n,int r) { long p; p=factorial(n)/factorial(n-r) ; return p; } long int factorial(int n) { int i; long int fact=1; if(n==0) return 1; for(i=n; i>1; i--) fact*=i; return fact; } /*P6.25 Pascal’s triangle*/ #include<stdio.h> long factorial(int); long comb(int,int); int main(void) { int i,j,k; printf("Enter number of rows for Pascal's triangle : "); scanf("%d",&k); for(i=0; i<k; i++) { for(j=0; j<=i; j++) printf("%5ld",comb(i,j)); printf("\n"); } return 0; } long comb(int n,int r) { long c; c=factorial(n)/(factorial(r)*factorial(n-r)); return c; } long int factorial(int n) { int i; long int fact=1; if(n==0) return 1; for(i=n; i>1; i--) fact*=i; return fact; } /*P6.26 Program to convert a decimal number to roman number*/ #include<stdio.h> int roman(int,int,char); int main(void) { int num; printf("Enter a number : "); scanf("%d",&num); if(num>=1000) num = roman(num,1000,'m'); if(num>=500) num = roman(num,500,'d'); if(num>=100) num = roman(num,100,'c'); if(num>=50) num = roman(num,50,'l'); if(num>=10) num = roman(num,10,'x'); if(num>=5) num = roman(num,5,'v'); if(num>=1) roman(num,1,'i'); printf("\n"); return 0; } int roman(int n,int k,char c) { if(n==9) { printf("ix"); return 0; } if(n==4) { printf("iv"); return 0; } while(n>=k) { printf("%c",c); n=n-k; } return n; } /*P6.27 Prime factors*/ #include<stdio.h> void pfact(int num); int main(void) { int num; printf("Enter a number : "); scanf("%d",&num); pfact(num); printf("\n"); return 0; } void pfact(int num) { int i; for(i=2; num!=1; i++) while(num%i==0) { printf("%d ",i); num=num/i; } } /*P6.28 Sum of series*/ #include<stdio.h> long int factorial(int n); double power(float x,int n); double series(float x,int n); int main(void) { float x; int n; printf("Enter x : "); scanf("%f",&x); printf("Enter number of terms : "); scanf("%d",&n); printf("%lf\n",series(x,n)); return 0; } double series(float x,int n) { int i,j,sign=1; float term,sum=0; for(i=1; i<=n; i++) { sign = (i%2==0)?-1:1; j=2*i-1; term=sign*power(x,j)/factorial(j); sum+=term; } return sum; } long int factorial(int n) { int i; long int fact=1; if(n==0) return 1; for(i=n; i>1; i--) fact*=i; return fact; } double power(float x,int n) { int i; float p=1; for(i=1; i<=n; i++) p=p*x; return p; }

/*P7.1 Program to find the factorial of a number by recursive method*/ #include<stdio.h> long int fact(int n); long int Ifact(int n); int main(void) { int num; printf("Enter a number : "); scanf("%d", &num); if(num<0) printf("No factorial for negative number\n"); else printf("Factorial of %d is %ld\n", num, fact(num) ); if(num<0) printf("No factorial for negative number\n"); else printf("Factorial of %d is %ld\n", num, Ifact(num) ); return 0; }/*End of main()*/ /*Recursive*/ long int fact(int n) { if(n==0) return 1; return(n*fact(n-1)); }/*End of fact()*/ /*Iterative*/ long int Ifact(int n) { long fact=1; while(n>0) { fact=fact*n; n--; } return fact; }/*End of ifact()*/

/*P7.2 Program to display numbers from 1 to n and find their sum*/ #include<stdio.h> int summation(int n); void display1(int n); void display2(int n); int main(void) { int n; printf("Enter number of terms : "); scanf("%d", &n); display1(n); printf("\n"); display2(n); printf("\n"); printf("sum = %d\n", summation(n)); return 0; }/*End of main()*/ int summation(int n) { if(n==0) return 0; return ( n + summation(n-1) ); }/*End of summation()*/ /*displays in reverse order*/ void display1(int n) { if(n==0) return; printf("%d ",n); display1(n-1); }/*End of display1()*/ void display2(int n) { if(n==0) return; display2(n-1); printf("%d ",n); }/*End of display2()*/

/*P7.3 Program to display and find out the sum of series*/ /* Series : 1 + 2 + 3 + 4 + 5 +....... */ #include<stdio.h> int series(int n); int rseries(int n); int main(void) { int n; printf("Enter number of terms : "); scanf("%d", &n); printf("\b\b = %d\n", series(n)); /* \b to erase last +sign */ printf("\b\b = %d\n\n\n", rseries(n)); return 0; }/*End of main()*/ /*Iterative function*/ int series(int n) { int i,sum=0; for(i=1; i<=n; i++) { printf("%d + ", i); sum+=i; } return sum; }/*End of series()*/ /*Recursive function*/ int rseries(int n) { int sum; if(n==0) return 0; sum=n+rseries(n-1); printf("%d + ",n); return sum; }/*End of rseries()*/

/*P7.4 Program to display integer as sequence of digits and find sum of its digits*/ #include<stdio.h> void display(long int n); void Rdisplay(long int n); int sumdigits( long int n); int main(void) { long int num; printf("Enter number : "); scanf("%ld", &num); printf("%d\n",sumdigits(num)); printf("\n"); display(num); printf("\n"); Rdisplay(num); printf("\n"); return 0; }/*End of main()*/ /*Finds the sum of digits of an integer*/ int sumdigits(long int n) { if(n/10==0) /* if n is a single digit number*/ return n; return n%10 + sumdigits(n/10); }/*End of sumdigits()*/ /*Displays the digits of an integer*/ void display(long int n) { if(n/10==0) { printf("%d",n); return; } display(n/10); printf("%d",n%10); }/*End of display()*/ /*Displays the digits of an integer in reverse order*/ void Rdisplay(long int n) { if(n/10==0) { printf("%d",n); return; } printf("%d",n%10); Rdisplay(n/10); }/*End of Rdisplay()*/

/*P7.5 Program to convert a positive decimal number to Binary, Octal or Hexadecimal */ #include<stdio.h> void convert(int, int); int main(void) { int num; printf("Enter a positive decimal number : "); scanf("%d",&num); convert(num,2); printf("\n"); convert(num,8); printf("\n"); convert(num,16); printf("\n"); return 0; }/*End of main()*/ void convert(int num,int base) { int rem=num%base; if(num==0) return; convert(num/base,base); if(rem<10) printf("%d",rem); else printf("%c",rem-10+'A'); }/*End of convert()*/

/*P7.6 Program to raise a floating point number to a positive integer*/ #include<stdio.h> float power(float a,int n); float Ipower(float a,int n); int main(void) { float a,p; int n; printf("Enter a and n : "); scanf("%f %d",&a,&n); p=power(a,n); printf("%f raised to power %d is %f\n",a,n,p); p=Ipower(a, n); printf("%f raised to power %d is %f\n",a,n,p); return 0; }/*End of main()*/ /*Recursive*/ float power(float a,int n) { if(n==0) return 1; else return(a * power(a,n-1)); }/*End of power()*/ /*Iterative*/ float Ipower(float a,int n) { int i; float result=1; for(i=1; i<=n; i++) result=result * a; return result; }/*End of Ipower()*/

/*P7.7 Program to print prime factors*/ #include<stdio.h> void PFactors(int num); void IPFactors(int n); int main(void) { int num; printf("Enter a number : "); scanf("%d", &num); PFactors(num); printf("\n"); IPFactors(num); printf("\n"); return 0; }/*End of main()*/ void PFactors(int num) { int i=2; if(num==1) return; while(num%i!=0) i++; printf("%d ", i); PFactors(num/i); }/*End of PFactors()*/ /*Iterative*/ void IPFactors(int num) { int i; for(i=2; num!=1; i++) while(num%i == 0) { printf("%d ",i); num = num/i; } }/*End of IPFactors()*/

/*P7.8 Program to find GCD of two numbers*/ #include<stdio.h> int GCD(int a,int b); int gcd(int a,int b); int main(void) { int a, b; printf("Enter a and b : \n"); scanf("%d%d",&a, &b); printf("%d\n",GCD(a,b)); printf("%d\n",gcd(a,b)); return 0; }/*End of main()*/ /*Recursive*/ int GCD(int a,int b) { if(b==0) return a; return GCD(b, a%b); }/*End of GCD()*/ /*Iterative*/ int gcd(int a,int b) { int rem; while(b!=0) { rem=a%b; a=b; b=rem; } return a; }/*End of gcd()*/

/*P7.9 Program to generate fibonacci series*/ #include<stdio.h> int fib(int n); int TailRecursiveFib(int n); int TRfib(int n, int next, int result); void Ifib(int n); int main(void) { int nterms, i; printf("Enter number of terms : "); scanf("%d",&nterms); for(i=0; i<nterms; i++) printf("%d ",fib(i)); printf("\n"); for(i=0; i<nterms; i++) printf("%d ",TailRecursiveFib(i)); printf("\n"); Ifib(nterms); printf("\n"); return 0; } /*Recursive*/ int fib(int n) { if(n==0 || n==1) return(1); return(fib(n-1) + fib(n-2)); } int TailRecursiveFib(int n) { return TRfib(n,1,1); } int TRfib(int n, int next, int result) { if(n==0) return(result); return TRfib(n-1, next+result, next); } /*Iterative*/ void Ifib(int n) { int i,x=0,y=1,z; printf("%d ", y); for(i=1; i<n; i++) { z=x+y; printf("%d ", z); x = y; y = z; } }

/*P7.10 Program that tests whether a number is divisible by 11 and 9 or not*/ #include<stdio.h> #include<math.h> int divisibleBy9(long int x); int divisibleBy11(long int x); int main(void) { long int num; printf("Enter the number to be tested : "); scanf("%ld", &num); if(divisibleBy9(abs(num))) printf("The number is divisible by 9\n"); else printf("The number is not divisible by 9\n"); if(divisibleBy11(abs(num))) printf("The number is divisible by 11\n"); else printf("The number is not divisible by 11\n"); return 0; }/*End of main()*/ int divisibleBy9(long int n) { int sumofDigits; if(n==9) return 1; if(n<9) return 0; sumofDigits=0; while(n>0) { sumofDigits += n%10; n/=10; } return divisibleBy9(sumofDigits); }/*End of divisibleBy9()*/ int divisibleBy11(long int n) { int s1=0, s2=0,diff; if(n==0) return 1; if(n<10) return 0; while(n>0) { s1+=n%10; n/=10; s2+=n%10; n/=10; } diff = s1>s2 ? (s1-s2) : (s2-s1); return divisibleBy11(diff); }/*End of divisibleBy11()*/

/*P7.11 Program to solve Tower of Hanoi problem using recursion*/ #include<stdio.h> void tofh(int ndisk, char source, char temp, char dest); int main(void) { char source='A',temp='B',dest='C'; int ndisk; printf("Enter the number of disks : "); scanf("%d", &ndisk ); printf("Sequence is :\n"); tofh(ndisk, source, temp, dest); return 0; }/*End of main()*/ void tofh(int ndisk, char source, char temp, char dest) { if(ndisk==1) { printf("Move Disk %d from %c-->%c\n", ndisk, source, dest); return; } tofh(ndisk-1, source, dest, temp); printf("Move Disk %d from %c-->%c\n", ndisk, source, dest); tofh(ndisk-1, temp, source, dest); }/*End of tofh( )*/

/*P7.12 Program to find the factorial of a number by tail recursion*/ #include<stdio.h> long TailRecursiveFact(int n); long TRfact(int n, int result); int main(void) { int num; printf("Enter a number : "); scanf("%d", &num); if(num<0) printf("No factorial for negative number\n"); printf("Factorial of %d is %ld\n", num, TailRecursiveFact(num)); return 0; } /*Tail recursive*/ long TRfact(int n, int result) { if(n==0) return result; return TRfact(n-1, n*result); }/*End of TRFact()*/ /*Helper function for tail recursive function*/ long TailRecursiveFact(int n) { return TRfact(n,1); }/*End of TailRecursiveFact()*/

/*P7.13 Program to search an element through binary search*/ #include <stdio.h> #define SIZE 100 int binary_search(int arr[],int item, int low, int high); int main(void) { int arr[SIZE],i, item, n; printf("Enter the number of elements : "); scanf("%d",&n); printf("Enter elements of the array(in sorted order) : \n"); for(i=0; i<n; i++) scanf("%d",&arr[i]); printf("Enter the item to be searched : "); scanf("%d", &item); i=binary_search(arr,item,0,n-1); if(i==-1) printf("Not Present\n"); else printf("Present at index %d\n",i); return 0; }/*End of main()*/ int binary_search(int arr[],int item,int low,int up) { int mid; if(up < low) return -1; /*not found*/ mid=(low+up)/2; if(item > arr[mid]) return binary_search(arr,item,mid+1,up); /*Search in right portion, tail recursive call*/ else if(item < arr[mid]) return binary_search(arr,item,low,mid-1); /*Search in left portion, tail recursive call*/ else return mid; /*found*/ }/*End of binary_search()*/

/*P8.1 Program to input values into an array and display them*/ #include<stdio.h> int main(void) { int arr[5],i; for(i=0; i<5; i++) { printf("Enter a value for arr[%d] : ",i); scanf("%d",&arr[i]);
} printf("The array elements are : \n"); for(i=0; i<5; i++) printf("%d\t",arr[i]); printf("\n"); return 0; }


/*P8.2 Program to add elements of an array*/ #include<stdio.h> int main(void) { int arr[10],i,sum=0; for(i=0; i<10; i++) { printf("Enter a value for arr[%d] : ",i); scanf("%d",&arr[i]); sum+=arr[i]; } printf("Sum=%d\n",sum); return 0; }

/*P8.3 Program to count even and odd numbers in an array*/ #include<stdio.h> #define SIZE 10 int main(void) { int arr[SIZE],i,even=0,odd=0; for(i=0; i<SIZE; i++) { printf("Enter a value for arr[%d] : ",i); scanf("%d",&arr[i]); if(arr[i]%2 == 0) even++; else odd++; } printf("Even numbers=%d, Odd numbers=%d\n",even,odd); return 0; }

/*P8.4 Program to find the largest and smallest number in an array*/ #include<stdio.h> int main(void) { int i,arr[10]={2,5,4,1,8,9,11,6,3,7}; int small,large; small=large=arr[0]; for(i=1; i<10; i++) { if(arr[i] < small) small=arr[i]; if(arr[i] > large) large=arr[i]; } printf("Smallest=%d,Largest=%d\n",small,large); return 0; }

/*P8.5 Program to reverse the elements of an array*/ #include<stdio.h> int main(void) { int i,j,temp,arr[10] = {1,2,3,4,5,6,7,8,9,10}; for(i=0,j=9; i<j; i++,j--) { temp=arr[i]; arr[i]=arr[j]; arr[j]=temp; } printf("After reversing, the array is : "); for(i=0; i<10; i++) printf("%d ",arr[i]); printf("\n"); return 0; }

/*P8.6 Program to convert a decimal number to binary number*/ #include<stdio.h> int main(void) { int num,arr[15],i,j; printf("Enter a decimal number : "); scanf("%d",&num); i=0; while(num>0) { arr[i] = num%2; /*store the remainder in array*/ num/=2; i++; } printf("Binary number is : "); for(j=i-1; j>=0; j--) /*print the array backwards*/ printf("%d",arr[j]); printf("\n"); return 0; }
/*P8.7 Program to pass array elements to a function*/ #include<stdio.h> void check(int num); int main(void) { int arr[10],i; printf("Enter the array elements : "); for(i=0; i<10; i++) { scanf("%d",&arr[i]); check(arr[i]); } return 0; } void check(int num) { if(num%2==0) printf("%d is even\n",num); else printf("%d is odd\n",num); }
/*P8.8 Program to understand the effect of passing an array to a function*/ #include<stdio.h> void func(int val[]); int main(void) { int i,arr[6]={1,2,3,4,5,6}; func(arr); printf("Contents of array are : "); for(i=0; i<6; i++) printf("%d ",arr[i]); printf("\n"); return 0; } void func(int val[]) { int sum=0,i; for(i=0; i<6; i++) { val[i]=val[i]*val[i]; sum+=val[i]; } printf("Sum of squares=%d\n",sum); }

/*P8.9 Program that uses a general function which works on arrays of different sizes*/ #include<stdio.h> int add(int arr[],int n); int main(void) { int a[5]={2,4,6,8,10}; int b[8]={1,3,5,7,9,11,13,15}; int c[10]={1,2,3,4,5,6,7,8,9,10}; printf("Sum of elements of array a : %d\n",add(a,5)); printf("Sum of elements of array b : %d\n",add(b,8)); printf("Sum of elements of array c : %d\n",add(c,10)); return 0; } int add(int arr[],int n) { int i,sum=0; for(i=0; i<n; i++) sum+=arr[i]; return sum; }

/*P8.10 Program to input and display a matrix*/ #define ROW 3 #define COL 4 #include<stdio.h> int main(void) { int mat[ROW][COL],i,j; printf("Enter the elements of the matrix(%dx%d) row-wise :\n",ROW,COL); for(i=0; i<ROW; i++) for(j=0; j<COL; j++) scanf("%d",&mat[i][j]); printf("The matrix that you have entered is :\n"); for(i=0; i<ROW; i++) { for(j=0; j<COL; j++) printf("%5d",mat[i][j]); printf("\n"); } printf("\n"); return 0; }

/*P8.11 Addition of two matrices*/ #define ROW 3 #define COL 4 #include<stdio.h> int main(void) { int i,j,mat1[ROW][COL],mat2[ROW][COL],mat3[ROW][COL]; printf("Enter matrix mat1(%dx%d)row-wise :\n",ROW,COL); for(i=0; i<ROW; i++) for(j=0; j<COL; j++) scanf("%d",&mat1[i][j]); printf("Enter matrix mat2(%dx%d)row-wise :\n",ROW,COL); for(i=0; i<ROW; i++) for(j=0; j<COL; j++) scanf("%d",&mat2[i][j] ); /*Addition*/ for(i=0; i<ROW; i++) for(j=0; j<COL; j++) mat3[i][j] = mat1[i][j] + mat2[i][j]; printf("The resultant matrix mat3 is :\n"); for(i=0; i<ROW; i++) { for(j=0; j<COL; j++) printf("%5d",mat3[i][j]); printf("\n"); } return 0; }

/*P8.12 Multiplication of two matrices*/ #include<stdio.h> #define ROW1 3 #define COL1 4 #define ROW2 COL1 #define COL2 2 int main(void) { int mat1[ROW1][COL1],mat2[ROW2][COL2],mat3[ROW1][COL2]; int i,j,k; printf("Enter matrix mat1(%dx%d)row-wise :\n",ROW1,COL1); for(i=0; i<ROW1; i++) for(j=0; j<COL1; j++) scanf("%d",&mat1[i][j]); printf("Enter matrix mat2(%dx%d)row-wise :\n",ROW2,COL2); for(i=0; i<ROW2; i++) for(j=0; j<COL2; j++) scanf("%d",&mat2[i][j] ); /*Multiplication*/ for(i=0; i<ROW1; i++) for(j=0; j<COL2; j++) { mat3[i][j] = 0; for(k=0; k<COL1; k++) mat3[i][j] += mat1[i][k] * mat2[k][j]; } printf("The Resultant matrix mat3 is :\n"); for(i=0; i<ROW1; i++) { for(j=0; j<COL2; j++) printf("%5d",mat3[i][j]); printf("\n"); } return 0; }
/*P8.13 Tranpose of matrix. */ #include<stdio.h> #define ROW 3 #define COL 4 int main(void) { int mat1[ROW][COL], mat2[COL][ROW],i,j; printf("Enter matrix mat1(%dx%d) row-wise : \n",ROW,COL); for(i=0; i<ROW; i++) for(j=0; j<COL; j++) scanf("%d",&mat1[i][j]); for(i=0; i<COL; i++) for(j=0; j<ROW; j++) mat2[i][j]=mat1[j][i]; printf("Transpose of matrix is:\n"); for(i=0; i<COL; i++) { for(j=0; j<ROW; j++) printf("%5d",mat2[i][j]); printf("\n"); } return 0; }

/*P8.14 Input and output of strings using scanf() and printf()*/ #include<stdio.h> int main(void) { char str[10]="Anpara"; printf("String is : %s\n",str); printf("Enter new value for string : "); scanf("%s",str); printf("String is : %s\n",str); return 0; }

/*P8.15 Program for input and output of strings using gets() and puts()*/ #include<stdio.h> int main(void) { char str[10]; printf("Enter a string : "); gets(str); printf("String is : "); puts(str); return 0; }
/*P8.16 Program to convert a decimal number to Binary, octal or hexadecimal*/ #include<stdio.h> void func(int num,int b); int main(void) { int num,ch; printf("Enter a decimal number : "); scanf("%d",&num); printf("1.Binary\n2.Octal\n3.Hexadecimal\n"); printf("Enter your choice : "); scanf("%d",&ch); switch(ch) { case 1: printf("Binary equivalent is : "); func(num,2); break; case 2: printf("Octal equivalent is : "); func(num,8); break; case 3: printf("Hexadecimal equivalent is : "); func(num,16); break; } printf("\n"); return 0; } void func(int num,int b) { int i=0,j,rem; char arr[20]; while(num>0) { rem=num%b; num/=b; if(rem>9 && rem<16) arr[i++]=rem-10+'A'; else arr[i++]=rem+'0'; } for(j=i-1; j>=0; j--) printf("%c",arr[j]); }

/*P8.17 Linear search in an array*/ #include <stdio.h> #define MAX 50 int LinearSearch(int arr[],int n,int item); int main(void) { int i,n,item,arr[MAX],index; printf("Enter the number of elements : "); scanf("%d",&n); printf("Enter the elements : \n"); for(i=0; i<n; i++) scanf("%d", &arr[i]); printf("Enter the item to be searched : "); scanf("%d", &item); index=LinearSearch(arr,n,item); if(index==-1) printf("%d not found in array\n",item); else printf("%d found at position %d\n",item,index); return 0; } int LinearSearch(int arr[],int n,int item) { int i=0; while(i<n && item!=arr[i]) i++; if(i<n) return i; else return -1; }

/*P8.18 Binary search in an array*/ #include <stdio.h> #define MAX 50 int BinarySearch(int arr[],int size,int item); int main(void) { int i,size,item,arr[MAX],index; printf("Enter the number of elements : "); scanf("%d",&size); printf("Enter the elements(in sorted order) : \n"); for(i=0; i<size; i++) scanf("%d",&arr[i]); printf("Enter the item to be searched : "); scanf("%d",&item); index=BinarySearch(arr,size,item); if(index==-1) printf("%d not found in array\n",item); else printf("%d found at position %d\n",item,index); return 0; } int BinarySearch(int arr[],int size,int item) { int low=0,up=size-1,mid; while(low<=up) { mid=(low+up)/2; if(item > arr[mid]) low=mid+1; /*Search in right half*/ else if(item < arr[mid]) up=mid-1; /*Search in left half*/ else return mid; } return -1; }

/*P8.19*/ #include <stdio.h> #define MAX 100 int main(void) { int arr[MAX],i,j,n,temp,min; printf("Enter the number of elements : "); scanf("%d",&n); for(i=0; i<n; i++) { printf("Enter element %d : ",i+1); scanf("%d", &arr[i]); } /*Find the index of smallest element*/ min=0; for(j=1; j<n; j++) { if(arr[min]>arr[j]) min=j ; } if(min!=0) { temp=arr[0]; arr[0]=arr[min]; arr[min]=temp ; } for(i=0; i<n; i++) printf("%d ", arr[i]); printf("\n"); return 0; }

/*P8.20 Selection sort*/ #include<stdio.h> #define MAX 100 int main(void) { int arr[MAX],i,j,n,temp,min; printf("Enter the number of elements : "); scanf("%d",&n); for(i=0; i<n; i++) { printf("Enter element %d : ",i+1); scanf("%d",&arr[i]); } /*Selection sort*/ for(i=0; i<n-1; i++) { /*Find the index of smallest element*/ min=i; for(j=i+1; j<n; j++) { if(arr[min]>arr[j]) min=j ; } if(i!=min) { temp=arr[i]; arr[i]=arr[min]; arr[min]=temp ; } } printf("Sorted list is : \n"); for(i=0; i<n; i++) printf("%d ",arr[i]); printf("\n"); return 0; }

/*P8.21*/ #include <stdio.h> #define MAX 100 int main(void) { int arr[MAX],i,j,temp,n,xchanges; printf("Enter the number of elements : "); scanf("%d",&n); for(i=0; i<n; i++) { printf("Enter element %d : ",i+1); scanf("%d",&arr[i]); } xchanges=0; for(j=0; j<n-1; j++) { if(arr[j] > arr[j+1]) { temp = arr[j]; arr[j] = arr[j+1]; arr[j+1] = temp; xchanges++; } } for(i=0; i<n; i++) printf("%d ",arr[i]); printf("\nTotal exchanges = %d ",xchanges); return 0; }
/*P8.22 Bubble sort*/ #include<stdio.h> #define MAX 100 int main(void) { int arr[MAX],i,j,temp,n,xchanges; printf("Enter the number of elements : "); scanf("%d",&n); for(i=0; i<n; i++) { printf("Enter element %d : ",i+1); scanf("%d",&arr[i]); } /*Bubble sort*/ for(i=0; i<n-1 ;i++) { xchanges = 0; for(j=0; j<n-1-i; j++) { if(arr[j] > arr[j+1]) { temp = arr[j]; arr[j] = arr[j+1]; arr[j+1] = temp; xchanges++; } } if(xchanges==0) /*If list is sorted*/ break; } printf("Sorted list is :\n"); for(i=0; i<n; i++) printf("%d ",arr[i]); printf("\n"); return 0; }

/*P8.23 Program to insert an item in an array at a specified index by moving other elements to the right*/ #include<stdio.h> #define SIZE 10 int main(void) { int arr[SIZE]; int i,item,index; printf("Enter elements of the array : \n"); for(i=0; i<SIZE-1; i++) /*rightmost space in the array should be empty*/ scanf("%d",&arr[i]); printf("Enter the item to be inserted : "); scanf("%d",&item); printf("Enter the index where item is to be inserted : "); scanf("%d",&index); for(i=SIZE-2; i>=index; i--) arr[i+1]=arr[i]; /*Shift elements to the right*/ arr[i+1]=item; /*Insert item at the proper place*/ for(i=0; i<SIZE; i++) printf("%d ",arr[i]); printf("\n"); return 0; }

/*P8.24 Program to insert an item in a sorted array at the proper place by shifting other elements to the right */ #include<stdio.h> #define SIZE 10 int main(void) { int arr[SIZE]; int i,item; printf("Enter elements of the array(in sorted order) : \n"); for(i=0; i<SIZE-1; i++) /*rightmost space in the array should be empty*/ scanf("%d",&arr[i] ); printf("Enter the item to be inserted : "); scanf("%d",&item); for(i=SIZE-2; item<arr[i] && i>=0; i--) arr[i+1]=arr[i]; /*Shift elements to the right*/ arr[i+1]=item; /*Insert item at the proper place*/ for(i=0; i<SIZE; i++) printf("%d ",arr[i]); printf("\n"); return 0; }
/*P8.25 Program of sorting using insertion sort*/ #include<stdio.h> #define MAX 100 int main(void) { int arr[MAX],i,j,k,n; printf("Enter the number of elements : "); scanf("%d",&n); for(i=0; i<n; i++) { printf("Enter element %d : ",i+1); scanf("%d", &arr[i]); } /*Insertion sort*/ for(i=1; i<n; i++) { k=arr[i]; /*k is to be inserted at proper place*/ for(j=i-1; j>=0 && k<arr[j]; j--) arr[j+1]=arr[j]; arr[j+1]=k; } printf("Sorted list is :\n"); for(i=0; i<n; i++) printf("%d ",arr[i]); printf("\n"); return 0; }/*End of main()*/
/*P8.26 Program of merging two sorted arrays into a third sorted array*/ #include<stdio.h> #define MAX 100 void merge(int arr1[],int arr2[],int arr3[],int n1,int n2); int main(void) { int arr1[MAX],arr2[MAX],arr3[2*MAX],n1,n2,i; printf("Enter the number of elements in array 1 : "); scanf("%d",&n1); printf("Enter all the elements in sorted order :\n"); for(i=0; i<n1; i++) { printf("Enter element %d : ",i+1); scanf("%d",&arr1[i]); } printf("Enter the number of elements in array 2 : "); scanf("%d",&n2); printf("Enter all the elements in sorted order :\n"); for(i=0; i<n2; i++) { printf("Enter element %d : ",i+1); scanf("%d",&arr2[i]); } merge(arr1,arr2,arr3,n1,n2); printf("\nMerged list : "); for(i=0; i<n1+n2; i++) printf("%d ",arr3[i]); printf("\n"); return 0; } void merge(int arr1[],int arr2[],int arr3[],int n1,int n2) { int i,j,k; i=0; /*Index for first array*/ j=0; /*Index for second array*/ k=0; /*Index for merged array*/ while((i<=n1-1)&&(j<=n2-1)) { if(arr1[i]<arr2[j]) arr3[k++]=arr1[i++]; else arr3[k++]=arr2[j++]; } while(i<=n1-1) /*Put remaining elements of arr1 into arr3*/ arr3[k++]=arr1[i++]; while(j<=n2-1) /*Put remaining elements of arr2 into arr3*/ arr3[k++]=arr2[j++]; }/*End of merge()*/
/*P8.27 Pascal's triangle*/ #include<stdio.h> #define MAX 15 int main(void) { int a[MAX][MAX]; int i,j,n; printf("Enter n : "); scanf("%d",&n); for(i=0; i<=n; i++) { for(j=0; j<=i; j++) if(j==0 || i==j) a[i][j]=1; else a[i][j]=a[i-1][j-1]+a[i-1][j]; } for(i=0; i<n; i++) { for(j=0; j<=i; j++) printf("%5d",a[i][j]); printf("\n"); } return 0; }

/*P8.28 Magic matrix*/ #include<stdio.h> #define MAX 20 int main(void) { int a[MAX][MAX],i,j,n,num; printf("Enter value of n(odd value) : "); scanf("%d",&n); i=n-1; /*Bottom row*/ j=(n-1)/2; /*Centre column*/ for(num=1; num<=n*n; num++) { a[i][j]=num; i++; /*move down*/ j--; /*move left*/ if(num%n==0) { i-=2; /*one above the previous row*/ j++; /*back to the previous column*/ } else if(i==n) i=0; /*go to topmost row*/ else if(j==-1) j=n-1; /*go to rightmost column*/ } for(i=0; i<n; i++) { for(j=0; j<n; j++) printf("%4d",a[i][j]); printf("\n"); } return 0; }

/*P8.29 Prime numbers using sieve*/ #include<stdio.h> #define MAX 10000 int main(void) { int p,i,n,a[MAX]={0}; printf("Enter n : "); scanf("%d",&n); p=2; while(p*p <= n) { for(i=2; i*p<=n; i++) /*Cross out all multiples of p*/ a[i*p]=1; for(i=p+1; i<=n; i++) /*Find next uncrossed*/ if(a[i]==0) { p=i; break; } } /*Print all uncrossed integers*/ for(i=2; i<=n; i++) if(a[i]==0) printf("%d ",i); return 0; }

/*P8.30*/ #include<stdio.h> #define MAX 20 int main(void) { int a[MAX][MAX],i,j,n,start,end; printf("Enter value of n : "); scanf("%d", &n); for(i=0; i<n; i++) { for(j=0; j<n; j++) scanf("%d", &a[i][j]); printf("\n"); } for(i=0; i<n; i++) { for(j=0; j<n; j++) printf("%4d", a[i][j]); printf("\n"); } printf("\n\n"); for(start=0,end=n-1; start<=end; start++,end--) { for(i=start; i<=end; i++) printf("%d ",a[start][i]); for(i=start+1; i<=end; i++) printf("%d ",a[i][end]); for(i=end-1; i>=start; i--) printf("%d ",a[end][i]); for(i=end-1; i>=start+1; i--) printf("%d ",a[i][start]); } return 0; }

/*P9.1 Program to print address of variables using address operator*/ #include<stdio.h> int main(void) { int age=30; float salary=1500.50; printf("Address of age=%p\n",&age); printf("Address of salary=%p\n",&salary); return 0; } /*P9.2 Dereferencing pointer variables*/ #include<stdio.h> int main(void) { int a=87; float b=4.5; int *p1=&a; float *p2=&b; printf("Value of p1 = Address of a = %p\n",p1); printf("Value of p2 = Address of b = %p\n",p2); printf("Address of p1 = %p\n",&p1); printf("Address of p2 = %p\n",&p2); printf("Value of a = %d %d %d\n",a,*p1,*(&a)); printf("Value of b = %.1f %.1f %.1f\n",b,*p2,*(&b)); return 0; } /*P9.3 Program to print size of pointer variables and size of values dereferenced by them*/ #include<stdio.h> int main(void) { char a='x',*p1=&a; int b=12,*p2=&b; float c=12.4,*p3=&c; double d=18.34,*p4=&d; printf("sizeof(p1)=%u, sizeof(*p1)=%u\n",sizeof(p1),sizeof(*p1)); printf("sizeof(p2)=%u, sizeof(*p2)=%u\n",sizeof(p2),sizeof(*p2)); printf("sizeof(p3)=%u, sizeof(*p3)=%u\n",sizeof(p3),sizeof(*p3)); printf("sizeof(p4)=%u, sizeof(*p4)=%u\n",sizeof(p4),sizeof(*p4)); return 0; } /*P9.4 Pointer arithmetic*/ #include<stdio.h> int main(void) { int a=5,*pi=&a; char b='x',*pc=&b; float c=5.5,*pf=&c; printf("Value of pi=Address of a=%p\n",pi); printf("Value of pc=Address of b=%p\n",pc); printf("Value of pf=Address of c=%p\n",pf); pi++; pc++; pf++; printf("Now value of pi=%p\n",pi); printf("Now value of pc=%p\n",pc); printf("Now value of pf=%p\n",pf); return 0; } /*P9.5 Postfix/prefix increment/decrement in a pointer variable of base type int*/ #include<stdio.h> int main(void) { int a=5; int *p; p = &a; printf("Value of p = Address of a = %p\n",p); printf("Value of p = %p\n",++p); printf("Value of p = %p\n",p++); printf("Value of p = %p\n",p); printf("Value of p = %p\n",--p); printf("Value of p = %p\n",p--); printf("Value of p = %p\n",p); return 0; } /*P9.6 Pointer to pointer*/ #include<stdio.h> int main(void) { int a=5; int *pa; int **ppa; pa = &a; ppa = &pa; printf("Address of a=%p\n",&a); printf("Value of pa=Address of a=%p\n",pa); printf("Value of *pa=Value of a=%d\n",*pa); printf("Address of pa=%p\n",&pa); printf("Value of ppa=Address of pa=%p\n",ppa); printf("Value of *ppa=Value of pa=%p\n",*ppa); printf("Value of **ppa=Value of a=%d\n",**ppa); printf("Address of ppa=%p\n",&ppa); return 0; } /*P9.7 Program to print the value and address of the elements of an array */ #include<stdio.h> int main(void) { int arr[5] = {5,10,15,20,25}; int i; for(i=0; i<5; i++) { printf("Value of arr[%d] = %d\t",i,arr[i]); printf("Address of arr[%d] = %p\n",i,&arr[i]); } return 0; } /*P9.8 Program to print the value and address of elements of an array using pointer notation*/ #include<stdio.h> int main(void) { int arr[5]={5,10,15,20,25}; int i; for(i=0; i<5; i++) { printf("Value of arr[%d] = %d\t",i,*(arr+i)); printf("Address of arr[%d] = %p\n",i,arr+i); } return 0; } /* P9.9 Program to print the value of array elements using pointer and subscript notation */ #include<stdio.h> int main(void) { int arr[5] = {5,10,15,20,25}; int i=0; for(i=0; i<5; i++) { printf("Value of arr[%d] = ",i); printf("%d\t",arr[i]); printf("%d\t",*(arr+i)); printf("%d\t",*(i+arr)); printf("%d\n",i[arr]); printf("Address of arr[%d] = %p\n",i,&arr[i]); } return 0; } /*P9.10 Program to print the value and address of array elements by subscripting a pointer variable*/ #include<stdio.h> int main(void) { int arr[5]={5,10,15,20,25}; int i,*p; p=arr; for(i=0; i<5; i++) { printf("Address of arr[%d]= %p %p %p %p\n",i,&arr[i],arr+i,p+i,&p[i]); printf("Value of arr[%d]= %d %d %d %d\n",i,arr[i],*(arr+i),*(p+i),p[i]); } return 0; } /*P9.11 Program to understand difference between pointer to an integer and pointer to an array of integers*/ #include<stdio.h> int main(void) { int *p; /*Can point to an integer*/ int (*ptr)[5]; /*Can point to an array of 5 integers*/ int arr[5]; p=arr; /*Points to 0th element of arr*/ ptr=&arr; /*Points to the whole array arr*/ printf("p=%p,ptr=%p\n",p,ptr); p++; ptr++; printf("p=%p,ptr=%p\n",p,ptr); return 0; } /*P9.12 Program to dereference a pointer to an array*/ #include<stdio.h> int main(void) { int arr[5] = {3,5,6,7,9}; int *p=arr; int (*ptr)[5]=&arr; printf("p=%p, ptr=%p\n",p,ptr); printf("*p=%d, *ptr=%p\n",*p,*ptr); printf("sizeof(p)=%u,sizeof(*p)=%u\n",sizeof(p),sizeof(*p)); printf("sizeof(ptr)=%u,sizeof(*ptr)=%u\n",sizeof(ptr),sizeof(*ptr)); return 0; } /*P9.13 Program to print the values and address of elements of a 2-D array*/ #include<stdio.h> int main(void) { int arr[3][4]= { {10,11,12,13}, {20,21,22,23}, {30,31,32,33} }; int i,j; for(i=0; i<3; i++) { printf("Address of %dth array = %p %p\n",i,arr[i],*(arr+i)); for(j=0; j<4; j++) printf("%d %d ",arr[i][j],*(*(arr+i)+j)); printf("\n"); } return 0; } /*P9.14 Program to print elements of a 2-D array by subscripting a pointer to an array variable*/ #include<stdio.h> int main(void) { int arr[3][4] = { {10,11,12,13}, {20,21,22,23}, {30,31,32,33} }; int (*ptr)[4]; ptr = arr; printf("%p %p %p\n",ptr,ptr+1,ptr+2); printf("%p %p %p\n",*ptr,*(ptr+1),*(ptr+2)); printf("%d %d %d\n",**ptr,*(*(ptr+1)+2),*(*(ptr+2)+3)); printf("%d %d %d\n",ptr[0][0],ptr[1][2],ptr[2][3]); return 0; } /*P9.15 Program to print the elements of 3-D array using pointer notation*/ #include<stdio.h> int main(void) { int arr[2][3][2] = { { {5,10}, {6,11}, {7,12}, }, { {20,30}, {21,31}, {22,32}, } }; int i,j,k; for(i=0; i<2; i++) for(j=0; j<3; j++) { for(k=0; k<2; k++) printf("%d\t",*(*(*(arr+i)+j)+k)); printf("\n"); } return 0; } /*P9.16 Call by value*/ #include<stdio.h> void value(int x,int y); int main(void) { int a=5,b=8; printf("a=%d,b=%d\n",a,b); value(a,b); printf("a=%d,b=%d\n",a,b); return 0; } void value(int x,int y) { x++; y++; printf("x=%d,y=%d\n",x,y); } /*P9.17 Program to explain call by reference*/ #include<stdio.h> void ref(int *p,int *q); int main(void) { int a=5,b=8; printf("a=%d,b=%d\n",a,b); ref(&a,&b); printf("a=%d,b=%d\n",a,b); return 0; } void ref(int *p,int *q) { (*p)++; (*q)++; printf("*p=%d, *q=%d\n",*p,*q); } /*P9.18 Returning more than one value from a function using call by reference*/ #include<stdio.h> void func(int x,int y,int *ps,int *pd,int *pp); int main(void) { int a,b,sum,diff,prod; a=6; b=4; func(a,b,&sum,&diff,&prod); printf("Sum=%d, Difference=%d, Product=%d\n",sum,diff,prod); return 0; } void func(int x,int y,int *ps,int *pd,int *pp) { *ps=x+y; *pd=x-y; *pp=x*y; } /*P9.19 Function returning pointer*/ #include<stdio.h> int *fun(int *p,int n); int main(void) { int n=5,arr[10]={1,2,3,4,5,6,7,8,9,10}; int *ptr; ptr=fun(arr,n); printf("Value of arr=%p, Value of ptr=%p, value of *ptr=%d\n",arr,ptr,*ptr); return 0; } int *fun(int *p,int n) { p = p+n; return p; }

/*P12.1 Program to understand the use of fputc() function*/ #include<stdio.h> #include<stdlib.h> int main(void) { FILE *fptr; int ch; if((fptr=fopen("myfile","w"))==NULL) { printf("File does not exist\n"); exit(1); } printf("Enter text :\n"); /*Press Ctrl+z in DOS and Ctrl+d in Unix to stop reading characters */ while((ch=getchar())!=EOF) fputc(ch,fptr); fclose(fptr); return 0; } /*P12.2 Program to understand the use of fgetc()*/ #include<stdio.h> #include<stdlib.h> int main(void) { FILE *p; char ch; if((p=fopen("myfile","r"))==NULL) { printf("Error in opening file\n"); exit(1); } while((ch=fgetc(p))!=EOF) printf("%c",ch); fclose(p); return 0; } /*P12.3 Copy a file to another file*/ #include<stdio.h> #include<stdlib.h> int main(void) { FILE *sptr, *dptr; char ch; if((sptr=fopen("source.txt","r"))==NULL) { printf("Error in opening source file\n"); exit(1); } if((dptr=fopen("destination.txt","w"))==NULL) { printf("Error in opening destination file\n"); exit(1); } while((ch=fgetc(sptr))!=EOF) fputc(ch,dptr); fclose(sptr); fclose(dptr); return 0; } /*P12.4 Program to understand the use of fputs()*/ #include<stdio.h> #include<stdlib.h> int main(void) { FILE *fptr; char str[80]; if((fptr=fopen("test","w"))==NULL) { printf("Error in opening file\n"); exit(1); } printf("Enter the text\n"); printf("To stop entering, press Ctrl+d/Ctrl+z\n"); while(gets(str)!=NULL) fputs(str,fptr); fclose(fptr); return 0; } /*P12.5 Program to understand the use of fputs()*/ #include<stdio.h> #include<stdlib.h> int main(void) { FILE *fptr; char str[80]; if((fptr=fopen("test1","w"))==NULL) { printf("Error in opening file\n"); exit(1); } printf("Enter the text\n"); printf("To stop entering, press Ctrl+d/Ctrl+z\n"); while(gets(str)!=NULL) { strcat(str,"\n"); fputs(str,fptr); } fclose(fptr); return 0; } /*P12.6 Program to understand the use of fgets() */ #include<stdio.h> #include<stdlib.h> int main(void) { FILE *fptr; char str[80]; if( (fptr=fopen("test","r"))==NULL) { printf("Error in opening file\n"); exit(1); } while(fgets(str,80,fptr)!=NULL) puts(str); fclose(fptr); return 0; } /*P12.7 Program to understand the use of fprintf()*/ #include<stdio.h> #include<stdlib.h> int main(void) { FILE *fp; char name[10]; int age; if( (fp = fopen("rec","w"))==NULL) { printf("Error in opening file\n"); exit(1); } printf("Enter your name and age : "); scanf("%s%d",name,&age); fprintf(fp,"My name is %s and age is %d ",name,age); fclose(fp); return 0; } /*P12.8 Program to understand the use of fprintf()*/ #include<stdio.h> #include<stdlib.h> struct student { char name[20]; float marks; }stu; int main(void) { FILE *fp; int i,n; if((fp=fopen("students","w"))==NULL) { printf("Error in opening file\n"); exit(1); } printf("Enter number of records : "); scanf("%d",&n); for(i=1; i<=n; i++) { printf("Enter name and marks : "); scanf("%s%f",stu.name,&stu.marks); fprintf(fp,"%s %f",stu.name,stu.marks); } return 0; } /*P12.9 Program to understand the use of fscanf()*/ #include<stdio.h> #include<stdlib.h> struct student { char name[20]; float marks; }stu; int main(void) { FILE *fp; if((fp=fopen("students","r"))==NULL) { printf("Error in opening file\n"); exit(1); } printf("NAME\tMARKS\n"); while(fscanf(fp,"%s %f",stu.name,&stu.marks)!=EOF ) printf("%s\t%f\n",stu.name,stu.marks); fclose(fp); return 0; } /*P12.10 Program to understand the use of fwrite()*/ #include<stdio.h> #include<stdlib.h> struct record { char name[20]; int roll; int marks; }student; int main(void) { int i,n; FILE *fp; if((fp=fopen("stu","wb"))==NULL) { printf("Error in opening file\n"); exit(1); } printf("Enter number of records : "); scanf("%d",&n); for(i=0; i<n; i++) { printf("Enter name : "); scanf("%s",student.name); printf("Enter roll no : "); scanf("%d",&student.roll); printf("Enter marks : "); scanf("%d",&student.marks); fwrite(&student,sizeof(student),1,fp); } fclose(fp); return 0; } /*P12.11 Program to understand the use of fread()*/ #include<stdio.h> #include<stdlib.h> struct record { char name[20]; int roll; int marks; }student; int main(void) { FILE *fp; fp = fopen("stu","rb"); if(fp==NULL) { printf("Error in opening file\n"); exit(1); } printf("\nNAME\tROLLNO\tMARKS\n"); while(fread(&student,sizeof(student),1,fp)==1) { printf("%s\t",student.name); printf("%d\t",student.roll); printf("%d\n",student.marks); } fclose(fp); return 0; } /*P12.12 Program to understand the use of fseek()*/ #include<stdio.h> #include<stdlib.h> struct record { char name[20]; int roll; int marks; }student; int main(void) { int n; FILE *fp; fp=fopen("stu","rb"); if(fp==NULL) { printf("Error in opening file\n"); exit(1); } printf("Enter the record number to be read : "); scanf("%d",&n); fseek(fp,(n-1)*sizeof(student),0); /*skip n-1 records*/ fread(&student,sizeof(student),1,fp); /*Read the nth record*/ printf("%s\t",student.name); printf("%d\t",student.roll); printf("%d\n",student.marks); fclose(fp); return 0; } /*P12.13 Program to understand the use of ftell()*/ #include<stdio.h> #include<stdlib.h> struct record { char name[20]; int roll; int marks; }student; int main(void) { FILE *fp; fp = fopen("stu","rb"); if(fp==NULL) { printf("Error in opening file\n"); exit(1); } printf("Position indicator in the beginning -> %ld\n",ftell(fp)); while(fread(&student,sizeof(student),1,fp)==1) { printf("Position indicator -> %ld\n",ftell(fp)); printf("%s\t",student.name); printf("%d\t",student.roll); printf("%d\n",student.marks); } printf("%d\n",ftell(fp)); fclose(fp); return 0; } /*P12.14 Program to understand the use of rewind()*/ #include<stdio.h> #include<stdlib.h> int main(void) { FILE *fp; fp = fopen("stu","rb+"); if(fp==NULL) { printf("Error in opening file\n"); exit(1); } printf("Position indicator -> %ld\n",ftell(fp)); fseek(fp, 0, 2); printf("Position indicator -> %ld\n",ftell(fp)); rewind(fp); printf("Position indicator -> %ld\n",ftell(fp)); fclose(fp); return 0; } /*P12.15 Program to append records to a file*/ #include<stdio.h> #include<stdlib.h> int main(void) { struct record { char name[20]; int roll; int marks; }student; FILE *fp; int choice=1; fp=fopen("stu","ab");/*opened in append mode*/ if(fp==NULL) { printf("Error in opening file\n"); exit(1); } while(choice==1) { printf("Enter name : "); scanf("%s",student.name); printf("Enter roll no : "); scanf("%d",&student.roll); printf("Enter marks : "); scanf("%d",&student.marks); fwrite(&student,sizeof(student),1,fp); printf("Want to enter more ?(1 for yes / 0 for no) : "); scanf("%d",&choice); } fclose(fp); return 0; } /* P12.16 Program to read records from a file and calculate grade of each student and display it grade= A if marks>=80 = B if marks>=60 and < 80 = C if marks<60 */ #include<stdio.h> #include<stdlib.h> int main(void) { struct record { char name[20]; int roll; int marks; }student; FILE *fp; fp = fopen("stu","rb");/*opened in read mode */ if(fp==NULL) { printf("Error in opening file\n"); exit(1); } printf("\nNAME\t\tMARKS\t\tGRADE\n\n"); while(fread(&student,sizeof(student),1,fp)==1) { printf("%s\t\t",student.name); printf("%4d\t\t",student.marks); if(student.marks>=80) printf("A\n"); else if(student.marks>=60) printf("B\n"); else printf("C\n"); } fclose(fp); return 0; } /*P12.17 Program to modify records in a file*/ #include<stdio.h> #include<stdlib.h> #include<string.h> int main(void) { struct record { char name[20]; int roll; int marks; }student; FILE *fp; char name[20]; long size = sizeof(student); unsigned flag = 0; fp = fopen("stu","rb+"); if(fp==NULL) { printf("Error in opening file\n"); exit(1); } printf("Enter name of student whose record is to be modified : "); scanf("%s",name); while(fread(&student,sizeof(student),1,fp)==1) if(strcmp(student.name, name)==0) { printf("Enter new data -->\n"); printf("Enter name : "); scanf("%s",student.name); printf("Enter roll no : "); scanf("%d",&student.roll); printf("Enter marks : "); scanf("%d",&student.marks); fseek(fp,-size,1); fwrite(&student,sizeof(student),1,fp); flag = 1; break; } if(flag==0) printf("Name not found in file\n"); else printf("Record Modified......\n"); fclose(fp); return 0; } /*P12.18 Program to delete a record from the file*/ #include<stdio.h> #include<stdlib.h> #include<string.h> int main(void) { struct record { char name[20]; int roll; int marks; }student; FILE *fp,*fptmp; char name[20]; unsigned flag = 0; fp=fopen("stu","rb"); if(fp==NULL) { printf("Error in opening file\n"); exit(1); } printf("Enter the name to be deleted : "); scanf("%s",name); fptmp=fopen("tempfile","wb"); while(fread(&student, sizeof(student),1,fp)==1) { if(strcmp(name,student.name)!=0) fwrite(&student,sizeof(student),1,fptmp); else flag = 1; } fclose(fp); fclose(fptmp); remove("stu"); rename("tempfile","stu"); if(flag==0) printf("Name not found in file\n"); else printf("Record deleted......\n"); return 0; } /*P12.19 Program to display the records in sorted order, sorting is performed in ascending order w.r.t. name*/ #include<stdio.h> #include<stdlib.h> #include<string.h> int main(void) { struct record { char name[20]; int roll; int marks; }student,temp,stu[50]; FILE *fp; int i,j,k=0; fp = fopen("stu","rb");/*opened in read mode*/ if(fp==NULL) { printf("Error in opening file\n"); exit(1); } while(fread(&student,sizeof(student),1,fp)==1) stu[k++] = student; /*Bubble sort*/ for(i=0; i<k; i++) { for(j=0; j<k-1-i; j++) { if(strcmp(stu[j].name, stu[j+1].name)>0) { temp=stu[j]; stu[j]=stu[j+1]; stu[j+1]=temp; } } } printf("\nNAME\t\tROLLNO\t\tMARKS\n\n"); for(i=0; i<k; i++) { printf("%s\t\t",stu[i].name); printf("%d\t\t",stu[i].roll); printf("%d\n",stu[i].marks); } fclose(fp); return 0; } /*P12.20 Program to merge two files*/ #include<stdio.h> #include<stdlib.h> struct record { char name[20]; int roll; int marks; }stu1, stu2; int main(void) { FILE *fp1,*fp2,*fp3; int i,j; if((fp1=fopen("sectionA","rb")) == NULL) { printf("Error in opening file\n"); exit(1); } if((fp2=fopen("sectionB","rb"))==NULL) { printf("Error in opening file\n"); exit(1); } if((fp3=fopen("merged","wb"))==NULL) { printf("Error in opening file\n"); exit(1); } i=fread(&stu1,sizeof(stu1),1,fp1); j=fread(&stu2,sizeof(stu2),1,fp2); while((i==1)&&(j==1)) { if(stu1.marks > stu2.marks) { fwrite(&stu1,sizeof(stu1),1,fp3); i = fread(&stu1,sizeof(stu1),1,fp1); } else { fwrite(&stu2,sizeof(stu2),1,fp3); j = fread(&stu2,sizeof(stu2),1,fp2); } } while(i==1) /*Write remaining records of sectionA into merged*/ { fwrite(&stu1,sizeof(stu1),1,fp3); i = fread(&stu1,sizeof(stu1),1,fp1); } while(j==1) /*Write remaining records of sectionB into merged*/ { fwrite(&stu1,sizeof(stu1),1,fp3); j = fread(&stu2,sizeof(stu2),1,fp2); } fclose(fp1); fclose(fp2); fclose(fp3); return 0; } /*P12.21 Write a program to manage a database of books*/ #include<stdio.h> #include<string.h> #include<stdlib.h> void insert(FILE *fp); void del(FILE *fp); void modify(FILE *fp); void booksold(FILE *fp); int search(FILE *fp,char *name); void display(FILE *fp); void list(FILE *fp); struct { char name[50]; int ncopies; float cost; }book; int main(void) { int choice; FILE *fp; fp = fopen("books","rb+"); if(fp==NULL) { fp=fopen("books","wb+"); if(fp==NULL) { puts("Error in opening file\n"); exit(1); } } while(1) { printf("1.Insert a new record\n"); printf("2.Delete a record\n"); printf("3.Display record of a book\n"); printf("4.Modify an existing record\n"); printf("5.List all records\n"); printf("6 Book sold\n"); printf("7.Exit\n"); printf("Enter your choice : "); scanf("%d",&choice); switch(choice) { case 1: insert(fp); break; case 2: del(fp); break; case 3: display(fp); break; case 4: modify(fp); break; case 5: list(fp); break; case 6: booksold(fp); break; case 7: fclose(fp); exit(1); default : printf("Wrong choice\n"); }/*End of switch */ }/*End of while */ return 0; }/*End of main( ) */ void insert(FILE *fp) { fseek(fp,0,2); printf("Enter book name : "); scanf("%s",book.name); printf("Enter number of copies : "); scanf("%d",&book.ncopies); printf("Enter cost of book : "); scanf("%f",&book.cost); fwrite(&book,sizeof(book),1,fp); }/*End of insert()*/ void del(FILE *fp) { FILE *fptmp; char name[20]; printf("Enter the name of book to be deleted from database : "); scanf("%s",name); if(search(fp,name)==0) return; fptmp = fopen("tempfile","wb"); rewind(fp); while(fread(&book, sizeof(book),1,fp) == 1) { if(strcmp(name,book.name)!=0) fwrite(&book,sizeof(book),1,fptmp); } fclose(fp); fclose(fptmp); remove("books"); rename("tempfile","books"); printf("\nRecord deleted........\n\n"); fp = fopen("books", "rb+"); }/*End of del()*/ void modify(FILE *fp) { char name[50]; long size = sizeof(book); printf("Enter the name of the book to be modified : "); scanf("%s",name); if(search(fp,name) == 1) { printf("Enter new data-->\n\n"); printf("Enter book name : "); scanf("%s",book.name); printf("Enter number of copies : "); scanf("%d",&book.ncopies); printf("Enter cost of book : "); scanf("%f",&book.cost); fseek(fp,-size,1); fwrite(&book,sizeof(book),1,fp); printf("\nRecord successfully modified\n\n"); } }/*End of modify()*/ void booksold(FILE *fp) { char name[50]; long size = sizeof(book); printf("Enter the name of the book to be sold : "); scanf("%s", name); if(search(fp,name)==1) { if(book.ncopies >0) { book.ncopies--; fseek(fp, -size, 1); fwrite(&book, sizeof(book), 1, fp); printf("One book sold\n"); printf("Now number of copies = %d\n", book.ncopies); } else printf("Book is out of stock\n\n"); } }/*End of booksold( )*/ void display(FILE *fp) { char name[50]; printf("Enter the name of the book : "); scanf("%s",name); if(search(fp,name)==1) { printf("\nName\t%s\n",book.name); printf("Copies\t%d\n",book.ncopies); printf("Cost\t%f\n\n",book.cost); } }/*End of display()*/ int search(FILE *fp,char *name) { unsigned flag=0; rewind(fp); while(fread(&book, sizeof(book),1,fp)==1) { if(strcmp(name,book.name)==0) { flag = 1; break; } } if(flag == 0) printf("\nName not found in file\n\n"); return flag; }/*End of search()*/ void list(FILE *fp) { rewind(fp); printf("\nNAME\tCOPIES\t\tCOST\n\n"); while(fread(&book, sizeof(book),1,fp)==1) { printf("%s\t",book.name); printf("%d\t\t",book.ncopies); printf("%f\n",book.cost); } printf("\n"); }/*End of list()*/ /*P12.22 Program to understand the use of ferror()*/ #include<stdio.h> #include<stdlib.h> int main(void) { FILE *fptr; int ch; fptr=fopen("test","w"); ch=getc(fptr); if(ferror(fptr)) { printf("Error in read operation\n"); exit(1); } else printf("%c",ch); fclose(fptr); return 0; } /*P12.23 Program to understand the use of feof() and ferror()*/ #include<stdio.h> #include<stdlib.h> int main(void) { FILE *fptr; int ch; if((fptr=fopen("myfile","r"))==NULL) { printf("File doesn't exist\n"); exit(1); } while((ch=getc(fptr))!=EOF) printf("%c",ch); if(feof(fptr)) printf("End of file\n"); if(ferror(fptr)) printf("Error\n"); fclose(fptr); return 0; } /*P12.24 Function to understand the use of clearerr()*/ #include<stdio.h> #include<stdlib.h> int main(void) { FILE *fptr; int ch; fptr = fopen("test","w"); ch = getc(fptr); if(ferror(fptr)) { printf("Error in read operation\n"); clearerr(fptr); } fclose(fptr); return 0; } /*P12.25 Program to understand the use of rename()*/ #include <stdio.h> int main(void) { char old_name[80],new_name[80]; printf("Enter the name of file to be renamed : "); gets(old_name); printf("Enter a new name for the file : "); gets(new_name); if(rename(old_name, new_name)==0) printf("File %s renamed to %s\n",old_name,new_name); else perror("File not renamed"); return 0; } /*P12.26 Program to understand command line arguments */ #include<stdio.h> int main(int argc, char *argv[]) { int i; printf("argc = %d\n",argc); for(i=0; i<argc; i++) printf("argv[%d] = %s\n",i,argv[i]); return 0; } /*P12.27 Program to copy a file to another*/ #include<stdio.h> #include<stdlib.h> int main(int argc,char *argv[]) { FILE *source,*dest; int c; if(argc!=3) { printf("Wrong number of arguments\n"); exit(1); } if((source=fopen(argv[1],"r"))==NULL) { printf("Can’t open source file\n"); exit(1); } if((dest=fopen(argv[2],"w"))==NULL) { printf("Can’t open destination file\n"); exit(1); } while((c=fgetc(source))!=EOF) fputc(c,dest); fclose(source); fclose(dest); return 0; } /*P12.28*/ #include<stdio.h> #include<stdlib.h> int main(void) { FILE *fp1,*fp2; char name[50]; int c1,c2,found='n'; printf("Enter the file name : "); scanf("%s",name); if((fp1=fopen(name,"r"))==NULL) { printf("Error in opening file\n"); exit(1); } fp2 = fopen("c:\\new.c","w"); c1 = fgetc(fp1); c2 = fgetc(fp1); while(c2!=EOF) { if(c1=='/' && c2=='*') found = 'y'; if(found=='n') fputc(c1,fp2); if(c1=='*' && c2=='/') { found = 'n'; c2 = fgetc(fp1); } c1 = c2; c2 = fgetc(fp1); } fclose(fp1); fclose(fp2); return 0; } /*P12.29 Program to count the number of words*/ #include<stdio.h> #include<stdlib.h> int is_end(int ch); int main(void) { char line[81]; int i,count=0; FILE *fptr; if((fptr=fopen("test.txt","r")) == NULL) { printf("File doesn't exist\n"); exit(1); } while((fgets(line,81,fptr))!=NULL) { for(i=0; line[i]!='\0'; i++) if(is_end(line[i])) count++; } printf("Number of words in the file = %d\n",count); fclose(fptr); return 0; } int is_end(int ch) { switch(ch) { case '\n': case '\t': case ' ': case ',': case '.': case ':': case ';': case '-' : return 1; } return 0; } /*P12.29 Program to count the number of words*/ /* #include<stdio.h> #include<stdlib.h> int is_end(int ch); main() { char line[81]; int i,count=0; FILE *fptr; if((fptr=fopen("test.txt","r")) == NULL) { printf("File doesn't exist\n"); exit(1); } while((fgets(line,81,fptr))!=NULL) { for(i=1; line[i]!='\0'; i++) if( is_end(line[i]) && !is_end(line[i-1]) ) count++; } printf("Number of words in the file = %d\n",count); fclose(fptr); } int is_end(int ch) { switch(ch) { case '\n': case '\t': case ' ': case ',': case '.': case ':': case ';': case '-' : return 1; } return 0; } */ /*P12.30*/ #include<stdio.h> #include<stdlib.h> #include<string.h> int display(char line[],char wordtext[]); int is_end(int ch); int main(void) { char line[81]; int total = 0; FILE *fptr; if((fptr=fopen("test.txt","r")) == NULL) { printf("File doesn't exist\n"); exit(1); } while((fgets(line,81,fptr))!=NULL) total = total + display( line, "that" ); printf("Number of times the given word occurs in file is %d\n",total); fclose(fptr); return 0; } int display(char line[],char wordtext[]) { int i,j,k,len; char str[80]; int count = 0; len = strlen(wordtext); for(i=0; line[i]!='\0'; i++) { k = 0; if(is_end(line[i-1]) && is_end(line[i+len]) ) { for(k=0,j=i; k<len; j++,k++) str[k] = line[j]; str[k] = '\0'; if(strcmp(str,wordtext) == 0) count++; } } if(count>0) { printf("%s",line); printf("count = %d\n",count); } return count; } int is_end(int ch) { switch(ch) { case '\n': case '\t': case ' ': case ', ': case '.': case ':': case ';': case '-' : return 1; } return 0; } /*P12.30*/ /* #include<stdio.h> #include<stdlib.h> int display(char line[],char wordtext[]); int is_end(int ch); main() { char line[81]; int total = 0; FILE *fptr; if((fptr=fopen("test.txt","r")) == NULL) { printf("File doesn't exist\n"); exit(1); } while((fgets(line,81,fptr))!=NULL) total = total + display( line, "that" ); printf("Number of times the given word occurs in file is %d\n",total); fclose(fptr); } int display(char line[],char wordtext[]) { int i,j,k,len; char str[80]; int count = 0; len = strlen(wordtext); for(i=0; line[i]!='\0'; i++) { k = 0; if((i==0 || is_end(line[i-1])) && is_end(line[i+len]) ) { for(k=0,j=i; k<len; j++,k++) str[k] = line[j]; str[k] = '\0'; if(strcmp_in(str,wordtext) == 0) count++; } } if(count>0) { printf("%s",line); printf("count = %d\n",count); } return count; } int is_end(int ch) { switch(ch) { case '\n': case '\t': case ' ': case ', ': case '.': case ':': case ';': case '-' : return 1; } return 0; } strcmp_in(char *str1, char *str2) { int i; for(i=0; str1[i]!='\0'; i++) if(toupper(str1[i]) != toupper(str2[i])) return 1; return 0; } */ /*P12.31*/ #include<stdio.h> #include<ctype.h> #include<stdlib.h> int is_vowel(int ch); int main(void) { char wrong[100],right[150]; int i,j; FILE *fptr1,*fptr2; if((fptr1 = fopen("wrong.txt","r")) == NULL) { printf("Error in opening file\n"); exit(1); } if((fptr2 = fopen("right.txt","w")) == NULL) { printf("Error in opening file\n"); exit(1); } while((fgets(wrong,100,fptr1)) != NULL) { i=j=0; while(wrong[i]!='\0') { if(islower(wrong[i]) && (i==0 || wrong[i-1]=='.')) right[j++] = toupper(wrong[i++] ); else if(wrong[i]==' ' && wrong[i-1]=='a' && is_vowel(wrong[i+1])) { right[j++]='n'; right[j++]=wrong[i++]; } else right[j++]=wrong[i++]; } right[j]='\0'; fputs(right, fptr2); } return 0; } int is_vowel(int ch) { switch(ch) { case 'a': case 'A': case 'e': case 'E': case 'i': case 'I': case 'o': case 'O': case 'u': case 'U': return 1; } return 0; }





























































Comments

Popular posts from this blog

circular linked list

calendar program odd days method