Anda di halaman 1dari 601

1

Programs
Pointer programs only

C & C++

Indexes :
C using basic. 2) C++ using basic. 3) Using if condition. 4) Using switch case condition. 5) Using while condition. 6) Using for loop condition. 7) Using do-while condition. 8) Using files. 9) Using functions. 10) Using structures. 11) Using polymorphism. 12) Using templates & exception. 13) Using classes. 14) Using inheritance. 15) Using overloading. 16) Using dynamic memory allocation
1)

3
C Using basic
Program No : 1 : Write a program in C to display the message " welcome to c program in C me writing" ?

#include <stdio.h> #include <conio.h> void main() { char *ch="welcome to c programming writting with pointers\0"; puts(ch); getch(); } Program No : 2 : Write a program in C to find the area of circle ?

#include <stdio.h> #include <conio.h> void main() { double radius=2.0,area; double *pradius, *parea; pradius=&radius; parea=&area; *parea= 3.14 * (*pradius) * (*pradius); printf("%lf", *parea); getch(); } Program No : 3 : write a program in C to find the area of triangle ? #include <stdio.h> #include <conio.h> void main()

4
{ double l=4.0, b=2.0,area; double *lp,*bp, *parea; lp=&l; bp=&b; parea=&area; *parea= 0.5 * (*lp) * (*bp); printf("%lf", *parea); getch(); } Program No : 4 : Write a program in C to find the area of sphere ? #include <stdio.h> #include <conio.h> void main() { double rad=1.0,area; double *rp, *parea; rp=&rad; parea=&area; *parea= 4.0/3.0 * *rp * *rp * *rp; printf("%lf", *parea); getch(); } Program No : 5 : Write a program in C to find the circumference of circle ?

#include <stdio.h> #include <conio.h> void main() { double rad=1.0,cir; double *rp, *pcir; rp=&rad; pcir=&cir; *pcir= 4.0/3.0 * *rp * *rp * *rp; printf("%lf", *pcir); getch(); }

5
Program No : 6 : Write a program in C to find the area of circle using input value ? #include <stdio.h> #include <conio.h> void main() { double r, area; double *rp,*parea; rp=&r; parea=&area; scanf("%lf",&r); *parea= 3.14 * (*rp) * (*rp); printf("%lf", *parea); getch(); } Program No : 7 : Write a program in C to find the area of triangle using input value ?

#include <stdio.h> #include <conio.h> void main() { double l, b,area; double *lp,*bp, *parea; lp=&l; bp=&b; parea=&area; scanf("%lf%lf",&l,&b); *parea= 0.5 * (*lp) * (*bp); printf("%lf", *parea); getch(); } Program No : 8 : Write a program in C to find the area of sphere using input value ?

#include <stdio.h> #include <conio.h> void main()

6
{ double r, area; double *rp,*parea; rp=&r; parea=&area; scanf("%lf",&r); *parea= 4.0/3.0 * (*rp) * (*rp) * (*rp); printf("%lf", *parea); getch(); } Program No : 9 : Write a program in C to find the circumference of circle using input value ?

#include <stdio.h> #include <conio.h> void main() { double r, cir; double *rp,*pcir; rp=&r; pcir=&cir; scanf("%lf",&r); *pcir= 2.0 * 3.14 * (*rp); printf("%lf", *pcir); getch(); } Program No : 10 : Write a program in C to read and print an given character ?

#include <stdio.h> #include <conio.h> void main() { char ch, *pch; pch=&ch; scanf("%c", &ch); printf("%c", *pch);

7
getch(); } Program No : 11 : Write a program in C to print the ascii value of given character ?

#include <stdio.h> #include <conio.h> void main() { char ch, *pch; pch=&ch; scanf("%c", &ch); printf("%d", *pch); getch(); } Program No : 12 : Write a program in C to read and print the given integer ? #include <stdio.h> #include <conio.h> void main() { int num, *pnum; pnum=&num; scanf("%d", &num); printf("%d", *pnum); getch(); } Program No : 13 : Write a program in C to read and print the given float ?

#include <stdio.h> #include <conio.h> void main() {

8
float num, *pnum; pnum=&num; scanf("%f", &num); printf("%f", *pnum); getch(); } Program No : 14 : Write a program in C to read and print the given float with two decimal places ? #include <stdio.h> #include <conio.h> void main() { float num, *pnum; pnum=&num; scanf("%f", &num); printf("%.2f", *pnum); getch(); } Program No : 15 : Write a program in C to read and print the given double ?

#include <stdio.h> #include <conio.h> void main() { double num, *pnum; pnum=&num; scanf("%lf", &num); printf("%lf", *pnum); getch(); } Program No : 16 : Write a program in C to read and print the given double with two decimal places ? #include <stdio.h> #include <conio.h>

9
void main() { double num, *pnum; pnum=&num; scanf("%lf", &num); printf("%.2lf", *pnum); getch(); } Program No : 17 : Write a program in C to Arithmatic operations of two integer numbers ? #include <stdio.h> void main() { int one,two; int *po, *pt; po=&one; pt=&two; scanf("%d%d", &one,&two); printf("sum : %d diff : %d pro : %d quo : %d remind :%d", *po+*pt,*po-*pt,*po**pt,*po/(*pt),*po%*pt); getch(); } Program No : 18 : Write a program in C to Arithmatic operations of two integer numbers .Print these results in separate lines ?

#include <stdio.h> void main() {

10
int one,two; int *po, *pt; po=&one; pt=&two; scanf("%d%d", &one,&two); printf("\nsum : %d \ndiff : %d \npro : %d \nquo : %d \nremind :%d", *po+*pt,*po-*pt,*po**pt,*po/(*pt),*po%*pt); getch(); } Program No : 19 : Write a program in C to Arithmatic operations of two long numbers .Print these results in separate lines ?
#include <stdio.h>

void main() { long one,two; long *po, *pt; po=&one; pt=&two; scanf("%ld%ld", &one,&two); printf("\nsum : %ld \ndiff : %ld \npro : %ld \nquo : %ld \nremind :%ld", *po+*pt,*po-*pt,*po**pt,*po/(*pt),*po%*pt); getch(); } Program No : 20 : Write a program in C to Arithmatic operations of two float numbers .Print these results in separate lines ?

#include <stdio.h> void main() { float one,two; float *po, *pt; po=&one; pt=&two; scanf("%f%f", &one,&two); printf("\nsum : %f \ndiff : %f \npro : %f \nquo : %f", *po+*pt,*po-*pt,*po**pt,*po/(*pt)); getch();

11
} Program No : 21 : Write a program in C to Arithmatic operations of two double numbers .Print these results in separate lines ?

#include <stdio.h> void main() { double one,two; double *po, *pt; po=&one; pt=&two; scanf("%lf%lf", &one,&two); printf("\nsum : %lf \ndiff : %lf \npro : %lf \nquo : %lf", *po+*pt,*po-*pt,*po**pt,*po/(*pt)); getch(); } Program No : 22 : Write a program in C to print the upper case letters to lower case #include <stdio.h> #include <conio.h> void main() { char any, *pany; pany=&any; scanf("%c", &any); printf("%c", *pany+32); getch(); } Program No : 23 : Write a program in C to print the lower case letters to upper case ? #include <stdio.h> #include <conio.h> void main() { char any, *pany; pany=&any; scanf("%c", &any);

12
printf("%c", *pany-32); getch(); } Program No : 24 : Write a program in C to read float value and print integer equivalent ?

#include <stdio.h> #include <conio.h> void main() { float fno, *pf; int ino, *pi; pi=&ino; pf=&fno; scanf("%f", &fno); *pi=(int)*pf; printf("%d",*pi); getch(); } Program No : 25 : Write a program in C to read int value and print float equivalent ?
#include <stdio.h>

#include <conio.h> void main() { float fno, *pf; int ino, *pi; pi=&ino; pf=&fno; scanf("%d", &ino); *pf=*pi; // *pf=(float)*pi , both are same. printf("%f",*pf); getch(); }

C++ Using basic


Program No : 1 : Write a program in C++ to display the message ? welcome to c++ programmer writing? ?

13
#include <iostream.h> #include <conio.h> void main() { char *ch="welcome to cpp sample programs"; cout<<ch<<endl; getch(); }
Program No : 2 : Write a program in C++ to find the area of circle ?

#include <iostream.h> #include <conio.h> void main() { double radius,area, *pr,*pa; pr=&radius; pa=&area; *pr=3.0; *pa= 3.14 *(*pr) *(*pr); cout<<*pa<<endl; getch(); }
Program No : 3 : write a program in C++ to find the area of triangle ?

#include <iostream.h> #include <conio.h> void main() { double breath,height, area, *pb, *ph, *pa; pa=&area; pb=&breath; ph=&height; *pb=1.0; *ph=2.0; *pa = 0.5 * (*pb) * (*ph); cout<<"area : "<<*pa<<endl;

14
getch(); }

Program No : 4 : Write a program in C++ to find the area of sphere ?

#include <iostream.h> #include <conio.h> void main() { double radius, area, *pr, *pa; pa=&area; pr=&radius; *pr=2.0; *pa = 4.0/3.0 * 3.1415 * (*pr) * (*pr) * (*pr); cout<<"area : "<<*pa<<endl; getch(); }
Program No : 5 : Write a program in C++ to find the circumference of circle ?

#include <iostream.h> #include <conio.h> void main() { double radius, cir, *pr, *pc; pc=&cir; pr=&radius; *pr=2.0; *pc = 2.0 * 3.1415 * (*pr) * (*pr); cout<<"Cir : "<<*pc<<endl; getch(); }
Program No : 6 : Write a program in C++ to find the area of circle using input value ?

15
#include <iostream.h> #include <conio.h> void main() { double radius,area, *pr,*pa; pr=&radius; pa=&area; cout<<"Enter Radius"<<endl; cin>>*pr; *pa= 3.14 *(*pr) *(*pr); cout<<*pa<<endl; getch(); }
Program No : 7 : Write a program in C++ to find the area of triangle using input value ?

#include <iostream.h> #include <conio.h> void main() { double breath,height, area, *pb, *ph, *pa; pa=&area; pb=&breath; ph=&height; cout<<"Enter Breath and Height"<<endl; cin>>*pb>>*ph; *pa = 0.5 * (*pb) * (*ph); cout<<"area : "<<*pa<<endl; getch(); }

Program No : 8 : Write a program in C++ to find the area of sphere using input value ?

16
#include <iostream.h> #include <conio.h> void main() { double radius, area, *pr, *pa; pa=&area; pr=&radius; cout<<"Enter Radius"<<endl; cin>>*pr; *pa = 4.0/3.0 * 3.1415 * (*pr) * (*pr) * (*pr); cout<<"area : "<<*pa<<endl; getch(); }
Program No : 9 : Write a program in C++ to find the circumference of circle using input value ?

#include <iostream.h> #include <conio.h> void main() { double radius, cir, *pr, *pc; pc=&cir; pr=&radius; cout<<"Enter Radius"<<endl; cin>>*pr; *pc = 2.0 * 3.1415 * (*pr) * (*pr); cout<<"Cir : "<<*pc<<endl; getch(); }
Program No : 10 : Write a program in C++ to read and print an given character ?

#include <iostream.h> #include <conio.h> void main()

17
{ char any, *p; p=&any; cout<<"enter character"<<endl; cin>>*p; cout<<"given character"<<endl; cout<<*p<<endl; getch(); }
Program No : 11 : Write a program in C++ to print the ascii value of given character ?

#include <iostream.h> #include <conio.h> void main() { char any,*p; p=&any; cout<<"enter character"<<endl; cin>>*p; cout<<"equivalent ascii value of given character"<<endl; cout<<(int)*p<<endl; getch(); }

Program No : 12 : Write a program in C++ to read and print the given integer ?

#include <iostream.h> #include <conio.h> void main() { int any, *p; p=&any; cout<<"Enter Number"<<endl; cin>>*p; cout<<"Given number"<<endl; cout<<*p<<endl; getch();

18
}

Program No : 13 : Write a program in C++ to read and print the given float ?

#include <iostream.h> #include <conio.h> void main() { float any, *p; p=&any; cout<<"Enter Number"<<endl; cin>>*p; cout<<"Given number"<<endl; cout<<*p<<endl; getch(); }
Program No : 14 : Write a program in C++ to read and print the given float with two decimal places ?

#include <iostream.h> #include <conio.h> #include <iomanip.h> void main() { float any, *p; p=&any; cout<<"Enter Number"<<endl; cin>>*p; cout<<"Given number"<<endl; cout<<setprecision(4)<<*p<<endl; getch(); }
Program No : 15 : Write a program in C++ to read and print the given double ?

#include <iostream.h> #include <conio.h> void main() { double any, *p;

19
p=&any; cout<<"Enter Number"<<endl; cin>>*p; cout<<"Given number"<<endl; cout<<*p<<endl; getch(); }
Program No : 16 : Write a program in C++ to read and print the given double with two decimal places ?

#include <iostream.h> #include <conio.h> #include <iomanip.h> void main() { double any, *p; p=&any; cout<<"Enter Number"<<endl; cin>>*p; cout<<"Given number"<<endl; cout<<setprecision(4)<<*p<<endl; getch(); }
Program No : 17 : Write a program in C++ to Arithmatic operations of two integer numbers ?

#include <iostream.h> #include <conio.h> void main() { int one,two, *p1, *p2; p1=&one; p2=&two; cout<<"Enter number"<<endl; cin>>*p1>>*p2; cout<<"sum : "<<*p1+*p2<<" diff: "<<*p1-*p2<<" pro : "<<*p1 * *p2 <<" quo : "<<*p1\*p2<<" rem : "<<*p1%*p2<<endl; getch(); }
Program No : 18 : Write a program in C++ to Arithmatic operations of two integer numbers .Print these results in separate lines ?

20
#include <iostream.h> #include <conio.h> void main() { int one,two; int *p1, *p2; p1=&one; p2=&two; cout<<"Enter number"<<endl; cin>>*p1>>*p2; cout<<"sum : "<<*p1+*p2<<endl; cout<<"diff: "<<*p1-*p2<<endl; cout<<"pro : "<<*p1* *p2<<endl; cout<<"quo : "<<(*p1)/(*p2)<<endl; cout<<"rem : "<<*p1%*p2<<endl; getch(); }

Program No : 19 : Write a program in C++ to Arithmatic operations of two long numbers .Print these results in separate lines ?

#include <iostream.h> #include <conio.h> void main() { long one,two; long *p1, *p2; p1=&one; p2=&two; cout<<"Enter number"<<endl; cin>>*p1>>*p2; cout<<"sum : "<<*p1+*p2<<endl; cout<<"diff: "<<*p1-*p2<<endl; cout<<"pro : "<<*p1* *p2<<endl; cout<<"quo : "<<(*p1)/(*p2)<<endl; cout<<"rem : "<<*p1%*p2<<endl; getch(); }

21
Program No : 20 : Write a program in C++ to Arithmatic operations of two float numbers .Print these results in separate lines ?

#include <iostream.h> #include <conio.h> void main() { float one,two; float *p1, *p2; p1=&one; p2=&two; cout<<"Enter number"<<endl; cin>>*p1>>*p2; cout<<"sum : "<<*p1+*p2<<endl; cout<<"diff: "<<*p1-*p2<<endl; cout<<"pro : "<<*p1* *p2<<endl; cout<<"quo : "<<*p1/(*p2)<<endl; getch(); }
Program No : 21 : Write a program in C++ to Arithmatic operations of two double numbers .Print these results in separate lines ?

#include <iostream.h> #include <conio.h> void main() { double one,two; double *p1, *p2; p1=&one; p2=&two; cout<<"Enter number"<<endl; cin>>*p1>>*p2; cout<<"sum : "<<*p1+*p2<<endl; cout<<"diff: "<<*p1-*p2<<endl; cout<<"pro : "<<*p1* *p2<<endl; cout<<"quo : "<<*p1/(*p2)<<endl; getch(); }
Program No : 22 : Write a program in C++ to print the upper case letters to lower case ?

22
#include <iostream.h> #include <conio.h> void main() { char any, *p; p=&any; cout<<"enter upper case letter"<<endl; cin>>*p; cout<<"small case for given letter"<<endl; cout<<(char)(*p+32)<<endl; getch(); }
Program No : 23 : Write a program in C++ to print the lower case letters to upper case ?

#include <iostream.h> #include <conio.h> void main() { char any, *p; p=&any; cout<<"enter lower case letter"<<endl; cin>>*p; cout<<"upper case for given letter"<<endl; cout<<(char)(*p-32)<<endl; getch(); }
Program No : 24 : Write a program in C++ to read float value and print integer equivalent ?

#include <iostream.h> #include <conio.h> void main() { float one, *pf; int two, *pi; pf=&one; pi=&two; cout<<"Enter ur float value"<<endl; cin>>*pf; cout<<"equivalent integer value"<<endl; *pi=(int)*pf;

23
cout<<*pi<<endl; getch(); }

Program No : 25 : Write a program in C++ to read int value and print float equivalent ?

#include <iostream.h> #include <conio.h> void main() { int one, *pi; float two, *pf; pi=&one; pf=&two; cout<<"Enter ur int value"<<endl; cin>>*pi; cout<<"equivalent float value"<<endl; *pf=(float)*pi; cout<<*pf<<endl; getch(); }
Program No : 26 : Write a program in C++ to show of a numeric value of a variable using hex, oct, and dec manipulator functions. ?

#include <iostream.h> #include <conio.h> void main() { int num, *p; p=&num; cout<<"Enter number"<<endl; cin>>*p; cout<<"Deciamal base : "<<dec<<*p<<endl; cout<<"hex base : "<<hex<<*p<<endl; cout<<"octal base : "<<oct<<*p<<endl; getch();

24
}

Program No : 27 : Write a program in C++ to show of a numeric value of a variable using setbase functions. ?

#include <iostream.h> #include <conio.h> #include <iomanip.h> void main() { int num, *p; p=&num; cout<<"Enter number"<<endl; cin>>*p; cout<<"Binary base : "<<setbase(2)<<*p<<endl; cout<<"Decimal base : "<<setbase(10)<<*p<<endl; cout<<"Octal base : "<<setbase(8)<<*p<<endl; cout<<"hex base : "<<setbase(16)<<*p<<endl; getch(); }
Program No : 28 : Write a program in C++ to disply the value using setw manipulator functions. ?

#include <iostream.h> #include <conio.h> #include <iomanip.h> void main() { int num1,num2, *p1, *p2; p1=&num1; p2=&num2; cout<<"Enter two numbers"<<endl; cin>>*p1>>*p2; cout<<num1<<num2<<endl; cout<<setw(1)<<*p1<<setw(1)<<*p2<<endl; cout<<setw(2)<<*p1<<setw(2)<<*p2<<endl; cout<<setw(3)<<*p1<<setw(3)<<*p2<<endl; cout<<setw(4)<<*p1<<setw(4)<<*p2<<endl; getch(); }

25
Program No : 29 : Write a program in C++ to disply the value and fill unused field width some characters using setfill function. ?

#include <iostream.h> #include <conio.h> #include <iomanip.h> void main() { int num1,num2, *p1, *p2; p1=&num1; p2=&num2; cout<<"Enter two numbers"<<endl; cin>>*p1>>*p2; cout<<setfill('*'); cout<<num1<<num2<<endl; cout<<setw(1)<<*p1<<setw(1)<<*p2<<endl; cout<<setw(2)<<*p1<<setw(2)<<*p2<<endl; cout<<setw(3)<<*p1<<setw(3)<<*p2<<endl; cout<<setw(4)<<*p1<<setw(4)<<*p2<<endl; getch(); }

Using if conditions
Program No : 1 : write a program in C++ to check whether the given number even or odd ?

#include <iostream.h> #include <conio.h> void main() { int number, *p; p=&number; cout<<"enter number"<<endl; cin>>*p; if (*p % 2 ==1) cout<<"odd"; else cout<<"even"; cout<<endl; getch(); }

26
Program No : 2 : write a program in C++ to check whether the given number positive ,negative or zero ?

#include <iostream.h> #include <conio.h> void main() { int number, *p; p=&number; cout<<"enter number"<<endl; cin>>*p; if (*p >0) cout<<"postive"; else { if (*p<0) cout<<"negative"; else cout<<"zero"; } cout<<endl; getch(); }

Program No : 3 : write a program in C++ to check whether the given year is leap or not ?

#include <iostream.h> #include <conio.h> void main() { int year, *p; p=&year; cout<<"enter year"<<endl; cin>>*p; if (*p % 4 == 0) cout<<"leap"; else cout<<"not a leap"; cout<<endl; getch(); }

Program No : 4 : Write a program in C++ to check whether the given character upper, lower , numbr or not ?

27
#include <iostream.h> #include <conio.h> void main() { char any, *p; p=&any; cout<<"Enter char"<<endl; cin>>*p; if(*p>='A' && *p<='Z') cout<<"upper case"; else if(*p>='a' && *p<='z') cout<<"lower case"; else if(*p>='0' && *p<='9') cout<<"number"; else cout<<"special "; cout<<endl; getch(); }

Program No : 5 : Write a program in C++ to change upper to lower ?

#include <iostream.h> #include <conio.h> void main() { char any, *p; p=&any; cout<<"enter upper char\n"; cin>>*p; cout<<"enter lower char\n"; if(*p>='A' && *p <='Z') cout<<(char)(*p+32); else cout<<*p; cout<<endl; getch(); }

Program No : 6 : Write a program in C++ to change lower to upper ?

28
#include <iostream.h> #include <conio.h> void main() { char any, *p; p=&any; cout<<"enter lower char\n"; cin>>*p; cout<<"enter upper char\n"; if(*p>='a' && *p<='z') cout<<(char)(*p-32); else cout<<*p; cout<<endl; getch(); }
Program No : 7 : Write a program in C++ to print a word form of given number between 0 to 9 ?

#include <iostream.h> #include <conio.h> void main() { int number, *p; p=&number; cout<<"enter number "<<endl; cin>>*p; cout<<"Word form : "; if (*p==0) cout<<"Zero"; else if (*p==1) cout<<"One"; else if (*p==2) cout<<"Two"; else if (*p==3) cout<<"Three"; else if (*p==4) cout<<"Four"; else if (*p==5) cout<<"Five"; else if (*p==6) cout<<"Six"; else

29
if (*p==7) cout<<"Seven"; else if (*p==8) cout<<"eight"; else if(*p==9) cout<<"Nine"; else cout<<"others"; cout<<endl; getch(); }
Program No : 8 : Write a program in C++ to print a word form of given number is tens between 1 to 99 ?

#include <iostream.h> #include <conio.h> void main() { int number, *p; p=&number; cout<<"enter number "<<endl; cin>>*p; cout<<"Word form : "; if (*p==10) cout<<"ten"; else if (*p==20) cout<<"twenty"; else if (*p==30) cout<<"Thirty"; else if (*p==40) cout<<"forty"; else if (*p==50) cout<<"Fifty"; else if (*p==60) cout<<"sixty"; else if (*p==70) cout<<"Seventy"; else if (*p==80) cout<<"eighty"; else

30
if (*p==90) cout<<"ninety"; else cout<<"others"; cout<<endl; getch(); }
Program No : 9 : Write a program in C++ for relations operations of two given integer numbers ?

#include <iostream.h> #include <conio.h> void main() { int one, two, *p1, *p2; p1=&one; p2=&two; cout<<"enter numbers"<<endl; cin>>*p1>>*p2; if (*p1==*p2) cout<<"first is equal to second"; else { if (*p1 >*p2) cout<<"first is greater than second"; else cout<<"first is less than second"; } cout<<endl; getch(); }
Program No : 10 : Write a program in C++ for relations operations of two given float numbers ?

#include <iostream.h> #include <conio.h> void main() { float one, two, *p1, *p2; p1=&one; p2=&two; cout<<"enter numbers"<<endl; cin>>*p1>>*p2; if (*p1==*p2) cout<<"first is equal to second"; else { if (*p1 >*p2) cout<<"first is greater than second";

31
else cout<<"first is less than second"; } cout<<endl; getch(); }
Program No : 11 : Write a program in C++ for given mark contain which grade ?

#include <iostream.h> #include <conio.h> void main() { int mark, *p; p=&mark; cout<<"enter mark"<<endl; cin>>*p; if (*p>=80) cout<<"distinction"; else if (*p>=60) cout<<"first class"; else if(*p>=50) cout<<"second class"; else if(*p>=40) cout<<"third class"; else cout<<"fail"; cout<<endl; getch(); }
Program No : 12 : Write a program in C++ to find biggest of two given numbers ?

#include <iostream.h> #include <conio.h> void main() { int one,two, *p1, *p2; p1=&one; p2=&two; cout<<"enter numbers"<<endl; cin>>*p1>>*p2; cout<<"big : ";

32
if (*p1>*p2) cout<<one; else cout<<two; cout<<endl; getch(); }
Program No : 13 : Write a program in C++ to find smallest of two given numbers ?

#include <iostream.h> #include <conio.h> void main() { int one,two, *p1, *p2; p1=&one; p2=&two; cout<<"enter numbers"<<endl; cin>>*p1>>*p2; cout<<"Small : "; if (*p1<*p2) cout<<one; else cout<<two; cout<<endl; getch(); }

Program No : 14 : Write a program in C++ to find biggest of three given numbers ?

#include <iostream.h> #include <conio.h> void main() { int one,two,three, *p1, *p2, *p3; p1=&one; p2=&two; p3=&three; cout<<"enter numbers"<<endl; cin>>*p1>>*p2>>*p3; cout<<"Big : "; if (*p1>*p2) { if (*p1>*p3) cout<<*p1; else cout<<*p3; } else

33
{ if (*p2>*p3) cout<<*p2; else cout<<*p3; } cout<<endl; getch(); }
Program No : 15 : Write a program in C++ to find smallest of three given numbers ?

#include <iostream.h> #include <conio.h> void main() { int one,two,three, *p1, *p2, *p3; p1=&one; p2=&two; p3=&three; cout<<"enter numbers"<<endl; cin>>*p1>>*p2>>*p3; cout<<"Small : "; if (*p1<*p2) { if (*p1<*p3) cout<<*p1; else cout<<*p3; } else { if (*p2<*p3) cout<<*p2; else cout<<*p3; } cout<<endl; getch(); }
Program No : 16 : Write a program in C++ to find biggest of three given numbers using && operator ?

#include <iostream.h> #include <conio.h> void main() { int one,two,three; int *p1, *p2, *p3; p1=&one;

34
p2=&two; p3=&three; cout<<"enter numbers"<<endl; cin>>*p1>>*p2>>*p3; cout<<"Big : "; if(*p1>*p2 && *p1>*p3) cout<<*p1; else if(*p2>*p3) cout<<*p2; else cout<<*p3; cout<<endl; getch(); }
Program No : 17 : Write a program in C++ to find smallest of three given numbers using && operator ?

#include <iostream.h> #include <conio.h> void main() { int one,two,three; int *p1, *p2, *p3; p1=&one; p2=&two; p3=&three; cout<<"enter numbers"<<endl; cin>>*p1>>*p2>>*p3; cout<<"small : "; if(*p1<*p2 && *p1<*p3) cout<<*p1; else if(*p2<*p3) cout<<*p2; else cout<<*p3; cout<<endl; getch(); }
Program No : 18 : Write a program in C++ to display the name of the day in a week .here given input range is 1 to 7. ?

#include <iostream.h> #include <conio.h> void main()

35
{ int day, *p; p=&day; cout<<"Enter weak day number"; cin>>*p; if (*p==1) cout<<"Sunday"; else if (*p==2) cout<<"Monday"; else if (*p==3) cout<<"Tuesday"; else if (*p==4) cout<<"wednesday"; else if (*p==5) cout<<"thursday"; else if (*p==6) cout<<"friday"; else if (*p==7) cout<<"Saturday"; else cout<<"others"; cout<<endl; getch(); }

Program No : 19 : Write a program in C++ to find an biggest of four given numbers ?

#include<iostream.h> #include <conio.h> void main() { int one,two,three,four, *p1, *p2, *p3, *p4; p1=&one; p2=&two; p3=&three; p4=&four; cout<<"Enter the numbers"<<endl; cin>>*p1>>*p2>>*p3>>*p4; cout<<"Big : "; if(*p1>*p2 && *p1>*p3 && *p1>*p4) cout<<*p1; else

36
if(*p2>*p4&&*p2>*p3) cout<<*p2; else if(*p3>*p4) cout<<*p3; else cout<<*p4; cout<<endl; getch(); }
Program No : 20 : Write a program in C++ to find an smallest of four given numbers ?

#include<iostream.h> #include <conio.h> void main() { int one,two,three,four, *p1, *p2, *p3, *p4; p1=&one; p2=&two; p3=&three; p4=&four; cout<<"Enter the numbers"<<endl; cin>>*p1>>*p2>>*p3>>*p4; cout<<"Small : "; if(*p1<*p2 && *p1<*p3 && *p1<*p4) cout<<*p1; else if(*p2<*p4&&*p2<*p3) cout<<*p2; else if(*p3<*p4) cout<<*p3; else cout<<*p4; cout<<endl; getch(); }

Program No : 21 : Write a program in C++ to find a vowel or not of given character ?

#include <iostream.h> #include <conio.h> void main() { char vowel, *p; p=&vowel;

37
cout<<"enter char"<<endl; cin>>*p; if(*p=='a' ||*p=='e' ||*p=='i' ||*p=='o' ||*p=='u' || *p=='A' ||*p=='E' ||*p=='I' ||*p=='O' ||*p=='U' ) cout<<"vowel"; else cout<<"not a vowel"; cout<<endl; getch(); }

38
Using switch case
Program No : 1 : Write a program in C to find biggest of two numbers using ternary operator ?

#include <stdio.h> #include <conio.h> void main() { int day,*pd; pd=&day; scanf("%d",pd); switch(*pd) { case 1: printf("sunday"); break; case 2: printf("monday"); break; case 3: printf("tuesday"); break; case 4: printf("wednesday"); break; case 5: printf("thursday"); break; case 6: printf("friday"); break; case 7: printf("saturday"); break; default : printf("wrong input"); } getch(); }
Program No : 2 : Write a program in C to find smallest of two numbers using ternary operator ?

#include <stdio.h>

39
void main() { int number,*pnum; pnum=&number; scanf("%d",pnum); switch(*pnum) { case 1: printf("one"); break; case 2: printf("two"); break; case 3: printf("three"); break; case 4: printf("four"); break; case 5: printf("five"); break; case 6: printf("six"); break; case 7: printf("seven"); break; case 8: printf("eight"); break; case 9: printf("nine"); break; case 0: printf("zero"); break; default : printf("wrong input"); } getch(); }
Program No : 3 : Write a program in C to find biggest of three numbers using ternary operator ?

#include <stdio.h> #include <conio.h> void main() { int number, *pnum; pnum=&number; scanf("%d",pnum);

40
switch(*pnum) { case 10: printf("ten"); break; case 20: printf("twenty"); break; case 30: printf("thirty"); break; case 40: printf("fourty"); break; case 50: printf("fifty"); break; case 60: printf("sixty"); break; case 70: printf("seventy"); break; case 80: printf("eighty"); break; case 90: printf("ninety"); break; default : printf("ohters"); } getch(); }

Program No : 4 : Write a program in C to find smallest of three numbers using ternary operator ?

#include <stdio.h> #include <conio.h> void main() { char vowel,*pch; pch=&vowel; scanf("%c",pch);

41
switch(*pch) { case 'a': case 'A': printf("%c is voewl",*pch); break; case 'e': case 'E': printf("%c is voewl",*pch); break; case 'i': case 'I': printf("%c is voewl",*pch); break; case 'o': case 'O': printf("%c is voewl",*pch); break; case 'u': case 'U': printf("%c is voewl",*pch); break; default : printf("%c is not a vowel",*pch); } getch(); }

using while condition


Program No : 1 : Write a program in C to print the numbers 1 to 100 ?

#include <stdio.h> #include <conio.h> void main() { int loop,*p; p=&loop; *p=1; while (*p<=100) {

42
printf("%d\n",*p); *p=*p+1; } getch(); }

Program No : 2 : Write a program in C to sum of first 100 numbers ?

#include <stdio.h> #include <conio.h> void main() { int loop,*p, sum, *psum; p=&loop; psum= *p=1; *psum=0; while (*p<=100) { *psum=*psum+*p; *p=*p+1; } printf("sum : %d ", *psum); getch(); }

Program No : 3 : Write a program in C to sum of first N numbers ?

#include <stdio.h> #include <conio.h> void main() { int loop,*p, sum,n,*pn, *psum; p=&loop;

43
psum= pn=&n; scanf("%d", pn); *p=1; *psum=0; while (*p<=*pn) { *psum=*psum+*p; *p=*p+1; } printf("sum of first %d numbers: %d ",*pn, *psum); getch(); }
Program No : 4 : Write a program in C to sum of M to N numbers ?

#include <stdio.h> #include <conio.h> void main() { int loop,*p, sum,*psum, m, *pm, n,*pn; p=&loop; psum= pn=&n; pm=&m; scanf("%d%d",pm,pn); *p=*pm; *psum=0; while (*p<=*pn) { *psum=*psum+*p;

44
*p=*p+1; } printf("sum of %d to %d numbers: %d ",*pm,*pn, *psum); getch(); }
Program No : 5 : Write a program in C to find the average of 1 to 100 ?

#include <stdio.h> #include <conio.h> void main() { int loop,*p ; float sum, *psum, avg, *pavg; p=&loop; psum= pavg=&avg; *p=1; *psum=0.0; while (*p<=100) { *psum=*psum+*p; *p=*p+1; } *pavg = (*psum)/(100); printf("avg= %f",*pavg); getch(); }
Program No : 6 : Write a program in C to sum of first N numbers ?

#include <stdio.h> #include <conio.h> void main() { int loop,*p, n, *pn;

45
float sum, *psum, avg, *pavg; p=&loop; psum= pavg=&avg; pn=&n; scanf("%d",pn); *p=1; *psum=0.0; while (*p<=*pn) { *psum=*psum+*p; *p=*p+1; } *pavg = (*psum)/(*pn); printf("avg= %f",*pavg); getch(); }
Program No : 7 : Write a program in C to average of M to N numbers ?

#include <stdio.h> #include <conio.h> void main() { int loop,*p, m, *pm,n, *pn; float sum, *psum, avg, *pavg; p=&loop; psum= pavg=&avg; pn=&n; pm=&m;

46
scanf("%d%d",pm,pn); *p=*pm; *psum=0.0; while (*p<=*pn) { *psum=*psum+*p; *p=*p+1; } *pavg = (*psum)/((*pn-*pm)+1); printf("avg= %f",*pavg); getch(); }
Program No : 8 : Write a program in C to display the even or odd numbers between 1 to 100 ?

#include <stdio.h> #include <conio.h> void main() { int no, *pno; pno=&no; *pno=1; while (*pno<=100) { if(*pno%2==0) printf("even %d\n",*pno); else printf("odd %d\n",*pno); ++*pno;//++(*pno) } getch(); }
Program No : 9 : Write a program in C to display the even or odd numbers of first N numbers ?

#include <stdio.h> #include <conio.h> void main() {

47
int no, *pno, n, *pn;; pno=&no; pn=&n; scanf("%d", pn); *pno=1; while (*pno<=*pn) { if(*pno%2==0) printf("even %d\n",*pno); else printf("odd %d\n",*pno); ++*pno;//++(*pno) } getch(); }

Program No : 10 : Write a program in C to display the even or odd numbers between to ranges ?

#include <stdio.h> #include <conio.h> void main() { int no, *pno, m, *pm, n, *pn;; pno=&no; pn=&n; pm=&m; scanf("%d%d", pm,pn); *pno=*pm; while (*pno<=*pn) { if(*pno%2==0) printf("even %d\n",*pno); else printf("odd %d\n",*pno); ++*pno;//++(*pno) } getch(); }
Program No : 11 : Write a program in C to display the leap years between 1000 to 2000 ?

48
#include <stdio.h> #include<conio.h> void main() { int m,n, *pm,*pn;; pm=&m; pn=&n; *pm=1000; *pn=2000; while (*pm<=*pn) { if(*pm%4==0) printf("Leap %d\n",*pm); else printf("not %d\n",*pm); (*pm)++; } getch(); }

Program No : 12 : Write a program in C to display the leap years of given range ?

#include <stdio.h> #include<conio.h> void main() { int m,n, *pm,*pn;; pm=&m; pn=&n; scanf("%d%d", pm, pn); while (*pm<=*pn) { if(*pm%4==0) printf("Leap %d\n",*pm); else printf("not %d\n",*pm); (*pm)++; }

49
getch(); }
Program No : 13 : Write a program in C to print the numbers between two ranges ?

#include <stdio.h> #include <conio.h> void main() { int M,N; scanf("%d%d",&M,&N); while (M<=N) { printf("%d\n",M); ++M; } getch(); }
Program No : 14 : Write a program in C to print the number until -1000 is given a input ?

#include <stdio.h> void main() { int input,*pin; pin=&input; scanf("%d",pin); while(*pin!=-1000) { printf("%d" , *pin); scanf("%d",pin); } }
Program No : 15 : Write a program in C to print the positive , negative and zero until -1000 is given a input ?

50
#include <stdio.h> void main() { int input,*pin; pin=&input; scanf("%d",pin); while(*pin!=-1000) { if(*pin>0) printf("%d is positve", *pin); else if(*pin<0) printf("%d is negative", *pin); else printf("zero"); scanf("%d",pin); } }
Program No : 16 : Write a program in C to count the positive , negative numbers and zero until -1000 is given a input ?

#include <stdio.h> #include <conio.h> void main() { int input,*pin,pos,neg,zero ; int *ppos, *pneg,*pzero; pin=&input; ppos=&pos; pneg=&neg; pzero=&zero; *ppos=*pneg=*pzero=0; scanf("%d",pin); while(*pin!=-1000) {

51
if(*pin>0) ++*ppos; else if(*pin<0) ++*pneg; else ++*pzero; scanf("%d",pin); } printf("positve %d\n",*ppos); printf("negative %d\n",*pneg); printf("zero %d\n",*pzero); getch(); }
Program No : 17 : Write a program in C to sum of the positive , negative numbers until -1000 is given a input ?

#include <stdio.h> #include <conio.h> void main() { int input,*pin; int psum, nsum, *ppsum,*pnsum; pin=&input; ppsum=&psum; pnsum=&nsum; *ppsum=*pnsum=0; scanf("%d",pin); while(*pin!=-1000) { if(*pin>0) *ppsum=*ppsum+*pin; else if(*pin<0) *pnsum=*pnsum+*pin; scanf("%d",pin); } printf("positve sum : %d\n",*ppsum);

52
printf("negative sum : %d\n",*pnsum); getch(); }

Program No : 18 : Write a program in C to mean of the positive , negative numbers until -1000 is given a input ?

#include <stdio.h> #include <conio.h> void main() { int input,*pin; float psum, nsum, *ppsum,*pnsum; int pcount, ncount, *pp, *pn; pin=&input; ppsum=&psum; pnsum=&nsum; pp=&pcount; pn=&ncount; *pp=*pn=0; *ppsum=*pnsum=0.0; scanf("%d",pin); while(*pin!=-1000) { if(*pin>0) { *ppsum=*ppsum+*pin; ++*pp ; } else if(*pin<0) { *pnsum=*pnsum+*pin; ++*pn; } scanf("%d",pin); } printf("positve avg : %f\n",*ppsum/(*pp)); printf("negative avg : %f\n",*pnsum/(*pn));

53
getch(); }
Program No : 19 : Write a program in C to factorial of given number until -1000 is given a input ?

#include <stdio.h> #include <conio.h> void main() { int input,fact, *pin, *pf ; pin=&input; pf=&fact; scanf("%d",pin); while(*pin!=-1000) { if(*pin==0) { *pf=1; } else { int i=1; *pf=1; while(i<=*pin) { *pf*=i; i++; } } printf("fact %d\n",*pf); scanf("%d",pin); } }
Program No : 20 : Write a program in C to find biggest of two numbers using ternary operator ?

54
#include <stdio.h> #include <conio.h> void main() { int one,two,big; int *po, *pt, *pb; po=&one ; pt=&two; pb=&big; scanf("%d%d",po,pt); *pb=*po>*pt?*po:*pt; printf("big %d",*pb); getch(); }

Program No : 21 : Write a program in C to find smallest of two numbers using ternary operator ?

#include <stdio.h> #include <conio.h> void main() { int one,two,small; int *po, *pt, *ps; po=&one ; pt=&two; ps=&small; scanf("%d%d",po,pt); *ps=*po<*pt?*po:*pt; printf("small %d",*ps); getch(); }
Program No : 22 : Write a program in C to find biggest of three numbers using ternary operator ?

55
#include <stdio.h> #include <conio.h> void main() { int one,two, three, big; int *po, *pt,*pr, *pb; po=&one ; pt=&two; pr=&three; pb=&big; scanf("%d%d%d",po,pt,pr); *pb=*po>*pt?(*po>*pr?*po:*pr) :(*pt>*pr?*pt:*pr); printf("big %d",*pb); getch(); }
Program No : 23 : Write a program in C to find smallest of three numbers using ternary operator ?

#include <stdio.h> #include <conio.h> void main() { int one,two, three, small; int *po, *pt,*pr, *ps; po=&one ; pt=&two; pr=&three; ps=&small; scanf("%d%d%d",po,pt,pr); *ps=*po<*pt?(*po<*pr?*po:*pr) :(*pt<*pr?*pt:*pr); printf("small %d",*ps); getch(); }
Program No : 24 : Write a program in C to find biggest of ten numbers ?

56
#include <stdio.h> #include <conio.h> void main() { int input,big,loop=1; int *pin, *pb; pin=&input; pb=&big; *pb=-32768; while(loop<=10) { scanf("%d",pin); *pb=*pin>*pb?*pin:*pb; loop++;

} printf("big %d",*pb); getch(); }


Program No : 25 : Write a program in C to find smallest of ten numbers ?

#include <stdio.h> #include <conio.h> void main() { int input,small,loop=1; int *pin, *ps; pin=&input; ps=&small; *ps=32767; while(loop<=10) { scanf("%d",pin); *ps=*pin<*ps?*pin:*ps; loop++;

57
} printf("small %d",*ps); getch(); }
Program No : 26 : Write a program in C to find biggest of N numbers ?

#include <stdio.h> #include <conio.h> void main() { int input,big,loop=1,n,*pn; int *pin, *pb; pin=&input; pn=&n; pb=&big; scanf("%d", pn); *pb=-32768; while(loop<=*pn) { scanf("%d",pin); *pb=*pin>*pb?*pin:*pb; loop++;

} printf("big %d",*pb); getch(); }


Program No : 27 : Write a program in C to find smallest of N numbers using ternary operator ?

#include <stdio.h> #include <conio.h> void main() { int input,small,loop=1,n,*pn; int *pin, *ps;

58
pin=&input; ps=&small; pn=&n; *ps=32767; scanf("%d", pn); while(loop<=*pn) { scanf("%d",pin); *ps=*pin<*ps?*pin:*ps; loop++; } printf("small %d",*ps); getch(); }
Program No : 28 : Write a program in C to read and display 10 numbers using array ?

#include <conio.h> #include<stdio.h> void main() { int num[10], *p; int i=0; p=&num; while(i<10) { scanf("%d",p+i); i++; } i=0; while(i<10) { printf("%d ",*(p+i)); i++; } getch(); }
Program No : 29 : Write a program in C to read and display of N numbers using array ?

59
#include <conio.h> #include<stdio.h> void main() { int num[100], n, *p; int i=0; p=&num; scanf("%d", &n); while(i<n) { scanf("%d",p+i); i++; } i=0; while(i<n) { printf("%d ",*(p+i)); i++; } getch(); }
Program No : 30 : Write a program in C to find mean of ten numbers using array ?

#include <conio.h> #include<stdio.h> void main() { float num[10], *p; int i=0; float sum,mean, *pm; pm= *pm=0.0; p=&num; while(i<10) {

60
scanf("%f",p+i); i++; } i=0; while(i<10) { *pm=*pm+*(p+i); i++; } mean=*pm/10.00; printf("mean %f", mean); getch(); }

Program No : 31 : Write a program in C to find mean of N numbers using array ?

#include <conio.h> #include<stdio.h> void main() { float num[100], *p; int i=0,n; float sum,mean, *pm; pm= *pm=0.0; p=&num; scanf("%d", &n); while(i<n) { scanf("%f",p+i); i++; } i=0; while(i<n) { *pm=*pm+*(p+i); i++;

61
} mean=*pm/n; printf("mean %f", mean); getch(); }
Program No : 32 : Write a program in C to find the biggest of 10 numbers using array ?

#include <conio.h> #include<stdio.h> void main() { int num[10], *p, big, *pb; int i=0; pb=&big; p=&num; *pb=-32768; while(i<10) { scanf("%d",p+i); i++; } i=0; while(i<10) { *pb=*(p+i)>*pb?*(p+i):*pb; i++; } printf("big %d",*pb); getch(); }
Program No : 33 : Write a program in C to find the smallest of 10 numbers using array ?

#include <conio.h>

62
#include<stdio.h> void main() { int num[10], *p, small, *ps; int i=0; ps=&small; p=&num; *ps=32767; while(i<10) { scanf("%d",p+i); i++; } i=0; while(i<10) { *ps=*(p+i)<*ps?*(p+i):*ps; i++; } printf("Small %d",*ps); getch(); }

Program No : 34 : Write a program in C to find the position of biggest number in the array ?

#include <conio.h> #include<stdio.h> void main() { int num[10], *p, pos; int i=0; p=&num; pos=0;

63
while(i<10) { scanf("%d",p+i); i++; } i=0; while(i<10) { pos=*(p+i)>*(p+pos)? i:pos; i++; } printf("biggest Position %d",pos); getch(); }
Program No : 35 : Write a program in C to find the position of smallest number in the array ?

#include <conio.h> #include<stdio.h> void main() { int num[10], *p, pos; int i=0; p=&num; pos=0; while(i<10) { scanf("%d",p+i); i++; } i=0; while(i<10) { pos=*(p+i)<*(p+pos)? i:pos; i++;

64
} printf("Smallest Position %d",pos); getch(); }
Program No : 36 : Write a program in C to find the positions of biggest&& smallest number in the array ?

#include <conio.h> #include<stdio.h> void main() { int num[10], *p, bpos, spos; int i=0; p=&num; bpos=spos=0; while(i<10) { scanf("%d",p+i); i++; } i=0; while(i<10) { spos=*(p+i)<*(p+spos)? i:spos; bpos=*(p+i)>*(p+bpos)? i:bpos; i++; } printf("Smallest Position %d\n",spos); printf("Big Position %d",bpos); getch(); }
Program No : 37 : Write a program in C to interchange the biggest&& smallest number in the array ?

65
#include <conio.h> #include<stdio.h> void main() { int num[10], *p, bpos, spos,temp; int i=0; p=&num; bpos=spos=0; while(i<10) { scanf("%d",p+i); i++; } i=0; while(i<10) { spos=*(p+i)<*(p+spos)? i:spos; bpos=*(p+i)>*(p+bpos)? i:bpos; i++; } temp=*(p+bpos); *(p+bpos)=*(p+spos); *(p+spos)=temp; printf("Interchanged Array"); i=0; while(i<10) { printf("\n%d ",*(p+i)); i++; } getch(); }

66
Program No : 38 : Write a program in C to find the second biggest of N numbers using array ?

#include <conio.h> #include<stdio.h> void main() { int num[10],n, *p, big1,big2; int i=0; p=&num; big1=-32678; big2=-32677; scanf("%d", &n); while(i<n) { scanf("%d",p+i); i++; } i=0; while(i<n) { big1=*(p+i)>big1? *(p+i):big1; i++; } i=0; while(i<n) { if(*(p+i)!=big1) big2=*(p+i)>big2? *(p+i):big2; i++; } printf("second biggest %d",big2); getch();

67
}

Program No : 39 : Write a program in C to find the second smallest of N numbers using array ?

#include <conio.h> #include<stdio.h> void main() { int num[10],n, *p, small1,small2; int i=0; p=&num; small1=32677; small2=32676; scanf("%d", &n); while(i<n) { scanf("%d",p+i); i++; } i=0; while(i<n) { small1=*(p+i)<small1? *(p+i):small1; i++; } i=0; while(i<n) { if(*(p+i)!=small1) small2=*(p+i)<small2? *(p+i):small2; i++; } printf("second smallest %d",small2);

68
getch(); }
Program No : 40 : Write a program in C to find the second biggest and smallest of N numbers using array ?

#include <conio.h> #include<stdio.h> void main() { int num[100],n, *p, big1, big2, small1,small2; int i=0; p=&num; small1=32677; small2=32676; big1=-32678; big2=-32677; scanf("%d", &n); while(i<n) { scanf("%d",p+i); i++; } i=0; while(i<n) { big1=*(p+i)>big1? *(p+i):big1; small1=*(p+i)<small1? *(p+i):small1; i++; } i=0; while(i<n) { if(*(p+i)!=small1) small2=*(p+i)<small2? *(p+i):small2; if(*(p+i)!=big1)

69
big2=*(p+i)>big2? *(p+i):big2; i++; } printf("second biggest %d\n",big2) ; printf("second smallest %d",small2); getch(); }
Program No : 41 : Write a program in C to find the positions of second biggest and smallest of N numbers using array ?

#include <conio.h> #include<stdio.h> void main() { int num[100],n, *p, b1pos, b2pos, s1pos,s2pos; int i=0; p=&num; b1pos=b2pos=s1pos=s2pos=0; scanf("%d", &n); while(i<n) { scanf("%d",p+i); i++; } i=0; while(i<n) { b1pos=*(p+i)>*(p+b1pos)? i:b1pos; s1pos=*(p+i)<*(p+s1pos)? i:s1pos; i++; } i=0; while(i<n) { if(*(p+i)!=*(p+s1pos))

70
s2pos=*(p+i)<*(p+s2pos)? i:s2pos; if(*(p+i)!=*(p+b1pos)) b2pos=*(p+i)>*(p+b2pos)? i:b2pos; i++; } printf("second biggest position %d\n",b2pos) ; printf("second smallest position %d",s2pos); getch(); }
Program No : 42 : Write a program in C to interchange the second biggest and smallest of N numbers using array ?

#include <conio.h> #include<stdio.h> void main() { int num[100],n, *p, b1pos, b2pos, s1pos,s2pos,temp; int i=0; p=&num; b1pos=b2pos=s1pos=s2pos=0; scanf("%d", &n); while(i<n) { scanf("%d",p+i); i++; } i=0; while(i<n) { b1pos=*(p+i)>*(p+b1pos)? i:b1pos; s1pos=*(p+i)<*(p+s1pos)? i:s1pos; i++; } i=0;

71
while(i<n) { if(*(p+i)!=*(p+s1pos)) s2pos=*(p+i)<*(p+s2pos)? i:s2pos; if(*(p+i)!=*(p+b1pos)) b2pos=*(p+i)>*(p+b2pos)? i:b2pos; i++; } temp=*(p+b2pos); *(p+b2pos)=*(p+s2pos); *(p+s2pos)=temp; printf("Interchanged Array"); i=0; while(i<n) { printf("\n%d ",*(p+i)); i++; }

getch(); }
Program No : 43 : Write a program in C to read and write 3x3 matrix ?

#include <conio.h> #include<stdio.h> void main() { int mat[3][3], *pm; int i=0,j=0; pm=&mat; while ( i<3) {

72
j=0; while (j<3) { scanf("%d", (pm+i*3)+j ); j++; } i++; } i=0,j=0; while ( i<3) { i=0; while (j<3) { printf("%d ", *((pm+i*3)+j)); j++; } i++; printf("\n"); } getch(); }
Program No : 44 : Write a program in C to read and display the transpose of 3x3 matrix ?

#include <conio.h> #include<stdio.h> void main() { int mat[3][3], trans[3][3], *pm, *pt; int i=0,j=0; pm=&mat;

73
pt=&trans; while ( i<3) { j=0; while (j<3) { scanf("%d", (pm+i*3)+j ); j++; } i++; } i=0,j=0; while ( i<3) { j=0; while (j<3) { *((pt+j*3)+i)=*((pm+i*3)+j); j++; } i++; } i=0,j=0; while ( i<3) { j=0; while (j<3) { printf("%d ", *((pt+i*3)+j)); j++; } i++; printf("\n"); }

74
getch(); }
Program No : 45 : Write a program in C to find the sum of matrix ?

#include <conio.h> #include<stdio.h> void main() { int mat1[3][3], mat2[3][3], sum[3][3], *p1, *p2, *ps ; int i=0,j=0; p1=&mat1; p2=&mat2; ps= printf("enter matrix 1 \n"); while ( i<3) { j=0; while (j<3) { scanf("%d", (p1+i*3)+j ); j++; } i++; } printf("enter matrix 2 \n"); i=0,j=0; while ( i<3) { j=0; while (j<3) {

75
scanf("%d", (p2+i*3)+j ); j++; } i++; } i=0,j=0; while ( i<3) { j=0; while (j<3) { *((ps+i*3)+j)=*((p1+i*3)+j) + *((p2+i*3)+j); j++; } i++; } printf("sum matrix \n"); i=0,j=0; while ( i<3) { j=0; while (j<3) { printf("%d ", *((ps+i*3)+j)); j++; } i++; printf("\n"); } getch(); }
Program No : 46 : Write a program in C to find the product of two matrixes ?

76
#include <conio.h> #include<stdio.h> void main() { int mat1[3][3], mat2[3][3], pro[3][3], *p1, *p2, *ps ; int i=0,j=0,k=0; p1=&mat1; p2=&mat2; ps=&pro; printf("enter matrix 1 \n"); while ( i<3) { j=0; while (j<3) { scanf("%d", (p1+i*3)+j ); j++; } i++; } printf("enter matrix 2 \n"); i=0,j=0; while ( i<3) { j=0; while (j<3) { scanf("%d", (p2+i*3)+j ); j++; } i++; }

77
i=0,j=0; while ( i<3) { j=0; while (j<3) { *((ps+i*3)+j) =0; k=0; while(k<3) { *((ps+i*3)+j) += *((p1+i*3)+k) * *((p2+k*3)+j); k++; } j++; } i++; } printf("product matrix \n"); i=0,j=0; while ( i<3) { j=0; while (j<3) { printf("%d ", *((ps+i*3)+j)); j++; } i++; printf("\n"); } getch(); }
Program No : 47 : Write a program in C to read and display MxN matrix ?

#include <conio.h>

78
#include<stdio.h> void main() { int mat[5][5],m,n, *pm; int i=0,j=0; pm=&mat; printf("enter rows and colums\n"); scanf("%d%d", &m,&n); printf("Enter matrix\n"); while ( i<m) { j=0; while (j<n) { scanf("%d", (pm+i*5)+j ); // here 5 is depends on declartion. j++; } i++; } i=0,j=0; while ( i<m) { j=0; while (j<n) { printf("%d ", *((pm+i*5)+j)); j++; } i++; printf("\n"); }

79
getch(); }
Program No : 48 : Write a program in C to read and display the transpose of MxN matrix ?

#include <conio.h> #include<stdio.h> void main() { int mat[5][5], trans[5][5], *pt, m,n, *pm; int i=0,j=0; pm=&mat; pt=&trans; printf("enter rows and colums\n"); scanf("%d%d", &m,&n); printf("Enter matrix\n"); while ( i<m) { j=0; while (j<n) { scanf("%d", (pm+i*5)+j ); // here 5 is depends on declartion. j++; } i++; } i=0; j=0; while (i<m) { j=0;

80
while(j<n) { *((pt+j*5)+i)=*((pm+i*5)+j); j++; } i++; } i=0,j=0; while ( i<n) { j=0; while (j<m) { printf("%d ", *((pt+i*5)+j)); j++; } i++; printf("\n"); } getch(); }
Program No : 49 : Write a program in C to sum of two MxN matrix ?

#include <conio.h> #include<stdio.h> void main() { int mat1[5][5], mat2[5][5], sum[5][5], *p1,*p2, * m,n, *pm; int i=0,j=0; pm= p1=&mat1;

81
p2=&mat2; printf("enter rows and colums\n"); scanf("%d%d", &m,&n); printf("Enter matrix 1 \n"); while ( i<m) { j=0; while (j<n) { scanf("%d", (p1+i*5)+j ); // here 5 is depends on declartion. j++; } i++; } printf("Enter matrix 2 \n"); i=0; j=0; while ( i<m) { j=0; while (j<n) { scanf("%d", (p2+i*5)+j ); // here 5 is depends on declartion. j++; } i++; } i=0; j=0; while (i<m) { j=0; while(j<n) { *((pm+i*5)+j)=*((p1+i*5)+j)+ *((p2+i*5)+j);

82
j++; } i++; } printf("sum Matrix \n"); i=0,j=0; while ( i<n) { j=0; while (j<m) { printf("%d ", *((pm+i*5)+j)); j++; } i++; printf("\n"); } getch(); }
Program No : 50 : Write a program in C to poduct of two MxN matrix ?

#include <conio.h> #include<stdio.h> void main() { int mat1[5][5], mat2[5][5], pro[5][5], *p1,*p2, m1,n1,m2,n2, *pm; int i=0,j=0,k=0; pm=&pro; p1=&mat1; p2=&mat2;

83
printf("enter rows and colums of matrix 1\n"); scanf("%d%d", &m1,&n1); printf("enter rows and colums of matrix 2\n"); scanf("%d%d", &m2,&n2); if(n1!=m2 ) { printf(" please give no of col of first is equal to no of row in second"); getch(); exit(); } printf("Enter matrix 1 \n"); while ( i<m1) { j=0; while (j<n1) { scanf("%d", (p1+i*5)+j ); // here 5 is depends on declartion. j++; } i++; } printf("Enter matrix 2 \n"); i=0; j=0; while ( i<m2) { j=0; while (j<n2) { scanf("%d", (p2+i*5)+j); // here 5 is depends on declartion. j++; } i++;

84
} i=0; j=0; while (i<m1) { j=0; while(j<n2) { *((pm+i*5)+j)=0; k=0; while(k<n1) // k<m2, both are same { *((pm+i*5)+j) += *((p1+i*5)+k) * *((p2+k*5)+j); k++; } j++; } i++; } printf("Result Matrix \n"); i=0,j=0; while ( i<m1) { j=0; while (j<n2) { printf("%d ", *((pm+i*5)+j)); j++; } i++; printf("\n"); } getch(); }

85
Program No : 51 : Write a program in C to read the characters and display until $ is given ?

#include <stdio.h> #include <conio.h> void main() { char ch, *p; p=&ch; scanf("%c",p); while(*p!='$') { printf("%c",*p); scanf("%c",p); } }
Program No : 52 : Write a program in C to read the characters and display ascii value until $ is given ?

#include <stdio.h> #include <conio.h> void main() { char ch, *p; p=&ch; scanf("%c",p); while(*p!='$') { printf("%d ", (int)(*p)); fflush(stdin); scanf("%c",p); } }

86
Program No : 53 : Write a program in C to classify the character is upper or lower case until $ is given ?

#include <stdio.h> #include <conio.h> void main() { char ch, *p; p=&ch; scanf("%c",p); while(*p!='$') { if('A'<=*p && 'Z'>=*p) printf("upper %c\n",*p); else if('a'<=*p && 'z'>=*p) printf("lower %c\n",*p); else printf("others %c\n",*p); fflush(stdin); scanf("%c",p); } }
Program No : 54 : Write a program in C to count the upper , lower , number and special characters until $ is given ?

#include <stdio.h> #include <conio.h> void main() { char ch, *p; int upper, lower, number, special; upper=lower=number=special=0; p=&ch; scanf("%c",p); while(*p!='$') {

87
if('A'<=*p && 'Z'>=*p) upper++; else if('a'<=*p && 'z'>=*p) lower++; else if('0'<=*p && '9'>=*p) number++; else special++; fflush(stdin); scanf("%c",p); } printf("upper %d\n",upper); printf("lower %d\n",lower); printf("number %d\n",number); printf("special %d\n",special); getch(); }
Program No : 55 : Write a program in C to read and print a word ?

#include <stdio.h> #include <conio.h> void main() { char ch[100], *p; p=&ch; printf("enter word\n"); scanf("%c",p); while(*p!=' ') { p++; scanf("%c",p);

88
} *p='\0'; printf("given word\n"); p=&ch; while(*p!='\0') { printf("%c",*p); p++; } getch(); }
Program No : 56 : Write a program in C to read and print a sentence until . is given ?

#include <stdio.h> #include <conio.h> void main() { char ch[100], *p; p=&ch; printf("enter text\n"); scanf("%c",p); while(*p!='.') { p++; scanf("%c",p); }

89
*p='\0'; printf("given text\n"); p=&ch; while(*p!='\0') { printf("%c",*p); p++; } getch(); }

Program No : 57 : Write a program in C to read a line until ?\n? is given ?

#include <stdio.h> #include <conio.h> void main() { char ch[100], *p; int i=0; p=&ch; printf("enter text\n"); scanf("%c",p); while(*p!='\n' && i<100) { p++; i++;

90
scanf("%c",p); } *p='\0'; printf("given text\n"); p=&ch; while(*p!='\0') { printf("%c",*p); p++; } getch(); }
Program No : 58 : Write a program in C to read a text until ?$? is given ?

#include <stdio.h> #include <conio.h> void main() { char ch[100], *p; int i=0; p=&ch; printf("enter text\n"); scanf("%c",p); while(*p!='$' && i<100) { p++;

91
i++; scanf("%c",p); } *p='\0'; printf("given text\n"); p=&ch; while(*p!='\0') { printf("%c",*p); p++; } getch(); }
Program No : 59 : Write a program in C to count the number of characters in the input text ?

#include <stdio.h> #include <conio.h> void main() { char ch[100], *p; int count=0; p=&ch; printf("enter text\n"); scanf("%c",p); while(*p!='$') {

92
p++; scanf("%c",p); } *p='\0'; count=p-ch; printf("Charcaters : %d", count); getch(); }
Program No : 60 : Write a program in C to count the number of words in the input text ?

#include <stdio.h> #include <conio.h> void main() { char ch[100], *p; int count=0; p=&ch; printf("enter text\n"); scanf("%c",p); while(*p!='$') { p++; scanf("%c",p); }

93
*p='\0'; p=&ch; while(*p!='\0') { if((*p==' '&&*(p+1)!=' ')|| (*p=='\t'&&*(p+1)!='\t')|| (*p=='\n' &&*(p+1)!='\n')) count++; p++; } printf("Words : %d", count); getch(); }
Program No : 61 : Write a program in C to count the number of characters ,words and lines ?

#include <stdio.h> #include <conio.h> void main() { char ch[100], *p; int line=0, word=0, characters=0; p=&ch; printf("enter text\n"); scanf("%c",p); while(*p!='$') { p++; scanf("%c",p);

94
} *p='\0'; p=&ch; while(*p!='\0') { if((*p==' '&&*(p+1)!=' ')|| (*p=='\t'&&*(p+1)!='\t')|| (*p=='\n' &&*(p+1)!='\n')) word++; if(*p=='\n') line++; p++; } if(*(p-1)!='\n') line++; characters=p-&ch; printf("Words : %d\n", word); printf("lines : %d\n", line); printf("chars : %d\n", characters); getch(); }
Program No : 62 : Write a program in C to copy one array to another array ?

#include <stdio.h> #include <conio.h> void main() { char a[100], b[100], *p, *q;

95
p=a; q=b; printf("enter text\n"); scanf("%c",p); while(*p!='$') { p++; scanf("%c",p); } *p='\0'; p=a; while(*p!='\0') { *q=*p; p++; q++; } *q='\0'; printf("copied Array..\n"); q=b; while(*q!='\0') { printf("%c", *q); q++; } getch();

96
}

Program No : 63 : Write a program in C to copy the first N characters ?

#include <stdio.h> #include <conio.h> void main() { char a[100], b[100], *p, *q; int n,i ; p=a; q=b; printf("enter text\n"); scanf("%c",p); while(*p!='$') { p++; scanf("%c",p); } *p='\0'; printf("Enter how many characters do u want copy ...\n"); scanf("%d", &n); p=a; i=0; while(*p!='\0' && i<n) { *q=*p; p++;

97
q++; i++; } *q='\0'; printf("copied Array..\n"); q=b; while(*q!='\0') { printf("%c", *q); q++; } getch(); }
Program No : 64 : Write a program in C to copy the N characters form M position onwards ?

#include <stdio.h> #include <conio.h> void main() { char a[100], b[100], *p, *q; int m,n,i ; p=a; q=b; printf("enter text\n"); scanf("%c",p); while(*p!='$') {

98
p++; scanf("%c",p); } *p='\0'; printf("starting positino of copy...\n"); scanf("%d", &m); printf("Enter how many characters do u want copy ...\n"); scanf("%d", &n); p=a+m-1; i=0; while(*p!='\0' && i<n) { *q=*p; p++; q++; i++; } *q='\0'; printf("copied Array..\n"); q=b; while(*q!='\0') { printf("%c", *q); q++; } getch(); }
Program No : 65 : Write a program in C to copy the last N characters ?

#include <stdio.h>

99
#include <conio.h> void main() { char a[100], b[100], *p, *q; int n; p=a; q=b; printf("enter text\n"); scanf("%c",p); while(*p!='$') { p++; scanf("%c",p); } *p='\0'; printf("Enter how many characters do u want copy from last ...\n"); scanf("%d", &n); p=p-n; while(*p!='\0') { *q=*p; p++; q++; } *q='\0'; printf("copied Array..\n"); q=b;

100
while(*q!='\0') { printf("%c", *q); q++; } getch(); }

Program No : 66 : Write a program in C to copy upper to lower case ?

#include <stdio.h> #include <conio.h> void main() { char a[100], b[100], *p, *q; p=a; q=b; printf("enter text\n"); scanf("%c",p); while(*p!='$') { p++; scanf("%c",p); } *p='\0';

101
p=a; while(*p!='\0') { if('A'<=*p && 'Z' >= *p) *q=(*p)+32; else *q=*p; p++; q++; } *q='\0'; printf("copied Array..\n"); q=b; while(*q!='\0') { printf("%c", *q); q++; } getch(); }
Program No : 67 : Write a program in C to copy lower to upper case ?

#include <stdio.h> #include <conio.h> void main() { char a[100], b[100], *p, *q; p=a; q=b;

102
printf("enter text\n"); scanf("%c",p); while(*p!='$') { p++; scanf("%c",p); } *p='\0'; p=a; while(*p!='\0') { if('a'<=*p && 'a' >= *p) *q=*p-32; else *q=*p; p++; q++; } *q='\0'; printf("copied Array..\n"); q=b; while(*q!='\0') { printf("%c", *q); q++; } getch();

103
}

Program No : 68 : Write a program in C to replace semicolon instead of comma ?

#include <stdio.h> #include <conio.h> void main() { char a[100], b[100], *p, *q; p=a; q=b; printf("enter text\n"); scanf("%c",p); while(*p!='$') { p++; scanf("%c",p); } *p='\0'; p=a; while(*p!='\0') { if(*p==',') *q=';'; else *q=*p; p++; q++;

104
} *q='\0'; printf("copied Array..\n"); q=b; while(*q!='\0') { printf("%c", *q); q++; } getch(); }
Program No : 69 : Write a program in C to replace by comma instead of semicolon; ?

#include <stdio.h> #include <conio.h> void main() { char a[100], b[100], *p, *q; p=a; q=b; printf("enter text\n"); scanf("%c",p); while(*p!='$') { p++; scanf("%c",p);

105
} *p='\0'; p=a; while(*p!='\0') { if(*p==';') *q=','; else *q=*p; p++; q++; } *q='\0'; printf("copied Array..\n"); q=b; while(*q!='\0') { printf("%c", *q); q++; } getch(); }
Program No : 70 : Write a program in C to delete comma from the given string ?

#include <stdio.h> #include <conio.h> void main() { char a[100], b[100], *p, *q;

106
p=a; q=b; printf("enter text\n"); scanf("%c",p); while(*p!='$') { p++; scanf("%c",p); } *p='\0'; p=a; while(*p!='\0') { if(*p!=',') { *q=*p; q++; } p++; } *q='\0'; printf("copied Array..\n"); q=b; while(*q!='\0') { printf("%c", *q); q++; }

107
getch(); }
Program No : 71 : Write a program in C to copy the reverse of the given string ?

#include <stdio.h> #include <conio.h> void main() { char a[100], b[100], *p, *q; p=a; q=b; printf("enter text\n"); scanf("%c",p); while(*p!='$') { p++; scanf("%c",p); } *p='\0'; p--; while(p>=a) { *q=*p; q++; p--; }

108
*q='\0'; printf("copied Array..\n"); q=b; while(*q!='\0') { printf("%c", *q); q++; } getch(); }
Program No : 72 : Write a program in C to merge to arrays ?

#include <stdio.h> #include <conio.h> void main() { char a[100], b[100], c[200], *p, *q, *mer; p=a; q=b; mer=c; printf("enter text 1\n"); scanf("%c",p); while(*p!='$') { p++; scanf("%c",p);

109
} *p='\0'; fflush(stdin); printf("enter text 2\n"); scanf("%c",q); while(*q!='$') { q++; scanf("%c",q); } *q='\0';

p=a; while(*p) { *mer=*p; mer++; p++; } q=b; while(*q) { *mer=*q; mer++; q++;

110
} *mer='\0'; printf("Merged Array..\n"); mer=c; while(*mer!='\0') { printf("%c", *mer); mer++; } getch(); }
Program No : 73 : Write a program in C to fortran to pascal statement ?

#include <stdio.h> #include <conio.h> void main() { char a[100], b[100], *p, *q; p=a; q=b; printf("enter text\n"); scanf("%c",p); while(*p!='$') { p++; scanf("%c",p);

111
} *p='\0'; p=a; while(*p!='\0') { if(*p=='=') { *q=':'; q++; } else if(*p=='\n') { *q=';'; q++; } *q=*p; p++; q++; } *q='\0'; printf("converted Array..\n"); q=b; while(*q!='\0') { printf("%c", *q); q++; } getch(); }
Program No : 74 : Write a program in C to pascal statement to fortran statement ?

112
#include <stdio.h> #include <conio.h> void main() { char a[100], b[100], *p, *q; p=a; q=b; printf("enter text\n"); scanf("%c",p); while(*p!='$') { p++; scanf("%c",p); } *p='\0'; p=a; while(*p!='\0') { if(*p!=':'&& *p!=';' ) { *q=*p; q++; } p++;

} *q='\0'; printf("converted Array..\n");

113
q=b; while(*q!='\0') { printf("%c", *q); q++; } getch(); }
Program No : 75 : Write a program in C to count the lines in the text ?

#include <stdio.h> #include <conio.h> void main() { char a[100], *p; int lines=0; p=a; printf("enter text\n"); scanf("%c",p); while(*p!='$') { p++; scanf("%c",p); } *p='\0';

114
p=a; while(*p!='\0') { if(*p=='\n') lines++; p++; } if(*(p-1)!='\n') lines++; printf("Lines getch(); }
Program No : 76 : Write a program in C to print the first position of every line in the given text ?

: %d\n", lines);

#include <stdio.h> #include <conio.h> void main() { char a[100], *p; p=a; printf("enter text\n"); scanf("%c",p); while(*p!='$') { p++; scanf("%c",p); }

115
*p='\0'; p=a; printf("Position of new line %d \n", 0); while(*p!='\0') { if(*p=='\n') { printf("Position of new line %d \n", p-a+1); } p++; } getch(); }
Program No : 77 : Write a program in C to store the first position of every line in the given text in array ?

#include <stdio.h> #include <conio.h> void main() { char a[100], *p; int pos[20], *pp; pp=&pos; p=a; printf("enter text\n"); scanf("%c",p); while(*p!='$') { p++;

116
scanf("%c",p); } *p='\0'; p=a; *pp=0; *pp++; while(*p!='\0') { if(*p=='\n') { *pp=p-a+1; pp++; } p++; } *pp=-1; pp=&pos; printf("position of new lines : "); while(*pp!=-1) { printf("%d ", *pp); pp++; } getch(); }
Program No : 78 : Write a program in C to print the M th line ?

117
#include <stdio.h> #include <conio.h> void main() { char a[100], *p; int pos[20], *pp, m; pp=&pos; p=a; printf("enter text\n"); scanf("%c",p); while(*p!='$') { p++; scanf("%c",p); } *p='\0'; printf("Enter no which line u want print ...\n"); scanf("%d", &m); p=a; *pp=0; *pp++; while(*p!='\0') { if(*p=='\n') { *pp=p-a+1; pp++;

118
} p++; } *pp=-1; pp=&pos; p=a; printf("%d th line of given text : ",m); p=p+*(pp+m-1); while(*p!='\n') { printf("%c", *p); p++; } getch(); }

Program No : 79 : Write a program in C to print the M th to N th line ?

#include <stdio.h> #include <conio.h> void main() { char a[100], *p; int pos[20], *pp, m,n; pp=&pos; p=a;

119
printf("enter text\n"); scanf("%c",p); while(*p!='$') { p++; scanf("%c",p); } *p='\0'; printf("Enter which range of lines u want print ...\n"); scanf("%d%d", &m,&n); p=a; *pp=0; *pp++; while(*p!='\0' ) { if(*p=='\n') { *pp=p-a+1; pp++; } p++; } *pp=-1; pp=&pos; p=a; printf("range given text \n"); p=p+*(pp+m-1);

120
while(*p!='\0' && p <= a+ *(pp+n-1) ) { printf("%c", *p); p++; } getch(); }
Program No : 80 : Write a program in C to find out given pattern exist or not ?

#include <stdio.h> #include <conio.h> void main() { char a[100], pat[50], *p, *q, *r; int count=0; p=a; q=pat; printf("enter text\n"); scanf("%c",p); while(*p!='$') { p++; scanf("%c",p); } *p='\0';

121
fflush(stdin); printf("enter pattern to check exist or not\n"); scanf("%c",q); while(*q!='$') { q++; scanf("%c",q); } *q='\0'; p=a; while (*p) { r=p; q=pat; while(*r==*q && *q!='\0') { r++; q++; } if(*q=='\0') { printf("pattern exist"); getch(); return 0; } p++; } printf("pattern does not exist"); getch(); }

122
Program No : 81 : Write a program in C to count the no of times occure in given pattern ?

#include <stdio.h> #include <conio.h> void main() { char a[100], pat[50], *p, *q, *r; int count=0; p=a; q=pat; printf("enter text\n"); scanf("%c",p); while(*p!='$') { p++; scanf("%c",p); } *p='\0'; fflush(stdin); printf("enter pattern to check exist or not\n"); scanf("%c",q); while(*q!='$') { q++; scanf("%c",q);

123
} *q='\0'; p=a; while (*p) { r=p; q=pat; while(*r==*q && *q!='\0') { r++; q++; } if(*q=='\0') { count++; } p++; } printf("pattern %d times exist", count); getch(); }
Program No : 82 : Write a program in C to delete the given pattern ?

#include <stdio.h> #include <conio.h> void main() { char a[100], pat[50], b[100], *p, *q, *r,*s; p=a; q=pat; s=b;

124
printf("enter text\n"); scanf("%c",p); while(*p!='$') { p++; scanf("%c",p); } *p='\0'; fflush(stdin); printf("enter pattern to check exist or not\n"); scanf("%c",q); while(*q!='$') { q++; scanf("%c",q); } *q='\0'; p=a; while (*p) { r=p; q=pat; while(*r==*q && *q!='\0') { r++; q++; }

125
if(*q=='\0') { p=r; } else { *s=*p; s++; } p++; } *s='\0'; printf("Modified text\n"); s=b; while(*s!='\0') { printf("%c", *s); s++; } getch(); }
Program No : 83 : Write a program in C to replace the given pattern ?

#include <stdio.h> #include <conio.h> void main() { char a[100], pat[50], b[100], c[50], *p, *q, *r,*s, *t; p=a;

126
q=pat; s=b; t=c; printf("enter text\n"); scanf("%c",p); while(*p!='$') { p++; scanf("%c",p); } *p='\0'; fflush(stdin); printf("enter pattern to delete\n"); scanf("%c",q); while(*q!='$') { q++; scanf("%c",q); } *q='\0'; fflush(stdin); printf("enter pattern by replace\n"); scanf("%c",t);

127
while(*t!='$') { t++; scanf("%c",t); } *t='\0'; p=a; while (*p) { r=p; q=pat; while(*r==*q && *q!='\0') { r++; q++; } if(*q=='\0') { p=r; t=c; while(*t!='\0') { *s=*t; s++; t++; } } else { *s=*p; s++; p++; } } *s='\0';

128
printf("Modified text\n"); s=b; while(*s!='\0') { printf("%c", *s); s++; } getch(); }

Using for loop condition


Program No : 1 : Write a program in C to print the numbers 1 to 100 ? #include <stdio.h> #include <conio.h> void main() { int loop, *p; p=&loop; printf("the numbers between 1 to 100\n"); for(*p=1; *p<=100; (*p)++) printf("%d ",*p); getch(); }

129
Program No : 2 : Write a program in C to sum of first 100 numbers ? #include <stdio.h> #include <conio.h> void main() { int loop, *p; int sum=0, *ps; p=&loop; ps= printf("the numbers between 1 to 100\n"); for(*p=1; *p<=100; (*p)++) { *ps=*ps+*p; } printf(" sum of first 100 numbers : %d", sum); getch(); } Program No : 3 : Write a program in C to sum of first N numbers ?

#include <stdio.h> #include <conio.h> void main() { int loop, *p, n; int sum=0, *ps; p=&loop;

130
ps= printf("Enter N value : \n"); scanf("%d", &n); for(*p=1; *p<=n; (*p)++) { *ps=*ps+*p; } printf("Sum of first %d numbers : %d",n, sum); getch(); } Program No : 4 : Write a program in C to sum of M to N numbers ? #include <stdio.h> #include <conio.h> void main() { int loop, *p, m,n; int sum=0, *ps; p=&loop; ps= printf("Enter M and N value \n"); scanf("%d%d", &m,&n); for(*p=m; *p<=n; (*p)++) { *ps+=*p; } printf("Sum of %d to %d numbers : %d",m,n, sum); getch();

131
} Program No : 5 : Write a program in C to find the average of 1 to 100 ? #include <stdio.h> #include <conio.h> void main() { int loop, *p ; float sum=0, *ps, average; p=&loop; ps= for(*p=1; *p<=100; (*p)++) { *ps+=*p; } average = *ps/100; printf("Avergae of first 100 Numbers : %f",average); getch(); } Program No : 6 : Write a program in C to sum of first N numbers ?

#include <stdio.h> #include <conio.h> void main() { int loop, *p, n ;

132
float sum=0, *ps, average; p=&loop; ps= printf("Enter n value \n"); scanf("%d", &n); for(*p=1; *p<=n; (*p)++) { *ps+=*p; } average = *ps/n; printf("Avergae of first %d Numbers : %f", n, average); getch(); }

Program No : 7 : Write a program in C to average of M to N numbers ?

#include <stdio.h> #include <conio.h> void main() { int loop, *p, m, n ; float sum=0, *ps, average; p=&loop; ps= printf("enter the starting range \n"); scanf("%d",&m);

133
printf("enter the ending range \n"); scanf("%d",&n); for(*p=m; *p<=n; (*p)++) { *ps+=*p; } average = *ps/(n-m+1); printf("Avergae of %d to %d Numbers : %f", m, n, average); getch(); } Program No : 8 : Write a program in C to display the even or odd numbers between 1 to 100 ? #include <stdio.h> #include <conio.h> void main() { int loop, *p; p=&loop; printf("the even or odd numbers between 1 to 100\n"); for(*p=1; *p<=100;(*p)++) { if(*p%2==0) printf("%d even\n",*p); else printf("%d odd\n",*p); } getch(); } Program No : 9 : Write a program in C to display the even or odd numbers of first N numbers ?

134
#include <stdio.h> #include <conio.h> void main() { int loop,n, *p; p=&loop; printf("enter n value \n"); scanf("%d",&n); printf("the even or odd numbers between 1 to %d \n", n); for(*p=1; *p<=n;(*p)++) { if(*p%2==0) printf("%d even\n",*p); else printf("%d odd\n",*p); } getch(); } Program No : 10 : Write a program in C to display the even or odd numbers between to ranges ? #include <stdio.h> #include <conio.h> void main() { int loop,m, n, *p; p=&loop; printf("enter starting value\n"); scanf("%d",&m); printf("enter ending value\n"); scanf("%d",&n); printf("the even or odd numbers between %d to %d \n", m, n); for(*p=m; *p<=n;(*p)++) { if(*p%2==0)

135
printf("%d even\n",*p); else printf("%d odd\n",*p); } getch(); } Program No : 11 : Write a program in C to display the leap years between 1000 to 2000 ?

#include <stdio.h> #include <conio.h> void main() { int loop, *p; p=&loop; printf("the leap year or not between 1000 to 2000\n"); for(*p=1000;*p<=2000;(*p)++) { if(*p%4==0) printf("%d leap year\n",*p); else printf("%d not\n",*p); } getch(); } Program No : 12 : Write a program in C to display the leap years of given range ?

#include <stdio.h> #include <conio.h> void main() { int loop, m,n, *p;

136
p=&loop; printf("enter the range\n"); scanf("%d%d",&m,&n); for(*p=m;*p<=n;(*p)++) { if(*p%4==0) printf("%d leap year\n",*p); else printf("%d not\n",*p); } getch(); }

Program No : 13 : Write a program in C to print the numbers between two ranges ?


#include <stdio.h>

#include <conio.h> void main() { int m,n; int *p; p=&m; printf("enter the ranges do you want diaplay\n"); scanf("%d%d",&m,&n); printf("the numbers between %d to %d \n",m,n); for(;*p<=n;(*p)++) printf("%d ",*p); getch(); }

137
Program No : 14 : Write a program in C to print the number until -1000 is given a input ? #include<stdio.h> void main() { int number, *p; p=&number; scanf("%d",p); for( ; *p!=-1000 ; ) { printf("%d ",*p); scanf("%d",p); } } Program No : 15 : Write a program in C to print the positive , negative and zero until -1000 is given a input ?

#include<stdio.h> void main() { int number,*p; p=&number; for(scanf("%d",p); *p!=-1000 ;scanf("%d",p) ) { if(*p>0) printf("%d pos",*p); else if(number<0) printf("%d neg",*p); else printf("%d zero",*p); } }

138
Program No : 16 : Write a program in C to count the positive , negative numbers and zero until -1000 is given a input ?

#include<stdio.h> void main() { int number,*p; int pos, neg, zero; int *pp, *pn, *pz; pp=&pos; pn=&neg; pz=&zero; p=&number; *pp=0; *pz=0; *pn=0; for(scanf("%d",p); *p!=-1000 ;scanf("%d",p) ) { if(*p>0) (*pp)++; else if(*p<0) (*pn)++; else (*pz)++; } printf("%d pos\n",*pp); printf("%d neg\n",*pn); printf("%d zero\n",*pz); getch(); } Program No : 17 : Write a program in C to sum of the positive , negative numbers until -1000 is given a input ?
#include<stdio.h>

#include<conio.h> void main() { int number,possum=0,negsum=0; int *p, *pp, *pn; p=&number; pp=&possum; pn=&negsum;

139
for(scanf("%d",p); *p!=-1000 ;scanf("%d",p) ) { if(*p>0) *pp+=*p; else if(*p<0) *pn+=*p; } printf("%d pos\n",*pp); printf("%d neg\n",*pn); getch(); } Program No : 18 : Write a program in C to mean of the positive , negative numbers until -1000 is given a input ?

#include<stdio.h> #include<conio.h> void main() { int number, *p, pos=0,neg=0; float *pp, *pn, possum,negsum; p=&number; pp=&possum; pn=&negsum; *pp=0.0; *pn=0.0; for(scanf("%d",p); *p!=-1000 ;scanf("%d",p) ) { if(*p>0) { *pp+=*p; pos++; } else if(*p<0) { neg++; *pn+=*p; }

140
} printf("%f pos\n",*pp/pos); printf("%f neg\n",*pn/neg); getch(); } Program No : 19 : Write a program in C to factorial of given number until -1000 is given a input ? #include <stdio.h> #include <conio.h> void main() { int number,fact, i, *p, *f; p=&number; f=&fact; for(scanf("%d",p); *p!=-1000 ;scanf("%d",p) ) { if(*p==0) { *f=1; } else { for(i=1,*f=1;i<=*p;i++) *f*=i; } printf("fact %d\n",*f); } } Program No : 20 : Write a program in C to find biggest of ten numbers ?
#include <stdio.h>

#include <conio.h> void main() { int number,big,i, *p, *bp; p=&number; bp=&big; *bp=-32768;

141
for(i=1; i<=10;i++) { scanf("%d",p); *bp=*p>*bp?*p:*bp; } printf("big %d",*bp); getch(); } Program No : 21 : Write a program in C to find smallest of ten numbers ?

#include <stdio.h> #include <conio.h> void main() { int number,small,i, *p, *bs; p=&number; bs=&small; *bs=32767; for(i=1; i<=10;i++) { scanf("%d",p); *bs=*p<*bs?*p:*bs; } printf("small %d",*bs); getch(); } Program No : 22 : Write a program in C to find biggest of N numbers ?

#include <stdio.h> #include <conio.h>

142
void main() { int number,big,i,n, *p, *bp; p=&number; bp=&big; *bp=-32768; printf("how many numbers...\n"); scanf("%d", &n); for(i=1; i<=n;i++) { scanf("%d",p); *bp=*p>*bp?*p:*bp; } printf("big %d",*bp); getch(); } Program No : 23 : Write a program in C to find smallest of N numbers ? #include <stdio.h> #include <conio.h> void main() { int number,small,i,n, *p, *bs; p=&number; bs=&small; *bs=32767; printf("how many numbers...\n"); scanf("%d", &n); for(i=1; i<=n;i++) { scanf("%d",p); *bs=*p<*bs?*p:*bs; } printf("small %d",*bs); getch(); }

143
Program No : 24 : Write a program in C to read and display 10 numbers using array ?

#include<stdio.h> #include<conio.h> void main() { int arr[10], *p; for(p=arr; p<arr+10; p++) scanf("%d",p); for(p=arr; p<arr+10; p++) printf(" %d", *p); getch(); } /* program no : P157 */ /* purpose : read and display of ten numbers using Array*/ /* date of written : 08/09/2006 */ #include<stdio.h> #include<conio.h> void main() { int arr[10], *p; for(p=arr; p<arr+10; p++) scanf("%d",p); for(p=arr; p<arr+10; p++) printf(" %d", *p); getch(); } Program No : 25 : Write a program in C to read and display of N numbers using array ?

#include<stdio.h>

144
#include<conio.h> void main() { int arr[10],n, *p; printf("Enter N Value..\n"); scanf("%d", &n); for(p=arr; p<arr+n; p++) scanf("%d",p); for(p=arr; p<arr+n; p++) printf(" %d", *p); getch(); } Program No : 26 : Write a program in C to find mean of ten numbers using array ?

#include<stdio.h> #include<conio.h> void main() { int arr[10], *p; float mean, *m; m=&mean; *m=0.0; for(p=arr; p<arr+10; p++) scanf("%d",p); for(p=arr; p<arr+10; p++) *m+=*p; printf("Mean of 10 numbers : %f", *m/10); getch(); }

145
Program No : 27 : Write a program in C to find mean of N numbers using array ? #include<stdio.h> #include<conio.h> void main() { int arr[10], n, *p; float mean, *m; m=&mean; printf("how many numbers? "); scanf("%d",&n); *m=0.0; for(p=arr; p<arr+n; p++) scanf("%d",p); for(p=arr; p<arr+n; p++) *m+=*p; printf("Mean of %d numbers : %f",n, *m/n); getch(); } Program No : 28 : Write a program in C to find the biggest of 10 numbers using array ?

#include<stdio.h> #include<conio.h> void main() { int arr[10], *p; int big, *bp; bp=&big;

146
*bp=-32678; for(p=arr; p<arr+10; p++) scanf("%d",p); for(p=arr; p<arr+10; p++) *bp=*p>*bp?*p:*bp; printf("Big : %d",*bp); getch(); } Program No : 29 : Write a program in C to find the smallest of 10 numbers using array ?

#include<stdio.h> #include<conio.h> void main() { int arr[10], *p; int small, *bs; bs=&small; *bs=32677; for(p=arr; p<arr+10; p++) scanf("%d",p); for(p=arr; p<arr+10; p++) *bs=*p<*bs?*p:*bs; printf("Small : %d",*bs); getch(); } Program No : 30 : Write a program in C to find the position of biggest number in the array ?

#include<stdio.h> #include<conio.h>

147
void main() { int arr[10], i, *p; int pos=0; for(p=arr,i=0; i<10; i++) scanf("%d",(p+i)); for(i=0; i<10; i++) pos=*(p+i)>(*(p+pos))? i: pos; printf("Big Pos : %d",pos); getch(); } Program No : 31 : Write a program in C to find the position of smallest number in the array ?

#include<stdio.h> #include<conio.h> void main() { int arr[10], i, *p; int pos=0; for(p=arr,i=0; i<10; i++) scanf("%d",(p+i)); for(i=0; i<10; i++) pos=*(p+i)< (*(p+pos))? i: pos; printf("Small Pos : %d",pos); getch(); } Program No : 32 : Write a program in C to find the positions of biggest&& smallest number in the array ?

148
#include<stdio.h> #include<conio.h> void main() { int arr[10], i, *p; int bpos=0, spos=0; int pos=0; for(p=arr,i=0; i<10; i++) scanf("%d",(p+i)); for(i=0; i<10; i++) { spos=*(p+i)< (*(p+spos))? i: spos; bpos=*(p+i)>(*(p+bpos))? i: bpos; } printf("Small Pos : %d",spos); printf("Big Pos : %d",bpos); getch(); } Program No : 33 : Write a program in C to interchange the biggest&& smallest number in the array ?

#include<stdio.h> #include<conio.h> void main() { int arr[10], i, *p; int bpos=0, spos=0,temp; int pos=0; for(p=arr,i=0; i<10; i++) scanf("%d",(p+i)); for(i=0; i<10; i++) { spos=*(p+i)< (*(p+spos))? i: spos; bpos=*(p+i)>(*(p+bpos))? i: bpos;

149
} temp=*(p+spos); *(p+spos)=*(p+bpos); *(p+bpos)=temp; for(p=arr,i=0; i<10; i++) printf(" %d",*(p+i)); getch(); } Program No : 34 : Write a program in C to find the second biggest of N numbers using array ?
#include<stdio.h>

#include<conio.h> void main() { int arr[10], n, *p; int i, big=-32768,sbig=-32767; p=arr; printf("enter n value..\n"); scanf("%d", &n); for(i=0;i<n;i++) scanf("%d",(p+i)); for(i=0;i<n;i++) big=big<*(p+i)?*(p+i):big; for(i=0;i<n;i++) { if(big!=*(p+i)) sbig=sbig<*(p+i)?*(p+i):sbig; } printf("\nsecond big %d",sbig); getch(); }

150
Program No : 35 : Write a program in C to find the second smallest of N numbers using array ?

#include<stdio.h> #include<conio.h> void main() { int arr[10], n, *p; int i, small=32767,ssmall=32766; p=arr; printf("enter n value..\n"); scanf("%d", &n); for(i=0;i<n;i++) scanf("%d",(p+i)); for(i=0;i<n;i++) small=small>*(p+i)?*(p+i):small; for(i=0;i<n;i++) { if(small!=*(p+i)) ssmall=ssmall>*(p+i)?*(p+i):ssmall; } printf("\nsecond small %d",ssmall); getch(); } Program No : 36 : Write a program in C to find the second biggest and smallest of N numbers using array ?

#include<stdio.h> #include<conio.h> void main() {

151
int arr[10], n, *p; int i, small=32767,ssmall=32766 ; int big=-32768,sbig=-32767; p=arr; printf("enter n value..\n"); scanf("%d", &n); for(i=0;i<n;i++) scanf("%d",(p+i)); for(i=0;i<n;i++) { big=big<*(p+i)?*(p+i):big; small=small>*(p+i)?*(p+i):small; } for(i=0;i<n;i++) { if(big!=*(p+i)) sbig=sbig<*(p+i)?*(p+i):sbig; if(small!=*(p+i)) ssmall=ssmall>*(p+i)?*(p+i):ssmall; } printf("\nsecond big %d\n",sbig); printf("\nsecond small %d",ssmall); getch(); } Program No : 37 : Write a program in C to find the positions of second biggest and smallest of N numbers using array ?

#include<stdio.h> #include<conio.h> void main() { int arr[10], n, *p; int i, bpos=0, sbpos=0, spos=0,sspos=0;

152
p=arr; printf("enter n value..\n"); scanf("%d", &n); for(i=0;i<n;i++) scanf("%d",(p+i)); for(i=0;i<n;i++) { bpos=*(p+bpos)<*(p+i)?i:bpos; spos=*(p+spos)>*(p+i)?i:spos; } for(i=0;i<n;i++) { if(*(p+bpos)!=*(p+i)) sbpos=*(p+sbpos)<*(p+i)?i:sbpos; if(*(p+spos)!=*(p+i)) sspos=*(p+sspos)>*(p+i)?i:sspos; } printf("\nsecond big pos %d\n",sbpos); printf("\nsecond small pos %d",sspos); getch(); } Program No : 38 : Write a program in C to interchange the second biggest and smallest of N numbers using array ?
#include<stdio.h>

#include<conio.h> void main() { int arr[10], n, *p; int i,temp, bpos=0, sbpos=0, spos=0,sspos=0; p=arr; printf("enter n value..\n"); scanf("%d", &n);

153
for(i=0;i<n;i++) scanf("%d",(p+i)); for(i=0;i<n;i++) { bpos=*(p+bpos)<*(p+i)?i:bpos; spos=*(p+spos)>*(p+i)?i:spos; } for(i=0;i<n;i++) { if(*(p+bpos)!=*(p+i)) sbpos=*(p+sbpos)<*(p+i)?i:sbpos; if(*(p+spos)!=*(p+i)) sspos=*(p+sspos)>*(p+i)?i:sspos; } temp=*(p+sspos); *(p+sspos)=*(p+sbpos); *(p+sbpos)=temp; printf("Interchanged Array..\n"); for(i=0;i<n;i++) printf("%d ",*(p+i)); getch(); } Program No : 39 : Write a program in C to read and write 3x3 matrix ?
#include <conio.h>

#include<stdio.h> void main() { int mat[3][3],*p; int i,j; p=mat; for(i=0; i<3;i++)

154
{ for(j=0;j<3;j++) scanf("%d", p+i*3+j); } for(i=0; i<3;i++) { for(j=0;j<3;j++) printf("%d ", *(p+i*3+j)); printf("\n"); } getch(); } Program No : 40 : Write a program in C to read and display the transpose of 3x3 matrix ?
#include <conio.h>

#include<stdio.h> void main() { int mat[3][3],*p, trans[3][3], *t; int i,j; p=mat; t=trans; for(i=0; i<3;i++) { for(j=0;j<3;j++) scanf("%d", p+i*3+j); } for(i=0; i<3;i++) { for(j=0;j<3;j++) *(t+j*3+i)=*(p+i*3+j); } printf("transpose Matrix\n");

155
for(i=0; i<3;i++) { for(j=0;j<3;j++) printf("%d ", *(t+i*3+j)); printf("\n"); } getch(); } Program No : 41 : Write a program in C to find the sum of matrix ?

#include <conio.h> #include<stdio.h> void main() { int mat1[3][3],*p1, mat2[3][3], *p2, sum[3][3], *s; int i,j; p1=mat1; p2=mat2; s=sum; printf("enter first matrix\n"); for(i=0; i<3;i++) { for(j=0;j<3;j++) scanf("%d", p1+i*3+j); } printf("enter second matrix\n"); for(i=0; i<3;i++) { for(j=0;j<3;j++) scanf("%d", p2+i*3+j); } for(i=0; i<3;i++) { for(j=0;j<3;j++)

156
*(s+i*3+j)=*(p1+i*3+j) + *(p2+i*3+j); } printf("sum Matrix\n"); for(i=0; i<3;i++) { for(j=0;j<3;j++) printf("%d ", *(s+i*3+j)); printf("\n"); } getch(); } Program No : 42 : Write a program in C to find the product of two matrixes ?
#include <conio.h>

#include<stdio.h> void main() { int mat1[3][3],*p1, mat2[3][3], *p2, pro[3][3], *s; int i,j,k; p1=mat1; p2=mat2; s=pro; printf("enter first matrix\n"); for(i=0; i<3;i++) { for(j=0;j<3;j++) scanf("%d", p1+i*3+j); } printf("enter second matrix\n"); for(i=0; i<3;i++) { for(j=0;j<3;j++) scanf("%d", p2+i*3+j); }

157
for(i=0; i<3;i++) { for(j=0;j<3;j++) { *(s+i*3+j)=0; for(k=0;k<3;k++) *(s+i*3+j) += *(p1+i*3+k) * *(p2+k*3+j); } } printf("Pro Matrix\n"); for(i=0; i<3;i++) { for(j=0;j<3;j++) printf("%d ", *(s+i*3+j)); printf("\n"); } getch(); } Program No : 43 : Write a program in C to read and display MxN matrix ?
#include <conio.h>

#include<stdio.h> void main() { int mat[10][10],*p; int i,j, m,n; p=mat; printf("how many rows and coloumns?\n"); scanf("%d%d",&m,&n); for(i=0; i<m;i++) { for(j=0;j<n;j++)

158
scanf("%d", p+i*10+j); } printf("given matrix\n"); for(i=0; i<m;i++) { for(j=0;j<n;j++) printf("%d ", *(p+i*10+j)); printf("\n"); } getch(); } Program No : 44 : Write a program in C to read and display the transpose of MxN matrix ? #include <conio.h> #include<stdio.h> void main() { int mat[10][10],*p , trans[10][10], *t; int i,j, m,n; p=mat; t=trans; printf("how many rows and coloumns?\n"); scanf("%d%d",&m,&n); for(i=0; i<m;i++) { for(j=0;j<n;j++) scanf("%d", p+i*10+j); } for(i=0; i<m;i++)

159
{ for(j=0;j<n;j++) *(t+j*10+i)=*(p+i*10+j); printf("\n"); } printf("Transpose matrix\n"); for(i=0; i<n;i++) { for(j=0;j<m;j++) printf("%d ", *(t+i*10+j)); printf("\n"); } getch(); } Program No : 45 : Write a program in C to sum of two MxN matrix ?

#include <conio.h> #include<stdio.h> void main() { int mat1[10][10],*p1, mat2[10][10], *p2, sum[10][10], *s; int i,j, m,n; p1=mat1; p2=mat2; s=sum; printf("how many rows and coloumns?\n"); scanf("%d%d",&m,&n); printf("enter matrix 1\n"); for(i=0; i<m;i++) {

160
for(j=0;j<n;j++) scanf("%d", p1+i*10+j); } printf("enter matrix 2\n"); for(i=0; i<m;i++) { for(j=0;j<n;j++) scanf("%d", p2+i*10+j); } for(i=0; i<m;i++) { for(j=0;j<n;j++) *(s+i*10+j)=*(p1+i*10+j) + *(p2+i*10+j); printf("\n"); } printf("Sum matrix\n"); for(i=0; i<m;i++) { for(j=0;j<n;j++) printf("%d ", *(s+i*10+j)); printf("\n"); } getch(); } Program No : 46 : Write a program in C to poduct of two MxN matrix ?

#include <conio.h> #include<stdio.h> void main() { int mat1[10][10],*p1, mat2[10][10], *p2, pro[10][10], *s;

161
int i,j,k, m1,n1,m2,n2; p1=mat1; p2=mat2; s=pro; printf("how many row and col in first?\n"); scanf("%d%d",&m1,&n1); printf("how many row and col in second?\n"); scanf("%d%d",&m2,&n2); if(n1!=m2) { printf("enter the right order!"); getch(); return; } printf("enter matrix 1\n"); for(i=0; i<m1;i++) { for(j=0;j<n1;j++) scanf("%d", p1+i*10+j); } printf("enter matrix 2\n"); for(i=0; i<m2;i++) { for(j=0;j<n2;j++) scanf("%d", p2+i*10+j); } for(i=0; i<m1;i++) { for(j=0;j<n2;j++) { *(s+i*10+j)=0; for(k=0;k<n1; k++) *(s+i*10+j)=*(p1+i*10+k) + *(p2+k*10+j); printf("\n");

162
} printf("pro matrix\n"); for(i=0; i<m1;i++) { for(j=0;j<n2;j++) printf("%d ", *(s+i*10+j)); printf("\n"); } getch(); } Program No : 47 : Write a program in C to read the characters and display until $ is given ?

#include<stdio.h> #include<conio.h> void main() { char ch,*p; p=&ch; for(scanf("%c",p);*p!='$'; scanf("%c",p)) printf("%c",*p); } Program No : 48 : Write a program in C to read the characters and display ascii value until $ is given ? #include<stdio.h> #include<conio.h> void main() { char ch, *p; p=&ch; for(scanf("%c",p);*p!='$'; scanf("%c",p)) { fflush(stdin); printf("%d",*p); }

163
} Program No : 49 : Write a program in C to classify the character is upper or lower case until $ is given ?

#include<stdio.h> #include<conio.h> void main() { char ch, *p; p=&ch; for(scanf("%c",p);*p!='$'; scanf("%c",p)) { if('A'<=*p && *p<='Z') printf("upper\n"); else if('a'<=*p && *p<='z') printf("lower\n"); else if('0'<=*p && *p<='9') printf("number\n"); else printf("special\n"); fflush(stdin); } } Program No : 50 : Write a program in C to count the upper , lower , number and special characters until $ is given ?

#include<stdio.h> #include<conio.h> void main() { char ch, *p; int upp=0,low=0,num=0,spe=0; p=&ch;

164
for(scanf("%c",p);*p!='$'; scanf("%c",p)) { if('A'<=*p && *p<='Z') upp++; else if('a'<=*p && *p<='z') low++; else if('0'<=*p && *p<='9') num++; else spe++; fflush(stdin); } printf("upp %d\n ",upp); printf("low %d\n ",low); printf("num %d\n ",num); printf("spe %d\n ",spe); getch(); } Program No : 51 : Write a program in C to read and print a word ? #include<stdio.h> #include<conio.h> void main() { char ch[100], *p; printf("enter the word\n"); for(p=ch,scanf("%c",p);*p!=' '; p++,scanf("%c",p)); *p='\0'; printf("given word\n"); for(p=ch; *p; p++) printf("%c", *p);

165
getch(); } Program No : 52 : Write a program in C to read and print a sentence until . is given ? #include<stdio.h> #include<conio.h> void main() { char ch[100], *p; for(p=ch,scanf("%c",p);*p!='.'; p++,scanf("%c",p)); *p='\0'; printf("given word\n"); for(p=ch; *p; p++) printf("%c", *p); getch(); } Program No : 53 : Write a program in C to read a line until ?\n? is given ?

#include<stdio.h> #include<conio.h> void main() { char ch[100], *p; for(p=ch,scanf("%c",p);*p!='\n'; p++,scanf("%c",p)); *p='\0';

166
printf("given word\n"); for(p=ch; *p; p++) printf("%c", *p); getch(); } Program No : 54 : Write a program in C to read a text until ?$? is given ? #include<stdio.h> #include<conio.h> void main() { char ch[100], *p; for(p=ch,scanf("%c",p);*p!='$'; p++,scanf("%c",p)); *p='\0'; printf("given word\n"); for(p=ch; *p; p++) printf("%c", *p); getch(); } Program No : 55 : Write a program in C to count the number of characters in the input text ?

#include<stdio.h> #include<conio.h> void main() { char ch[100], *p;

167
for(p=ch,scanf("%c",p); *p!='$'; p++,scanf("%c",p)); *p='\0'; printf("No of characters : %d", p-&ch); getch(); } Program No : 56 : Write a program in C to count the number of words in the input text ?

#include<stdio.h> #include<conio.h> void main() { char ch[100], *p; int words=0; for(p=ch,scanf("%c",p); *p!='$'; p++,scanf("%c",p)); *p='\0'; for(p=ch; *p; p++) { if((*p!=' '&&*(p+1)==' ') ||(*p!='\t'&&*(p+1)=='\t') ||(*p!='\n' &&*(p+1)=='\n')) words++; }

printf("No of words : %d", words); getch(); } Program No : 57 : Write a program in C to count the number of characters ,words and lines ?

168
#include<stdio.h> #include<conio.h> void main() { char ch[100], *p; int words=0, lines=0, chars=0; for(p=ch,scanf("%c",p); *p!='$'; p++,scanf("%c",p)); *p='\0'; for(p=ch; *p; p++) { if((*p!=' '&&*(p+1)==' ') ||(*p!='\t'&&*(p+1)=='\t') ||(*p!='\n' &&*(p+1)=='\n')) words++; if(*p=='\n') lines++; } if(*(p-1)!='\n') lines++; if(*(p-1)!='\n'&&*(p-1)!=' '&&*(p-1)!='\t') words++; chars=p-ch; printf("No of words : %d", words); printf("No of lines : %d", lines); printf("No of chars : %d", chars); getch(); } Program No : 58 : Write a program in C to copy one array to another array ?
#include<stdio.h>

#include<conio.h> void main() {

169
char ch[100], *p , res[100], *t; for(p=ch,scanf("%c",p); *p!='$'; p++,scanf("%c",p)); *p='\0'; for(t=res,p=ch; *p; p++,t++) *t=*p; *t='\0'; printf("Copied Array...\n"); for(t=res; *t; t++) printf("%c", *t); getch(); } Program No : 59 : Write a program in C to copy the first N characters ?
#include<stdio.h>

#include<conio.h> void main() { char ch[100], *p , res[100], *t; int n; printf("Enter ur text...\n"); for(p=ch,scanf("%c",p); *p!='$'; p++,scanf("%c",p)); *p='\0'; printf("Enter N value...\n"); scanf("%d",&n); for(t=res,p=ch; *p && p-ch<=n ; p++,t++) *t=*p; *t='\0'; printf("Copied Array...\n"); for(t=res; *t; t++) printf("%c", *t); getch();

170
} Program No : 60 : Write a program in C to copy the N characters form M position onwards ?
#include<stdio.h>

#include<conio.h> void main() { char ch[100], *p , res[100], *t; int m,n; printf("Enter ur text...\n"); for(p=ch,scanf("%c",p); *p!='$'; p++,scanf("%c",p)); *p='\0'; printf("Enter Postion to be start copy..\n"); scanf("%d",&m); printf("Enter N value...\n"); scanf("%d",&n); for(t=res,p=ch+m-1; *p && p-ch<=m+n-1 ; p++,t++) *t=*p; *t='\0'; printf("Copied Array...\n"); for(t=res; *t; t++) printf("%c", *t); getch(); } Program No : 61 : Write a program in C to copy the last N characters ?

#include<stdio.h> #include<conio.h> void main()

171
{ char ch[100], *p , res[100], *t; int n; printf("Enter ur text...\n"); for(p=ch,scanf("%c",p); *p!='$'; p++,scanf("%c",p)); *p='\0'; printf("Enter N value...\n"); scanf("%d",&n); for(t=res,p=p-n; *p ; p++,t++) *t=*p; *t='\0'; printf("Copied Array...\n"); for(t=res; *t; t++) printf("%c", *t); getch(); } Program No : 62 : Write a program in C to copy upper to lower case ?

#include<stdio.h> #include<conio.h> void main() { char ch[100], *p , res[100], *t; printf("Enter ur text...\n"); for(p=ch,scanf("%c",p); *p!='$'; p++,scanf("%c",p)); *p='\0';

172
for(t=res,p=ch; *p ; p++,t++) { if(*p>='A' && *p<='Z') *t=*p+32; else *t=*p; } *t='\0'; printf("Converted Array...\n"); for(t=res; *t; t++) printf("%c", *t); getch(); } Program No : 63 : Write a program in C to copy lower to upper case ?

#include<stdio.h> #include<conio.h> void main() { char ch[100], *p , res[100], *t; printf("Enter ur text...\n"); for(p=ch,scanf("%c",p); *p!='$'; p++,scanf("%c",p)); *p='\0';

for(t=res,p=ch; *p ; p++,t++) { if(*p>='a' && *p<='z') *t=*p-32; else *t=*p; } *t='\0'; printf("Converted Array...\n");

173
for(t=res; *t; t++) printf("%c", *t); getch(); } Program No : 64 : Write a program in C to replace by comma instead of semicolon; /* program in C no : 197 */ ?
#include<stdio.h>

#include<conio.h> void main() { char ch[100], *p , res[100], *t; printf("Enter ur text...\n"); for(p=ch; (*p=getchar())!='$'; p++); *p='\0';

for(t=res,p=ch; *p ; p++,t++) { if(*p==';') *t=','; else *t=*p; } *t='\0'; printf("Modifed Array...\n"); for(t=res; *t; putchar(*t),t++) ; getch(); } Program No : 65 : Write a program in C to delete comma from the given string ?
#include<stdio.h>

#include<conio.h> void main()

174
{ char ch[100], *p , res[100], *t; printf("Enter ur text...\n"); for(p=ch; (*p=getchar())!='$'; p++); *p='\0';

for(t=res,p=ch; *p ; p++) { if(*p!=',') { *t=*p; t++; } } *t='\0'; printf("Modifed Array...\n"); for(t=res; *t; putchar(*t),t++) ; getch(); } Program No : 66 : Write a program in C to copy the reverse of the given string ?

#include<stdio.h> #include<conio.h> void main() { char ch[100], *p , res[100], *t; printf("Enter ur text...\n"); for(p=ch; (*p=getchar())!='$'; p++); *p='\0';

175
for(t=res, p--; p>=ch ; p--, t++) *t=*p; *t='\0'; printf("Reverse Array...\n"); for(t=res; *t; putchar(*t),t++) ; getch(); } Program No : 67 : Write a program in C to merge to arrays ?

#include<stdio.h> #include<conio.h> void main() { char ch1[100],ch2[100], *p, res[200], *t; printf("Enter first text...\n"); for(p=ch1; (*p=getchar())!='$'; p++); *p='\0'; fflush(stdin) ; //clear buffer printf("Enter second text...\n"); for(p=ch2; (*p=getchar())!='$'; p++); *p='\0'; for(t=res, p=ch1; *p !='\0'; p++, t++) *t=*p; for(p=ch2; *p!='\0'; p++, t++) *t=*p; *t='\0';

176
printf("Merged Array...\n"); for(t=res; *t; putchar(*t),t++) ; getch(); } Program No : 68 : Write a program in C to fortran to pascal statement ?
#include<stdio.h>

#include<conio.h> void main() { char ch[100], *p, res[200], *t; printf("Enter text...\n"); for(p=ch; (*p=getchar())!='$'; p++); *p='\0'; for(t=res, p=ch; *p !='\0'; p++, t++) { if(*p=='=') { *t=':'; t++; } else if(*p=='\n') { *t=';'; t++; } *t=*p; } *t='\0'; printf("fortran to Pascal ...\n");

177
for(t=res; *t; putchar(*t),t++) ; getch(); } Program No : 69 : Write a program in C to pascal to fortran statement ?

#include<stdio.h> #include<conio.h> void main() { char ch[100], *p, res[200], *t; printf("Enter text...\n"); for(p=ch; (*p=getchar())!='$'; p++); *p='\0'; for(t=res, p=ch; *p !='\0'; p++ ) { if(*p!=':' && *p!=';' ) { *t=*p; t++; }

} *t='\0'; printf("pascal to fortran ...\n"); for(t=res; *t; putchar(*t),t++) ; getch(); }

178
Program No : 70 : Write a program in C to count the lines in the text ?
#include<stdio.h>

#include<conio.h> void main() { char ch[100], *p; int count=0; printf("Enter text...\n"); for(p=ch; (*p=getchar())!='$'; p++); *p='\0'; for(p=ch; *p !='\0'; p++ ) { if(*p=='\n') count++; } printf("No of lines : %d",count); getch(); } Program No : 71 : Write a program in C to print the first position of every line in the given text ?

#include<stdio.h> #include<conio.h> void main() { char ch[100], *p; int count=1;

179
printf("Enter text...\n"); for(p=ch; (*p=getchar())!='$'; p++); *p='\0'; printf(" 1 st line position : 0\n"); for(p=ch; *p !='\0'; p++ ) { if(*p=='\n') { count++; printf(" %d st line position : %d\n", count, p-ch+1); } } getch(); }

Program No : 72 : Write a program in C to store the first position of every line in the given text in array ?

#include<stdio.h> #include<conio.h> void main() { char ch[100], *p ; int count=1 , sto[10], *s; printf("Enter text...\n"); for(p=ch; (*p=getchar())!='$'; p++);

180
*p='\0'; s=&sto; *s=0; s++; for( p=ch; *p !='\0'; p++ ) { if(*p=='\n') { *s=p-ch+1; s++; } } *s=-1; printf("line positions : "); for(s=&sto; *s!=-1; s++) printf(" %d", *s); getch(); } Program No : 73 : Write a program in C to print the M th line ?

#include<stdio.h> #include<conio.h> void main() { char ch[100], *p ; int sto[10], *s , m; printf("Enter text...\n"); for(p=ch; (*p=getchar())!='$'; p++); *p='\0'; s=&sto;

181
*s=0; s++; for( p=ch; *p !='\0'; p++ ) { if(*p=='\n') { *s=p-ch+1; s++; } } *s=-1; printf("Which line do u want print..\n"); scanf("%d", &m); for(s=sto , p=ch+*(s+m-1); *p!='\n'; p++) printf("%c", *p); getch(); } Program No : 74 : Write a program in C to print the M th to N th line ? #include<stdio.h> #include<conio.h> void main() { char ch[100], *p , *temp ; int sto[10], *s , m,n; printf("Enter text...\n"); for(p=ch; (*p=getchar())!='$'; p++); *p='\0'; s=&sto; *s=0; s++; for( p=ch; *p !='\0'; p++ )

182
{ if(*p=='\n') { *s=p-ch+1; s++; } } *s=-1; printf("Which range do u want print..\n"); scanf("%d%d", &m,&n); s=sto; temp=ch+*(s+n)-1; for(p=ch+*(s+m-1); *p && p<temp; p++) printf("%c", *p); getch(); } Program No : 75 : Write a program in C to find out given pattern exist or not ?

#include<stdio.h> #include<conio.h> void main() { char ch[100], *p , *temp, pat[50], *q ; int count=0; printf("Enter text...\n"); for(p=ch; (*p=getchar())!='$'; p++); *p='\0'; fflush(stdin); printf("Enter pattern...\n");

183
for(q=pat; (*q=getchar())!='$'; q++); *q='\0'; for( p=ch; *p !='\0'; p++ ) { for(temp=p, q=pat; *temp==*q && *q!='\o' ; temp++, q++) ; if(*q=='\0') { count++; printf("pattern is exist"); getch(); return; } } printf("Pattern is not exist"); getch(); } Program No : 76 : Write a program in C to count the no of times occure in given pattern ?

#include<stdio.h> #include<conio.h> void main() { char ch[100], *p , *temp, pat[50], *q ; int count=0; printf("Enter text...\n"); for(p=ch; (*p=getchar())!='$'; p++); *p='\0'; fflush(stdin); printf("Enter pattern...\n");

184
for(q=pat; (*q=getchar())!='$'; q++); *q='\0'; for( p=ch; *p !='\0'; p++ ) { for(temp=p, q=pat; *temp==*q && *q!='\0' ; temp++, q++) ; if(*q=='\0') count++; } printf("Pattern is exist on %d times ", count); getch(); } Program No : 77 : Write a program in C to delete the given pattern ?

#include<stdio.h> #include<conio.h> void main() { char ch[100], *p , *temp, pat[50], *q , *r, res[100] ; int count=0; printf("Enter text...\n"); for(p=ch; (*p=getchar())!='$'; p++); *p='\0'; fflush(stdin); printf("Enter pattern...\n"); for(q=pat; (*q=getchar())!='$'; q++); *q='\0';

185
for(r=res, p=ch; *p !='\0'; p++ ) { for(temp=p, q=pat; *temp==*q && *q!='\0' ; temp++, q++) ; if(*q=='\0') { p=temp; p++; } *r++=*p;

} *r='\0'; printf("Modfied Pattern..\n"); for(r=res; *r;r++) putchar(*r); getch(); } Program No : 78 : Write a program in C to replace the given pattern ?

#include<stdio.h> #include<conio.h> void main() { char ch[100], *p , *temp, pat[50], *q , *r, res[100], *t,rep[50] ; int count=0; printf("Enter text...\n"); for(p=ch; (*p=getchar())!='$'; p++); *p='\0'; fflush(stdin);

186
printf("Enter pattern...\n"); for(q=pat; (*q=getchar())!='$'; q++); *q='\0'; fflush(stdin); printf("Enter new pattern...\n"); for(t=rep; (*t=getchar())!='$'; t++); *t='\0'; for(r=res, p=ch; *p !='\0'; p++ ) { for(temp=p, q=pat; *temp==*q && *q!='\0' ; temp++, q++) ; if(*q=='\0') { p=temp; for(t=rep; *t; t++, r++) *r=*t; } *r++=*p;

} *r='\0'; printf("Modfied Pattern..\n"); for(r=res; *r;r++) putchar(*r); getch(); }

187

using do-while condition


Program No : 1 : Write a program in C++ to print the numbers between 1 to 1000 ?

#include<iostream.h> #include<conio.h> #include<iomanip.h> void main() { int *p, loop; p=&loop; *p=1; cout<<"Nos between 1 to 100"<<endl; do { cout<<setw(4)<<*p; (*p)++; } while(*p<=100); cout<<endl; getch(); }
Program No : 2 : Write a program in C++ to print even or odd the given range ?

#include<iostream.h> #include<conio.h> #include<iomanip.h> void main() { int *p, loop;

188
p=&loop; *p=1; cout<<"Nos between 1 to 100"<<endl; do { if(*p%2==0) cout<<*p<<" is even"<<endl; else cout<<*p<<" is odd"<<endl; (*p)++; } while(*p<=100); getch(); }
Program No : 3 : Write a program in C++ to find leap or not ?

#include<iostream.h> #include<conio.h> #include<iomanip.h> void main() { int *p, year; p=&year; *p=1000; cout<<"Nos between 1 to 100"<<endl; do { if(*p%4==0) cout<<*p<<" is leap"<<endl; else cout<<*p<<" is not leap"<<endl; (*p)++; } while(*p<=2000); getch(); }

189

Using files
Program No : 1 : Write a program in C++ to read a character one by one until $ is given ?

#include<iostream.h> #include<conio.h> #include<stdio.h> #include<fstream.h> void main() { fstream *out=new fstream; char *ch=new char; out->open("bio.dat", ios::out); if(out->fail()) { cout<<"file opening error"<<endl; getch(); return; } fflush(stdin); *ch=cin.get(); while(*ch!='$') { out->put(*ch); *ch=cin.get(); } out->close(); cout<<"text stored"<<endl; getch(); }

190
Program No : 2 : Write a program in C++ to display the text form file ?

#include<iostream.h> #include<conio.h> #include<stdio.h> #include<fstream.h> void main() { fstream *in=new fstream; char *ch=new char; in->open("bio.dat", ios::in); if(in->fail()) { cout<<"file opening error"<<endl; getch(); return; } while(!in->eof()) { *ch=in->get(); cout.put(*ch); } in->close(); cout<<endl; getch(); }
Program No : 3 : Write a program in C to read the biodata until $ is given for name ?

#include<stdio.h> #include<conio.h> void main() { FILE *fp; typedef struct bio

191
{ char name[20]; int age; float salary; char address[20]; }BIO; BIO user, *p; p=&user; fp=fopen("bio.dat","w"); if(fp==NULL) { printf("file opening error"); return; } scanf("%s",p->name); while(p->name[0]!='$') { scanf("%d%f%s",&p->age,&p->salary,p->address); fprintf(fp," %s %d %f %s",p->name,p->age,p->salary,p->address); scanf("%s",p->name); } fclose(fp); }
Program No : 4 : Write a program in C to read the bio from file and print to screen ?

#include<stdio.h> #include<conio.h> void main() { FILE *fp;

192
typedef struct bio { char name[20]; int age; float salary; char address[20]; }BIO; BIO user, *p; p=&user; fp=fopen("bio.dat","r"); if(fp==NULL) { printf("file opening error"); return; } while(feof(fp)==0) { fscanf(fp,"%s%d%f%s",p->name,&p->age,&p->salary,p->address); printf("%s %d %f %s\n",p->name,p->age,p->salary,p->address); } fclose(fp); getch(); }
Program No : 5 : Write a program in C to read a character one by one until $ is given ?

#include<stdio.h> #include<conio.h> void main() { FILE *fp; char ch,*p;

193
fp=fopen("bio.dat","w"); if(fp==NULL) { printf("file opening error"); return; } for(p=&ch;((*p)=getchar())!='$'; ) fprintf(fp,"%c",*p); fclose(fp); }
Program No : 6 : Write a program in C to display the text form file ?

#include<stdio.h> #include<conio.h> void main() { FILE *fp; char ch,*p; fp=fopen("bio.dat","r"); if(fp==NULL) { printf("file opening error"); return; } for(p=&ch,fscanf(fp,"%c",p); feof(fp)==0 ; fscanf(fp,"%c",p)) printf("%c",*p); fclose(fp); getch();

194
}

Program No : 7 : Write a program in C to read the biodata until $ is given for name using command line arguments ?

#include<stdio.h> #include<conio.h> void main(argc,argv) int argc; char *argv[]; { FILE *fp; typedef struct bio { char name[20]; int age; float salary; char address[20]; }BIO; BIO user, *p; if(argc!=2) { printf("usage is wrong"); return; } fp=fopen(argv[1],"w"); if(fp==NULL) { printf("file opening error"); return; } p=&user; for(scanf("%s",p->name);p->name[0]!='$';scanf("%s",p->name)) {

195
scanf("%d %f %s",&p->age,&p->salary,p->address); fprintf(fp," %s %d %f %s",p->name,p->age,p->salary,p->address); fflush(stdin); } fclose(fp); }
Program No : 8 : Write a program in C to read the biodata from file using command line arguments ?

#include<stdio.h> #include<conio.h> void main(argc,argv) int argc; char *argv[]; { FILE *fp; typedef struct bio { char name[20]; int age; float salary; char address[20]; }BIO; BIO user, *p; if(argc!=2) { printf("usage is wrong"); return; } fp=fopen(argv[1],"r"); if(fp==NULL) {

196
printf("file opening error"); return; } p=&user; while(feof(fp)==0) { fscanf(fp,"%s%d%f%s",p->name,&p->age,&p->salary,p->address); printf("%s %d %f %s\n",p->name,p->age,p->salary,p->address); } fclose(fp); getch(); }
Program No : 9 : Write a program in C to read a character until $ is given using command line arguments ?

#include<stdio.h> #include<conio.h> void main(argc,argv) int argc; char *argv[]; { FILE *fp; char ch, *p ; if(argc!=2) { printf("usage is wrong"); return; } fp=fopen(argv[1],"w"); if(fp==NULL) { printf("file opening error");

197
return; } for(p=&ch, scanf("%c",p);*p!='$';scanf("%c",p)) { fprintf(fp,"%c",*p); } fclose(fp); }

Program No : 10 : Write a program in C to display the text from file using command line arguments(cat command in unix) ?

#include<stdio.h> #include<conio.h> void main(argc,argv) int argc; char *argv[]; { FILE *fp; char ch,*p; if(argc!=2) { printf("usage is wrong"); return; } fp=fopen(argv[1],"r"); if(fp==NULL) { printf("file opening error"); return; }

198
for(p=&ch, fscanf(fp,"%c",p);feof(fp)==0;fscanf(fp,"%c",p)) { printf("%c",*p); } fclose(fp); getch(); }
Program No : 11 : Write a program in C to display the text from given files using command line arguments(cat all files command in unix) ?

#include<stdio.h> #include<conio.h> void main(argc,argv) int argc; char *argv[]; { FILE *fp; char ch,*p ; int loop; if(argc<2) { printf("usage is wrong"); return; } p=&ch; for(loop=1;loop<argc;loop++) { fp=fopen(argv[loop],"r"); if(fp==NULL) { printf("file opening error");

199
return; } printf("\ncontent of %s : \n", argv[loop]); for(fscanf(fp,"%c",p);feof(fp)==0;fscanf(fp,"%c",p)) { printf("%c",*p); } fclose(fp); getch(); } } Program No : 12 : Write a program in C to copy one file to another file using command line arguments(cp command in unix) ?

#include<stdio.h> #include<conio.h> void main(argc,argv) int argc; char *argv[]; { FILE *fp,*fpc; char ch,*p; if(argc!=3) { printf("usage is wrong"); return; } fp=fopen(argv[1],"r");

200
if(fp==NULL) { printf("file opening error"); return; } fpc=fopen(argv[2],"w"); if(fp==NULL) { printf("file opening error"); return; } for(p=&ch, *p=getc(fp);feof(fp)==0; *p=getc(fp)) { putc(*p,fpc); } fcloseall(); } Program No : 13 : Write a program in C to merge two files ? #include<stdio.h> #include<conio.h> void main(argc,argv) int argc; char *argv[]; { FILE *fp,*fp1,*fpc; char ch,*p; if(argc!=4) {

201
printf("usage is wrong"); return; } fp=fopen(argv[1],"r"); if(fp==NULL) { printf("file opening error"); return; } fp1=fopen(argv[2],"r"); if(fp==NULL) { printf("file opening error"); return; } fpc=fopen(argv[3],"w"); if(fpc==NULL) { printf("file opening error"); return; } p=&ch; for(*p=getc(fp);feof(fp)==0;*p=getc(fp)) putc(*p,fpc); for(*p=getc(fp1);feof(fp1)==0;*p=getc(fp1)) putc(*p,fpc); printf("file merged"); fcloseall();

202
Program No : 14 : Write a program in C to convert upper case letters into lower case ? #include<stdio.h> #include<conio.h> void main(argc,argv) int argc; char *argv[]; { FILE *fp,*fpc; char ch, *p; if(argc!=3) { printf("usage is wrong"); return; } fp=fopen(argv[1],"r"); if(fp==NULL) { printf("file opening error"); return; } fpc=fopen(argv[2],"w"); if(fpc==NULL) { printf("file opening error"); return; } p=&ch; for(*p=getc(fp);feof(fp)==0;*p=getc(fp)) (*p>='A'&&*p<='Z')?putc(*p+32,fpc):putc(*p,fpc);

203
fcloseall(); } Program No : 15 : Write a program in C to encraph the given file ?

#include<stdio.h> #include<conio.h> void main(argc,argv) int argc; char *argv[]; { FILE *fp,*fpc; char ch,*p; if(argc!=3) { printf("usage is wrong"); return; } fp=fopen(argv[1],"r"); if(fp==NULL) { printf("file opening error"); return; } fpc=fopen(argv[2],"w"); if(fpc==NULL) { printf("file opening error"); return; } for(p=&ch,*p=getc(fp);feof(fp)==0;*p=getc(fp))

204
putc(*p+2,fpc); fcloseall(); printf("file Encrpedted"); getch(); } Program No : 16 : Write a program in C to decraph the given file ? #include<stdio.h> #include<conio.h> void main(argc,argv) int argc; char *argv[]; { FILE *fp,*fpc; char ch,*p; if(argc!=3) { printf("usage is wrong"); return; } fp=fopen(argv[1],"r"); if(fp==NULL) { printf("file opening error"); return; } fpc=fopen(argv[2],"w"); if(fpc==NULL) { printf("file opening error");

205
return; } for(p=&ch, *p=getc(fp);feof(fp)==0;*p=getc(fp)) putc(*p-2,fpc); fcloseall(); printf("file decrpted"); getch(); } Program No : 17 : Write a program in C to copy the contents of array into file. ? #include<stdio.h> #include<conio.h> void main(argc,argv) int argc; char *argv[]; { FILE *fp; char ch[100],*p; if(argc!=2) { printf("usage is wrong"); return; }

fp=fopen(argv[1],"w"); if(fp==NULL) { printf("file opening error"); return; } printf("string\n");

206
for(p=&ch; (*p=getchar())!='$'; p++ ); *p='\0'; for(p=&ch; *p;p++) putc(*p,fp); fcloseall();

} Program No : 18 : Write a program in C to copy the file contents into array . ? #include<stdio.h> #include<conio.h> void main(argc,argv) int argc; char *argv[]; { FILE *fp; char ch[200], *p; if(argc!=2) { printf("usage is wrong"); return; } fp=fopen(argv[1],"r"); if(fp==NULL) { printf("file opening error"); return; } printf("string\n");

207
p=&ch; while(feof(fp)==0) { *p=getc(fp); p++; } *p='\0'; p=&ch; printf("%s",p); fcloseall(); getch(); } Program No : 19 : Write a program in C to display line by line of the file contents ? #include<stdio.h> #include<conio.h> void main(argc,argv) int argc; char *argv[]; { FILE *fp; char ch[200],*p; if(argc!=2) { printf("usage is wrong"); return; }

fp=fopen(argv[1],"r"); if(fp==NULL)

208
{ printf("file opening error"); return; } printf("string\n"); for(p=ch, *p=getc(fp);feof(fp)==0; *(p)=getc(fp)) { if(*p=='\n') { *(++p)='\0'; printf("%s",ch); p=&ch; } else p++; } *p='\0'; printf("%s",ch); fcloseall(); getch(); } Program No : 20 : Write a program in C to display line by line of the file contents including line number. ? #include<stdio.h> #include<conio.h> void main(argc,argv) int argc; char *argv[]; { FILE *fp; int line=0; char ch[200],*p;

209
if(argc!=2) { printf("usage is wrong"); return; }

fp=fopen(argv[1],"r"); if(fp==NULL) { printf("file opening error"); return; } printf("string\n"); for(p=ch, *p=getc(fp);feof(fp)==0; *(p)=getc(fp)) { if(*p=='\n') { line++; *(++p)='\0'; printf("%d : %s",line,ch); p=&ch; } else p++; } *p='\0'; line++; printf("%d : %s",line,ch); fcloseall(); getch(); } Program No : 21 : Write a program in C to display page by page of the file contents including line number. ?

210
#include<stdio.h> #include<conio.h> void main(argc,argv) int argc; char *argv[]; { FILE *fp; int line=0; char ch[200],*p; if(argc!=2) { printf("usage is wrong"); return; }

fp=fopen(argv[1],"r"); if(fp==NULL) { printf("file opening error"); return; } printf("string\n"); for(p=ch, *p=getc(fp);feof(fp)==0; *(p)=getc(fp)) { if(*p=='\n') { line++; *(++p)='\0'; printf("%d : %s",line,ch); if(line%10==0) getch(); p=&ch; } else p++; }

211
*p='\0'; line++; printf("%d : %s",line,ch); fcloseall(); getch(); } Program No : 22 : Write a program in C to display line by line and count the no of upper and lower case letters and numbers . ? #include<stdio.h> #include<conio.h> void main(argc,argv) int argc; char *argv[]; { FILE *fp; int low, upp, spe, num; char ch[200],*p; if(argc!=2) { printf("usage is wrong"); return; }

fp=fopen(argv[1],"r"); if(fp==NULL) { printf("file opening error"); return; } printf("string\n");

212
low=upp=spe=num=0; for(p=ch, *p=getc(fp);feof(fp)==0; *(p)=getc(fp)) { if('A'<=*p&&*p<='Z') upp++; else if ('a'<=*p&&*p<='z') low++; else if ('0'<=*p&&*p<='9') num++; else spe++; if(*p=='\n') { *(++p)='\0'; printf("%s",ch); printf("upp %d low %d num %d spe %d \n",upp,low,num,spe); getch(); low=upp=spe=num=0; p=&ch; } else p++; } *p='\0'; fcloseall(); getch(); } Program No : 23 : Write a program in C to count the no of upper and lower case letters and numbers in file.. ? #include<stdio.h> #include<conio.h> void main(argc,argv) int argc; char *argv[]; { FILE *fp; int low, upp, spe, num;

213
char ch[200],*p; if(argc!=2) { printf("usage is wrong"); return; }

fp=fopen(argv[1],"r"); if(fp==NULL) { printf("file opening error"); return; } printf("string\n"); low=upp=spe=num=0; for(p=ch, *p=getc(fp);feof(fp)==0; p++, *(p)=getc(fp) ) { if('A'<=*p&&*p<='Z') upp++; else if ('a'<=*p&&*p<='z') low++; else if ('0'<=*p&&*p<='9') num++; else spe++; } *p='\0'; printf("%s",ch); printf("upp %d low %d num %d spe %d \n",upp,low,num,spe); fcloseall(); getch(); } Program No : 24 : Write a program in C to count the no of lines and words and characters in file.. ?

214
#include<stdio.h> #include<conio.h> void main(argc,argv) int argc; char *argv[]; { FILE *fp; int words, lines,chars; char ch[200],*p; if(argc!=2) { printf("usage is wrong"); return; }

fp=fopen(argv[1],"r"); if(fp==NULL) { printf("file opening error"); return; } printf("string\n"); words=chars=lines=0; for(p=ch, *p=getc(fp);feof(fp)==0; p++, *(p)=getc(fp) ) { if(*p=='\n') lines++; if(*p=='\n'&&*(p+1)!='\n' || *p=='\t'&&*(p+1)!='\t' || *p==' '&&*(p+1)!=' ' ) words++;

215
*p='\0'; chars=p-ch; printf("%s",ch); printf("lines %d chars %d words %d",lines,chars,words); fcloseall(); getch(); } Program No : 25 : Write a program in C to count the given pattern ? #include<stdio.h> #include<conio.h> void main(argc,argv) int argc; char *argv[]; { FILE *fp; int count=0; char ch[200],*p, pat[50],*q, *r; if(argc!=2) { printf("usage is wrong"); return; }

fp=fopen(argv[1],"r"); if(fp==NULL) { printf("file opening error"); return; }

216
printf("string\n"); for(q=pat;(*q=getchar())!='$';q++); *q='\0'; for(p=ch, *p=getc(fp);feof(fp)==0; p++, *(p)=getc(fp)); *p='\0'; for(p=ch; *p; p++) { for( r=p, q=pat; *r==*q && *q!='\0'; r++, q++); if(*q=='\0') count++; } printf("%s",ch); printf("pattern : %d",count); fcloseall(); getch(); } Program No : 26 : Write a program in C to count the given pattern using command line arguments. ? #include<stdio.h> #include<conio.h> void main(argc,argv) int argc; char *argv[]; { FILE *fp; int count=0; char ch[200],*p, *q, *r; if(argc!=3)

217
{ printf("usage is wrong"); return; }

fp=fopen(argv[2],"r"); if(fp==NULL) { printf("file opening error"); return; } q=argv[1];

printf("string : %s\n",q); for(p=ch, *p=getc(fp);feof(fp)==0; p++, *(p)=getc(fp)); *p='\0'; for(p=ch; *p; p++) { for( r=p, q=argv[1]; *r==*q && *q!='\0'; r++, q++); if(*q=='\0') count++; } printf("%s",ch); printf("pattern : %d",count); fcloseall(); getch(); }

218
Program No : 27 : Write a program in C to print the particular line when pattern is occured using command line arguments.(grep command in unix: ) ? #include<stdio.h> #include<conio.h> void main(argc,argv) int argc; char *argv[]; { FILE *fp; int count=0; char ch[200],*p, *q, *r; if(argc!=3) { printf("usage is wrong"); return; }

fp=fopen(argv[2],"r"); if(fp==NULL) { printf("file opening error"); return; } q=argv[1]; printf("string : %s\n",q); for(p=ch, *p=getc(fp);feof(fp)==0; *(p)=getc(fp)) { if(*p=='\n') { *(++p)='\0'; for(p=ch; *p; p++) { for( r=p, q=argv[1]; *r==*q && *q!='\0'; r++, q++); if(*q=='\0') {

219
count++; printf("%s",ch); } } p=&ch; } else p++; } printf("pattern : %d",count); fcloseall(); getch(); } Program No : 28 : Write a program in C to print the particular line with line number when pattern is occured using command line arguments.(grep command in unix: ) ? #include<stdio.h> #include<conio.h> void main(argc,argv) int argc; char *argv[]; { FILE *fp; int line=0; char ch[200],*p, *q, *r; if(argc!=3) { printf("usage is wrong"); return; }

fp=fopen(argv[2],"r"); if(fp==NULL) { printf("file opening error");

220
return; } q=argv[1]; printf("string : %s\n",q); for(p=ch, *p=getc(fp);feof(fp)==0; *(p)=getc(fp)) { if(*p=='\n') { line++; *(++p)='\0'; for(p=ch; *p; p++) { for( r=p, q=argv[1]; *r==*q && *q!='\0'; r++, q++); if(*q=='\0') { printf("%d : %s",line, ch); } } p=&ch; } else p++; }

fcloseall(); getch(); } Program No : 29 : Write a program in C to print the particular line with line number when pattern is occured using command line arguments in all files(Grep command in unix: ) ? #include<stdio.h> #include<conio.h>

221
void main(argc,argv) int argc; char *argv[]; { FILE *fp; int line=0; char ch[200],*p, *q, *r; int i=0; if(argc<3) { printf("usage is wrong"); return; } for(i=2; i<argc; i++) { line=0; printf("\nfile name : %s\n", argv[i]); fp=fopen(argv[i],"r"); if(fp==NULL) { printf("file opening error"); return; } q=argv[1];

for(p=ch, *p=getc(fp);feof(fp)==0; *(p)=getc(fp)) { if(*p=='\n') { line++; *(++p)='\0'; for(p=ch; *p; p++) { for( r=p, q=argv[1]; *r==*q && *q!='\0'; r++, q++); if(*q=='\0') {

222
printf("%d : %s",line, ch); } } p=&ch; } else p++; }

fcloseall(); getch(); } } Program No : 30 : Write a program in C to print the particular line with line number when pattern dost not occur using command line arguments in all files ? #include<stdio.h> #include<conio.h> void main(argc,argv) int argc; char *argv[]; { FILE *fp; int line=0; int check=0; char ch[200],*p, *q, *r; int i=0; if(argc<3) { printf("usage is wrong"); return; } for(i=2; i<argc; i++) { line=0;

223
printf("\nfile name : %s\n", argv[i]); fp=fopen(argv[i],"r"); if(fp==NULL) { printf("file opening error"); return; } q=argv[1];

for(p=ch, *p=getc(fp);feof(fp)==0; *(p)=getc(fp)) { if(*p=='\n') { check=0; line++; *(++p)='\0'; for(p=ch; *p; p++) { for( r=p, q=argv[1]; *r==*q && *q!='\0'; r++, q++); if(*q=='\0') { check=1; } } if(check==0) printf("%d : %s",line, ch); p=&ch; } else p++; }

fcloseall();

224
getch(); } } Program No : 31 : Write a program in C to read and store the bio-data into file ?

#include<stdio.h> void main() { FILE *fp; union info { struct bio { char name[20]; int age; float sal; char add[20]; }a; char b[sizeof(struct bio)]; }; union info stu, *p; int i; fp=fopen("bio.dat","w"); if(fp==NULL) { printf("file opening error"); return; } p=&stu; scanf("%s",p->a.name); scanf("%d",&p->a.age); scanf("%f",&p->a.sal); scanf("%s",p->a.add); for(i=0;i<sizeof(struct bio); i++) putc(p->b[i],fp); fclose(fp);

225
} Program No : 32 : Write a program in C to read the bio-data from file and print it to the screen ?

#include<stdio.h> void main() { FILE *fp; union info { struct bio { char name[20]; int age; float sal; char add[20]; }a; char b[sizeof(struct bio)]; }; union info stu, *p; int i; fp=fopen("bio.dat","r"); if(fp==NULL) { printf("file opening error"); return; } p=&stu; for(i=0;i<sizeof(struct bio); i++) p->b[i]=getc(fp); printf("%s %d %f %s ",p->a.name,p->a.age,p->a.sal,p->a.add); getch(); fcloseall(); }

226
Program No : 33 : Write a program in C to read the bio-datae and print it to the file until $ is given for name. ?

#include<stdio.h> void main(argc,argv) int argc; char *argv[]; { FILE *fp; union info { struct bio { char name[20]; int age; float sal; char add[20]; }a; char b[sizeof(struct bio)]; }; union info stu ,*p; int i; if(argc!=2) { printf("usage is wrong"); return; } fp=fopen(argv[1],"w"); if(fp==NULL) { printf("file opening error"); return; } p=&stu; for(scanf("%s",p->a.name);p->a.name[0]!='$';scanf("%s",p->a.name)) {

227
scanf("%d",&p->a.age); scanf("%f",&p->a.sal); scanf("%s",p->a.add); for(i=0;i<sizeof(struct bio ); i++) putc(stu.b[i],fp); fflush(stdin); } } Program No : 34 : Write a program in C to read the bio-datae from file and print it to the screen until EOF is occur. ?

#include<stdio.h> #include<conio.h> void main(argc,argv) int argc; char *argv[]; { FILE *fp; union info { struct bio { char name[20]; int age; float sal; char add[20]; }a; char b[sizeof(struct bio)]; }; union info stu,*p; int i; if(argc!=2) { printf("usage is wrong"); return; }

228
fp=fopen("bio.dat","r"); if(fp==NULL) { printf("file opening error"); return; } p=&stu; while(feof(fp)==0) { for(i=0;i<sizeof(struct bio ); p->b[i++]=getc(fp)); printf("\n%s %d %f %s\n",p->a.name,p->a.age,p->a.sal,p->a.add); } fclose(fp); getch(); } Program No : 35 : Write a program in C to read the bio-datae from file and calculate hra and da ?

#include<stdio.h> #include<conio.h> void main(argc,argv) int argc; char *argv[]; { FILE *fp; union info { struct bio { char name[20]; int age; float sal; char add[20]; }a;

229
char b[sizeof(struct bio)]; }; union info stu,*p; int i; double hra,da; if(argc!=2) { printf("usage is wrong"); return; } fp=fopen("bio.dat","r"); if(fp==NULL) { printf("file opening error"); return; } p=&stu; while(feof(fp)==0) { for(i=0;i<sizeof(struct bio ); p->b[i++]=getc(fp)); hra=p->a.sal*0.10; da=p->a.sal*0.05; printf("\n%s %d %f %f %f %s \n",p->a.name,p->a.age,p->a.sal, hra,da, p->a.add); } fclose(fp); getch(); } Program No : 36 : Write a program in C to display the Nth recored ? #include<stdio.h> #include<conio.h>

230
void main(argc,argv) int argc; char *argv[]; { FILE *fp; union info { struct bio { char name[20]; int age; float sal; char add[20]; }a; char b[sizeof(struct bio)]; }; union info stu,*p; int i,j,N,rp;

if(argc!=2) { printf("usage is wrong"); return; } fp=fopen("bio.dat","r"); if(fp==NULL) { printf("file opening error"); return; } printf("enter record number : \n"); scanf("%d",&N); p=&stu; i= (N-1) * sizeof(struct bio); rp=fseek(fp,i,0); for(j=0;j<sizeof(struct bio) ;j++) p->b[j]=getc(fp);

231
printf("%s\n%d\n%f\n%s\n",p->a.name,p->a.age,p->a.sal,p->a.add); fclose(fp); getch(); } Program No : 37 : Write a program in C to display the payslip of Nth recored ? #include<stdio.h> #include<conio.h> void main(argc,argv) int argc; char *argv[]; { FILE *fp; union info { struct bio { char name[20]; int age; float sal; char add[20]; }a; char b[sizeof(struct bio)]; }; union info stu,*p; int i,j,N,rp; double pf,hra,basic,da; if(argc!=2) { printf("usage is wrong"); return; } fp=fopen("bio.dat","r");

232
if(fp==NULL) { printf("file opening error"); return; } printf("enter record number : \n"); scanf("%d",&N); p=&stu; i= (N-1) * sizeof(struct bio); rp=fseek(fp,i,0); for(j=0;j<sizeof(struct bio) ;j++) p->b[j]=getc(fp); printf("%s\n%d\n%f\n%s\n",p->a.name,p->a.age,p->a.sal,p->a.add); hra=0.10*p->a.sal; pf=0.05*p->a.sal; da=0.10*p->a.sal; basic=p->a.sal-pf-hra-da; printf(" pf %lf hra %lf da %lf basic %lf",pf,hra,da,basic); fclose(fp); getch(); } Program No : 38 : Write a program in C to edit the nth record ? #include<stdio.h> #include<conio.h> void main(argc,argv) int argc; char *argv[]; { FILE *fp; union info { struct bio {

233
char name[20]; int age; float sal; char add[20]; }a; char b[sizeof(struct bio)]; }; union info stu,*p; int i,j,N,rp; if(argc!=2) { printf("usage is wrong"); return; } fp=fopen("bio.dat","r+"); if(fp==NULL) { printf("file opening error"); return; } printf("enter record number : \n"); scanf("%d",&N); p=&stu; i= (N-1) * sizeof(struct bio); rp=fseek(fp,i,0); for(j=0;j<sizeof(struct bio) ;j++) p->b[j]=getc(fp); printf("%s\n%d\n%f\n%s\n",p->a.name,p->a.age,p->a.sal,p->a.add); rp=fseek(fp,i,0); printf("Enter new value : \n"); scanf("%s%d%f%s",stu.a.name,&stu.a.age,&stu.a.sal,stu.a.add); for(j=0;j<sizeof(struct bio) ;j++) putc(p->b[j],fp);

234
fclose(fp); getch(); } Program No : 39 : Write a program in C to read a biodata until record no is zero ?

#include<stdio.h> #include<conio.h> void main(argc,argv) int argc; char *argv[]; { FILE *fp,*fp1; union info { struct bio { int recno; char name[20]; int age; float sal; char add[20]; }a; char b[sizeof(struct bio)]; };

union info stu, *p; int i=0; if(argc!=3) { printf("in valid arguments\n"); return; } fp=fopen(argv[1],"w"); fp1=fopen(argv[2],"w"); if(fp==NULL||fp1==NULL) {

235
printf("file opening error\n"); return; } p=&stu; for(scanf("%d",&p->a.recno); p->a.recno!=0;scanf("%d",&p->a.recno)) { scanf("%s%d%f%s",p->a.name,&p->a.age,&p->a.sal,p->a.add); for(i=0; i<sizeof(struct bio ); putc(p->b[i++],fp)); fprintf(fp1,"%d",p->a.recno); } fcloseall(); } Program No : 40 : Write a program in C to display the biodata for givenl record no ?

#include<stdio.h> #include<conio.h> void main(argc,argv) int argc; char *argv[]; { FILE *fp,*fp1; union info { struct bio { int recno; char name[20]; int age; float sal; char add[20]; }a;

236
char b[sizeof(struct bio)]; };

union info stu,*p; int i=0,rp,k; int found=0; int N; if(argc!=3) { printf("in valid arguments\n"); return; } fp=fopen(argv[1],"r"); fp1=fopen(argv[2],"r"); if(fp==NULL&&fp1==NULL) { printf("file opening error\n"); return; } printf("enter record number\n"); scanf("%d",&N); p=&stu; for(fscanf(fp,"%d",&p->a.recno); feof(fp)==0;fscanf(fp,"%d",&p->a.recno)) {

if(p->a.recno==N) { i=(N-1)*sizeof(struct bio); rp=fseek(fp1,i,0); for( k=0;k<sizeof(struct bio); p->b[k++]=getc(fp1)); printf("\n%s\t%d\t%f\t%s",p->a.name,p->a.age,p->a.sal,p->a.add);

237
found=1; }

} if(found!=1) printf("not found"); fcloseall(); getch(); } Program No : 41 : Write a program in C to display the pay slip for given record no. ?

#include<stdio.h> #include<conio.h> void main(argc,argv) int argc; char *argv[]; { FILE *fp,*fp1; union info { struct bio { int recno; char name[20]; int age; float sal; char add[20]; }a; char b[sizeof(struct bio)]; };

238
union info stu,*p; int i=0,rp,k; int found=0; double da,tax,hr; int N; if(argc!=3) { printf("in valid arguments\n"); return; } fp=fopen(argv[1],"r"); fp1=fopen(argv[2],"r"); if(fp==NULL&&fp1==NULL) { printf("file opening error\n"); return; } printf("enter record number\n"); scanf("%d",&N); p=&stu; for(fscanf(fp,"%d",&p->a.recno); feof(fp)==0;fscanf(fp,"%d",&p->a.recno)) {

if(p->a.recno==N) { i=(N-1)*sizeof(struct bio); rp=fseek(fp1,i,0); for( k=0;k<sizeof(struct bio); p->b[k++]=getc(fp1)); printf("\n%s\t%d\t%f\t%s",p->a.name,p->a.age,p->a.sal,p->a.add); da=stu.a.sal * 0.10; tax=stu.a.sal * 0.10; hr=2000;

239
printf("\n basic %lf",p->a.sal); printf("\n da %lf",da); printf("\n tax %lf",tax); printf("\n hr %lf",hr); printf("\nfinal salary %lf",p->a.sal+da-tax-hr); found=1; }

} if(found!=1) printf("not found"); fcloseall(); getch(); } Program No : 42 : Write a program in C to edit the n th record ?

#include<stdio.h> #include<conio.h> void main(argc,argv) int argc; char *argv[]; { FILE *fp,*fp1; union info { struct bio { int recno; char name[20]; int age; float sal; char add[20]; }a;

240
char b[sizeof(struct bio)]; };

union info stu,*p; int i=0,rp,k; int found=0; int N; if(argc!=3) { printf("in valid arguments\n"); return; } fp=fopen(argv[1],"r"); fp1=fopen(argv[2],"r"); if(fp==NULL&&fp1==NULL) { printf("file opening error\n"); return; } printf("enter record number\n"); scanf("%d",&N); p=&stu; for(fscanf(fp,"%d",&p->a.recno); feof(fp)==0;fscanf(fp,"%d",&p->a.recno)) {

if(p->a.recno==N) { i=(N-1)*sizeof(struct bio); rp=fseek(fp1,i,0); for( k=0;k<sizeof(struct bio); p->b[k++]=getc(fp1)); printf("\n%s\t%d\t%f\t%s",p->a.name,p->a.age,p->a.sal,p->a.add);

241
printf("\nenter the new name "); scanf("%s",p->a.name); rp=fseek(fp1,i,0); for(k=0; k<sizeof(struct bio); putc(p->b[k++],fp1)); found=1; }

} if(found!=1) printf("not found"); fcloseall(); getch(); }

using functions
Program No : 1 : Write a program in C to sum of two integer numbers. ? #include<stdio.h> #include<conio.h> void main () { int a,b,total; input(&a,&b); sum(a,b,&total); printf("%d", total); getch();

242
} input(x,y) int *x, *y; { scanf("%d%d", x,y); } sum(x,y,tot) int x,y,*tot; { *tot = x +y ; } Program No : 2 : Write a program in C to find the biggest of two integer numbers. ? #include<stdio.h> #include<conio.h> void main () { int a,b,big; input(&a,&b); biggest(a,b,&big); printf("Big : %d", big); getch(); } input(x,y) int *x, *y; { scanf("%d%d", x,y); } biggest(x,y,temp) int x,y, *temp; { *temp = (x>y) ? x : y ; }

243
Program No : 3 : Write a program in C to find the smallest of two integer numbers. ? #include<stdio.h> #include<conio.h> void main () { int a,b,small; input(&a,&b); smallest(a,b,&small); printf("Small : %d", small); getch(); } input(x,y) int *x, *y; { scanf("%d%d", x,y); } smallest(x,y,temp) int x,y, *temp; { *temp = (x < y) ? x : y ; } Program No : 4 : Write a program in C to find the area of circle. ? #include<stdio.h> #include<conio.h> void main () { float radius,result; input(&radius); area(radius, &result); printf("%f", result); getch(); } input(rad) float *rad;

244
{ scanf("%f", rad); } area(r, ar) float r,*ar; { *ar = 3.14 * r * r ; } Program No : 5 : Write a program in C to find the volume of sphere. ?

#include<stdio.h> #include<conio.h> void main () { float radius,result; input(&radius); volume(radius, &result); printf("%f", result); getch(); } input(rad) float *rad; { scanf("%f", rad); } volume(r, ar) float r,*ar; { *ar = 4.0/3.0 * r * r * r; } Program No : 6 : Write a program in C to find out the given number is even or odd. ? #include<stdio.h> #include<conio.h> main () { int number;

245
input(&number); evenorodd(&number); getch(); } input(num) int *num; { scanf("%d", num); } evenorodd(num) int *num; { if (*num%2==0) printf("Given number is even"); else printf("Given number is odd"); } Program No : 7 : Write a program in C to find out the given year is leap year or not. ?

#include<stdio.h> #include<conio.h> main () { int year; input(&year); evenorodd(&year); getch(); } input(yr) int *yr; { scanf("%d", yr); } evenorodd(yr) int *yr; {

246
if (*yr%4==0) printf("Given year is leap"); else printf("Given year is not a leep"); } Program No : 8 : Write a program in C to print the single digit number into words. ?

#include<stdio.h> #include<conio.h> main () { int digit; input(&digit); singledigit(&digit); getch(); } input(int *dig) { scanf("%d", dig); } singledigit(dig) int *dig; { switch(*dig) { case 1 : printf("one"); break; case 2 : printf("two"); break; case 3 : printf("three"); break; case 4 : printf("four"); break; case 5 : printf("five"); break; case 6 : printf("six"); break; case 7 : printf("seven"); break; case 8 : printf("eight"); break; case 9 : printf("nine"); break; case 0 : printf("zero"); break; default: printf("Given number is not a single digit"); break; } }

247
Program No : 9 : Write a program in C to print 11-19 range to words. ? #include<stdio.h> #include<conio.h> main () { int digit; input(&digit); tenrange(&digit); getch(); } input (int *dig) { scanf("%d", dig); } tenrange(dig) int *dig; { switch(*dig) { case 11 : printf("eleven"); break; case 12 : printf("twevel"); break; case 13 : printf("thirteen"); break; case 14 : printf("fourteen"); break; case 15 : printf("fifteen"); break; case 16 : printf("sixteen"); break; case 17 : printf("seventeen"); break; case 18 : printf("eighteen"); break; case 19 : printf("nineteen"); break; default : printf("Given number is not a specified range"); break; } } Program No : 10 : Write a program in C to print the tens (10,20,..90) into words ?

#include<stdio.h> #include<conio.h>

248
main () { int digit; input(&digit); tens(&digit); getch(); } input (int *dig) { scanf("%d", dig); } tens(dig) int *dig; { switch(*dig) { case 10 : printf("ten"); break; case 20 : printf("twenty"); break; case 30 : printf("thirty"); break; case 40 : printf("fourty"); break; case 50 : printf("fifty"); break; case 60 : printf("sixty"); break; case 70 : printf("seventy");break; case 80 : printf("eighty"); break; case 90 : printf("ninety"); break; default : printf("Given number is not a specified range"); break; } } Program No : 11 : Write a program in C to print the limit 0-99 into words. ?

#include<stdio.h> #include<conio.h> main () { int digit; input(&digit); if(digit>=0 && digit<=99) twodigit(&digit); else

249
printf("given number is not a specified range"); getch(); } input(num) int *num; { scanf("%d", num); } twodigit(num) int *num; { int r,m; if (*num >= 0 && *num <= 9 ) singledigit(num); else if (*num>=11 && *num <=19) tenrange(num); else if (*num%10==0) tens(num); else { r=*num/10 * 10; m=*num-r; tens(&r); printf(" "); singledigit(&m); } } tens(dig) int *dig; { switch(*dig) { case 10 : printf("ten"); break;

250
case 20 : printf("twenty"); break; case 30 : printf("thirty"); break; case 40 : printf("fourty"); break; case 50 : printf("fifty"); break; case 60 : printf("sixty"); break; case 70 : printf("seventy"); break; case 80 : printf("eighty"); break; case 90 : printf("ninety"); break; default: printf("Given number is not a specified range"); break; } } tenrange(dig) int *dig; { switch(*dig) { case 11 : printf("eleven"); break; case 12 : printf("twevel"); break; case 13 : printf("thirteen"); break; case 14 : printf("fourteen"); break; case 15 : printf("fifteen"); break; case 16 : printf("sixteen"); break; case 17 : printf("seventeen"); break; case 18 : printf("eighteen"); break; case 19 : printf("nineteen"); break; default: printf("Given number is not a specified range"); break; } } singledigit(dig) int *dig; { switch(*dig) { case 1 : printf("one"); break; case 2 : printf("two"); break; case 3 : printf("three"); break; case 4 : printf("four"); break; case 5 : printf("five"); break; case 6 : printf("six"); break;

251
case 7 : printf("seven"); break; case 8 : printf("eight"); break; case 9 : printf("nine"); break; case 0 : printf("zero"); break; default: printf("Given number is not a single digit"); break; } } Program No : 12 : Write a program in C to print the limit 0-999 into words ?

#include<stdio.h> #include<conio.h> main () { int digit; input(&digit); if(digit>=0 && digit<=999) { threedigit(&digit); } else printf("given number is not a specified range"); getch(); } input(num) int *num; { scanf("%d", num); } threedigit(num) int *num; { int temp, temp1; temp=*num/100; temp1=*num-temp*100; if(temp>0) {

252
singledigit(&temp); printf(" hundred "); if(temp1!=0) twodigit(&temp1); } else { twodigit(num); }

} twodigit(num) int *num; { int r,m; if (*num >= 0 && *num <= 9 ) singledigit(num); else if (*num>=11 && *num <=19) tenrange(num); else if (*num%10==0) tens(num); else { r=*num/10 * 10; m=*num-r; tens(&r); printf(" "); singledigit(&m); } }

253
tens(dig) int *dig; { switch(*dig) { case 10 : printf("ten"); break; case 20 : printf("twenty"); break; case 30 : printf("thirty"); break; case 40 : printf("fourty"); break; case 50 : printf("fifty"); break; case 60 : printf("sixty"); break; case 70 : printf("seventy"); break; case 80 : printf("eighty"); break; case 90 : printf("ninety"); break; default: printf("Given number is not a specified range"); break; } } tenrange(dig) int *dig; { switch(*dig) { case 11 : printf("eleven"); break; case 12 : printf("twevel"); break; case 13 : printf("thirteen"); break; case 14 : printf("fourteen"); break; case 15 : printf("fifteen"); break; case 16 : printf("sixteen"); break; case 17 : printf("seventeen"); break; case 18 : printf("eighteen"); break; case 19 : printf("nineteen"); break; default: printf("Given number is not a specified range"); break; } } singledigit(dig) int *dig; {

254
switch(*dig) { case 1 : printf("one"); break; case 2 : printf("two"); break; case 3 : printf("three"); break; case 4 : printf("four"); break; case 5 : printf("five"); break; case 6 : printf("six"); break; case 7 : printf("seven"); break; case 8 : printf("eight"); break; case 9 : printf("nine"); break; case 0 : printf("zero"); break; default: printf("Given number is not a single digit"); break; } } Program No : 13 : Write a program in C to read and print the ten numbers ? #include<stdio.h> #include<conio.h> main () { int i, num[10], *p ; p=&num; readnum(p); for(i=0;i<10; i++) printf("%d ", *(p+i)); getch(); } readnum(temp) int *temp; { int j; for(j=0;j<10; j++) scanf("%d", temp+j); }

255
Program No : 14 : Write a program in C to read and print the ten numbers using separate functions. ? #include<stdio.h> #include<conio.h> main () { int i, num[10], *p ; p=&num; readnum(p); printnum(p); getch(); } readnum(temp) int *temp; { int j; for(j=0;j<10; j++) scanf("%d", temp+j); } printnum(temp) int *temp; { int j; for(j=0;j<10; j++) printf("%d ", *(temp+j)); } Program No : 15 : Write a program in C to find out the biggest of ten numbers. ? #include<stdio.h> #include<conio.h>

256
main () { int big=-32678, num[10]; readnum(num); findbiggest(num,&big); printf("big : %d",big); getch(); } readnum(temp) int *temp; { int j; for(j=0;j<10; j++) scanf("%d", temp+j); } findbiggest(no, bi) int *no, *bi; { int k; for(k=0; k<10; k++) { *bi = *(no+k) > *bi ? *(no+k) : *bi; } } Program No : 16 : Write a program in C to find out the smallest of ten numbers. ? #include<stdio.h> #include<conio.h> main () { int small=32677, num[10]; readnum(num);

257
findsmallest(num,&small); printf("small : %d",small); getch(); } readnum(temp) int *temp; { int j; for(j=0;j<10; j++) scanf("%d", temp+j); } findsmallest(no, bi) int *no, *bi; { int k; for(k=0; k<10; k++) { *bi = *(no+k) < *bi ? *(no+k) : *bi; } } Program No : 17 : Write a program in C to find out the biggest and smallest of ten numbers ? #include<stdio.h> #include<conio.h> main () { int small=32677, num[10] , big=-32678;; readnum(num); findsmallest(num,&small); findbiggest(num,&big);

258
printf("small : %d \n",small); printf("big : %d \n",big); getch(); } readnum(temp) int *temp; { int j; for(j=0;j<10; j++) scanf("%d", temp+j); } findsmallest(no, bi) int *no, *bi; { int k; for(k=0; k<10; k++) { *bi = *(no+k) < *bi ? *(no+k) : *bi; } } findbiggest(no, bi) int *no, *bi; { int k; for(k=0; k<10; k++) { *bi = *(no+k) > *bi ? *(no+k) : *bi; } } Program No : 18 : Write a program in C to find out the index of biggest of ten numbers in array ? #include<stdio.h>

259
#include<conio.h> main () { int bigpos, num[10]; readnum(num); findbiggest(num, &bigpos); printf("Big Position big : %d",bigpos); getch(); } readnum(temp) int *temp; { int j; for(j=0;j<10; j++) scanf("%d", temp+j); } findbiggest(no, bp) int *no, *bp; { int k=0; *bp=0; for(k=0; k<10; k++) { if(*(no+k)> *(no+*bp)) *bp=k; } }

Program No : 19 : Write a program in C to find out the index of smallest of ten numbers in array ?

260
#include<stdio.h> #include<conio.h> main () { int smallpos, num[10]; readnum(num); findsmallest(num, &smallpos); printf("small Position : %d",smallpos); getch(); } readnum(temp) int *temp; { int j; for(j=0;j<10; j++) scanf("%d", temp+j); } findsmallest(no, sp) int *no, *sp; { int k=0; *sp=0; for(k=0; k<10; k++) { if(*(no+k) < *(no+*sp)) *sp=k; } }

261

Program No : 20 : Write a program in C to interchange the biggest and smallest of ten numbers. ? #include<stdio.h> #include<conio.h> main () { int bigpos, smallpos, num[10]; readnum(num); interchange(num); printf("interchanged Array\n"); printnum(num); getch(); } readnum(temp) int *temp; { int j; for(j=0;j<10; j++) scanf("%d", temp+j); } printnum(temp) int *temp; { int j; for(j=0;j<10; j++) printf("%d ", *(temp+j)); } interchange(no) int *no; {

262
int bigpos, smallpos,temp; findbiggest(&no,&bigpos); findsmallest(&no, &smallpos); temp=*(no+bigpos); *(no+bigpos)=*(no+smallpos); *(no+smallpos)=temp; } findsmallest(no, sp) int **no, *sp; // ** pointer to pointer concept { int k=0; *sp=0; for(k=0; k<10; k++) { if(*(*no+k) < *(*no+*sp) ) *sp=k; } } findbiggest(no, bp) int **no, *bp; { int k=0; *bp=0; for(k=0; k<10; k++) { if(*(*no+k) > *(*no+*bp) ) *bp=k; } }

263
Program No : 21 : Write a program in C to merge two arrays (array size is 10). ? #include<stdio.h> #include<conio.h> main () { int num1[10],num2[10], num[20]; readnum(num1); readnum(num2); merge(num1,num2,num); printf("Merged Array\n"); printnum(num); getch(); } readnum(temp) int *temp; { int j; for(j=0;j<10; j++) scanf("%d", temp+j); } merge(num1,num2,num) int *num1,*num2,*num; { int i,j; for(i=0,j=0; i<10; i++, j++) *(num+j)=*(num1+i); for(i=0; i<10;j++, i++) *(num+j)=*(num2+i); }

264
printnum(temp1) int *temp1; { int k; for(k=0; k<20; k++) printf("%d ", *(temp1+k)); } Program No : 22 : Write a program in C for ascending order of ten numbers ? #include<stdio.h> #include<conio.h> main () { int num[10]; readnum(num); ascend(num); printf("Ascended Array\n"); printnum(num); getch(); } readnum(temp) int *temp; { int j; for(j=0;j<10; j++) scanf("%d", temp+j); } ascend(num) int *num; { int i,j, temp; for(i=0; i<10; i++)

265
for(j=i; j<10;j++) { if(*(num+i) > *(num+j) ) { temp=*(num+j); *(num+j)=*(num+i); *(num+i)=temp; } }

} printnum(temp1) int *temp1; { int k; for(k=0; k<10; k++) printf("%d ", *(temp1+k)); } Program No : 23 : Write a program in C for descending order of ten numbers ? #include<stdio.h> #include<conio.h> main () { int num[10]; readnum(num); desend(num); printf("desended Array\n"); printnum(num); getch(); } readnum(temp) int *temp;

266
{ int j; for(j=0;j<10; j++) scanf("%d", temp+j); } desend(num) int *num; { int i,j, temp; for(i=0; i<10; i++) for(j=i; j<10;j++) { if(*(num+i) < *(num+j) ) { temp=*(num+j); *(num+j)=*(num+i); *(num+i)=temp; } }

} printnum(temp1) int *temp1; { int k; for(k=0; k<10; k++) printf("%d ", *(temp1+k)); } Program No : 24 : Write a program in C to read and display of N numbers ?

#include<stdio.h> #include<conio.h>

267
main () { int num[10], n; printf("enter n value:\n"); scanf("%d", &n); readnum(num,n); printf("Given Array\n"); printnum(num,n); getch(); } readnum(temp,n) int *temp,n; { int j; for(j=0;j<n; j++) scanf("%d", temp+j); } printnum(temp1,n) int *temp1,n; { int k; for(k=0; k<n; k++) printf("%d ", *(temp1+k)); } Program No : 25 : Write a program in C to read and print the numbers which are odd and even ?

#include<stdio.h> #include<conio.h> main () { int num[10], n;

268
printf("enter n value:\n"); scanf("%d", &n); readnum(num,n); printevenorodd(num,n); getch(); } readnum(temp,n) int *temp,n; { int j; for(j=0;j<n; j++) scanf("%d", temp+j); } printevenorodd(no,n) int *no,n; { int k; for(k=0; k<n; k++) { (*(no+k) % 2 == 0) ? printf("%d is even ", *(no+k)) : printf("%d is odd ", *(no+k)); } } Program No : 26 : Write a program in C to find the average of N numbers ?

#include<stdio.h> #include<conio.h> main () { int n, num[100]; float avg; printf("enter N value\n");

269
scanf("%d", &n); readnum(num,n); findavg(num,n, &avg); printf("Avg : %f", avg); getch(); } readnum(temp,n) int *temp,n; { int j; for(j=0;j<n; j++) scanf("%d", temp+j); } findavg(temp,n,avg) int *temp,n ; float *avg; { int k; float sum=0.00; for(k=0; k<n; k++) sum+= *(temp+k); *avg = sum/n; } Program No : 27 : Write a program in C to read and print the reverse order ?

#include<stdio.h> #include<conio.h> main () { int n, num[100]; printf("enter N value\n"); scanf("%d", &n); readnum(num,n); printf("reverse order\n"); reverseorder(num,n); getch();

270
} readnum(temp,n) int *temp,n; { int j; for(j=0;j<n; j++) scanf("%d", temp+j); } reverseorder(temp,n) int *temp,n; { int k; for(k=n-1; k>=0; k--) printf("%d ",*(temp+k)); } Program No : 28 : Write a program in C to read and display a 3 x 3 Matrix. ?

#include<stdio.h> #include<conio.h> main () { int a[3][3]; readmatrix(a); printmatrix(a); getch(); } readmatrix(m) int *m; { int i, j; for(i=0;i<3;i++) for (j=0; j<3;j++) scanf("%d", m+i*3+j);

271
} printmatrix(m) int *m; { int i,j; for(i=0;i<3;i++) { for (j=0; j<3;j++) printf("%d ", *(m+i*3+j)); printf("\n"); } } Program No : 29 : Write a program in C to transpose the 3 x 3 Matrix ?

#include<stdio.h> #include<conio.h> main () { int a[3][3], b[3][3]; readmatrix(a); transpose(a,b); printf("transpose matrix\n"); printmatrix(b); getch(); } readmatrix(m) int *m; { int i, j; for(i=0;i<3;i++) for (j=0; j<3;j++) scanf("%d", m+i*3+j); }

272
transpose(m,n) int *m, *n; { int i,j; for(i=0;i<3;i++) for (j=0; j<3;j++) *(n+j*3+i) =*(m+i*3+j); } printmatrix(m) int *m; { int i,j; for(i=0;i<3;i++) { for (j=0; j<3;j++) printf("%d ", *(m+i*3+j)); printf("\n"); } } Program No : 30 : Write a program in C to print the sum of two 3 x 3 Matrix. ? #include<stdio.h> #include<conio.h> main () { int a[3][3], b[3][3], sum[3][3]; readmatrix(a); readmatrix(b); summat(a,b,sum); printf("sum matrix\n"); printmatrix(sum); getch(); } readmatrix(m)

273
int *m; { int i, j; for(i=0;i<3;i++) for (j=0; j<3;j++) scanf("%d", m+i*3+j); } summat(m,n,sum) int *m, *n,*sum; { int i,j; for(i=0;i<3;i++) for (j=0; j<3;j++) *(sum+i*3+j) = *(m+i*3+j) + *(n+i*3+j); } printmatrix(m) int *m; { int i,j; for(i=0;i<3;i++) { for (j=0; j<3;j++) printf("%d ", *(m+i*3+j)); printf("\n"); } } Program No : 31 : Write a program in C to print the product of two 3 x 3 Matrix. ?

#include<stdio.h> #include<conio.h> main () { int a[3][3], b[3][3], pro[3][3]; readmatrix(a); readmatrix(b);

274
promat(a,b,pro); printf("product matrix\n"); printmatrix(pro); getch(); } readmatrix(m) int *m; { int i, j; for(i=0;i<3;i++) for (j=0; j<3;j++) scanf("%d", m+i*3+j); } promat(mat1,mat2,pro) int *mat1, *mat2,*pro; { int i,j,k; for(i=0;i<3;i++) for (j=0; j<3;j++) { *(pro+i*3+j)=0; for(k=0; k<3;k++) *(pro+i*3+j) += *(mat1+i*3+k) * *(mat2+k*3+j); } } printmatrix(m) int *m; { int i,j; for(i=0;i<3;i++) { for (j=0; j<3;j++) printf("%d ", *(m+i*3+j)); printf("\n"); } }

275
Program No : 32 : Write a program in C to read and display a N x N Matrix. ? #include<stdio.h> #include<conio.h> main () { int a[10][10]; int n; printf("N value : \n"); scanf("%d", &n); readmatrix(a,n); printf("given %d x %d Matrix\n",n,n); printmatrix(a,n); getch(); } readmatrix(mat,n) int *mat, n; { int i, j; for(i=0;i<n;i++) for (j=0; j<n;j++) scanf("%d", mat+i*10+j); } printmatrix(mat,n) int *mat,n; { int i,j; for(i=0;i<n;i++) { for (j=0; j<n;j++) printf("%d ", *(mat+i*10+j)); printf("\n"); } }

276
Program No : 33 : Write a program in C to transpose the N x N Matrix ?

#include<stdio.h> #include<conio.h> main () { int a[10][10], b[10][10]; int n; printf("N value : \n"); scanf("%d", &n); readmatrix(a,n); transpose(a,b,n); printf("transpose %d x %d Matrix\n",n,n); printmatrix(b,n); getch(); } readmatrix(mat,n) int *mat, n; { int i, j; for(i=0;i<n;i++) for (j=0; j<n;j++) scanf("%d", mat+i*10+j); } transpose(mat,trans,n) int *mat,*trans,n; { int i,j; for(i=0;i<n;i++) { for (j=0; j<n;j++) *(trans+j*10+i) = *(mat+i*10+j);

277
} } printmatrix(mat,n) int *mat,n; { int i,j; for(i=0;i<n;i++) { for (j=0; j<n;j++) printf("%d ", *(mat+i*10+j)); printf("\n"); } } Program No : 34 : Write a program in C to print the sum of two N x N Matrix. ?

#include<stdio.h> #include<conio.h> main () { int a[10][10], b[10][10], sum[10][10]; int n; printf("N value : \n"); scanf("%d", &n); readmatrix(a,n); readmatrix(b,n); summatrix(a,b,sum,n); printf("sum %d x %d Matrix\n",n,n); printmatrix(sum,n); getch(); }

278
readmatrix(mat,n) int *mat, n; { int i, j; for(i=0;i<n;i++) for (j=0; j<n;j++) scanf("%d", mat+i*10+j); } summatrix(mat1,mat2,sum,n) int *mat1,*mat2,*sum,n; { int i,j; for(i=0;i<n;i++) { for (j=0; j<n;j++) *(sum+i*10+j) = *(mat1+i*10+j) + *(mat2+i*10+j); } } printmatrix(mat,n) int *mat,n; { int i,j; for(i=0;i<n;i++) { for (j=0; j<n;j++) printf("%d ", *(mat+i*10+j)); printf("\n"); } } Program No : 35 : Write a program in C to print the product of two N x N Matrix ? #include<stdio.h> #include<conio.h> main () { int a[10][10], b[10][10], pro[10][10]; int n; printf("N value : \n");

279
scanf("%d", &n); readmatrix(a,n); readmatrix(b,n); promatrix(a,b,pro,n); printf("product %d x %d Matrix\n",n,n); printmatrix(pro,n); getch(); } readmatrix(mat,n) int *mat, n; { int i, j; for(i=0;i<n;i++) for (j=0; j<n;j++) scanf("%d", mat+i*10+j); } promatrix(mat1,mat2,pro,n) int *mat1,*mat2,*pro,n; { int i,j,k=0;; for(i=0;i<n;i++) { for (j=0; j<n;j++) { *(pro+i*10+j)=0; for(k=0;k<n;k++) *(pro+i*10+j) += *(mat1+i*10+k) * *(mat2+k*10+j); } } } printmatrix(mat,n) int *mat,n; { int i,j; for(i=0;i<n;i++) { for (j=0; j<n;j++)

280
printf("%d ", *(mat+i*10+j)); printf("\n"); } } Program No : 36 : Write a program in C to read and display a M x N Matrix ?

#include<stdio.h> #include<conio.h> main () { int a[10][10]; int row,col; printf("Enter row and column Size\n"); scanf("%d%d", &row,&col); printf("\nEnter %d x %d Matrix\n",row,col); readmatrix(a,row,col); printf("output Matrix\n"); printmatrix(a,row,col); getch(); } readmatrix(mat,row,col) int *mat, row,col; { int i, j; for(i=0;i<row;i++) for (j=0; j<col;j++) scanf("%d", mat+i*10+j); } printmatrix(mat,row,col) int *mat,row,col; {

281
int i,j; for(i=0;i<row;i++) { for (j=0; j<col;j++) printf("%d ", *(mat+i*10+j)); printf("\n"); } } Program No : 37 : Write a program in C to transpose of M x N Matrix. ? #include<stdio.h> #include<conio.h> main () { int a[10][10], trans[10][10]; int row,col; printf("Enter row and column Size\n"); scanf("%d%d", &row,&col); printf("\nEnter %d x %d Matrix\n",row,col); readmatrix(a,row,col); transpose(a,trans,row,col); printf("transposed Matrix\n"); printmatrix(trans,col,row); getch(); } readmatrix(mat,row,col) int *mat, row,col; { int i, j; for(i=0;i<row;i++) for (j=0; j<col;j++) scanf("%d", mat+i*10+j);

282
} transpose(mat,trans, row,col) int *mat, *trans, row,col; { int i, j; for(i=0;i<row;i++) for (j=0; j<col;j++) *(trans+j*10+i) = *(mat+i*10+j); } printmatrix(mat,row,col) int *mat,row,col; { int i,j; for(i=0;i<row;i++) { for (j=0; j<col;j++) printf("%d ", *(mat+i*10+j)); printf("\n"); } } Program No : 38 : Write a program in C to print the sum of two M x N Matrix ?

#include<stdio.h> #include<conio.h> main () { int a[10][10], b[10][10], sum[10][10]; int row,col; printf("Enter row and column Size\n"); scanf("%d%d", &row,&col); printf("\nEnter %d x %d Matrix\n",row,col); printf("Enter matrix 1\n"); readmatrix(a,row,col);

283
printf("Enter matrix 2\n"); readmatrix(b,row,col); summatrix(a,b,sum,row,col); printf("sum Matrix\n"); printmatrix(sum,row,col); getch(); } readmatrix(mat,row,col) int *mat, row,col; { int i, j; for(i=0;i<row;i++) for (j=0; j<col;j++) scanf("%d", mat+i*10+j); } summatrix(mat1,mat2,sum, row,col) int *mat1,*mat2, *sum,row,col; { int i, j; for(i=0;i<row;i++) for (j=0; j<col;j++) *(sum+i*10+j) = *(mat1+i*10+j) + *(mat2+i*10+j); } printmatrix(mat, row,col) int *mat, row,col; { int i,j; for(i=0;i<row;i++) { for (j=0; j<col;j++) printf("%d ", *(mat+i*10+j)); printf("\n"); } }

284
Program No : 39 : Write a program in C to print the product of two M x N Matrix ? #include<stdio.h> #include<conio.h> /* program no : 312 */ /* purpose : product of two M x N matrix*/ /* date of written : 01/09/2006 */ #include<stdio.h> #include<conio.h> main () { int a[10][10],b[10][10],res[10][10]; int row1,col1, row2, col2; printf("Enter row and column Size of matrix one\n"); scanf("%d%d", &row1,&col1); printf("Enter row and column Size of matrix second\n"); scanf("%d%d", &row2,&col2); if(col1!=row2) { printf("Row and Column sizes does not match for product"); getch(); return 0; } printf("\nEnter %d x %d Matrix 1 \n",row1,col1); readmatrix(a,row1,col1); printf("\nEnter %d x %d Matrix 2 \n",row2,col2); readmatrix(b,row2,col2); promatrix(a,b,res,row1,col1,row2,col2); printf("output Matrix\n");

285
printmatrix(res,row1,col2); getch(); } readmatrix(mat,row,col) int *mat, row,col; { int i, j; for(i=0;i<row;i++) for (j=0; j<col;j++) scanf("%d", mat+i*10+j); } promatrix(mat1,mat2,pro,row1,col1,row2,col2) int *mat1,*mat2, *pro,row1,col1,row2,col2; { int i, j, k; for(i=0;i<row1;i++) for (j=0; j<col2;j++) { *(pro+i*10+j)=0; for(k=0; k<col1; k++) *(pro+i*10+j) += *(mat1+i*10+k) * *(mat2+k*10+j); } } printmatrix(mat,row,col) int *mat, row,col; { int i,j; for(i=0;i<row;i++) { for (j=0; j<col;j++) printf("%d ", *(mat+i*10+j)); printf("\n"); } } Program No : 40 : Write a program in C to read and print the text ?

#include<stdio.h>

286
#include<conio.h> main () { char a[100]; printf("Enter text\n"); readtext(a); printf("Given text\n"); printtext(a); getch(); } readtext(b) char *b; { for( ; (*b=getchar())!='$'; b++); *b='\0'; } printtext(b) char *b; { for( ; *b ; putchar(*b), b++); } Program No : 40 : Write a program in C to read and print the text ?

#include<stdio.h> #include<conio.h> main () { char a[100]; printf("Enter text\n"); readtext(a); printf("Given text\n"); printtext(a);

287
getch(); } readtext(b) char *b; { for( ; (*b=getchar())!='$'; b++); *b='\0'; } printtext(b) char *b; { for( ; *b ; putchar(*b), b++); } Program No : 42 : Write a program in C to count the no. of lines, words & characters of given text ? #include<stdio.h> #include<conio.h> int lines, words, chars; main () { char a[100]; printf("Enter text\n"); readtext(a); counttext(a); printf("lines : %d words : %d chars : %d", lines, words, chars); getch(); } readtext(b) char *b; { for( ; (*b=getchar())!='$'; b++);

288
*b='\0'; } counttext(b) char *b; { lines=words=chars=0; while(*b) { if( (*b==' ' && *(b+1)!=' ') || (*b=='\t' && *(b+1)!='\t') || (*b=='\n' && *(b+1)!='\n') ) words++; if (*b=='\n') lines++; chars++; b++; }

} Program No : 43 : Write a program in C to copy one array to another array ? #include<stdio.h> #include<conio.h> main () { char a[100], b[100]; printf("Enter text\n"); readtext(a); copytext(a,b); printf("copied array\n"); printtext(b); getch();

289
} readtext(p) char *p; { for( ; (*p=getchar())!='$'; p++); *p='\0'; } copytext(p,q) char *p,*q; { while(*q) { *q=*p; p++; q++; } *q='\0'; } printtext(p) char *p; { for( ; *p ; putchar(*p), p++); } Program No : 44 : Write a program in C to merge the two arrays ?

#include<stdio.h> #include<conio.h> main () { char a[100], b[100],c[200]; printf("Enter text\n"); readtext(a);

290
printf("Enter text\n"); readtext(b); mergetext(a,b,c); printf("merged array\n"); printtext(c); getch(); } readtext(p) char *p; { for( ; (*p=getchar())!='$'; p++); *p='\0'; } mergetext(p,q,r) char *p,*q, *r; { for( ; *p ; p++, r++) *r=*p; for( ; *q ; q++, r++) *r=*q; *r='\0'; } printtext(p) char *p; { for( ; *p ; putchar(*p), p++); } Program No : 45 : Write a program in C to copy the left N Characters ? #include<stdio.h> #include<conio.h>

291
main () { char a[100],b[100]; int n; printf("enter text\n"); readtext(a); printf("how many characters to be copied\n"); scanf("%d", &n); printf("copied array..\n"); copytext(a,b,n); printtext(b); getch(); } readtext(p) char *p; { for(; (*p=getchar())!='$'; p++); *p='\0'; } copytext(p,q,n) char *p, *q; int n; { char *temp; for(temp=p+n; *p && p<temp ; p++, q++) *q=*p; *q='\0'; }

292
printtext(p) char *p; { for(; *p ; putchar(*p), p++); } Program No : 46 : Write a program in C to copy the N Characters from M th position onwards ? #include<stdio.h> #include<conio.h> main () { char a[100],b[100]; int m,n; printf("enter text\n"); readtext(a); printf("M & N value : \n"); scanf("%d%d", &m, &n); printf("copied array..\n"); copytext(a,b,m,n); printtext(b); getch(); } readtext(p) char *p; { for(; (*p=getchar())!='$'; p++); *p='\0'; }

293
copytext(p,q,m,n) char *p, *q; int m,n; { char *temp; for(temp=p+m+n, p=p+m-1 ; *p && p<temp ; p++, q++) *q=*p; *q='\0'; } printtext(p) char *p; { for(; *p ; putchar(*p), p++); } Program No : 47 : Write a program in C to find the length of given text or array ?

#include<stdio.h> #include<conio.h> main () { char a[100]; int len; printf("enter text\n"); readtext(a); lengthoftext(a,&len); printf("length : %d", len); getch(); } readtext(p)

294
char *p; { for(; (*p=getchar())!='$'; p++); *p='\0'; } lengthoftext(p,len) char *p; int *len; { int *temp; *len=0; for(temp=p; *p ; p++) ; *len=p-temp; } Program No : 48 : Write a program in C to copy the last N characters ? #include<stdio.h> #include<conio.h> main () { char a[100], b[100]; int n; printf("enter text\n"); readtext(a); printf("how many characters copied from last onwards..\n"); scanf("%d", &n); copytext(a,b,n); printf("Copied Text..\n"); printtext(b); getch();

295
} readtext(p) char *p; { for(; (*p=getchar())!='$'; p++); *p='\0'; } copytext(p,q,n) char *p,*q; int n; { int *temp; int len; lengthoftext(p, &len); for(p=p+len-n; *p ; p++ , q++) *q=*p; *q='\0'; } lengthoftext(p,len) char *p; int *len; { int *temp; *len=0; for(temp=p; *p ; p++) ; *len=p-temp; } printtext(p) char *p; { for(; *p ; putchar(*p), p++); }

296
Program No : 49 : Write a program in C to print the reverse of given array ? #include<stdio.h> #include<conio.h> main () { char a[100], b[100]; int n; printf("enter text\n"); readtext(a); reversetext(a,b); printf("reversed Text..\n"); printtext(b); getch(); } readtext(p) char *p; { for(; (*p=getchar())!='$'; p++); *p='\0'; } reversetext(p,q) char *p,*q; { int *temp; int len; lengthoftext(p, &len); for(temp=p, p=p+len-1; p>=temp ; p-- , q++) *q=*p; *q='\0';

297
} lengthoftext(p,len) char *p; int *len; { int *temp; *len=0; for(temp=p; *p ; p++) ; *len=p-temp; } printtext(p) char *p; { for(; *p ; putchar(*p), p++); } Program No : 50 : Write a program in C to merge three arrays ?

#include<stdio.h> #include<conio.h> main () { char a[100], b[100], c[100], mer[200]; int n; printf("enter text 1\n"); readtext(a); printf("enter text 2\n"); readtext(b); printf("enter text 3\n");

298
readtext(c); mer[0]='\0'; mergetext(a,mer); mergetext(b,mer); mergetext(c,mer); printf("merged Text..\n"); printtext(mer); getch(); } readtext(p) char *p; { for(; (*p=getchar())!='$'; p++); *p='\0'; } mergetext(p,q) char *p,*q; { int len=0; lengthoftext(q, &len); for(q=q+len ; *p ; p++ , q++) *q=*p; *q='\0'; } lengthoftext(p,len) char *p; int *len; { char *temp; *len=0;

299
for(temp=p; *p ; p++) ; *len=p-temp; } printtext(p) char *p; { for(; *p ; putchar(*p), p++); } Program No : 51 : Write a program in C to convert upper case to lower case letters ?

#include<stdio.h> #include<conio.h> main () { char a[100], b[100]; int n; printf("enter text \n"); readtext(a); conversion(a,b); printf("converted Text..\n"); printtext(b); getch(); } readtext(p) char *p; { for(; (*p=getchar())!='$'; p++); *p='\0'; }

300
conversion(p,q) char *p,*q; { for(; *p ; p++ , q++) { if(*p>='A' && *p<='Z') *q=*p+32; else *q=*p; } *q='\0'; } printtext(p) char *p; { for(; *p ; putchar(*p), p++); } Program No : 52 : Write a program in C to convert lower case to upper case letters ? #include<stdio.h> #include<conio.h> main () { char a[100], b[100]; int n; printf("enter text \n"); readtext(a); conversion(a,b); printf("converted Text..\n"); printtext(b);

301
getch(); } readtext(p) char *p; { for(; (*p=getchar())!='$'; p++); *p='\0'; } conversion(p,q) char *p,*q; { for(; *p ; p++ , q++) { if(*p>='a' && *p<='z') *q=*p-32; else *q=*p; } *q='\0'; } printtext(p) char *p; { for(; *p ; putchar(*p), p++); } Program No : 53 : Write a program in C to replace comma to semicolon ?

#include<stdio.h> #include<conio.h> main () {

302
char a[100], b[100]; int n; printf("enter text \n"); readtext(a); conversion(a,b); printf("converted Text..\n"); printtext(b); getch(); } readtext(p) char *p; { for(; (*p=getchar())!='$'; p++); *p='\0'; } conversion(p,q) char *p,*q; { for(; *p ; p++ , q++) { if(*p==',') *q=';'; else *q=*p; } *q='\0'; } printtext(p) char *p; { for(; *p ; putchar(*p), p++); }

303
Program No : 54 : Write a program in C to replace semicolon to comma ?

#include<stdio.h> #include<conio.h> main () { char a[100], b[100]; int n; printf("enter text \n"); readtext(a); conversion(a,b); printf("converted Text..\n"); printtext(b); getch(); } readtext(p) char *p; { for(; (*p=getchar())!='$'; p++); *p='\0'; } conversion(p,q) char *p,*q; { for(; *p ; p++ , q++) { if(*p==';') *q=','; else *q=*p;

304
} *q='\0'; } printtext(p) char *p; { for(; *p ; putchar(*p), p++); } Program No : 55 : Write a program in C to delete the comma in given string ?

#include<stdio.h> #include<conio.h> main () { char a[100], b[100]; int n; printf("enter text \n"); readtext(a); conversion(a,b); printf("converted Text..\n"); printtext(b); getch(); } readtext(p) char *p; { for(; (*p=getchar())!='$'; p++); *p='\0';

305
} conversion(p,q) char *p,*q; { for(; *p ; p++ ) { if(*p!=',') *q++=*p; } *q='\0'; } printtext(p) char *p; { for(; *p ; putchar(*p), p++); } Program No : 56 : Write a program in C to store the first position of every new line ? #include<stdio.h> #include<conio.h> main () { char a[100]; int b[20], *p; int n; printf("enter text \n"); readtext(a); findposition(a,b); printf("postion..\n");

306
for(p=b; *p!=-1000; p++) printf("%d ", *p); getch(); } readtext(p) char *p; { for(; (*p=getchar())!='$'; p++); *p='\0'; } findposition(p,q) char *p ; int *q; { char *temp; *q=0; q++; for(temp=p; *p ; p++ ) { if(*p=='\n') *q++=p-temp+1; } *q=-1000; } Program No : 57 : Write a program in C to print the Nth line ?

#include<stdio.h> #include<conio.h> main () { char a[100],*p;

307
int b[20]; int m; printf("enter text \n"); readtext(a); printf("enter m value..\n"); scanf("%d", &m); findposition(a,b); printf("%d th line.\n",m); printmthline(a,b,m); getch(); } readtext(p) char *p; { for(; (*p=getchar())!='$'; p++); *p='\0'; } findposition(p,q) char *p ; int *q; { char *temp; *q=0; q++; for(temp=p; *p ; p++ ) { if(*p=='\n') *q++=p-temp+1; } *q=-1000;

308
} printmthline(p,q,m) char *p; int *q , m; { for( p=p+*(q+m-1) ; *p && *p!='\n' ; putchar(*p), p++); } Program No : 58 : Write a program in C to print the Mth line to Nth line ? #include<stdio.h> #include<conio.h> main () { char a[100]; int b[20]; int m,n; printf("enter text \n"); readtext(a); printf("enter m, n value..\n"); scanf("%d%d", &m,&n); findposition(a,b); printf("%d th to %d line.\n",m,n); printmthtonthline(a,b,m,n); getch(); } readtext(p) char *p;

309
{ for(; (*p=getchar())!='$'; p++); *p='\0'; } findposition(p,q) char *p ; int *q; { char *temp; *q=0; q++; for(temp=p; *p ; p++ ) { if(*p=='\n') *q++=p-temp+1; } *q=-1000; } printmthtonthline(p,q,m,n) char *p; int *q , m,n; { char *temp; for( temp=p+ *(q+n)-1, p=p+*(q+m-1) ; *p && p<temp ; putchar(*p), p++); } Program No : 59 : Write a program in C to count the no of patterns exist on given string ? #include<stdio.h> #include<conio.h> main () {

310
char a[100], b[50]; int count; printf("enter text \n"); readtext(a); fflush(stdin); printf("enter pat \n"); readtext(b); countpattern(a,b,&count); printf("Pattern %d", count); getch(); } readtext(p) char *p; { for(; (*p=getchar())!='$'; p++); *p='\0'; }

countpattern(p,q,count) char *p, *q; int *count; { char *temp, *r; *count=0; temp=q; for( ; *p ; p++) { for(q=temp, r=p; *r==*q && *q!='\0'; r++, q++) ; if(*q=='\0') (*count)++; }

311
}
Program No : 60 : Write a program in C to delete the pattern ?

#include<stdio.h> #include<conio.h> main () { char a[100], b[50], c[100]; int count; printf("enter text \n"); readtext(a); fflush(stdin); printf("enter pat \n"); readtext(b); deletepattern(a,b,c); printf("modifed text\n"); printtext(c); getch(); } readtext(p) char *p; { for(; (*p=getchar())!='$'; p++); *p='\0'; }

deletepattern(p,q,t) char *p, *q, *t;

312
{ char *temp, *r;

temp=q; for( ; *p ; p++) { for(q=temp, r=p; *r==*q && *q!='\0'; r++, q++) ; if(*q=='\0') { p=r; } else *t++=*p; } *t='\0'; }

printtext(p) char *p; { for(; *p ; putchar(*p), p++); }


Program No : 61 : Write a program in C to replace the given pattern ?

#include<stdio.h> #include<conio.h> main () { char a[100], b[50], c[50], d[100]; int count; printf("enter text \n");

313
readtext(a); fflush(stdin); printf("pattern \n"); readtext(b); fflush(stdin); printf("replace text \n"); readtext(c); replacepattern(a,b,c,d); printf("modifed text\n"); printtext(d); getch(); } readtext(p) char *p; { for(; (*p=getchar())!='$'; p++); *p='\0'; } replacepattern(p,q,s,t) char *p, *q,*s, *t; { char *temp, *temp1, *r;

temp=q; for( ; *p ; p++) { for(q=temp, r=p; *r==*q && *q!='\0'; r++, q++) ; if(*q=='\0') { p=--r;

314
for(temp1=s; *temp1 ; temp1++) *t++=*temp1; } else *t++=*p; } *t='\0'; } printtext(p) char *p; { for(; *p ; putchar(*p), p++); }
Program No : 62 : Write a program in C to read text and store into file ?

#include<stdio.h> #include<conio.h> main (argc, argv) int argc; char *argv[]; { FILE *fp; if(argc!=2) { printf("usage is wrong"); return; } fp=fopen(argv[1], "w"); if(fp==NULL) { printf("file Opening Error"); return; } readfile(fp);

315
fclose(fp); } readfile(tp) FILE *tp; { char ch, *p; p=&ch; for(scanf("%c", p) ; *p!='$'; scanf("%c", p)) fprintf(tp, "%c", *p); } Program No : 63 : Write a program in C to read text from file and print to screen ? #include<stdio.h> #include<conio.h> main (argc, argv) int argc; char *argv[]; { FILE *fp; if(argc!=2) { printf("usage is wrong"); return; } fp=fopen(argv[1], "r"); if(fp==NULL) { printf("file Opening Error"); return; } readfile(fp); fclose(fp);

316
getch(); } readfile(tp) FILE *tp; { char ch, *p; p=&ch; for(fscanf(tp,"%c", p) ; feof(tp)==0; fscanf(tp,"%c", p)) printf("%c", *p); } Program No : 64 : Write a program in C to cat all files. (cat command in unix) ? #include<stdio.h> #include<conio.h> main (argc, argv) int argc; char *argv[]; { FILE *fp; int i; if(argc<2) { printf("usage is wrong"); return; } for ( i=1; i<argc; i++) { fp=fopen(argv[i], "r"); if(fp==NULL) { printf("file Opening Error"); return;

317
} printf("File %d contents\n", i); printfile(fp); } fclose(fp); getch(); } printfile(tp) FILE *tp; { char ch ,*p; p=&ch; for(fscanf(tp,"%c", p) ; feof(tp)==0; fscanf(tp,"%c", p)) printf("%c", *p); }

Program No : 65 : Write a program in C to copy one file into another file (copy command) ? #include<stdio.h> #include<conio.h> main (argc, argv) int argc; char *argv[]; { FILE *sp,*dp; int i; if(argc!=3) { printf("usage is wrong"); return; }

318
sp=fopen(argv[1], "r"); dp=fopen(argv[2], "w"); if(sp==NULL && dp==NULL) { printf("file Opening Error"); return; } copyfile(sp,dp); printf("file copied"); getch(); fclose(sp); fclose(dp); } copyfile(sp,dp) FILE *sp,*dp; { char ch, *p; p=&ch; for(fscanf(sp,"%c", p) ; feof(sp)==0; fscanf(sp,"%c", p)) fprintf(dp,"%c", *p); } Program No : 66 : Write a program in C to merge two files ? #include<stdio.h> #include<conio.h> main (argc, argv) int argc; char *argv[]; { FILE *fp1,*fp2,*dp; int i; if(argc!=4) {

319
printf("usage is wrong"); return; } fp1=fopen(argv[1], "r"); fp2=fopen(argv[2], "r"); dp=fopen(argv[3], "w"); if(fp1==NULL && fp2==NULL && dp==NULL) { printf("file Opening Error"); return; } mergefile(fp1,dp); mergefile(fp2,dp); printf("file merged"); getch(); fclose(fp1); fclose(fp2); fclose(dp); } mergefile(sp,dp) FILE *sp,*dp; { char ch ,*p; p=&ch; for(*p=getc(sp); *p!=EOF; *p=getc(sp)) putc(*p,dp); } Program No : 66 : Write a program in C to merge two files ?

#include<stdio.h> #include<conio.h>

320
main (argc, argv) int argc; char *argv[]; { FILE *fp1,*fp2,*dp; int i; if(argc!=4) { printf("usage is wrong"); return; } fp1=fopen(argv[1], "r"); fp2=fopen(argv[2], "r"); dp=fopen(argv[3], "w"); if(fp1==NULL && fp2==NULL && dp==NULL) { printf("file Opening Error"); return; } mergefile(fp1,dp); mergefile(fp2,dp); printf("file merged"); getch(); fclose(fp1); fclose(fp2); fclose(dp); } mergefile(sp,dp) FILE *sp,*dp; { char ch ,*p; p=&ch; for(*p=getc(sp); *p!=EOF; *p=getc(sp)) putc(*p,dp);

321
} Program No : 67 : Write a program in C to print the entire file using array ? #include<stdio.h> #include<conio.h> main (argc, argv) int argc; char *argv[]; { FILE *sp; char a[200]; if(argc!=2) { printf("usage is wrong"); return; } sp=fopen(argv[1], "r"); if(sp==NULL) { printf("file Opening Error"); return; } loadarray(sp,a); printtext(a); getch(); fclose(sp); } loadarray(tp,ch) FILE *tp; char *ch; {

322
for(*ch=getc(tp); *ch!=EOF; *(++ch)=getc(tp)); *ch='\0'; }

printtext(b) char *b; { for( ; *b ; putchar(*b++)); } Program No : 68 : Write a program in C to print the entire file along the line number ?

#include<stdio.h> #include<conio.h> main (argc, argv) int argc; char *argv[]; { FILE *sp; char a[200]; int line; if(argc!=2) { printf("usage is wrong"); return; } sp=fopen(argv[1], "r"); if(sp==NULL) { printf("file Opening Error"); return; } line=0;

323
while(loadarray(sp,a)) { line++; printf("\n%d : ",line); printtext(a); } line++; printf("\n%d : ",line); printtext(a); getch(); fclose(sp); } loadarray(sp,ch) FILE *sp; char *ch; {

for(*ch=getc(sp); *ch!=EOF ; *(++ch)=getc(sp)) { if(*ch=='\n') { *ch='\0'; return 1; }

} *ch='\0'; return 0; }

printtext(b)

324
char *b; { for( ; *b ; putchar(*b), b++); } Program No : 69 : Write a program in C to print the entire file on page by page along the line number ?

#include<stdio.h> #include<conio.h> main (argc, argv) int argc; char *argv[]; { FILE *sp; char a[200]; int line; if(argc!=2) { printf("usage is wrong"); return; } sp=fopen(argv[1], "r"); if(sp==NULL) { printf("file Opening Error"); return; } line=0; while(loadarray(sp,a)) { line++; printf("\n%d : ",line); printtext(a); if(line%24==0) getch(); }

325
line++; printf("\n%d : ",line); printtext(a); getch(); fclose(sp); } loadarray(sp,ch) FILE *sp; char *ch; {

for(*ch=getc(sp); *ch!=EOF ; *(++ch)=getc(sp)) { if(*ch=='\n') { *ch='\0'; return 1; }

} *ch='\0'; return 0; }

printtext(b) char *b; { for( ; *b ; putchar(*b), b++); }

326
Program No : 70 : Write a program in C to count the no of upper, lower and special charactes in the file ?

#include<stdio.h> #include<conio.h> int upper, lower, number,special; main (argc, argv) int argc; char *argv[]; { FILE *sp; char a[200]; int line; if(argc!=2) { printf("usage is wrong"); return; } sp=fopen(argv[1], "r"); if(sp==NULL) { printf("file Opening Error"); return; } upper=lower=number=special=0; loadarray(sp,a); counttext(a); printf("upper : %d lower : %d number : %d special : %d", upper, lower, number, special); getch(); fclose(sp); } loadarray(sp,ch)

327
FILE *sp; char *ch; { for(*ch=getc(sp); *ch!=EOF ; ch++, *ch=getc(sp) ) ; *ch='\0'; } counttext(b) char *b; { upper=lower=number=special=0; for( ; *b; b++) { if(*b>='A' && *b<='Z') upper++; else if(*b>='a' && *b<='z') lower++; else if(*b>='0' && *b <='9') number++; else special++; } }
Program No : 71 : Write a program in C to count the no of lines, words and characters ?

#include<stdio.h> #include<conio.h> int line,word,characters; main (argc, argv) int argc; char *argv[]; { FILE *sp; char a[200]; if(argc!=2) { printf("usage is wrong"); return; }

328
sp=fopen(argv[1], "r"); if(sp==NULL) { printf("file Opening Error"); return; } loadarray(sp,a); counttext(a);

printf("line : %d word : %d characters : %d", line,word,characters); getch(); fclose(sp); } loadarray(sp,ch) FILE *sp; char *ch; { for(*ch=getc(sp); *ch!=EOF ; ch++, *ch=getc(sp)) ; *ch='\0';

} counttext(b) char *b; { line=word=characters=0; for( ; *b; b++) {

329
if( (*b==' ' && *(b+1) !=' ') || (*b=='\t' && *(b+1) !='\t') || (*b=='\n' && *(b+1) !='\n') ) word++; if(*b=='\n') line++; characters++; } if(*(b-1)!='\n') line++; }
Program No : 72 : Write a program in C to count the given pattern into the file ?

#include<stdio.h>

#include<conio.h> main (argc, argv) int argc; char *argv[]; { FILE *sp; char a[200],b[100]; int count; if(argc!=2) { printf("usage is wrong"); return; } sp=fopen(argv[1], "r"); if(sp==NULL) { printf("file Opening Error"); return; } printf("enter the pattern : ");

330
readtext(b); loadarray(sp,a); countpattern(a,b,&count); printf("pattern : %d",count); getch(); fclose(sp); } loadarray(sp,ch) FILE *sp; char *ch; { for(*ch=getc(sp); *ch!=EOF ; *(++ch)=getc(sp)) ; *ch='\0';

} countpattern(a,b, count) char *a,*b; int *count; { char *temp, *p; *count=0; for ( ; *a; a++) { for(temp=b, p=a ; *p==*temp && *temp!='\0'; temp++, p++); if(*temp=='\0') (*count)++; }

331
}

readtext(tem) char *tem; { fflush(stdin); for(; (*tem=getchar())!='$'; tem++); *tem='\0'; } Program No : 73 : Write a program in C to grep command in linux. (Display the patten is occur or not). ? #include<stdio.h> #include<conio.h> main (argc, argv) int argc; char *argv[]; { FILE *sp; char a[200] ; int count; if(argc!=3) { printf("usage is wrong"); return; } sp=fopen(argv[2], "r"); if(sp==NULL) { printf("file Opening Error"); return; } loadarray(sp,a);

332
countpattern(a,argv[1],&count); if(count!=0) printf("pattern exist %d", count); else printf("pattern not exist"); getch(); fclose(sp); } loadarray(sp,ch) FILE *sp; char *ch; { for(*ch=getc(sp); *ch!=EOF ; *(++ch)=getc(sp)) ; *ch='\0';

} countpattern(a,b, count) char *a,*b; int *count; { char *temp, *p; *count=0; for ( ; *a; a++) { for(temp=b, p=a ; *p==*temp && *temp!='\0'; temp++, p++); if(*temp=='\0') (*count)++; }

333
} Program No : 74 : Write a program in C to grep command in linux. (Display the line number where is pattern is occurred ). ? #include<stdio.h> #include<conio.h> main (argc, argv) int argc; char *argv[]; { FILE *sp; char a[200],b[100]; int line,count; if(argc!=3) { printf("usage is wrong"); return; } sp=fopen(argv[2], "r"); if(sp==NULL) { printf("file Opening Error"); return; }

line=0; while(loadarray(sp,a)) { countpattern(a,argv[1],&count); line++; if(count) { printf("pattern is occured at line number : %d \n",line);

334
} } countpattern(a,argv[1], &count); line++; if(count) { printf("pattern is occured at line number : %d \n",line); }

getch(); fclose(sp); } loadarray(sp,ch) FILE *sp; char *ch; { for(*ch=getc(sp); *ch!=EOF ; ch++, *ch=getc(sp)) { if(*ch=='\n') { *ch='\0'; return 1; } } *ch='\0'; return 0; } countpattern(a,b, count) char *a,*b;

335
int *count; { char *temp, *p; *count=0; for ( ; *a; a++) { for(temp=b, p=a ; *p==*temp && *temp!='\0'; temp++, p++); if(*temp=='\0') (*count)++; } } Program No : 75 : Write a program in C to grep all files command in linux. (Display the line number where is pattern is occurred ). ?

#include<stdio.h> #include<conio.h> main (argc, argv) int argc; char *argv[]; { FILE *sp; char a[200],b[100]; int line,count,j; if(argc<3) { printf("usage is wrong"); return; } for(j=2; j<argc; j++) { printf("file name for grep : %s \n", argv[j]);

336
sp=fopen(argv[j], "r"); if(sp==NULL) { printf("file Opening Error"); return; }

line=0; while(loadarray(sp,a)) { countpattern(a,argv[1],&count); line++; if(count) { printf("pattern is occured at line number : %d \n",line); } } countpattern(a,argv[1], &count); line++; if(count) { printf("pattern is occured at line number : %d \n",line); }

getch(); fclose(sp); } } loadarray(sp,ch) FILE *sp; char *ch; { for(*ch=getc(sp); *ch!=EOF ; ch++, *ch=getc(sp))

337
{ if(*ch=='\n') { *ch='\0'; return 1; } } *ch='\0'; return 0; } countpattern(a,b, count) char *a,*b; int *count; { char *temp, *p; *count=0; for ( ; *a; a++) { for(temp=b, p=a ; *p==*temp && *temp!='\0'; temp++, p++); if(*temp=='\0') (*count)++; } }

338
using structures
Program No : 1 : Write a program in C to read and display the bio-data using structure ?

#include<stdio.h> #include<conio.h> void main() { struct bio { char name[20]; int age; float sal; char address[20]; }; struct bio info,*p ; p=&info; scanf("%s%d%f%s", (*p).name, (*p).age,&(*p).sal,(*p).address); printf("%s %d %f %s",(*p).name, (*p).age,(*p).sal,(*p).address); getch(); } Program No : 2 : Write a program in C to area of circle using structure ? #include<stdio.h> #include<conio.h> void main() {

339
struct circle { float area; float radius; }; struct circle one,*p; p=&one; scanf("%f",&(*p).radius); (*p).area=3.1415 * (*p).radius * (*p).radius; printf("\n area = %f ",(*p).area); getch(); } Program No : 3 : Write a program in C to find the biggest of three numbes using structure ? #include<stdio.h> #include<conio.h> void main() { struct biggest { int a,b,c; int big; }; struct biggest num, *p; p=&num; scanf("%d%d%d",&p->a,&p->b,&p->c); // p->a , (*p).a , both are same p->big=(p->a>p->b) ? (p->a>p->c?p->a:p->c) : (p->b>p->c?p->b:p->c); printf("big %d ",p->big);

340
getch(); } Program No : 4 : Write a program in C to find the smallest of three numbes using structure ? #include<stdio.h> #include<conio.h> void main() { struct smallest { int a,b,c; int small; }; struct smallest num, *p; p=&num; scanf("%d%d%d",&p->a,&p->b,&p->c); p->small=(p->a < p->b) ? (p->a < p->c ? p->a:p->c) : (p->b < p->c ? p->b:p->c); printf("small %d ",p->small); getch(); } Program No : 5 : Write a program in C to read and display the bio-data using structure ? #include<stdio.h> #include<conio.h> void main() {

341
struct address { char street[20]; char city[20]; long int pincode; }; struct bio { char name[20]; int age; float sal; struct address place; }; struct bio info,*p; p=&info; scanf("%s%d%f%s%s%ld",p->name,&p->age,&p->sal,p->place.street, p->place.city,&p->place.pincode); printf("%s %d %f %s %s %ld",p->name,p->age,p->sal, p->place.street,p->place.city,p->place.pincode); getch(); }

Program No : 6 : Write a program in C to read and display the bio-data with date of birth using structure ?

#include<stdio.h> #include<conio.h> void main() { struct address {

342
char street[20]; char city[20]; long int pincode; }; struct date_birth { int day; int month; int year; }; struct bio { char name[20]; int age; float sal; struct date_birth *time; struct address *place; }; struct address ad; struct date_birth db; struct bio info, *p; p=&info; p->time=&db; p->place=&ad; scanf("%s%d%f%d%d%d%s%s%ld",p->name,&p->age,&p->sal, &p->time->day,&p->time->month,&p->time->year,p->place->street, p->place->city,&p->place->pincode); printf("%s %d %f %d %d %d %s %s %ld",p->name,p->age,p->sal, p->time->day,p->time->month,p->time->year,p->place->street,p->place->city, p->place->pincode); getch(); }

Program No : 7 : Write a program in C to read and display the bio-data of 10 students using structure ?

343
#include<stdio.h> #include<conio.h> void main() { struct address { char street[20]; char city[20]; long int pincode; }; struct date_birth { int day; int month; int year; }; struct bio { char name[20]; int age; float sal; struct date_birth *time; struct address *place; }; struct address ad[10]; struct date_birth db[10]; struct bio info[10], *p[10]; int i=0; for(i=0; i<10;i++) { p[i]=&info[i]; p[i]->time=&db[i]; p[i]->place=&ad[i]; scanf("%s%d%f%d%d%d%s%s%ld",p[i]->name,&p[i]->age, &p[i]->sal,&p[i]->time->day,&p[i]->time->month,&p[i]->time->year, p[i]->place->street,p[i]->place->city,&p[i]->place->pincode);

344
} for(i=0; i<10;i++) { printf("%s %d %f %d %d %d %s %s %ld\n",p[i]->name,p[i]->age,p[i]->sal, p[i]->time->day,p[i]->time->month,p[i]->time->year,p[i]->place->street, p[i]->place->city,p[i]->place->pincode); } getch(); } Program No : 8 : Write a program in C to read and display the numbers using typedef ?

#include<stdio.h> #include<conio.h> void main() { typedef int integer; integer a,b,*num1,*num2; num1=&a; num2=&b; scanf("%d%d",num1,num2); printf("\n%d %d",*num1,*num2); getch(); } Program No : 9 : Write a program in C to read and display the bio-data using typedef ?

#include<stdio.h> #include<conio.h> void main()

345
{ struct bio { char name[20]; int age; float sal; char address[20]; }; typedef struct bio BIO; BIO info, *p; p=&info; scanf("%s%d%f%s",p->name,&p->age,&p->sal,p->address); printf("%s %d %f %s",p->name,p->age,p->sal,p->address); getch(); } Program No : 10 : Write a program in C to read and display the bio-data using typedef (declaration structure) ?

#include<stdio.h> #include<conio.h> void main() { typedef struct bio { char name[20]; int age; float sal; char address[20]; }BIO; BIO info, *p;

346
p=&info; scanf("%s%d%f%s",p->name,&p->age,&p->sal,p->address); printf("%s %d %f %s",p->name,p->age,p->sal,p->address); getch(); } Program No : 11 : Write a program in C to read and display the complete bio-data using typedef (declaration structure) ? #include<stdio.h> #include<conio.h> void main() { typedef struct address { char street[20]; char city[20]; long int pincode; }ADDRESS; typedef struct date_birth { int day; int month; int year; }DOB; typedef struct bio { char name[20]; int age; float sal; DOB *time; ADDRESS *place; }BIO; ADDRESS ad; DOB db; BIO info, *p; p=&info;

347
p->time=&db; p->place=&ad; scanf("%s%d%f%d%d%d%s%s%ld",p->name,&p->age,&p->sal, &p->time->day,&p->time->month,&p->time->year,p->place->street, p->place->city,&p->place->pincode); printf("%s %d %f %d %d %d %s %s %ld",p->name,p->age,p->sal, p->time->day,p->time->month,p->time->year,p->place->street,p->place->city, p->place->pincode); getch(); } Program No : 12 : Write a program in C to read and display the complex number ? #include<stdio.h> #include<conio.h> void main() { typedef struct complex { int real,image; } COMPLEX; COMPLEX com, *p; p=&com; scanf("%d%d",&p->real,&p->image); printf("%d %d",p->real,p->image); getch(); } Program No : 13 : Write a program in C to read and display the two complex number ? #include<stdio.h> #include<conio.h>

348
void main() { typedef struct complex { int real,image; } COMPLEX; COMPLEX com1,com2, *p1,*p2; p1=&com1; p2=&com2; scanf("%d%d%d%d",&p1->real,&p1->image,&p2->real,&p2->image); printf("%d %d %d %d",p1->real,p1->image,p2->real,p2->image); getch(); } Program No : 14 : Write a program in C to read and display the sum of two complex numbers ?

#include<stdio.h> #include<conio.h> void main() { typedef struct complex { int real,image; } COMPLEX; COMPLEX com1,com2,sum, *p1,*p2,*p; p1=&com1; p2=&com2; p=

349
scanf("%d%d%d%d",&p1->real,&p1->image,&p2->real,&p2->image); p->real = p1->real + p2->real; p->image = p1->image + p2->image; printf(" sum : %d %d",p->real,p->image); getch(); } Program No : 15 : Write a program in C to read and display the difference of two complex numbers ? #include<stdio.h> #include<conio.h> void main() { typedef struct complex { int real,image; } COMPLEX; COMPLEX com1,com2,diff, *p1,*p2,*p; p1=&com1; p2=&com2; p=&diff; scanf("%d%d%d%d",&p1->real,&p1->image,&p2->real,&p2->image); p->real = p1->real - p2->real; p->image = p1->image - p2->image; printf(" diff : %d %d",p->real,p->image); getch(); }

350
Program No : 16 : Write a program in C to read and display a point of curve ? #include<stdio.h> #include<conio.h> void main() { typedef struct point { int xaxis; int yaxis; }POINT ;

POINT one, *p; p=&one; scanf("%d%d",&p->xaxis,&p->yaxis); printf("%d%d",p->xaxis,p->yaxis); getch(); } Program No : 17 : Write a program in C to distance between two points ?

#include<stdio.h> #include<conio.h> void main() { typedef struct point {

351
int xaxis; int yaxis; }POINT ;

POINT one, two, *p1,*p2; float dis=0; p1=&one; p2=&two; scanf("%d%d",&p1->xaxis,&p1->yaxis); scanf("%d%d",&p2->xaxis,&p2->yaxis); dis= sqrt((p1->xaxis-p2->xaxis)*(p1->xaxis-p2->xaxis) - (p1->yaxis-p2->yaxis)*(p1>yaxis-p2->yaxis)); printf("%f",dis); getch();
}

using polymorphism
Program No : 1 : Write a program in C++ to demonstrate the static binding concept ?

#include<iostream.h> #include<conio.h> #include<iomanip.h> class baseA { public : void display() { cout<<"BaseA"<<endl; } }; class derivedB : public baseA

352
{ public : void display() { cout<<"DerivedB"<<endl; } }; class derivedC : public derivedB { public : void display() { cout<<"DerivedC"<<endl; } }; void main(void) { baseA obja; derivedB objb; derivedC objc; baseA *ptr; cout<<"Assigning baseA"<<endl; ptr=&obja; ptr->display(); cout<<"Assigning derivedB"<<endl; ptr=&objb; ptr->display(); cout<<"Assigning derivedC"<<endl; ptr=&objc; ptr->display(); getch(); } Program No : 2 : Write a program in C++ to demonstrate the static binding concept with array of pointer objects ?

#include<iostream.h> #include<conio.h>

353
#include<iomanip.h> class baseA { public : void display() { cout<<"BaseA"<<endl; } }; class derivedB : public baseA { public : void display() { cout<<"DerivedB"<<endl; } }; class derivedC : public derivedB { public : void display() { cout<<"DerivedC"<<endl; } }; void main(void) { baseA obja; derivedB objb; derivedC objc; baseA *ptr[3]; ptr[0]=&obja; ptr[1]=&objb; ptr[2]=&objc; for(int i=0; i<3; i++) ptr[i]->display(); getch(); }

354
Program No : 3 : Write a program in C++ to demonstrate the late binding concept(Virtual functions) ?

#include<iostream.h> #include<conio.h> #include<iomanip.h> class baseA { public : virtual void display() { cout<<"BaseA"<<endl; } }; class derivedB : public baseA { public : virtual void display() { cout<<"DerivedB"<<endl; } }; class derivedC : public derivedB { public : virtual void display() { cout<<"DerivedC"<<endl; } }; void main(void) { baseA obja; derivedB objb; derivedC objc; baseA *ptr; cout<<"Assigning baseA"<<endl; ptr=&obja;

355
ptr->display(); cout<<"Assigning derivedB"<<endl; ptr=&objb; ptr->display(); cout<<"Assigning derivedC"<<endl; ptr=&objc; ptr->display(); getch(); } Program No : 4 : Write a program in C++ to demonstrate the static binding concept with array of pointer objects ? #include<iostream.h> #include<conio.h> #include<iomanip.h> class baseA { public : virtual void display() { cout<<"BaseA"<<endl; } }; class derivedB : public baseA { public : virtual void display() { cout<<"DerivedB"<<endl; } }; class derivedC : public derivedB { public : virtual void display() { cout<<"DerivedC"<<endl; }

356
}; void main(void) { baseA obja; derivedB objb; derivedC objc; baseA *ptr[3]; ptr[0]=&obja; ptr[1]=&objb; ptr[2]=&objc; for(int i=0; i<3; i++) ptr[i]->display(); getch(); } Program No : 5 : Write a program in C++ to demonstrate the pure virtual function concept. ?

#include<iostream.h> #include<conio.h> class base { public : virtual void getdata() = 0; virtual void display() = 0; }; class derived : public base { private : long int rollno; char name[20]; public : void getdata(); void display(); }; void derived::getdata() {

357
cout<<"Enter roll no and name of student"<<endl; cin>>rollno>>name; } void derived::display() { cout<<"given info of student"<<endl; cout<<"R.No : "<<rollno<<endl; cout<<"Name : "<<name<<endl; } void main(void) { base *ptr; derived obj; ptr=&obj; ptr->getdata(); ptr->display(); getch(); } Program No : 6 : Write a program in C++ to demonstrate the abstract base class ? #include<iostream.h> #include<conio.h> class base { public : virtual void getdata() = 0; virtual void display() = 0; }; class student : public base //here class base are abstract base class { private : long int rollno; char name[20]; public : void getdata(); void display(); }; class staff : public base //here class base are abstract base class { private :

358
long int empno; char name[20]; public : void getdata(); void display(); }; void student::getdata() { cout<<"Enter roll no and name of student"<<endl; cin>>rollno>>name; } void student::display() { cout<<"given info of student"<<endl; cout<<"R.No : "<<rollno<<endl; cout<<"Name : "<<name<<endl; } void staff::getdata() { cout<<"Enter emp no and name of staff"<<endl; cin>>empno>>name; } void staff::display() { cout<<"given info of staff"<<endl; cout<<"R.No : "<<empno<<endl; cout<<"Name : "<<name<<endl; } void main(void) { base *ptr; student obj1; ptr=&obj1; ptr->getdata(); ptr->display(); staff obj2; ptr=&obj2; ptr->getdata(); ptr->display();

359
getch(); }

using templates & exception


Program No : 1 : Write a program in C++ to swaping two items of varius data types ? #include<iostream.h> #include<conio.h> template <class T> T swap(T &first, T &second) { T temp; temp=first; first=second; second=temp; return (0); } void main(void) { int a, b; float fa, fb; char ca, cb; cout<<"Enter integers"<<endl; cin>>a>>b; cout<<"Enter floats"<<endl; cin>>fa>>fb; cout<<"Enter chars"<<endl; cin>>ca>>cb; cout<<"Integers Before swaping"<<endl; cout<<a<<" "<<b<<endl; swap(a,b); cout<<"Integers After swaping"<<endl; cout<<a<<" "<<b<<endl;

360
cout<<"Floats Before swaping"<<endl; cout<<fa<<" "<<fb<<endl; swap(fa,fb); cout<<"Floats After swaping"<<endl; cout<<fa<<" "<<fb<<endl; cout<<"Chars Before swaping"<<endl; cout<<ca<<" "<<cb<<endl; swap(ca,cb); cout<<"Chars After swaping"<<endl; cout<<ca<<" "<<cb<<endl; getch(); } Program No : 2 : Write a program in C++ to find the sum of array of integers and floats ?
#include<iostream.h>

#include<conio.h> template <class T> T sum(T *arr, int n) { T temp=0; for(int i=0; i<n; i++) temp = temp + arr[i]; return (temp); } template <class T> T display(T *arr, int n) { for(int i=0; i<n; i++) cout<<arr[i]<<endl; return 0; } void main(void) { static int a[]={1,2,3,4,5}; static double b[]={1.1,2.1,3.1,4.1,5.1}; int n=5 ; cout<<"Given intergers"<<endl;

361
display(a,n); cout<<"Sum Of integers : "<<sum(a,n)<<endl; cout<<"Given Doubles"<<endl; display(b,n); cout<<"Sum Of Doubles : "<<sum(b,n)<<endl; getch(); }

Program No : 3 : Write a program in C++ to find the sum of two numbers of all data types ? #include<iostream.h> #include<conio.h> template <class T> T sum(T x, T y) { T temp=0; temp=x+y; return (temp); } template <class T> void display(T x, T y) { cout<<"X : "<<x<<" Y : "<<y<<endl; } template <class T> void read(T &x, T &y) { cin>>x>>y; } void main(void) { int a, b; float fa, fb; double da, db; cout<<"Enter integers"<<endl; read(a,b); cout<<"Enter floats"<<endl; read(fa,fb); cout<<"Enter doubles"<<endl;

362
read(da, db); cout<<"Given numbers"<<endl; display(a,b); cout<<"sum integ : "<<sum(a,b)<<endl; cout<<"Given numbers"<<endl; display(fa,fb); cout<<"sum float : "<<sum(fa,fb)<<endl; cout<<"Given numbers"<<endl; display(da,db); cout<<"sum double: "<<sum(da,db)<<endl; getch(); } Program No : 4 : Write a program in C++ to read and print the value of all data types ? #include<iostream.h> #include<conio.h> template <class T> void print(T x) { cout<<x<<endl; } template <class T> void read(T &x) { cin>>x; } void main(void) { int a, b; float fa, fb; double da, db; char ca; char str[20]; cout<<"Enter integers"<<endl; read(a); cout<<"Enter floats"<<endl; read(fa); cout<<"Enter doubles"<<endl;

363
read(da); cout<<"Enter char"<<endl; read(ca); cout<<"Enter String"<<endl; read(str); cout<<"Integer : "; print(a); cout<<"float : "; print(fa); cout<<"double : "; print(da); cout<<"Integer : "; print(ca); cout<<"String : "; print(str); getch(); } Program No : 5 : Write a program in C++ to find the square value of different data types ? #include<iostream.h> #include<conio.h> template <class T> void print(T x) { cout<<x<<endl; } template <class T> void read(T &x) { cin>>x; } template <class T> T square(T x) { return (x*x); } void main(void) { int a; float fa; double da;

364
cout<<"Enter integer"<<endl; read(a); cout<<"Enter float"<<endl; read(fa); cout<<"Enter double"<<endl; read(da); cout<<"Integer : "; print(a); cout<<"it square value : "<<square(a)<<endl; cout<<"float : "; print(fa); cout<<"it square value : "<<square(fa)<<endl; cout<<"double : "; print(da); cout<<"it square value : "<<square(da)<<endl; getch(); } Program No : 6 : Write a program in C++ to read and display values of all data types ?
#include<iostream.h>

#include<conio.h> template <class T> class sample { private : T x, y; public : void read(); void print(); }; template <class T> void sample<T>::print() { cout<<"X : "<<x<<" Y : "<<y<<endl; } template <class T> void sample<T>::read() {

365
cin>>x>>y; } void main(void) { sample <int> obj1; sample <float> obj2; sample <char> obj3; cout<<"Enter Integer values : "<<endl; obj1.read(); cout<<"Enter float values : "<<endl; obj2.read(); cout<<"Enter char values : "<<endl; obj3.read(); obj1.print(); obj2.print(); obj3.print(); getch(); } Program No : 7 : Write a program in C++ to demonstrate templates with default constructor ? #include<iostream.h> #include<conio.h> template <class T> class sample { private : T x, y; public : sample(T=0) { }; void display(); };

template <class T> void sample<T>::display() { cout<<"X : "<<x<<" Y : "<<y<<endl; }

366
void main(void) { sample <int> obj1; obj1.display(); getch(); } Program No : 8 : Write a program in C++ to demonstrate templates with two arguments constructor ? #include<iostream.h> #include<conio.h> template <class T> class sample { private : T x, y; public : sample(T, T); void display(); }; template <class T> sample<T>::sample(T m, T n) { x=m; y=n; } template <class T> void sample<T>::display() { cout<<"X : "<<x<<" Y : "<<y<<endl; } void main(void) { sample <int> obj1(20,345); sample <float> obj2(34.567, 0.004); sample <char> obj3('a', 'v'); obj1.display(); obj2.display(); obj3.display();

367
getch(); } Program No : 9 : Write a program in C++ to demonstrate templates with constructor and destructor ? #include<iostream.h> #include<conio.h> template <class T> class sample { private : T x, y; public : sample(T, T); ~sample(); void display(); }; template <class T> sample<T>::sample(T m, T n) { cout<<"Object Created and intialized"<<endl; x=m; y=n; } template <class T> sample<T>::~sample() { cout<<"Object destroyed"<<endl; } template <class T> void sample<T>::display() { cout<<"X : "<<x<<" Y : "<<y<<endl; } void main(void) { void demonstrate(); demonstrate(); getch(); } void demonstrate() {

368
sample <int> obj(10,20); obj.display(); } Program No : 10 : Write a program in C++ to demonstrate exception handing of array declaration ? #include<iostream.h> #include<conio.h> void main(void) { enum {minsize=1, maxsize=1000}; char *str; int size; cout<<"Enter size of array u want to be declare"<<endl; cin>>size; try { if(size<minsize || size>maxsize) throw(size); str= new char[size]; if(str==0) throw("out of memory \n"); cout<<"memory was allocated with size of "<<size<<endl; catch(int k) { cerr<<"out of range...\n"<<endl; str=new char[maxsize]; cout<<"memory was allocated with size of "<<maxsize<<endl; } getch(); } Program No : 11 : Write a program in C++ to demonstrate exception handing of array declaration with class ?
#include<iostream.h>

#include<conio.h>

369
class sample { private : char *str; public : enum {minsize=1, maxsize=1000}; sample(); sample(int); }; sample::sample(int size) { if(size<minsize || size>maxsize) throw(size); str= new char[size]; if(str==0) throw("out of memory \n"); } void main(void) { int size; void funct(int); cout<<"Enter size of array u want to be declare"<<endl; cin>>size; funct(size); getch(); } void funct(int size) { try { sample obj(size); cout<<"memory was allocated with size of "<<size<<endl; } catch(int k) { cerr<<"out of range...\n"<<endl; funct(sample::maxsize); } }

370
using classes
Program No : 1 : Write a program in C++ to sum of two integer numbers ? #include<iostream.h> #include<conio.h> class sum { private : int a; int b; int tot; public : void read() { cout<<"Enter Two numbers"<<endl; cin>>a>>b; } void sumoftwo() { tot=a+b; } void print() { cout<<"Sum of two numbers : "; cout<<tot<<endl; } }; void main () { sum *p, obj; p=&obj; p->read(); p->sumoftwo(); p->print(); getch(); }

371
Program No : 2 : Write a program in C++ to sum of two integer numbers with assign inputs value on main ?
#include<iostream.h>

#include<conio.h> class sum { public : int a, b; int tot; void sumoftwo() { tot=a+b; } }; void main (void) { sum obj, *p; p=&obj; p->a=23; p->b=34; p->sumoftwo(); cout<<"Sum of two numbers : "<<p->tot<<endl; getch(); } Program No : 3 : Write a program in C++ to sum of two integer numbers with passing arguments from main function ?
#include<iostream.h>

#include<conio.h> class sum { public : int sumoftwo(int a, int b) { int tot; tot=a+b; return tot; } }; void main (void)

372
{ sum *obj=new sum; int m,n; cout<<"Enter numbers"<<endl; cin>>m>>n; int tot=obj->sumoftwo(m,n); cout<<"Sum of two numbers : "<<tot<<endl; getch(); } Program No : 4 : Write a program in C++ to all arithmetic operations of two integer numbers ? #include<iostream.h> #include<conio.h> class arithmetic { private : int a, b ; int su, di, mu, qu, re; public : void read() { cout<<"Enter Two numbers"<<endl; cin>>a>>b; } int sum() { su=a+b; return su; } int difference() { di=a-b; return di; } int multi() { mu=a*b; return mu; }

373
int quotient() { qu=a/b; return qu; } int remainder() { re=a%b; return re; } }; void main (void) { arithmetic obj,*p; p=&obj; p->read(); cout<<"Sum : "<<p->sum()<<endl; cout<<"Dif : "<<p->difference()<<endl; cout<<"Mul : "<<p->multi()<<endl; cout<<"Quo : "<<p->quotient()<<endl; cout<<"Rem : "<<p->remainder()<<endl; getch(); } Program No : 5 : Write a program in C++ to find the biggest of two integer numbers ?
#include<iostream.h>

#include<conio.h> class bignum { private : int a, b, big; public : void read() { cout<<"Enter Two numbers"<<endl; cin>>a>>b; } int biggest()

374
{ big=(a>b) ? a : b; return big; } }; void main (void) { bignum obj, *p; p=&obj; p->read(); cout<<"Biggest of two numbers : "<<p->biggest()<<endl; getch(); } Program No : 6 : Write a program in C++ to find the Smallest of two integer numbers ?
#include<iostream.h>

#include<conio.h> class smallnum { private : int a, b, small; public : void read() { cout<<"Enter Two numbers"<<endl; cin>>a>>b; } int smallest() { small=(a<b) ? a : b; return small; } }; void main (void) { smallnum obj, *p; p=&obj; p->read(); cout<<"small of two numbers : "<<p->smallest()<<endl; getch(); }

375
Program No : 7 : Write a program in C++ to find the area of circle ?
#include<iostream.h>

#include<conio.h> class area { private : float rad; float ar; public : void read() { cout<<"Enter Radius"<<endl; cin>>rad; } float areaofcircle() { ar= 3.1415 * rad * rad; return ar; } }; void main () { area obj, *p; p=&obj; p->read(); cout<<"Area : "<<p->areaofcircle()<<endl; getch(); } Program No : 8 : Write a program in C++ to find the volume of sphere ?
#include<iostream.h>

#include<conio.h> class volume { private : float rad; float vol; public : void read() { cout<<"Enter Radius"<<endl; cin>>rad; } float volumeofsphere() {

376
vol= 4.0/3.0 * 3.1415 * rad * rad * rad; return vol; } }; void main () { volume obj, *p; p=&obj; p->read(); float vol=p->volumeofsphere(); cout<<"Volume : "<<vol<<endl; getch(); } Program No : 9 : Write a program in C++ to find out the given number is even or odd. ?
#include<iostream.h>

#include<conio.h> class evenodd { private : int num; public : void read() { cout<<"Enter no"<<endl; cin>>num; } void evenorodd() { if(num%2==0) cout<<"Given no is even"<<endl; else cout<<"Given no is odd"<<endl; } }; void main () { evenodd *obj=new evenodd; obj->read(); obj->evenorodd(); getch();

377
} Program No : 10 : Write a program in C++ to find out the given year is leap year or not. ?
#include<iostream.h>

#include<conio.h> class leap { private : int year; public : void read() { cout<<"Enter year"<<endl; cin>>year; } void leapornot() { if(year%4==0) cout<<"Given year is leap year"<<endl; else cout<<"Given year is not a leap year"<<endl; } }; void main () { leap *obj=new leap; obj->read(); obj->leapornot(); getch(); } Program No : 11 : Write a program in C++ to print the single digit number into words. ?
#include<iostream.h>

#include<conio.h> class digit { public : void singledigit(int dig); }; void digit::singledigit(int dig)

378
{ switch(dig) { case 1 : cout<<"one"<<endl; break; case 2 : cout<<"two"<<endl; break; case 3 : cout<<"three"<<endl; break; case 4 : cout<<"four"<<endl; break; case 5 : cout<<"five"<<endl; break; case 6 : cout<<"six"<<endl; break; case 7 : cout<<"seven"<<endl; break; case 8 : cout<<"eight"<<endl; break; case 9 : cout<<"nine"<<endl; break; case 0 : cout<<"zero"<<endl; break; default: cout<<"Given number is not a single digit"<<endl; break; } } void main () { digit *obj=new digit; int num; cout<<"Enter No"<<endl; cin>>num; cout<<"given no in word format"<<endl; obj->singledigit(num); getch(); } Program No : 12 : Write a program in C++ to print 11-19 range to words ? #include<iostream.h> #include<conio.h> class digit { public : void tensrange(int dig); }; void digit::tensrange(int dig) { switch(dig) { case 11 : cout<<"eleven"<<endl; break; case 12 : cout<<"twelve"<<endl; break;

379
case 13 : cout<<"thriteen"<<endl; break; case 14 : cout<<"fourteen"<<endl; break; case 15 : cout<<"fifteen"<<endl; break; case 16 : cout<<"sixteen"<<endl; break; case 17 : cout<<"seventeen"<<endl; break; case 18 : cout<<"eighteen"<<endl; break; case 19 : cout<<"nineteen"<<endl; break; default: cout<<"Given number is tens range"<<endl; break; } } void main () { digit *obj=new digit; int num; cout<<"Enter No"<<endl; cin>>num; cout<<"given no in word format"<<endl; obj->tensrange(num); getch(); } Program No : 13 : Write a program in C++ to print the tens (10,20,..90) into words. ? #include<iostream.h> #include<conio.h> class digit { public : void tens(int dig); }; void digit::tens(int dig) { switch(dig) { case 10 : cout<<"ten"<<endl; break; case 20 : cout<<"twenty"<<endl; break; case 30 : cout<<"thrity"<<endl; break; case 40 : cout<<"fourty"<<endl; break; case 50 : cout<<"fifty"<<endl; break; case 60 : cout<<"sixty"<<endl; break; case 70 : cout<<"seventy"<<endl; break; case 80 : cout<<"eighty"<<endl; break; case 90 : cout<<"ninety"<<endl; break; default: cout<<"Given number is not a tens range"<<endl;

380
} } void main () { digit *obj=new digit; int num; cout<<"Enter No"<<endl; cin>>num; cout<<"given no in word format"<<endl; obj->tens(num); getch(); } Program No : 14 : Write a program in C++ to print the limit 0-99 into words ? #include<iostream.h> #include<conio.h> class digit { private : void tens(int); void tensrange(int); void singledigit(int); public : void twodigit(int); }; void digit::twodigit(int num) { int r,m; if (num >= 0 && num <= 9 ) singledigit(num); else if (num>=11 && num <=19) tensrange(num); else if (num%10==0) tens(num);

381
else { r=num/10 * 10; m=num-r; tens(r); cout<<" "; singledigit(m); } } void digit::singledigit(int dig) { switch(dig) { case 1 : cout<<"one"; break; case 2 : cout<<"two"; break; case 3 : cout<<"three"; break; case 4 : cout<<"four"; break; case 5 : cout<<"five"; break; case 6 : cout<<"six"; break; case 7 : cout<<"seven"; break; case 8 : cout<<"eight"; break; case 9 : cout<<"nine"; break; case 0 : cout<<"zero"; break; default: cout<<"Given number is not a single digit"<<endl; break; } } void digit::tensrange(int dig) { switch(dig) { case 11 : cout<<"eleven"; break; case 12 : cout<<"twelve"; break; case 13 : cout<<"thriteen"; break; case 14 : cout<<"fourteen"; break; case 15 : cout<<"fifteen"; break; case 16 : cout<<"sixteen"; break; case 17 : cout<<"seventeen"; break; case 18 : cout<<"eighteen"; break; case 19 : cout<<"nineteen"; break; default: cout<<"Given number is tens range"; break; } } void digit::tens(int dig) {

382
switch(dig) { case 10 : cout<<"ten"; break; case 20 : cout<<"twenty"; break; case 30 : cout<<"thrity"; break; case 40 : cout<<"fourty"; break; case 50 : cout<<"fifty"; break; case 60 : cout<<"sixty"; break; case 70 : cout<<"seventy"; break; case 80 : cout<<"eighty"; break; case 90 : cout<<"ninety"; break; default: cout<<"Given number is not a tens range"<<endl; } } void main () { digit *obj=new digit; int num; cout<<"Enter No"<<endl; cin>>num; cout<<"given no in word format"<<endl; if(num>=0 && num<=99) obj->twodigit(num); else cout<<"Given no is not a double digit"; cout< Program No : 15 : Write a program in C++ to print the limit 0-999 into words ?
#include<iostream.h>

#include<conio.h> class digit { private : void tens(int); void tensrange(int); void singledigit(int); void twodigit(int); public : void threedigit(int); }; void digit::threedigit(int num) { int temp, temp1;

383
temp=num/100; temp1=num-temp*100; if(temp>0) { singledigit(temp); cout<<" hundred "; if(temp1!=0) { cout<<"and "; twodigit(temp1); } } else twodigit(num); } void digit::twodigit(int num) { int r,m; if (num >= 0 && num <= 9 ) singledigit(num); else if (num>=11 && num <=19) tensrange(num); else if (num%10==0) tens(num); else { r=num/10 * 10; m=num-r; tens(r); cout<<" "; singledigit(m); } }

384
void digit::singledigit(int dig) { switch(dig) { case 1 : cout<<"one"; break; case 2 : cout<<"two"; break; case 3 : cout<<"three"; break; case 4 : cout<<"four"; break; case 5 : cout<<"five"; break; case 6 : cout<<"six"; break; case 7 : cout<<"seven"; break; case 8 : cout<<"eight"; break; case 9 : cout<<"nine"; break; case 0 : cout<<"zero"; break; default: cout<<"Given number is not a single digit"<<endl; break; } } void digit::tensrange(int dig) { switch(dig) { case 11 : cout<<"eleven"; break; case 12 : cout<<"twelve"; break; case 13 : cout<<"thriteen"; break; case 14 : cout<<"fourteen"; break; case 15 : cout<<"fifteen"; break; case 16 : cout<<"sixteen"; break; case 17 : cout<<"seventeen"; break; case 18 : cout<<"eighteen"; break; case 19 : cout<<"nineteen"; break; default: cout<<"Given number is tens range"; break; } } void digit::tens(int dig) { switch(dig) { case 10 : cout<<"ten"; break; case 20 : cout<<"twenty"; break; case 30 : cout<<"thrity"; break; case 40 : cout<<"fourty"; break; case 50 : cout<<"fifty"; break; case 60 : cout<<"sixty"; break; case 70 : cout<<"seventy"; break; case 80 : cout<<"eighty"; break; case 90 : cout<<"ninety"; break; default: cout<<"Given number is not a tens range"<<endl; }

385
} void main () { digit *obj=new digit; int num; cout<<"Enter No"<<endl; cin>>num; cout<<"given no in word format"<<endl; if(num>=0 && num<=999) obj->threedigit(num); else cout<<"Given no is not a double digit"; cout< Program No : 16 : Write a program in C++ to read and print the ten numbers ? #include<iostream.h> #include<conio.h> #include<iomanip.h> class number { private : int a[10]; public : void readnum(); void printnum(); }; void number::readnum() { for(int *p=a; p<a+10; p++) cin>>*p; } void number::printnum() { for(int *p=a; p<a+10; p++) cout<<setw(4)<<*p; }

386
void main () { number *obj=new number; cout<<"Enter Numbers"<<endl; obj->readnum(); cout<<"Given Numbers"<<endl; obj->printnum(); cout<<endl; getch(); } Program No : 17 : Write a program in C++ to find out the biggest of ten numbers ? #include<iostream.h> #include<conio.h> #include<iomanip.h> class number { private : int a[10]; public : void readnum(); int biggest(); }; void number::readnum() { for(int *p=a;p<a+10; p++) cin>>*p; } int number::biggest() { int big=a[0]; for(int *p=a; p<a+10; p++) big=(big<*p) ? *p : big; return big; }

387
void main () { number *obj=new number; cout<<"Enter Numbers"<<endl; obj->readnum(); cout<<"Biggest of 10 Numbers : "<<obj->biggest()<<endl; getch(); } Program No : 18 : Write a program in C++ to find out the smallest of ten numbers ? #include<iostream.h> #include<conio.h> #include<iomanip.h> class number { private : int a[10]; public : void readnum(); int smallest(); }; void number::readnum() { for(int *p=a;p<a+10; p++) cin>>*p; } int number::smallest() { int small=a[0]; for(int *p=a; p<a+10; p++) small=(small>*p) ? *p : small; return small; } void main () { number *obj=new number; cout<<"Enter Numbers"<<endl;

388
obj->readnum(); cout<<"Smallest of 10 Numbers : "<<obj->smallest()<<endl; getch(); } Program No : 19 : Write a program in C++ to find out the biggest and smallest of ten numbers. ?

#include<iostream.h> #include<conio.h> #include<iomanip.h> class number { private : int a[10]; public : void readnum(); int smallest(); int biggest(); }; void number::readnum() { for(int *p=a;p<a+10; p++) cin>>*p; } int number::biggest() { int big=a[0]; for(int *p=a; p<a+10; p++) big=(big<*p) ? *p : big; return big; } int number::smallest() { int small=a[0]; for(int *p=a; p<a+10; p++) small=(small>*p) ? *p : small; return small; }

389
void main () { number *obj=new number; cout<<"Enter Numbers"<<endl; obj->readnum(); cout<<"Biggest of 10 Numbers : "<<obj->biggest()<<endl; cout<<"Smallest of 10 Numbers : "<<obj->smallest()<<endl; getch(); }

Program No : 20 : Write a program in C++ to find out the index of biggest of ten numbers in array ?

#include<iostream.h> #include<conio.h> #include<iomanip.h> class number { private : int a[10]; public : void readnum(); int* bigindex(); }; void number::readnum() { for(int *p=a;p<a+10; p++) cin>>*p; } int* number::bigindex() { int *big=a; for(int *p=a;p<a+10; p++) big=(*big<*p) ? p : big; return big; } void main ()

390
{ number *obj=new number; cout<<"Enter Numbers"<<endl; obj->readnum(); cout<<"Array Index of Biggest of 10 Numbers : "<<(int)obj->bigindex()<<endl; getch(); } Program No : 21 : Write a program in C++ to find out the index of smallest of ten numbers in array ?

#include<iostream.h> #include<conio.h> #include<iomanip.h> class number { private : int a[10]; public : void readnum(); int* smallindex(); }; void number::readnum() { for(int *p=a;p<a+10; p++) cin>>*p; } int* number::smallindex() { int *small=a; for(int *p=a;p<a+10; p++) small=(*small>*p) ? p : small; return small; } void main ()

391
{ number *obj=new number; cout<<"Enter Numbers"<<endl; obj->readnum(); cout<<"Array Index of Smallest of 10 Numbers : "<<(int)obj->smallindex()<<endl; getch(); } Program No : 22 : Write a program in C++ to interchange the biggest and smallest of ten numbers. ?

#include<iostream.h> #include<conio.h> #include<iomanip.h> class number { private : int a[10]; public : void readnum(); int* smallindex(); int* bigindex(); void interchange(); void printnum(); }; void number::readnum() { for(int *p=a;p<a+10; p++) cin>>*p; } int* number::smallindex() { int *small=a; for(int *p=a;p<a+10; p++) small=(*small>*p) ? p : small; return small; }

392
int* number::bigindex() { int *big=a; for(int *p=a;p<a+10; p++) big=(*big<*p) ? p : big; return big; } void number::interchange() { int *bpos, *spos, temp; bpos=bigindex(); spos=smallindex(); temp=*bpos; *bpos=*spos; *spos=temp; } void number::printnum() { for(int *p=a;p<a+10; p++) cout<<setw(4)<<*p; } void main () { number *obj=new number; cout<<"Enter Numbers"<<endl; obj->readnum(); obj->interchange(); cout<<"Interchanged Array"<<endl; obj->printnum(); cout<<endl; getch(); } Program No : 23 : Write a program in C++ to merge two arrays (array size is 10). ? #include<iostream.h>

393
#include<conio.h> #include<iomanip.h> class number { private : int a[10], b[10], mer[20]; public : void readnum1(); void readnum2(); void merge(); void printnum(); }; void number::readnum1() { for(int *p=a; p<a+10; p++) cin>>*p; } void number::readnum2() { for(int *p=b; p<b+10; p++) cin>>*p; } void number::merge() { int *m=mer; for(int *p=a; p<a+10; p++, m++) *m=*p; for(p=b; p<b+10; p++, m++) *m=*p; } void number::printnum() { for(int *p=mer; p<mer+20; p++) cout<<setw(4)<<*p; } void main () { number *obj=new number; cout<<"Enter Numbers"<<endl; obj->readnum1();

394
cout<<"Enter Numbers"<<endl; obj->readnum2(); obj->merge(); cout<<"Merged Array"<<endl; obj->printnum(); cout<<endl; getch(); } Program No : 24 : Write a program in C++ to merge two arrays using separate objects ? #include<iostream.h> #include<conio.h> #include<iomanip.h> class number { private : int a[10]; public : void readnum(); void merge(number*, number*); void printnum(); }; void number::readnum() { for(int *p=a; p<a+10; p++) cin>>*p; } void number::merge(number *obj1, number *obj2) { int *m=a; for(int *p=obj1->a; p<(obj1->a+10); p++, m++) *m=*p; for(p=obj2->a; p<(obj2->a+10); p++, m++) *m=*p; }

395
void number::printnum() { for(int *p=a; p<a+20; p++) cout<<setw(4)<<*p; } void main () { number *obj=new number; number *obj1=new number; number *obj2=new number; cout<<"Enter Numbers"<<endl; obj1->readnum(); cout<<"Enter Numbers"<<endl; obj2->readnum(); obj->merge(obj1, obj2); cout<<"Merged Array"<<endl; obj->printnum(); cout<<endl; getch(); } Program No : 25 : Write a program in C++ for ascending order of ten numbers ? #include<iostream.h> #include<conio.h> #include<iomanip.h> class number { private : int a[10]; public : void readnum(); void ascend(); void printnum(); }; void number::readnum() {

396
for(int *p=a; p<a+10; p++) cin>>*p; } void number::ascend() { int temp; for(int *p=a; p<a+10; p++) for(int*q=p+1; q<a+10; q++) { if(*p>*q) { temp=*p; *p=*q; *q=temp; } } } void number::printnum() { for(int *p=a; p<a+10; p++) cout<<setw(4)<<*p; } void main () { number *obj=new number; cout<<"Enter Numbers"<<endl; obj->readnum(); obj->ascend(); cout<<"Ascended Array"<<endl; obj->printnum(); cout<<endl; getch(); } Program No : 26 : Write a program in C++ for descending order of ten numbers ? #include<iostream.h> #include<conio.h> #include<iomanip.h> class number

397
{ private : int a[10]; public : void readnum(); void descend(); void printnum(); }; void number::readnum() { for(int *p=a; p<a+10; p++) cin>>*p; } void number::descend() { int temp; for(int *p=a; p<a+10; p++) for(int*q=p+1; q<a+10; q++) { if(*p<*q) { temp=*p; *p=*q; *q=temp; } } } void number::printnum() { for(int *p=a; p<a+10; p++) cout<<setw(4)<<*p; } void main () { number *obj=new number; cout<<"Enter Numbers"<<endl; obj->readnum(); obj->descend(); cout<<"Descended Array"<<endl; obj->printnum(); cout<<endl;

398
getch(); } Program No : 27 : Write a program in C++ to read and display of N numbers ? #include<iostream.h> #include<conio.h> #include<iomanip.h> class number { private : int a[20]; int size; public : void readnum(int); void printnum(); }; void number::readnum(int n) { for(int *p=a;p<a+n; p++) cin>>*p; size=n; } void number::printnum() { for(int *p=a;p<a+size; p++) cout<<setw(4)<<*p; } void main () { number obj, *p; p=&obj; int n; cout<<"How many numbers to be read"<<endl; cin>>n; cout<<"Enter Numbers"<<endl; p->readnum(n); cout<<"Given Array"<<endl;

399
p->printnum(); cout<<endl; getch(); } Program No : 28 : Write a program in C++ to read and print the numbers which are odd and even ? #include<iostream.h> #include<conio.h> #include<iomanip.h> class number { private : int a[20]; int size; public : void readnum(int); void printevenorodd(); }; void number::readnum(int n) { for(int *p=a;p<a+n; p++) cin>>*p; size=n; } void number::printevenorodd() { for(int *p=a; p<a+size; p++) { if(*p%2==0) cout<<*p<<" is even"<<endl; else cout<<*p<<" is odd"<<endl; } } void main () { number obj, *p; p=&obj; int n; cout<<"How many numbers to be read"<<endl;

400
cin>>n; cout<<"Enter Numbers"<<endl; p->readnum(n); cout<<"Even or Odd result of Given Array"<<endl; p->printevenorodd(); cout<<endl; getch(); } Program No : 29 : Write a program in C++ to find the average of N numbers ?

#include<iostream.h> #include<conio.h> #include<iomanip.h> class number { private : int a[20]; int size; public : void readnum(int); float average(); }; void number::readnum(int n) { for(int *p=a;p<a+n; p++) cin>>*p; size=n; } float number::average() { float sum=0.0; for(int *p=a; p<a+size; p++) { sum+=*p; } return sum/size; }

401
void main () { number obj, *p; p=&obj; int n; cout<<"How many numbers to be read"<<endl; cin>>n; cout<<"Enter Numbers"<<endl; p->readnum(n); cout<<"Average : "<<p->average()<<endl; getch(); } Program No : 30 : Write a program in C++ to read and print the reverse order ?
#include<iostream.h>

#include<conio.h> #include<iomanip.h> class number { private : int a[20]; int size; public : void readnum(int); void reverse(); }; void number::readnum(int n) { for(int *p=a;p<a+n; p++) cin>>*p; size=n; } void number::reverse() { for(int *p=a+size-1; p>=a; p--) { cout<<setw(4)<<*p; } }

402
void main () { number obj, *p; p=&obj; int n; cout<<"How many numbers to be read"<<endl; cin>>n; cout<<"Enter Numbers"<<endl; p->readnum(n); cout<<"Reverse order of Numbers"<<endl; p->reverse(); cout<<endl; getch(); } Program No : 31 : Write a program in C++ to read and display a 3 x 3 Matrix ?
#include<iostream.h>

#include<conio.h> #include<iomanip.h> class matrix { private : int a[3][3]; public : void readmatrix(); void printmatrix(); }; void matrix::readmatrix() { int *p=&a[0][0]; for(int i=0;i<3; i++) for(int j=0; j<3; j++) cin>>*(p+i*3+j); } void matrix::printmatrix() { int *p=&a[0][0]; for(int i=0;i<3;i++) {

403
for (int j=0; j<3;j++) cout<<setw(3)<<*(p+i*3+j); cout<<endl; } } void main () { matrix *obj=new matrix; cout<<"Enter Matrix"<<endl; obj->readmatrix(); cout<<"Given Matrix"<<endl; obj->printmatrix(); cout<<endl; getch(); } Program No : 32 : Write a program in C++ to transpose the 3 x 3 Matrix ?
#include<iostream.h>

#include<conio.h> #include<iomanip.h> class matrix { private : int a[3][3]; public : void readmatrix(); void transpose(matrix *); void printmatrix(); }; void matrix::readmatrix() { int *p=&a[0][0]; for(int i=0;i<3; i++) for(int j=0; j<3; j++) cin>>*(p+i*3+j); } void matrix::transpose(matrix *mat) { int *p=&a[0][0]; int *pt=&mat->a[0][0]; for(int i=0;i<3; i++)

404
for(int j=0; j<3; j++) *(p+j*3+i) = *(pt+i*3+j); } void matrix::printmatrix() { int *p=&a[0][0]; for(int i=0;i<3;i++) { for (int j=0; j<3;j++) cout<<setw(3)<<*(p+i*3+j); cout<<endl; } } void main () { matrix *obj=new matrix; matrix *trans=new matrix; cout<<"Enter Matrix"<<endl; obj->readmatrix(); trans->transpose(obj); cout<<"Transpose Matrix"<<endl; trans->printmatrix(); cout<<endl; getch(); } Program No : 33 : Write a program in C++ to print the sum of two 3 x 3 Matrix ? #include<iostream.h> #include<conio.h> #include<iomanip.h> class matrix { private : int a[3][3]; public : void readmatrix(); void sumofmatrix(matrix *, matrix *); void printmatrix(); };

405
void matrix::readmatrix() { int *p=&a[0][0]; for(int i=0;i<3; i++) for(int j=0; j<3; j++) cin>>*(p+i*3+j); } void matrix::sumofmatrix(matrix *mat1, matrix *mat2) { int *p=&a[0][0]; int *pt1=&mat1->a[0][0]; int *pt2=&mat2->a[0][0]; for(int i=0;i<3; i++) for(int j=0; j<3; j++) *(p+i*3+j) = *(pt1+i*3+j) + *(pt2+i*3+j); } void matrix::printmatrix() { int *p=&a[0][0]; for(int i=0;i<3;i++) { for (int j=0; j<3;j++) cout<<setw(3)<<*(p+i*3+j); cout<<endl; } } void main () { matrix *obj1=new matrix; matrix *obj2=new matrix; matrix *sum =new matrix; cout<<"Enter Matrix"<<endl; obj1->readmatrix(); cout<<"Enter Matrix"<<endl; obj2->readmatrix(); sum->sumofmatrix(obj1,obj2); cout<<"Sum of Matrix"<<endl; sum->printmatrix(); cout<<endl; getch(); }

406
Program No : 34 : Write a program in C++ to print the product of two 3 x 3 Matrix. ?

#include<iostream.h> #include<conio.h> #include<iomanip.h> class matrix { private : int a[3][3]; public : void readmatrix(); void productofmatrix(matrix, matrix); void printmatrix(); }; void matrix::readmatrix() { for(int i=0;i<3; i++) for(int j=0; j<3; j++) cin>>a[i][j]; } void matrix::productofmatrix(matrix tem1,matrix tem2) { for(int i=0;i<3; i++) for(int j=0; j<3; j++) { a[i][j]=0; for(int k=0; k<3; k++) a[i][j] += tem1.a[i][k] * tem2.a[k][j]; } } void matrix::printmatrix() { for(int i=0;i<3;i++) { for (int j=0; j<3;j++) cout<<setw(3)<<a[i][j]; cout<<endl; } } void main () { matrix mat1,mat2, pro;

407
cout<<"Enter Matrix 1"<<endl; mat1.readmatrix(); cout<<"Enter Matrix 2"<<endl; mat2.readmatrix(); pro.productofmatrix(mat1,mat2); cout<<"product matrix"<<endl; pro.printmatrix(); cout<<endl; getch(); } Program No : 35 : Write a program in C++ to read and display a N x N Matrix ? #include<iostream.h> #include<conio.h> #include<iomanip.h> class matrix { private : int a[10][10]; int n; public : void readmatrix(int); void printmatrix(); }; void matrix::readmatrix(int m) { int *p=&a[0][0]; n=m; for(int i=0;i<n; i++) for(int j=0; j<n; j++) cin>>*(p+i*10+j); }

void matrix::printmatrix() { int *p=&a[0][0]; for(int i=0;i<n;i++) {

408
for (int j=0; j<n;j++) cout<<setw(3)<<*(p+i*10+j); cout<<endl; } } void main () { matrix *obj =new matrix; int n; cout<<"Enter N"<<endl; cin>>n; cout<<"Enter Matrix"<<endl; obj->readmatrix(n); cout<<"Given Matrix"<<endl; obj->printmatrix(); cout<<endl; getch(); } Program No : 36 : Write a program in C++ to transpose the N x N Matrix ? #include<iostream.h> #include<conio.h> #include<iomanip.h> class matrix { private : int a[10][10]; int n; public : void readmatrix(int); void transpose(matrix *); void printmatrix(); }; void matrix::readmatrix(int m) { int *p=&a[0][0]; n=m; for(int i=0;i<n; i++) for(int j=0; j<n; j++) cin>>*(p+i*10+j);

409
} void matrix::transpose(matrix *mat) { int *p=&a[0][0]; int *pt=&mat->a[0][0]; n=mat->n; for(int i=0;i<n; i++) for(int j=0; j<n; j++) *(p+j*10+i) = *(pt+i*10+j); } void matrix::printmatrix() { int *p=&a[0][0]; for(int i=0;i<n;i++) { for (int j=0; j<n;j++) cout<<setw(3)<<*(p+i*10+j); cout<<endl; } } void main () { matrix *obj =new matrix; matrix *trans =new matrix; int n; cout<<"Enter N"<<endl; cin>>n; cout<<"Enter Matrix"<<endl; obj->readmatrix(n); trans->transpose(obj); cout<<"Transpose Matrix"<<endl; trans->printmatrix(); cout<<endl; getch(); } Program No : 37 : Write a program in C++ to print the sum of two N x N Matrix. ?

#include<iostream.h> #include<conio.h> #include<iomanip.h>

410
class matrix { private : int a[10][10]; int n; public : void readmatrix(int); void sumofmatrix(matrix *, matrix *); void printmatrix(); }; void matrix::readmatrix(int m) { int *p=&a[0][0]; n=m; for(int i=0;i<n; i++) for(int j=0; j<n; j++) cin>>*(p+i*10+j); } void matrix::sumofmatrix(matrix *mat1, matrix *mat2) { int *p=&a[0][0]; int *pt1=&mat1->a[0][0]; int *pt2=&mat2->a[0][0]; n=mat1->n; for(int i=0;i<n; i++) for(int j=0; j<n; j++) *(p+i*10+j) = *(pt1+i*10+j) + *(pt2+i*10+j); } void matrix::printmatrix() { int *p=&a[0][0]; for(int i=0;i<n;i++) { for (int j=0; j<n;j++) cout<<setw(3)<<*(p+i*10+j); cout<<endl; } } void main () { matrix *obj1 =new matrix; matrix *obj2 =new matrix; matrix *sum =new matrix; int n;

411
cout<<"Enter N"<<endl; cin>>n; cout<<"Enter Matrix"<<endl; obj1->readmatrix(n); cout<<"Enter Matrix"<<endl; obj2->readmatrix(n); sum->sumofmatrix(obj1, obj2); cout<<"Sum of Matrix"<<endl; sum->printmatrix(); cout<<endl; getch(); } Program No : 38 : Write a program in C++ to print the product of two N x N Matrix. ?
#include<iostream.h>

#include<conio.h> #include<iomanip.h> class matrix { private : int a[10][10]; int n; public : void readmatrix(int); void productofmatrix(matrix *, matrix *); void printmatrix(); }; void matrix::readmatrix(int m) { int *p=&a[0][0]; n=m; for(int i=0;i<n; i++) for(int j=0; j<n; j++) cin>>*(p+i*10+j); } void matrix::productofmatrix(matrix *mat1, matrix *mat2) { int *p=&a[0][0]; int *pt1=&mat1->a[0][0]; int *pt2=&mat2->a[0][0];

412
n=mat1->n; for(int i=0;i<n; i++) for(int j=0; j<n; j++) { *(p+i*10+j) = 0; for(int k=0; k<n; k++) *(p+i*10+j) += *(pt1+i*10+k) * *(pt2+k*10+j); } } void matrix::printmatrix() { int *p=&a[0][0]; for(int i=0;i<n;i++) { for (int j=0; j<n;j++) cout<<setw(3)<<*(p+i*10+j); cout<<endl; } } void main () { matrix *obj1 =new matrix; matrix *obj2 =new matrix; matrix *pro =new matrix; int n; cout<<"Enter N"<<endl; cin>>n; cout<<"Enter Matrix"<<endl; obj1->readmatrix(n); cout<<"Enter Matrix"<<endl; obj2->readmatrix(n); pro->productofmatrix(obj1, obj2); cout<<"Product of Matrix"<<endl; pro->printmatrix(); cout<<endl; getch(); } Program No : 39 : Write a program in C++ to read and display a M x N Matrix ?
#include<iostream.h>

413
#include<conio.h> #include<iomanip.h> class matrix { private : int a[10][10]; int row, col; public : void readmatrix(int, int); void printmatrix(); }; void matrix::readmatrix(int m , int n) { int *p=&a[0][0]; row=m; col=n; for(int i=0;i<row; i++) for(int j=0; j<col; j++) cin>>*(p+i*10+j); } void matrix::printmatrix() { int *p=&a[0][0]; for(int i=0;i<row;i++) { for (int j=0; j<col;j++) cout<<setw(3)<<*(p+i*10+j); cout<<endl; } } void main () { matrix *obj =new matrix; int m,n; cout<<"Enter M & N"<<endl; cin>>m>>n; cout<<"Enter "<<m<<" x "<<n<<" matrix"<<endl; obj->readmatrix(m,n); cout<<"Given "<<m<<" x "<<n<<" matrix"<<endl; obj->printmatrix();

414
cout<<endl; getch(); } Program No : 40 : Write a program in C++ to transpose of M x N Matrix ? #include<iostream.h> #include<conio.h> #include<iomanip.h> class matrix { private : int a[10][10]; int row, col; public : void readmatrix(int, int); void transpose(matrix *); void printmatrix(); }; void matrix::readmatrix(int m , int n) { int *p=&a[0][0]; row=m; col=n; for(int i=0;i<row; i++) for(int j=0; j<col; j++) cin>>*(p+i*10+j); } void matrix::transpose(matrix *mat) { int *p=&a[0][0]; int *pt=&mat->a[0][0]; row=mat->col; col=mat->row; for(int i=0;i<mat->row; i++) for(int j=0; j<mat->col; j++) *(p+j*10+i)=*(pt+i*10+j); } void matrix::printmatrix() { int *p=&a[0][0]; for(int i=0;i<row;i++) { for (int j=0; j<col;j++)

415
cout<<setw(3)<<*(p+i*10+j); cout<<endl; } } void main () { matrix *obj =new matrix; matrix *trans =new matrix; int m,n; cout<<"Enter M & N"<<endl; cin>>m>>n; cout<<"Enter "<<m<<" x "<<n<<" matrix"<<endl; obj->readmatrix(m,n); trans->transpose(obj); cout<<"Transpose "<<n<<" x "<<m<<" matrix"<<endl; trans->printmatrix(); cout<<endl; getch(); } Program No : 41 : Write a program in C++ to print the sum of two M x N Matrix. ?
#include<iostream.h>

#include<conio.h> #include<iomanip.h> class matrix { private : int a[10][10]; int row, col; public : void readmatrix(int, int); void sumofmatrix(matrix *, matrix *); void printmatrix(); }; void matrix::readmatrix(int m , int n) { int *p=&a[0][0]; row=m; col=n; for(int i=0;i<row; i++)

416
for(int j=0; j<col; j++) cin>>*(p+i*10+j); } void matrix::sumofmatrix(matrix *mat1, matrix *mat2) { int *p=&a[0][0]; int *pt1=&mat1->a[0][0]; int *pt2=&mat2->a[0][0]; col=mat1->col; row=mat1->row; for(int i=0;i<mat1->row; i++) for(int j=0; j<mat1->col; j++) *(p+i*10+j)=*(pt1+i*10+j) + *(pt2+i*10+j); } void matrix::printmatrix() { int *p=&a[0][0]; for(int i=0;i<row;i++) { for (int j=0; j<col;j++) cout<<setw(3)<<*(p+i*10+j); cout<<endl; } } void main () { matrix *obj1 =new matrix; matrix *obj2 =new matrix; matrix *sum =new matrix; int m1,n1,m2,n2; cout<<"Enter M & N for matrix one"<<endl; cin>>m1>>n1; cout<<"Enter M & N for matrix one"<<endl; cin>>m2>>n2; if(m1!=m2 || n1!=n2) { cout<<"Iilegal values"<<endl; getch(); return; } cout<<"Enter "<<m1<<" x "<<n1<<" matrix one"<<endl; obj1->readmatrix(m1,n1);

417
cout<<"Enter "<<m2<<" x "<<n2<<" matrix two"<<endl; obj2->readmatrix(m2,n2); sum->sumofmatrix(obj1, obj2); cout<<"Sum of matrix "<<m1<<" x "<<n1<<" matrix"<<endl; sum->printmatrix(); cout<<endl; getch(); } Program No : 42 : Write a program in C++ to print the product of two M x N Matrix. ? #include<iostream.h> #include<conio.h> #include<iomanip.h> class matrix { private : int a[10][10]; int row, col; public : void readmatrix(int, int); void productofmatrix(matrix *, matrix *); void printmatrix(); }; void matrix::readmatrix(int m , int n) { int *p=&a[0][0]; row=m; col=n; for(int i=0;i<row; i++) for(int j=0; j<col; j++) cin>>*(p+i*10+j); } void matrix::productofmatrix(matrix *mat1, matrix *mat2) { int *p=&a[0][0]; int *pt1=&mat1->a[0][0]; int *pt2=&mat2->a[0][0]; col=mat2->col; row=mat1->row; for(int i=0;i<mat1->row; i++) for(int j=0; j<mat2->col; j++)

418
{ *(p+i*10+j)=0; for(int k=0; k<mat1->col; k++) *(p+i*10+j) += *(pt1+i*10+k) * *(pt2+k*10+j); } } void matrix::printmatrix() { int *p=&a[0][0]; for(int i=0;i<row;i++) { for (int j=0; j<col;j++) cout<<setw(3)<<*(p+i*10+j); cout<<endl; } } void main () { matrix *obj1 =new matrix; matrix *obj2 =new matrix; matrix *pro =new matrix; int m1,n1,m2,n2; cout<<"Enter M & N for matrix one"<<endl; cin>>m1>>n1; cout<<"Enter M & N for matrix one"<<endl; cin>>m2>>n2; if(n1!=m2) { cout<<"Iilegal values"<<endl; getch(); return; } cout<<"Enter "<<m1<<" x "<<n1<<" matrix one"<<endl; obj1->readmatrix(m1,n1); cout<<"Enter "<<m2<<" x "<<n2<<" matrix two"<<endl; obj2->readmatrix(m2,n2); pro->productofmatrix(obj1, obj2); cout<<"Product of matrix "<<m1<<" x "<<n2<<" matrix"<<endl; pro->printmatrix(); cout<<endl;

419
getch(); } Program No : 43 : Write a program in C++ to read and print the text ?
#include<iostream.h>

#include<conio.h> class text { private : char ch[100]; public : void readtext(); void printtext(); }; void text::readtext() { for(char *p=ch; (*p=cin.get())!='$'; p++); *p='\0'; } void text::printtext() { for(char *p=ch; *p ; cout.put(*p), p++); } void main (void) { text tp, *ptr; ptr=&tp; cout<<"Enter String"<<endl; ptr->readtext(); cout<<"Given String"<<endl; ptr->printtext(); cout<<endl; getch(); } Program No : 44 : Write a program in C++ to count the upper, lower, number and special character of given text ? #include<iostream.h> #include<conio.h> class text

420
{ private : char ch[100]; public : int upp, low, num, spe; void readtext(); void counttext(); void printtext(); }; void text::readtext() { for(char *p=ch; (*p=cin.get())!='$'; p++); *p='\0'; } void text::counttext() { upp=low=spe=num=0; for(char *p=ch; *p ; p++) { if(*p>='A' && *p <='Z') upp++; else if(*p>='a' && *p<='z') low++; else if(*p>='0' && *p <='9') num++; else spe++; } } void text::printtext() { for(char *p=ch; *p ; cout.put(*p), p++); } void main (void) { text tp, *ptr; ptr=&tp; cout<<"Enter String"<<endl; ptr->readtext(); cout<<"Given String"<<endl; ptr->printtext(); ptr->counttext(); cout<<"Result"<<endl; cout<<"upper : "<<ptr->upp<<endl;

421
cout<<"lower : "<<ptr->low<<endl; cout<<"number : "<<ptr->num<<endl; cout<<"special : "<<ptr->spe<<endl; getch(); } Program No : 45 : Write a program in C++ to count the no. of lines, words & characters of given text ? #include<iostream.h> #include<conio.h> class text { private : char ch[100]; public : int lines, words, chars; void readtext(); void counttext(); void printtext(); }; void text::readtext() { for(char *p=ch; (*p=cin.get())!='$'; p++); *p='\0'; } void text::counttext() { lines=words=chars=0; for(char *p=ch; *p ; p++) { if( (*p==' ' && *(p+1) !=' ') || (*p=='\t' && *(p+1) !='\t') || (*p=='\n' && *(p+1) !='\n') ) words++; if (*p=='\n') lines++; } chars=p-ch; } void text::printtext()

422
{ for(char *p=ch; *p ; cout.put(*p), p++); } void main (void) { text tp, *ptr; ptr=&tp; cout<<"Enter String"<<endl; ptr->readtext(); cout<<"Given String"<<endl; ptr->printtext(); ptr->counttext(); cout<<"Result"<<endl; cout<<"lines : "<<ptr->lines<<endl; cout<<"words : "<<ptr->words<<endl; cout<<"chars : "<<ptr->chars<<endl; getch(); } Program No : 46 : Write a program in C++ to copy one array to another array ? #include<iostream.h> #include<conio.h> class text { private : char ch[100]; public : void readtext(); void copytext(text *); void printtext(); }; void text::readtext() { for(char *p=ch; (*p=cin.get())!='$'; p++); *p='\0'; } void text::copytext(text *te) { char *pt=&te->ch[0]; for(char *p=ch; *pt ; p++, pt++) *p=*pt;

423
*p='\0'; } void text::printtext() { for(char *p=ch; *p ; cout.put(*p), p++); } void main (void) { text tp,ts, *p1, *p2; p1=&tp; p2=&ts; cout<<"Enter String"<<endl; p1->readtext(); p2->copytext(p1); cout<<"Copied String"<<endl; p2->printtext(); cout<<endl; getch(); }

Program No : 47 : Write a program in C++ to merge the two arrays ?

#include<iostream.h> #include<conio.h> class text { private : char ch[100]; public : void readtext(); void mergetext(text *, text*); void printtext(); }; void text::readtext() { for(char *p=ch; (*p=cin.get())!='$'; p++); *p='\0'; } void text::mergetext(text *tp1, text *tp2) {

424
char *pt=&tp1->ch[0]; for(char *p=ch; *pt; p++, pt++) *p=*pt; pt=&tp2->ch[0]; for( ; *pt; p++, pt++) *p=*pt; *p='\0'; } void text::printtext() { for(char *p=ch; *p ; cout.put(*p), p++); } void main (void) { text tp1,tp2,ts, *p, *p1, *p2; p1=&tp1; p2=&tp2; p=&ts; cout<<"Enter String 1 "<<endl; p1->readtext(); cout<<"Enter String 2 "<<endl; p2->readtext(); p->mergetext(p1,p2); cout<<"Merged String"<<endl; p->printtext(); cout<<endl; getch(); } Program No : 48 : Write a program in C++ to copy the left N Characters ? #include<iostream.h> #include<conio.h> class text { private : char ch[100]; public : void readtext(); void copyleftNchars(text*, int); void printtext(); };

425
void text::readtext() { for(char *p=ch; (*p=cin.get())!='$'; p++); *p='\0'; } void text::copyleftNchars(text *tp1, int n) { char *pt=&tp1->ch[0]; for(char *p=ch; *pt && (p-ch)<n; p++, pt++) *p=*pt; *p='\0'; } void text::printtext() { for(char *p=ch; *p ; cout.put(*p), p++); } void main (void) { text *tp=new text; text *ts=new text; int n; cout<<"Enter String 1 "<<endl; tp->readtext(); cout<<"how many characters copied from first onwards"<<endl; cin>>n; ts->copyleftNchars(tp,n); cout<<"Copied String"<<endl; ts->printtext(); cout<<endl; getch(); }

Program No : 49 : Write a program in C++ to copy the N Characters from M th position onwards ?

#include<iostream.h>

426
#include<conio.h> class text { private : char ch[100]; public : void readtext(); void copyleftMtoNchars(text*, int, int); void printtext(); }; void text::readtext() { for(char *p=ch; (*p=cin.get())!='$'; p++); *p='\0'; } void text::copyleftMtoNchars(text *tp1, int m, int n) { char *pt=&tp1->ch[0]+m-1; for(char *p=ch; *pt && (p-ch)<n; p++, pt++) *p=*pt; *p='\0'; } void text::printtext() { for(char *p=ch; *p ; cout.put(*p), p++); } void main (void) { text *tp=new text; text *ts=new text; int m,n; cout<<"Enter String 1 "<<endl; tp->readtext(); cout<<"Enter M th Position and how many characters "<<endl; cin>>m>>n; ts->copyleftMtoNchars(tp,m,n); cout<<"Copied String"<<endl; ts->printtext();

427
cout<<endl; getch(); }

Program No : 50 : Write a program in C++ to find the length of given text or array ?
#include<iostream.h>

#include<conio.h> class text { private : char ch[100]; int len; public : void readtext(); int length(); void printtext(); }; void text::readtext() { for(char *p=ch; (*p=cin.get())!='$'; p++); *p='\0'; } int text::length() { for(char *p=ch; *p; p++); len=p-ch; return len; } void text::printtext() { for(char *p=ch; *p ; cout.put(*p), p++); } void main (void) { text *tp=new text; cout<<"Enter String "<<endl; tp->readtext(); cout<<"Length of String : "<<tp->length()<<endl; getch(); }

428
Program No : 51 : Write a program in C++ to copy the last N characters ? #include<iostream.h> #include<conio.h> class text { private : char ch[100]; int len; public : void readtext(); int length(); void copylastNcharacters(text*, int); void printtext(); }; void text::readtext() { for(char *p=ch; (*p=cin.get())!='$'; p++); *p='\0'; } void text::copylastNcharacters(text *tp, int n) { char *pt; pt=&tp->ch[0]+tp->length()-n; for(char *p=ch; *pt; pt++, p++) *p=*pt; *p='\0'; } int text::length() { for(char *p=ch; *p; p++); len=p-ch; return len; } void text::printtext() { for(char *p=ch; *p ; cout.put(*p), p++); } void main (void) { text *tp=new text;

429
text *ts=new text; int n; cout<<"Enter String "<<endl; tp->readtext(); cout<<"Enter N"<<endl; cin>>n; cout<<"Copied String "<<endl; ts->copylastNcharacters(tp,n); ts->printtext(); cout<<endl; getch(); } Program No : 52 : Write a program in C++ to print the reverse of given array ?

#include<iostream.h> #include<conio.h> class text { private : char ch[100]; int len; public : void readtext(); int length(); void reverse(text*); void printtext(); }; void text::readtext() { for(char *p=ch; (*p=cin.get())!='$'; p++); *p='\0'; } void text::reverse(text *tp) { char *pt; pt=&tp->ch[0]+tp->length()-1; for(char *p=ch; pt>=&tp->ch[0]; pt--, p++) *p=*pt; *p='\0'; }

430
int text::length() { for(char *p=ch; *p; p++); len=p-ch; return len; } void text::printtext() { for(char *p=ch; *p ; cout.put(*p), p++); } void main (void) { text *tp=new text; text *ts=new text; cout<<"Enter String "<<endl; tp->readtext(); cout<<"Reverse of array "<<endl; ts->reverse(tp); ts->printtext(); cout<<endl; getch(); }

Program No : 53 : Write a program in C++ to merge three arrays ?

#include<iostream.h> #include<conio.h> #include<stdio.h> class text { private : char ch[100]; int len; public : text(){ ch[0]='\0';};

431
void readtext(); int length(); void merge(text*); void printtext(); }; void text::readtext() { fflush(stdin); for(char *p=ch; (*p=cin.get())!='$'; p++); *p='\0'; } void text::merge(text *tp) { char *pt=&tp->ch[0]; for(char *p=ch+length(); *pt; pt++, p++) *p=*pt; *p='\0'; } int text::length() { for(char *p=ch; *p; p++); len=p-ch; return len; } void text::printtext() { for(char *p=ch; *p ; cout.put(*p), p++); } void main (void) { text *tp1=new text; text *tp2=new text; text *tp3=new text; text *ts=new text; cout<<"Enter String "<<endl; tp1->readtext(); cout<<"Enter String "<<endl; tp2->readtext(); cout<<"Enter String "<<endl; tp3->readtext();

432
cout<<"Merged String"<<endl; ts->merge(tp1); ts->merge(tp2); ts->merge(tp3); ts->printtext(); cout<<endl; getch(); } Program No : 54 : Write a program in C++ to convert upper case to lower case letters ?
#include<iostream.h>

#include<conio.h> #include<stdio.h> class text { private : char ch[100]; public : void readtext(); void conversion(text*); void printtext(); }; void text::readtext() { fflush(stdin); for(char *p=ch; (*p=cin.get())!='$'; p++); *p='\0'; } void text::conversion(text *tp) { char *pt=&tp->ch[0]; for(char *p=ch; *pt; pt++, p++) { *p = (*pt>='A'&& *pt<='Z') ? *pt+32 : *pt; } *p='\0'; } void text::printtext() { for(char *p=ch; *p ; cout.put(*p), p++); }

433
void main (void) { text *tp=new text; text *ts=new text; cout<<"Enter String "<<endl; tp->readtext(); cout<<"Converted Text"<<endl; ts->conversion(tp); ts->printtext(); cout<<endl; getch(); } Program No : 55 : Write a program in C++ to convert lower case to upper case letters ?
#include<iostream.h>

#include<conio.h> #include<stdio.h> class text { private : char ch[100]; public : void readtext(); void conversion(text*); void printtext(); }; void text::readtext() { fflush(stdin); for(char *p=ch; (*p=cin.get())!='$'; p++); *p='\0'; } void text::conversion(text *tp) { char *pt=&tp->ch[0]; for(char *p=ch; *pt; pt++, p++) { *p = (*pt>='a'&& *pt<='z') ? *pt-32 : *pt; } *p='\0';

434
} void text::printtext() { for(char *p=ch; *p ; cout.put(*p), p++); } void main (void) { text *tp=new text; text *ts=new text; cout<<"Enter String "<<endl; tp->readtext(); cout<<"Converted Text"<<endl; ts->conversion(tp); ts->printtext(); cout<<endl; getch(); } Program No : 56 : Write a program in C++ to replace comma instead of semicolon ?

#include<iostream.h> #include<conio.h> #include<stdio.h> class text { private : char ch[100]; public : void readtext(); void conversion(text*); void printtext(); }; void text::readtext() { fflush(stdin); for(char *p=ch; (*p=cin.get())!='$'; p++); *p='\0'; }

435
void text::conversion(text *tp) { char *pt=&tp->ch[0]; for(char *p=ch; *pt; pt++, p++) { *p = (*pt==';') ? ',' : *pt; } *p='\0'; } void text::printtext() { for(char *p=ch; *p ; cout.put(*p), p++); } void main (void) { text *tp=new text; text *ts=new text; cout<<"Enter String "<<endl; tp->readtext(); cout<<"Converted Text"<<endl; ts->conversion(tp); ts->printtext(); cout<<endl; getch(); } Program No : 57 : Write a program in C++ to replace semicolon instead of comma ? #include<iostream.h> #include<conio.h> #include<stdio.h> class text { private : char ch[100]; public : void readtext(); void conversion(text*); void printtext(); };

436
void text::readtext() { fflush(stdin); for(char *p=ch; (*p=cin.get())!='$'; p++); *p='\0'; } void text::conversion(text *tp) { char *pt=&tp->ch[0]; for(char *p=ch; *pt; pt++, p++) { *p = (*pt==',') ? ';' : *pt; } *p='\0'; } void text::printtext() { for(char *p=ch; *p ; cout.put(*p), p++); } void main (void) { text *tp=new text; text *ts=new text; cout<<"Enter String "<<endl; tp->readtext(); cout<<"Converted Text"<<endl; ts->conversion(tp); ts->printtext(); cout<<endl; getch(); } Program No : 58 : Write a program in C++ to delete the comma in given string ? #include<iostream.h> #include<conio.h> #include<stdio.h> class text { private :

437
char ch[100]; public : void readtext(); void conversion(text*); void printtext(); }; void text::readtext() { fflush(stdin); for(char *p=ch; (*p=cin.get())!='$'; p++); *p='\0'; } void text::conversion(text *tp) { char *pt=&tp->ch[0]; for(char *p=ch; *pt; pt++) { if(*pt!=',') *p++=*pt; } *p='\0'; } void text::printtext() { for(char *p=ch; *p ; cout.put(*p), p++); } void main (void) { text *tp=new text; text *ts=new text; cout<<"Enter String "<<endl; tp->readtext(); cout<<"Converted Text"<<endl; ts->conversion(tp); ts->printtext(); cout<<endl; getch(); }

Program No : 59 : Write a program in C++ to store the first position of every new line. ?

438
#include<iostream.h> #include<conio.h> #include<stdio.h> class text { private : char ch[100]; public : char *pos[10]; void readtext(); void storeposition(); void printtext(); }; void text::readtext() { fflush(stdin); for(char *p=ch; (*p=cin.get())!='$'; p++); *p='\0'; } void text::storeposition() { int j=0; pos[j++]=ch; for(char *p=ch; *p; p++) { if(*p=='\n') pos[j++]=p+1; } pos[j]=NULL; } void text::printtext() { for(char *p=ch; *p ; cout.put(*p), p++); } void main (void) { text *tp=new text; int j=1;

439
cout<<"Enter String "<<endl; tp->readtext(); tp->storeposition(); cout<<"Position : "<<endl; for(char **p=&tp->pos[0]; *p!=NULL ; p++,j++) cout<<j<<" line : "<<(int)*p<<endl; getch(); } Program No : 60 : Write a program in C++ to print the Nth line ? #include<iostream.h> #include<conio.h> #include<stdio.h> class text { private : char ch[100]; public : char *pos[10]; void readtext(); void storeposition(); void printNthline(int); void printtext(); }; void text::readtext() { fflush(stdin); for(char *p=ch; (*p=cin.get())!='$'; p++); *p='\0'; } void text::printNthline(int n) { storeposition() ; for(char *p=pos[n-1]; *p && *p!='\n'; p++) cout.put(*p); } void text::storeposition()

440
{ int j=0; pos[j++]=ch; for(char *p=ch; *p; p++) { if(*p=='\n') pos[j++]=p+1; } pos[j]=NULL; } void text::printtext() { for(char *p=ch; *p ; cout.put(*p), p++); } void main (void) { text *tp=new text; int n; cout<<"Enter String "<<endl; tp->readtext(); cout<<"Enter line number to print"<<endl; cin>>n; cout<<n<<"th line"<<endl; tp->printNthline(n); cout<<endl; getch(); } Program No : 61 : Write a program in C++ to print the Mth line to Nth line ?

#include<iostream.h> #include<conio.h> #include<stdio.h> class text { private : char ch[100]; public : char *pos[10]; void readtext(); void storeposition(); void printMtoNthline(int,int); void printtext();

441
}; void text::readtext() { fflush(stdin); for(char *p=ch; (*p=cin.get())!='$'; p++); *p='\0'; } void text::printMtoNthline(int m,int n) { storeposition() ; for(char *p=pos[m-1]; *p && p<pos[n]; p++) cout.put(*p); } void text::storeposition() { int j=0; pos[j++]=ch; for(char *p=ch; *p; p++) { if(*p=='\n') pos[j++]=p+1; } pos[j]=NULL; } void text::printtext() { for(char *p=ch; *p ; cout.put(*p), p++); } void main (void) { text *tp=new text; int m,n; cout<<"Enter String "<<endl; tp->readtext(); cout<<"Enter line ranges to print"<<endl; cin>>m>>n; cout<<m<<" to "<<n<<"th line"<<endl; tp->printMtoNthline(m,n); cout<<endl; getch(); }

442
Program No : 62 : Write a program in C++ to count the no of patterns exist on given string ? #include<iostream.h> #include<conio.h> #include<stdio.h> class text { private : char ch[100]; public : void readtext(); int countpattern(text*); }; void text::readtext() { char *p=ch; fflush(stdin); for(cin>>*p; *p!='$'; cin>>*(++p)); *p='\0'; } int text::countpattern(text *pat) { int count=0; for(char *p=ch; *p; p++) { for(char *pt=&pat->ch[0], *pk=p; *pt==*pk && *pt!='\0'; pt++, pk++); if(*pt=='\0') count++; } return count; } void main (void) { text *tp=new text; text *pat=new text; cout<<"Enter String "<<endl; tp->readtext(); cout<<"Enter pattern "<<endl;

443
pat->readtext(); cout<<"No of Pat : "<<tp->countpattern(pat)<<endl; cout<<endl; getch(); } Program No : 63 : Write a program in C++ to delete the pattern ? #include<iostream.h> #include<conio.h> #include<stdio.h> class text { private : char ch[100]; public : void readtext(); int deletepattern(text*, text*); void printtext(); }; void text::readtext() { char *p=ch; fflush(stdin); for(cin>>*p; *p!='$'; cin>>*(++p)); *p='\0'; } int text::deletepattern(text *pat, text *mod) { int count=0; char *pd=&mod->ch[0]; for(char *p=ch; *p; p++) { for(char *pt=&pat->ch[0], *pk=p; *pt==*pk && *pt!='\0'; pt++, pk++); if(*pt=='\0') { count++; p=pk-1; } else *pd++=*p; }

444
*pd='\0'; return count; } void text::printtext() { for(char *p=ch; *p ; cout.put(*p), p++); } void main (void) { text *tp=new text; text *pat=new text; text *mod=new text; cout<<"Enter String "<<endl; tp->readtext(); cout<<"Enter pattern "<<endl; pat->readtext(); tp->deletepattern(pat,mod); cout<<"Modified Pattern"<<endl; mod->printtext(); cout<<endl; getch(); } Program No : 64 : Write a program in C++ to replace the given pattern ? #include<iostream.h> #include<conio.h> #include<stdio.h> class text { private : char ch[100]; public : void readtext(); int replacepattern(text*, text*, text*); void printtext(); }; void text::readtext() { char *p=ch; fflush(stdin);

445
for(cin>>*p; *p!='$'; cin>>*(++p)); *p='\0'; } int text::replacepattern(text *pat, text *re, text *mod) { int count=0; char *pd=&mod->ch[0]; for(char *p=ch; *p; p++) { for(char *pt=&pat->ch[0], *pk=p; *pt==*pk && *pt!='\0'; pt++, pk++); if(*pt=='\0') { count++; p=pk-1; for(char *r=&re->ch[0]; *r; r++, pd++) *pd=*r; } else *pd++=*p; } *pd='\0'; return count; } void text::printtext() { for(char *p=ch; *p ; cout.put(*p), p++); } void main (void) { text *tp=new text; text *pat=new text; text *rep=new text; text *mod=new text; cout<<"Enter String "<<endl; tp->readtext(); cout<<"Enter pattern "<<endl; pat->readtext(); cout<<"Enter new pattern "<<endl; rep->readtext(); tp->replacepattern(pat,rep,mod); cout<<"Modified Pattern"<<endl; mod->printtext();

446
cout<<endl; getch(); } Program No : 65 : Write a program in C++ to read text and store into file ? #include<iostream.h> #include<conio.h> #include<fstream.h> class file { private : char ch; public : void readcontent(fstream *); }; void file::readcontent(fstream *out) { char *p=&ch; for(*p=cin.get(); *p!='$'; *p=cin.get()) out->put(*p); } void main (int argc, char *argv[]) { fstream *out=new fstream; file obj, *p; p=&obj; if(argc!=2) { cout<<"usage is wrong"<<endl; getch(); return; } out->open(argv[1], ios::out); if(out->fail()) { cout<<"file Opening Error"<<endl; getch(); return; }

447
p->readcontent(out); out->close(); cout<<"information stored"<<endl; getch(); } Program No : 66 : Write a program in C++ to read text from file and print to screen ? #include<iostream.h> #include<conio.h> #include<fstream.h> class file { private : char ch; public : void displaycontent(fstream *); }; void file::displaycontent(fstream *in) { char *p=&ch; for(*p=in->get(); !in->eof(); *p=in->get()) cout.put(*p); } void main (int argc, char *argv[]) { fstream *in=new fstream; file obj, *p; p=&obj; if(argc!=2) { cout<<"usage is wrong"<<endl; getch(); return; } in->open(argv[1], ios::in); if(in->fail()) {

448
cout<<"file Opening Error"<<endl; getch(); return; } p->displaycontent(in); in->close(); cout<<"information printed"<<endl; getch(); } Program No : 67 : Write a program in C++ to cat all files. (cat command in unix) ? #include<iostream.h> #include<conio.h> #include<fstream.h> class file { private : char ch; public : void displaycontent(fstream *); }; void file::displaycontent(fstream *in) { char *p=&ch; for(*p=in->get(); !in->eof(); *p=in->get()) cout.put(*p); } void main (int argc, char *argv[]) { fstream *in=new fstream; file obj, *p; p=&obj; if(argc<2) { cout<<"usage is wrong"<<endl; getch(); return; }

449
for(int i=1; i<argc; i++) { cout<<argv[i]<<" contents"<<endl; in->open(argv[i], ios::in); if(in->fail()) { cout<<"file Opening Error"<<endl; getch(); return; } p->displaycontent(in); in->close(); cout<<endl; getch(); } } Program No : 68 : Write a program in C++ to copy one file into another file (copy command) ?

#include<iostream.h> #include<conio.h> #include<fstream.h> class file { private : char ch; public : void copycontent(fstream *, fstream *); }; void file::copycontent(fstream *in, fstream *out) { char *p=&ch; for(*p=in->get(); !in->eof(); *p=in->get()) out->put(*p); } void main (int argc, char *argv[])

450
{ fstream *in=new fstream; fstream *out=new fstream; file obj, *p; p=&obj; if(argc!=3) { cout<<"usage is wrong"<<endl; getch(); return; } in->open(argv[1], ios::in); out->open(argv[2], ios::out); if(in->fail() || out->fail()) { cout<<"file Opening Error"<<endl; getch(); return; } p->copycontent(in,out); in->close(); out->close(); cout<<"File Copied"<<endl; getch(); } Program No : 69 : Write a program in C++ to merge two files ?

#include<iostream.h> #include<conio.h> #include<fstream.h> class file { private : char ch; public : void mergecontent(fstream *, fstream *); };

451
void file::mergecontent(fstream *in, fstream *out) { char *p=&ch; for(*p=in->get(); !in->eof(); *p=in->get()) out->put(*p); } void main (int argc, char *argv[]) { fstream *in1=new fstream; fstream *in2=new fstream; fstream *out=new fstream; file obj, *p; p=&obj; if(argc!=4) { cout<<"usage is wrong"<<endl; getch(); return; } in1->open(argv[1], ios::in); in2->open(argv[2], ios::in); out->open(argv[3], ios::out); if(in1->fail() || in2->fail() || out->fail()) { cout<<"file Opening Error"<<endl; getch(); return; } p->mergecontent(in1,out); p->mergecontent(in2,out); in1->close(); in2->close(); out->close(); cout<<"File Merged"<<endl; getch(); }

452
Program No : 70 : Write a program in C++ to print the entire file using array ? #include<iostream.h> #include<conio.h> #include<fstream.h> class file { private : char ch[500]; public : void storecontent(fstream*); void printfile(); }; void file::storecontent(fstream *in) { char *p=&ch[0]; for(*p=in->get(); !in->eof(); *(++p)=in->get()); *p='\0'; } void file::printfile() { for(char *p=ch; *p; cout.put(*p++)); } void main (int argc, char *argv[]) { fstream *in=new fstream; file obj, *p; p=&obj; if(argc!=2) { cout<<"usage is wrong"<<endl; getch(); return; } in->open(argv[1], ios::in); if(in->fail()) { cout<<"file Opening Error"<<endl;

453
getch(); return; } p->storecontent(in); in->close(); p->printfile(); cout<<endl; getch(); } Program No : 71 : Write a program in C++ to print the entire file along the line number ? #include<iostream.h> #include<conio.h> #include<fstream.h> class file { private : char ch[512]; int storecontent(fstream *); public : void printline(fstream *); }; int file::storecontent(fstream *in) { char *p=ch; while(!in->eof()) { *p=in->get(); if(*p=='\n') { *(++p)='\0'; return 1; } p++; } *p='\0'; return 0; }

454
void file::printline(fstream *in) { int line=0; while(storecontent(in)) { line++; cout<<line<<" : "<<ch; } line++; cout<<line<<" : "<<ch; } main (int argc, char *argv[]) { fstream *in=new fstream; file obj, *p; p=&obj; if(argc!=2) { cout<<"usage is wrong"<<endl; getch(); return; } in->open(argv[1], ios::in); if(in->fail()) { cout<<"file Opening Error"<<endl; getch(); return; } p->printline(in); in->close(); cout<<endl; getch(); } Program No : 72 : Write a program in C++ to print the entire file on page by page along the line number ? #include<iostream.h>

455
#include<conio.h> #include<fstream.h> class file { private : char ch[512]; int storecontent(fstream *); public : void printline(fstream *); }; int file::storecontent(fstream *in) { char *p=ch; while(!in->eof()) { *p=in->get(); if(*p=='\n') { *p='\0'; return 1; } p++; } *p='\0'; return 0; } void file::printline(fstream *in) { int line=0; while(storecontent(in)) { line++; cout<<line<<" : "<<ch<<endl; if(line%24==0) getch(); } line++; cout<<line<<" : "<<ch; } main (int argc, char *argv[]) { fstream *in=new fstream; file obj, *p;

456
p=&obj; if(argc!=2) { cout<<"usage is wrong"<<endl; getch(); return; } in->open(argv[1], ios::in); if(in->fail()) { cout<<"file Opening Error"<<endl; getch(); return; } p->printline(in); in->close(); cout<<endl; getch(); } Program No : 73 : Write a program in C++ to count the no of upper, lower and special charactes in the file ?

#include<iostream.h> #include<conio.h> #include<fstream.h> class file { private : char ch[512]; int storecontent(fstream *); public : void printline(fstream *); void countcharacters(); }; int file::storecontent(fstream *in) { char *p=ch; while(!in->eof())

457
{ *p=in->get(); if(*p=='\n') { *p='\0'; return 1; } p++; } *p='\0'; return 0; } void file::printline(fstream *in) { int line=0; while(storecontent(in)) { line++; cout<<line<<" : "<<ch<<endl; countcharacters(); } line++; cout<<line<<" : "<<ch; } void file::countcharacters() { int upp, low, spe, num; upp=low=spe=num=0; for(char *p=ch; *p ; p++) { if(*p>='A' && *p <='Z') upp++; else if(*p>='a' && *p <='z') low++; else if(*p>='0' && *p <='9') num++; else spe++; } cout<<"upper : "<<upp<<endl; cout<<"lower : "<<low<<endl; cout<<"number : "<<num<<endl; cout<<"special : "<<spe<<endl; }

458
main (int argc, char *argv[]) { fstream *in=new fstream; file obj, *p; p=&obj; if(argc!=2) { cout<<"usage is wrong"<<endl; getch(); return; } in->open(argv[1], ios::in); if(in->fail()) { cout<<"file Opening Error"<<endl; getch(); return; } p->printline(in); in->close(); cout<<endl; getch(); } Program No : 74 : Write a program in C++ to count the no of lines, words and characters ? #include<iostream.h> #include<conio.h> #include<fstream.h> class file { private : char ch[512]; void storecontent(fstream *); public : void countcharacters(fstream *); }; void file::storecontent(fstream *in)

459
{ char *p=ch; while(!in->eof()) { *p=in->get(); p++; } *p='\0'; } void file::countcharacters(fstream *in) { int lines,chars,words ; storecontent(in); lines=chars=words=0; for(char *p=ch; *p; p++) { if ((*p=='\n' && *(p+1)!='\n') || (*p=='\t' && *(p+1)!='\t') || (*p==' ' && *(p+1)!=' ') ) words++; if(*p=='\n') lines++; } chars=p-ch; cout<<ch<<endl; cout<<"lines : "<<lines<<endl; cout<<"words : "<<words<<endl; cout<<"chars : "<<chars<<endl; } main (int argc, char *argv[]) { fstream *in=new fstream; file obj, *p; p=&obj; if(argc!=2) { cout<<"usage is wrong"<<endl; getch(); return; } in->open(argv[1], ios::in);

460
if(in->fail()) { cout<<"file Opening Error"<<endl; getch(); return; } p->countcharacters(in); in->close(); cout<<endl; getch(); } Program No : 75 : Write a program in C++ to count the given pattern into the file ? #include<iostream.h> #include<conio.h> #include<fstream.h> #include<stdio.h> class file { private : char ch[512]; void storecontent(fstream *); public : int countpattern(fstream *, char *); void readtext(char *); }; void file::readtext(char *p) { fflush(stdin); for(cin>>*p; *p!='$'; cin>>*(++p));; *p='\0'; } void file::storecontent(fstream *in) { char *p=ch; while(!in->eof()) { *p=in->get(); p++;

461
} *p='\0'; } int file::countpattern(fstream *in, char *pat) { int count=0; storecontent(in); for(char *p=ch; *p; p++) { for(char *pt=p, *pk=pat; *pt==*pk && *pk!='\0'; pk++, pt++); if(*pk=='\0') { count++; p=pt; } } return count; }

main (int argc, char *argv[]) { fstream *in=new fstream; file obj, *p; char pat[20]; p=&obj; if(argc!=2) { cout<<"usage is wrong"<<endl; getch(); return; } in->open(argv[1], ios::in); if(in->fail()) { cout<<"file Opening Error"<<endl; getch(); return;

462
} cout<<"Enter Pattern "<<endl; p->readtext(pat); int count=p->countpattern(in,pat); cout<<"No of patterns : "<<count<<endl; in->close(); cout<<endl; getch(); } Program No : 76 : Write a program in C++ to grep command in linux. (Display the patten is occur or not) ? #include<iostream.h> #include<conio.h> #include<fstream.h> #include<stdio.h> class file { private : char ch[512]; void storecontent(fstream *); public : int countpattern(fstream *, char *); void readtext(char *); }; void file::readtext(char *p) { fflush(stdin); for(cin>>*p; *p!='$'; cin>>*(++p));; *p='\0'; } void file::storecontent(fstream *in) { char *p=ch; while(!in->eof()) { *p=in->get(); p++; }

463
*p='\0'; } int file::countpattern(fstream *in, char *pat) { int count=0; storecontent(in); for(char *p=ch; *p; p++) { for(char *pt=p, *pk=pat; *pt==*pk && *pk!='\0'; pk++, pt++); if(*pk=='\0') { count++; p=pt; } } return count; }

main (int argc, char *argv[]) { fstream *in=new fstream; file obj, *p; char pat[20]; p=&obj; if(argc!=2) { cout<<"usage is wrong"<<endl; getch(); return; } in->open(argv[1], ios::in); if(in->fail()) { cout<<"file Opening Error"<<endl; getch(); return; }

464
cout<<"Enter Pattern "<<endl; p->readtext(pat); int count=p->countpattern(in,pat); if (count==0) cout<<"pattern does not exist"<<endl; else cout<<"pattern exist "<<count<<" times"<<endl; in->close(); cout<<endl; getch(); } Program No : 77 : Write a program in C++ to read the biodata and store into file ? #include<iostream.h> #include<conio.h> #include<stdio.h> #include<fstream.h> class biodata { private : char name[20]; int age; float salary; char address[20]; public : void readbio(); void printbio(); }; void biodata::readbio() { cout<<"Enter name, age, sal, add"<<endl; cin>>name>>age>>salary>>address; } void biodata::printbio() { cout<<"Given name, age, sal, add"<<endl; cout<<name<<" "<<age<<" "<<salary<<" "<<address<<endl; }

465
void main(int argc, char *argv[]) { fstream *out=new fstream; biodata user, *p; p=&user; if(argc!=2) { cout<<"In valid no of cmd line arguments"<<endl; getch(); return; } out->open(argv[1], ios::out); if(out->fail()) { cout<<"file opening error"<<endl; getch(); return; }

p->readbio(); out->write((char *)&user, sizeof(biodata)); out->close(); cout<<"Biodata Stored"<<endl; getch(); } Program No : 78 : Write a program in C++ to read the biodata from file and display into screen ?

#include<iostream.h> #include<conio.h> #include<stdio.h> #include<fstream.h> class biodata { private :

466
char name[20]; int age; float salary; char address[20]; public : void readbio(); void printbio(); }; void biodata::readbio() { cout<<"Enter name, age, sal, add"<<endl; cin>>name>>age>>salary>>address; } void biodata::printbio() { cout<<"Given name, age, sal, add"<<endl; cout<<name<<" "<<age<<" "<<salary<<" "<<address<<endl; }

void main(int argc, char *argv[]) { fstream *in=new fstream; biodata user, *p; p=&user; if(argc!=2) { cout<<"In valid no of cmd line arguments"<<endl; getch(); return; } in->open(argv[1], ios::in); if(in->fail()) { cout<<"file opening error"<<endl; getch(); return; } in->read((char *)p, sizeof(biodata)); p->printbio();

467
in->close(); cout<<"Biodata Displayed"<<endl; getch(); } Program No : 79 : Write a program in C++ to read the biodata until $ is given for name using command line arguments ? #include<iostream.h> #include<conio.h> #include<stdio.h> #include<fstream.h> class biodata { private : char name[20]; int age; float salary; char address[20]; public : bool readbio(); void printbio(); }; bool biodata::readbio() { cout<<"Enter name"<<endl; cin>>name; if(name[0]!='$') { cout<<"Enter age, sal, add"<<endl; cin>>age>>salary>>address; return true; } return false; } void biodata::printbio() { cout<<"Given name, age, sal, add"<<endl; cout<<name<<" "<<age<<" "<<salary<<" "<<address<<endl; }

void main(int argc, char *argv[])

468
{ fstream *out=new fstream; biodata *user=new biodata; if(argc!=2) { cout<<"In valid no of cmd line arguments"<<endl; getch(); return; } out->open(argv[1], ios::out); if(out->fail()) { cout<<"file opening error"<<endl; getch(); return; } while(user->readbio()) out->write((char *)user, sizeof(biodata)); out->close(); cout<<"Biodata stored"<<endl; getch(); }

Program No : 80 : Write a program in C++ to read the biodata from file using command line arguments ?
#include<iostream.h>

#include<conio.h> #include<stdio.h> #include<fstream.h> class biodata { private : char name[20]; int age;

469
float salary; char address[20]; public : bool readbio(); void printbio(); }; bool biodata::readbio() { cout<<"Enter name"<<endl; cin>>name; if(name[0]!='$') { cout<<"Enter age, sal, add"<<endl; cin>>age>>salary>>address; return true; } return false; } void biodata::printbio() { cout<<"Given name, age, sal, add"<<endl; cout<<name<<" "<<age<<" "<<salary<<" "<<address<<endl; }

void main(int argc, char *argv[]) { fstream *in=new fstream; biodata *user=new biodata; if(argc!=2) { cout<<"In valid no of cmd line arguments"<<endl; getch(); return; } in->open(argv[1], ios::in); if(in->fail()) { cout<<"file opening error"<<endl;

470
getch(); return; } while(!in->eof()) { in->read((char *)user, sizeof(biodata)); user->printbio(); } in->close(); cout<<"Biodata displayed"<<endl; getch(); } Program No : 81 : Write a program in C++ to read the bio-data from file and calculate hra and da ?

#include<iostream.h> #include<conio.h> #include<stdio.h> #include<fstream.h> class biodata { private : char name[20]; int age; float salary; char address[20]; void calculate(); public : bool readbio(); void printbio(); }; bool biodata::readbio() { cout<<"Enter name"<<endl; cin>>name; if(name[0]!='$') { cout<<"Enter age, sal, add"<<endl; cin>>age>>salary>>address;

471
return true; } return false; } void biodata::calculate() { float hra, da, tax,net; hra=salary * 0.10; tax=salary * 0.10; da=250; net=salary+hra+da-tax; cout<<"da : "<<da<<endl; cout<<"hra : "<<hra<<endl; cout<<"tax : "<<tax<<endl; cout<<"net : "<<net<<endl; } void biodata::printbio() { cout<<"Given name, age, sal, add"<<endl; cout<<name<<" "<<age<<" "<<salary<<" "<<address<<endl; calculate(); }

void main(int argc, char *argv[]) { fstream *in=new fstream; biodata *user=new biodata; if(argc!=2) { cout<<"In valid no of cmd line arguments"<<endl; getch(); return; } in->open(argv[1], ios::in); if(in->fail()) { cout<<"file opening error"<<endl; getch(); return;

472
} while(!in->eof()) { in->read((char *)user, sizeof(biodata)); user->printbio(); } in->close(); cout<<"Biodata displayed"<<endl; getch(); } Program No : 82 : Write a program in C++ to find the area of Circle without input ?

#include<iostream.h> #include<conio.h> class area { private : float rad; float ar; public : area() { rad=2.0; } float areaofcircle() { ar= 3.1415 * rad * rad; return ar; } }; void main () { area obj, *p; p=&obj; float ar=p->areaofcircle(); cout<<"Area : "<<ar<<endl; getch(); }

473
Program No : 83 : Write a program in C++ to find the area of Circle with passing input via constructor ? #include<iostream.h> #include<conio.h> class area { private : float rad; float ar; public : area(float r) { rad=r; } float areaofcircle() { ar= 3.1415 * rad * rad; return ar; } }; void main () { area obj(2.31), *p; p=&obj; float ar=p->areaofcircle(); cout<<"Area : "<<ar<<endl; getch(); } Program No : 84 : Write a program in C++ to print the Fibonacci series using constructor ? #include<iostream.h> #include<conio.h> #include<iomanip.h> class fibonacci { private : unsigned long int f0, f1, fib; public : fibonacci();

474
void increment(); void display(); }; fibonacci::fibonacci() { f0=0; f1=1; fib=f0+f1; } void fibonacci::increment() { f0=f1; f1=fib; fib=f0+f1; } void fibonacci::display() { cout<<setw(4)<<fib<<'\t'; } void main (void) { fibonacci obj, *p; p=&obj; cout<<"Fibonacci seris of 15 numbers"<<endl; for(int i=1; i<=15; i++) { p->display(); p->increment(); } cout<<endl; getch(); } Program No : 85 : Write a program in C++ to initialize the biodata ?

#include<iostream.h> #include<conio.h> #include<iomanip.h> class biodata { private : char name[20];

475
int age; float sal; char address[20]; public : biodata(); void display(); }; biodata::biodata() { name[0]='\0'; age=0; sal=0.00; address[0]='\0'; } void biodata::display() { cout<<"name = "<<name<<endl; cout<<"age = "<<age<<endl; cout<<"sal = "<<sal<<endl; cout<<"add = "<<address<<endl; } void main (void) { biodata *obj=new biodata; obj->display(); getch(); } Program No : 86 : write a program in C++ to read value and clone the object using copy constructor ? #include<iostream.h> #include<conio.h> #include<iomanip.h> class number { private : int num; public : number(); number(number *); void read(); void display();

476
}; number::number() { } number::number(number *obj) { num=obj->num; } void number::read() { cout<<"Enter Number"<<endl; cin>>num; } void number::display() { cout<<num<<endl; } void main (void) { number obj, *p; p=&obj; p->read(); number newobj(p), *t; t=&newobj; cout<<"GIVEN NUMBER via second object"<<endl; t->display(); getch(); } Program No : 87 : write a program in C++ to clone the biodata using copy constructor ?

#include<iostream.h> #include<conio.h> #include<iomanip.h> class biodata { private : char name[20]; int age;

477
float sal; char address[20]; public : biodata(); biodata(biodata *); void read(); void display(); }; biodata::biodata() { name[0]='\0'; age=0; sal=0.00; address[0]='\0'; } biodata::biodata(biodata *ptr) { for(char *pt=&ptr->name[0], *pc=name; *pt; pt++, pc++) *pc=*pt; *pc='\0'; age=ptr->age; sal=ptr->sal; for(pt=&ptr->address[0], pc=address; *pt; pt++, pc++) *pc=*pt; *pc='\0'; } void biodata::read() { cout<<"name "<<endl; cin>>name; cout<<"age "<<endl; cin>>age; cout<<"sal "<<endl; cin>>sal; cout<<"add "<<endl; cin>>address; } void biodata::display() { cout<<"name = "<<name<<endl; cout<<"age = "<<age<<endl; cout<<"sal = "<<sal<<endl; cout<<"add = "<<address<<endl; }

478
void main (void) { biodata obj1, *p; p=&obj1; cout<<"Read values via first obj"<<endl; p->read(); biodata obj2(p), *pt; pt=&obj2; cout<<"Display values via second obj"<<endl; pt->display(); getch(); } Program No : 88 : Write a program in C++ to demonstrate destructor function. ? #include<iostream.h> #include<conio.h> #include<iomanip.h> class biodata { private : char name[20]; int age; float sal; char address[20]; public : biodata(); ~biodata(); void read(); void display(); }; biodata::biodata() { name[0]='\0'; age=0; sal=0.00; address[0]='\0'; cout<<"Object Created"<<endl; } biodata::~biodata() { cout<<"Object Destroyed"<<endl; } void biodata::read() {

479
cout<<"name "<<endl; cin>>name; cout<<"age "<<endl; cin>>age; cout<<"sal "<<endl; cin>>sal; cout<<"add "<<endl; cin>>address; } void biodata::display() { cout<<"name = "<<name<<endl; cout<<"age = "<<age<<endl; cout<<"sal = "<<sal<<endl; cout<<"add = "<<address<<endl; } void demonstrate(); //prototype declaration void main (void) { demonstrate(); getch(); } void demonstrate() //function definition { biodata *obj1=new biodata; obj1->read(); obj1->display(); } Program No : 89 : Write a program in C++ to demonstrate static members ? #include<iostream.h> #include<conio.h> #include<iomanip.h> class number { private : static int num; public : number(); static void display(); };

480
number::number() { num++; }

void number::display() { cout<<"Objects created : "<<num<<endl; } int number::num=10; void main (void) { cout<<"Before Object creation : "<<endl; number::display(); number *obj1=new number; number *obj2=new number; number *obj3=new number; // static variable common for all objects. cout<<"After Object creation : "<<endl; number::display(); getch(); } Program No : 90 : Write a program in C++ to print factorial value of 1 to 15 using static members ?

#include<iostream.h> #include<conio.h> #include<iomanip.h> class factorial { private : static int fact; public : factorial(); static void calculate(int); }; void factorial::calculate(int n) {

481
fact=fact*n; cout<<n<<" : "<<fact<<endl; } int factorial::fact=1; void main (void) { for(int i=1; i<=13; i++) factorial::calculate(i); getch(); } Program No : 91 : Write a program in C++ to read and display given number ?

#include<iostream.h> #include<conio.h> #include<iomanip.h> class number { private : int num; public : void readnum(); void printnum(); }; inline void number::readnum() { cout<<"Enter number"<<endl; cin>>num; } inline void number::printnum() { cout<<"Given number"<<endl; cout<<num<<endl; } void main (void) { number *obj=new number; obj->readnum(); obj->printnum(); getch(); }

482
Program No : 92 : Write a program in C++ to perform all arithmetic operations ? #include<iostream.h> #include<conio.h> #include<iomanip.h> class number { private : int x,y; public : void readnum(); int sum(); int diff(); int mult(); int quot(); int remin(); void printinfo(); }; inline void number::readnum() { cout<<"Enter number"<<endl; cin>>x>>y; } inline void number::printinfo() { cout<<"Given numbers"<<endl; cout<<x<<" "<<y<<endl; cout<<"Sum : "<<sum()<<endl; cout<<"Diff : "<<diff()<<endl; cout<<"Mult : "<<mult()<<endl; cout<<"quot : "<<quot()<<endl; cout<<"remin : "<<remin()<<endl; } inline int number::sum() { return (x+y); } inline int number::diff() { return (x-y); }

483
inline int number::mult() { return (x*y); } inline int number::quot() { return (x/y); } inline int number::remin() { return (x%y); } void main (void) { number *obj=new number; obj->readnum(); obj->printinfo(); getch(); }

Program No : 93 : Write a program in C++ to read and display given number ?

#include<iostream.h> #include<conio.h> #include<iomanip.h> class number { private : int num; public : void readnum(); friend void printnum(number *); }; inline void number::readnum() { cout<<"Enter number"<<endl; cin>>num; }

484
void printnum(number *ob) { cout<<"Given number"<<endl; cout<<ob->num<<endl; } void main (void) { number obj, *p; p=&obj; p->readnum(); printnum(p); getch(); } Program No : 94 : Write a program in C++ to compare number value between two objects ?

#include<iostream.h> #include<conio.h> #include<iomanip.h> class number1; class number2 { friend class number1; private : int num; public : void readnum(); friend void compareobjects(number1 *, number2 *); }; class number1 { friend class number2; private : int num; public : void readnum(); friend void compareobjects(number1 *, number2 *); }; inline void number1::readnum()

485
{ cout<<"Enter number"<<endl; cin>>num; } inline void number2::readnum() { cout<<"Enter number"<<endl; cin>>num; } void compareobjects(number1 *obj1, number2 *obj2) { cout<<"Given number of first obj : "<<obj1->num<<endl; cout<<"Given number of second obj : "<<obj2->num<<endl; if(obj1->num>obj2->num) cout<<"obj1 num value is biggest"<<endl; else if(obj1->num<obj2->num) cout<<"obj2 num value is biggest"<<endl; else cout<<"Equal"<<endl; } void main (void) { number1 obj1,*p1; number2 obj2, *p2; p1=&obj1; p2=&obj2; p1->readnum(); p2->readnum(); compareobjects(p1,p2); getch(); }

using inheritance

486
Program No : 1 : Write a program in C++ to sum of two integer numbers ?

#include<iostream.h> #include<conio.h> class numbers { public : int a, b; void read(); void print(); }; class sum : public numbers { private : int tot; public: void sumoftwo(); void print(); }; void numbers::read() { cout<<"Enter Two numbers"<<endl; cin>>a>>b; } void sum::print() { numbers::print(); cout<<"Sum of two numbers : "; cout<<tot<<endl; } void numbers::print() { cout<<"Given numbers : "; cout<<a<<" "<<b<<endl; } void sum::sumoftwo() {

487
tot=a+b; } void main () { sum obj, *p; p=&obj; p->read(); p->sumoftwo(); p->print(); getch(); } Program No : 2 : Write a program in C++ to sum of two integer numbers with assign inputs value on main ?

#include<iostream.h> #include<conio.h> class numbers { public : int a, b; void print(); };

class sum : public numbers { private : int tot; public: sum(int, int); void sumoftwo(); void print(); }; sum::sum(int x, int y) { a=x; b=y; }

488
void sum::print() { numbers::print(); cout<<"Sum of two numbers : "; cout<<tot<<endl; } void numbers::print() { cout<<"Given numbers : "; cout<<a<<" "<<b<<endl; } void sum::sumoftwo() { tot=a+b; } void main () { sum obj(20,30), *p; p=&obj; p->sumoftwo(); p->print(); getch(); } Program No : 3 : Write a program in C++ to all arithmetic operations of two integer numbers ?

#include<iostream.h> #include<conio.h> class numbers { public : int a, b; void read(); void print(); }; class arith : public numbers { private : int sum(); int diff();

489
int mult(); int quot(); int remin(); public: void printalloperations(); }; void numbers::read() { cout<<"Enter Two numbers"<<endl; cin>>a>>b; } void numbers::print() { cout<<"Given numbers : "; cout<<a<<" "<<b<<endl; } int arith::sum() { return (a+b); } int arith::diff() { return (a-b); } int arith::mult() { return (a*b); } int arith::quot() { return (a/b); } int arith::remin() { return (a%b); } void arith::printalloperations() { print();

490
cout<<"Sum : "<<sum()<<endl; cout<<"Diff : "<<diff()<<endl; cout<<"Mult : "<<mult()<<endl; cout<<"quot : "<<quot()<<endl; cout<<"remin : "<<remin()<<endl; } void main () { arith obj, *p; p=&obj; p->read(); p->printalloperations(); getch(); } Program No : 4 : Write a program in C++ to find the biggest and smallest of two integer numbers ?

#include<iostream.h> #include<conio.h> class numbers { public : int a, b,c; void read(); void print(); }; class compare : public numbers { private : int big, small; void biggest(); void smallest(); public: void comparenumbers(); void print(); }; void numbers::read() { cout<<"Enter Three numbers"<<endl; cin>>a>>b>>c;

491
} void numbers::print() { cout<<"Given numbers : "; cout<<a<<" "<<b<<" "<<c<<endl; } void compare::biggest() { big = (a > b) ? ( a > c ? a : c ) : ( b > c ? b : c ); } void compare::smallest() { small = (a < b) ? ( a < c ? a : c ) : ( b < c ? b : c ); } void compare::print() { numbers::print(); cout<<"Big : "<<big<<endl; cout<<"Small : "<<small<<endl; } void compare::comparenumbers() { biggest(); smallest(); } void main () { compare obj, *p; p=&obj; p->read(); p->comparenumbers(); p->print(); getch(); } Program No : 5 : Write a program in C++ to find the area of circle and volume of sphere ? #include<iostream.h> #include<conio.h>

492
class base { public : float radius; void read(); void print(); }; class circle : public base { private : float area; public: void read(); void calculate(); void print(); }; class sphere : public base { private : float vol; public: void read(); void calculate(); void print(); }; void base::read() { cin>>radius; } void base::print() { cout<<"Radius : "<<radius<<endl; } void circle::read() { base::read(); } void circle::calculate() { area= 3.1415 * radius * radius; }

493
void circle::print() { base::print(); cout<<"area of circle : "<<area<<endl; } void sphere::read() { base::read(); } void sphere::calculate() { vol= 4.0/3.0 * 3.1415 * radius * radius * radius; } void sphere::print() { base::print(); cout<<"vol of sphere : "<<vol<<endl; } void main () { circle cir, *p1; sphere sph, *p2; p1=&cir; cout<<"Enter Radius of circle"<<endl; p1->read(); p2=&sph; cout<<"Enter Radius of sphere"<<endl; p2->read(); p1->calculate(); p2->calculate(); cout<<"Area of circle"<<endl; p1->print(); cout<<"Vol of Sphere"<<endl; p2->print(); getch(); }

494
Program No : 6 : Write a program in C++ to find out the biggest and smallest of ten numbers ? #include<iostream.h> #include<conio.h> #include<iomanip.h> class numbers { protected : int a[10]; void read(); void print(); }; class compare : public numbers { private : int big, small; public: void read(); void calculate(); void print(); }; void numbers::read() { cout<<"Enter Numbers"<<endl; for(int *p=a; p<a+10; p++) cin>>*p; } void numbers::print() { cout<<"Given Numbers"<<endl; for(int *p=a; p<a+10; p++) cout<<setw(4)<<*p; cout<<endl; } void compare::calculate() { big=a[0]; small=a[0]; for(int *p=a; p<a+10; p++) {

495
big = (*p>big) ? *p : big; small = (*p<small) ? *p : small; } } void compare::read() { numbers::read(); } void compare::print() { numbers::print(); cout<<"Big : "<<big<<endl; cout<<"small : "<<small<<endl; } void main () { compare obj, *p; p=&obj; p->read(); p->calculate(); p->print(); getch(); } Program No : 7 : Write a program in C++ to transpose the 3 x 3 Matrix ? #include<iostream.h> #include<conio.h> #include<iomanip.h> class matrix { protected : int a[3][3]; void read(); void print(); }; class transpose:protected matrix { private : int tr[3][3]; public : void read();

496
void trans(); void print(); }; void matrix::read() { int *p=&a[0][0]; cout<<"Enter Matrix 3 x 3"<<endl; for(int i=0; i<3; i++) for(int j=0; j<3; j++) cin>>*(p+i*3+j); } void matrix::print() { int *p=&a[0][0]; cout<<"given Matrix"<<endl; for(int i=0;i<3;i++) { for (int j=0; j<3;j++) cout<<setw(3)<<*(p+i*3+j); cout<<endl; } } void transpose::trans() { int *p=&a[0][0]; int *pr=&tr[0][0]; for(int i=0;i<3; i++) for(int j=0; j<3; j++) *(pr+j*3+i)=*(p+i*3+j); } void transpose::read() { matrix::read(); } void transpose::print() { int *pr=&tr[0][0]; matrix::print(); cout<<"Transpose Matrix"<<endl; for(int i=0;i<3;i++) { for (int j=0; j<3;j++) cout<<setw(3)<<*(pr+i*3+j);

497
cout<<endl; } } void main () { transpose mat, *p; p=&mat; p->read(); p->trans(); p->print(); cout<<endl; getch(); } Program No : 8 : Write a program in C++ to print the sum of two 3 x 3 Matrix ? #include<iostream.h> #include<conio.h> #include<iomanip.h> class matrixone { protected : int a[3][3]; void read(); void print(); }; class matrixtwo { protected : int a[3][3]; void read(); void print(); }; class sum:protected matrixone, matrixtwo { private : int sm[3][3]; public : void read();

498
void sumofmatrix(); void print(); }; void matrixone::read() { int *p=&a[0][0]; cout<<"Enter Matrix 3 x 3"<<endl; for(int i=0; i<3; i++) for(int j=0; j<3; j++) cin>>*(p+i*3+j); } void matrixone::print() { int *p=&a[0][0]; cout<<"given Matrix one"<<endl; for(int i=0;i<3;i++) { for (int j=0; j<3;j++) cout<<setw(3)<<*(p+i*3+j); cout<<endl; } } void matrixtwo::read() { int *p=&a[0][0]; cout<<"Enter Matrix 3 x 3"<<endl; for(int i=0; i<3; i++) for(int j=0; j<3; j++) cin>>*(p+i*3+j); } void matrixtwo::print() { int *p=&a[0][0]; cout<<"given Matrix two"<<endl; for(int i=0;i<3;i++) { for (int j=0; j<3;j++) cout<<setw(3)<<*(p+i*3+j); cout<<endl; } }

499
void sum::sumofmatrix() { int *p1, *p2, *pm; p1=&matrixone::a[0][0]; p2=&matrixtwo::a[0][0]; pm=&sm[0][0]; for(int i=0;i<3; i++) for(int j=0; j<3; j++) *(pm+i*3+j)=*(p1+i*3+j) + *(p2+i*3+j); } void sum::read() { matrixone::read(); matrixtwo::read(); } void sum::print() { matrixone::print(); matrixtwo::print(); cout<<"Sum Matrix"<<endl; int *p=&sm[0][0]; for(int i=0;i<3;i++) { for (int j=0; j<3;j++) cout<<setw(3)<<*(p+i*3+j); cout<<endl; } } void main () { sum mat, *p; p=&mat; p->read(); p->sumofmatrix(); p->print(); cout<<endl; getch(); }

500
Program No : 9 : Write a program in C++ to print the product of two 3 x 3 Matrix ? #include<iostream.h> #include<conio.h> #include<iomanip.h> class matrixone { protected : int a[3][3]; void read(); void print(); }; class matrixtwo { protected : int a[3][3]; void read(); void print(); }; class product:protected matrixone, matrixtwo { private : int pro[3][3]; public : void read(); void productofmatrix(); void print(); }; void matrixone::read() { int *p=&a[0][0]; cout<<"Enter Matrix 3 x 3"<<endl; for(int i=0; i<3; i++) for(int j=0; j<3; j++) cin>>*(p+i*3+j); } void matrixone::print() { int *p=&a[0][0]; cout<<"given Matrix one"<<endl; for(int i=0;i<3;i++) {

501
for (int j=0; j<3;j++) cout<<setw(3)<<*(p+i*3+j); cout<<endl; } } void matrixtwo::read() { int *p=&a[0][0]; cout<<"Enter Matrix 3 x 3"<<endl; for(int i=0; i<3; i++) for(int j=0; j<3; j++) cin>>*(p+i*3+j); } void matrixtwo::print() { int *p=&a[0][0]; cout<<"given Matrix two"<<endl; for(int i=0;i<3;i++) { for (int j=0; j<3;j++) cout<<setw(3)<<*(p+i*3+j); cout<<endl; } } void product::productofmatrix() { int *p1, *p2, *pm; p1=&matrixone::a[0][0]; p2=&matrixtwo::a[0][0]; pm=&pro[0][0]; for(int i=0;i<3; i++) for(int j=0; j<3; j++) { *(pm+i*3+j)=0; for(int k=0; k<3; k++) *(pm+i*3+j) += *(p1+i*3+k) * *(p2+k*3+j); } } void product::read() { matrixone::read(); matrixtwo::read();

502
} void product::print() { matrixone::print(); matrixtwo::print(); cout<<"Product Matrix"<<endl; int *p=&pro[0][0]; for(int i=0;i<3;i++) { for (int j=0; j<3;j++) cout<<setw(3)<<*(p+i*3+j); cout<<endl; } } void main () { product mat, *p; p=&mat; p->read(); p->productofmatrix(); p->print(); cout<<endl; getch(); } Program No : 10 : Write a program in C++ to transpose the N x N Matrix ?

#include<iostream.h> #include<conio.h> #include<iomanip.h> class matrix { protected : int a[10][10]; int row,col; void read(); void print(); };

503
class transpose:protected matrix { private : int tr[10][10]; public : transpose(int n); void read(); void trans(); void print(); }; transpose::transpose(int n) { matrix::row=matrix::col=n; } void matrix::read() { int *p=&a[0][0]; cout<<"Enter Matrix "<<row<<" x "<<col<<endl; for(int i=0;i<row; i++) for(int j=0; j<col; j++) cin>>*(p+i*10+j); } void matrix::print() { int *p=&a[0][0]; cout<<"given Matrix "<<row<<" x "<<col<<endl; for(int i=0;i<row;i++) { for (int j=0; j<col;j++) cout<<setw(3)<<*(p+i*10+j); cout<<endl; } } void transpose::trans() { int *p=&a[0][0]; int *pr=&tr[0][0]; for(int i=0;i<row; i++) for(int j=0; j<col; j++) *(pr+j*10+i)=*(p+i*10+j); } void transpose::read()

504
{ matrix::read(); } void transpose::print() { int *pr=&tr[0][0]; matrix::print(); cout<<"Transpose Matrix "<<row<<" x "<<col<<endl; for(int i=0;i<row;i++) { for (int j=0; j<col;j++) cout<<setw(3)<<*(pr+i*10+j); cout<<endl; } } void main () { int n; cout<<"Enter N value : "<<endl; cin>>n; transpose mat(n), *p; p=&mat; p->read(); p->trans(); p->print(); cout<<endl; getch(); } Program No : 11 : Write a program in C++ to print the sum of two N x N Matrix ?

#include<iostream.h> #include<conio.h> #include<iomanip.h> class matrixone

505
{ protected : int a[10][10]; int row,col; void read(); void print(); }; class matrixtwo { protected : int a[10][10]; int row,col; void read(); void print(); }; class sum:protected matrixone, matrixtwo { private : int sm[10][10]; public : sum(int n); int row, col; void read(); void sumofmatrix(); void print(); }; sum::sum(int n) { row=col=n; matrixone::row=matrixone::col=n; matrixtwo::row=matrixtwo::col=n; } void matrixone::read() { int *p=&a[0][0]; cout<<"Enter Matrix one : "<<row<<" x "<<col<<endl; for(int i=0;i<row; i++) for(int j=0; j<col; j++) cin>>*(p+i*10+j); } void matrixone::print() { int *p=&a[0][0]; cout<<"given Matrix one : "<<row<<" x "<<col<<endl;

506
for(int i=0;i<row;i++) { for (int j=0; j<col;j++) cout<<setw(3)<<*(p+i*10+j); cout<<endl; } } void matrixtwo::read() { int *p=&a[0][0]; cout<<"Enter Matrix two : "<<row<<" x "<<col<<endl; for(int i=0;i<row; i++) for(int j=0; j<col; j++) cin>>*(p+i*10+j); } void matrixtwo::print() { int *p=&a[0][0]; cout<<"given Matrix two : "<<row<<" x "<<col<<endl; for(int i=0;i<row;i++) { for (int j=0; j<col;j++) cout<<setw(3)<<*(p+i*10+j); cout<<endl; } } void sum::sumofmatrix() { int *p1=&matrixone::a[0][0]; int *p2=&matrixtwo::a[0][0]; int *pm=&sm[0][0]; for(int i=0;i<row; i++) for(int j=0; j<col; j++) *(pm+i*10+j)=*(p1+i*10+j) + *(p2+i*10+j); } void sum::read() { matrixone::read(); matrixtwo::read(); } void sum::print() {

507
int *pm=&sm[0][0]; matrixone::print(); matrixtwo::print(); cout<<"Sum Matrix "<<row<<" x "<<col<<endl; for(int i=0;i<row;i++) { for (int j=0; j<col;j++) cout<<setw(3)<<*(pm+i*10+j); cout<<endl; } } void main () { int n; cout<<"Enter N value : "<< Program No : 12 : Write a program in C++ to print the product of two N x N Matrix ? #include<iostream.h> #include<conio.h> #include<iomanip.h> class matrixone { protected : int a[10][10]; int row,col; void read(); void print(); }; class matrixtwo { protected : int a[10][10]; int row,col; void read(); void print(); }; class product:protected matrixone, matrixtwo { private : int pro[10][10];

508
public : product(int n); int row, col; void read(); void productofmatrix(); void print(); }; product::product(int n) { row=col=n; matrixone::row=matrixone::col=n; matrixtwo::row=matrixtwo::col=n; } void matrixone::read() { int *p=&a[0][0]; cout<<"Enter Matrix one : "<<row<<" x "<<col<<endl; for(int i=0;i<row; i++) for(int j=0; j<col; j++) cin>>*(p+i*10+j); } void matrixone::print() { int *p=&a[0][0]; cout<<"given Matrix one : "<<row<<" x "<<col<<endl; for(int i=0;i<row;i++) { for (int j=0; j<col;j++) cout<<setw(3)<<*(p+i*10+j); cout<<endl; } } void matrixtwo::read() { int *p=&a[0][0]; cout<<"Enter Matrix two : "<<row<<" x "<<col<<endl; for(int i=0;i<row; i++) for(int j=0; j<col; j++) cin>>*(p+i*10+j); } void matrixtwo::print() { int *p=&a[0][0]; cout<<"given Matrix two : "<<row<<" x "<<col<<endl;

509
for(int i=0;i<row;i++) { for (int j=0; j<col;j++) cout<<setw(3)<<*(p+i*10+j); cout<<endl; } } void product::productofmatrix() { int *p1=&matrixone::a[0][0]; int *p2=&matrixtwo::a[0][0]; int *pm=&pro[0][0]; for(int i=0;i<row; i++) for(int j=0; j<col; j++) { *(pm+i*10+j)=0; for(int k=0; k<row; k++) *(pm+i*10+j) += *(p1+i*10+k) * *(p2+k*10+j); } } void product::read() { matrixone::read(); matrixtwo::read(); } void product::print() { int *pm=&pro[0][0]; matrixone::print(); matrixtwo::print(); cout<<"Product Matrix "<<row<<" x "<<col<<endl; for(int i=0;i<row;i++) { for (int j=0; j<col;j++) cout<<setw(3)<<*(pm+i*10+j); cout<<endl; } } void main () { int n; cout<<"Enter N value : "<<

510
Program No : 13 : Write a program in C++ to transpose of M x N Matrix. ? #include<iostream.h> #include<conio.h> #include<iomanip.h> class matrix { protected : int a[10][10]; int row,col; void read(); void print(); }; class transpose:protected matrix { private : int tr[10][10]; public : transpose(int m, int n); void read(); void trans(); void print(); }; transpose::transpose(int m, int n) { matrix::row=m; matrix::col=n; } void matrix::read() { int *p=&a[0][0]; cout<<"Enter Matrix "<<row<<" x "<<col<<endl; for(int i=0;i<row; i++) for(int j=0; j<col; j++) cin>>*(p+i*10+j); } void matrix::print() { int *p=&a[0][0];

511
cout<<"given Matrix "<<row<<" x "<<col<<endl; for(int i=0;i<row;i++) { for (int j=0; j<col;j++) cout<<setw(3)<<*(p+i*10+j); cout<<endl; } } void transpose::trans() { int *p=&a[0][0]; int *pr=&tr[0][0]; for(int i=0;i<row; i++) for(int j=0; j<col; j++) *(pr+j*10+i)=*(p+i*10+j); } void transpose::read() { matrix::read(); } void transpose::print() { int *pr=&tr[0][0]; matrix::print(); cout<<"Transpose Matrix "<<col<<" x "<<row<<endl; for(int i=0;i<col;i++) { for (int j=0; j<row;j++) cout<<setw(3)<<*(pr+i*10+j); cout<<endl; } } void main () { int m,n; cout<<"Enter M & N value : "<<endl; cin>>m>>n; transpose mat(m,n), *p; p=&mat;

512
p->read(); p->trans(); p->print(); cout<<endl; getch(); } Program No : 14 : Write a program in C++ to print the sum of two M x N Matrix ? #include<iostream.h> #include<conio.h> #include<iomanip.h> class matrixone { protected : int a[10][10]; int row,col; void read(); void print(); }; class matrixtwo { protected : int a[10][10]; int row,col; void read(); void print(); }; class sum:protected matrixone, matrixtwo { private : int sm[10][10]; public : sum(int m,int n); int row, col; void read(); void sumofmatrix(); void print(); }; sum::sum(int m, int n)

513
{ matrixone::row=matrixtwo::row=row=m; matrixone::col=matrixtwo::col=col=n; } void matrixone::read() { int *p=&a[0][0]; cout<<"Enter Matrix one : "<<row<<" x "<<col<<endl; for(int i=0;i<row; i++) for(int j=0; j<col; j++) cin>>*(p+i*10+j); } void matrixone::print() { int *p=&a[0][0]; cout<<"given Matrix one : "<<row<<" x "<<col<<endl; for(int i=0;i<row;i++) { for (int j=0; j<col;j++) cout<<setw(3)<<*(p+i*10+j); cout<<endl; } } void matrixtwo::read() { int *p=&a[0][0]; cout<<"Enter Matrix Two : "<<row<<" x "<<col<<endl; for(int i=0;i<row; i++) for(int j=0; j<col; j++) cin>>*(p+i*10+j); } void matrixtwo::print() { int *p=&a[0][0]; cout<<"given Matrix two : "<<row<<" x "<<col<<endl; for(int i=0;i<row;i++) { for (int j=0; j<col;j++) cout<<setw(3)<<*(p+i*10+j); cout<<endl; } } void sum::sumofmatrix()

514
{ int *p1=&matrixone::a[0][0]; int *p2=&matrixtwo::a[0][0]; int *pm=&sm[0][0]; for(int i=0;i<row; i++) for(int j=0; j<col; j++) *(pm+i*10+j)=*(p1+i*10+j) + *(p2+i*10+j); } void sum::read() { matrixone::read(); matrixtwo::read(); } void sum::print() { int *pm=&sm[0][0]; matrixone::print(); matrixtwo::print(); cout<<"Sum Matrix "<<row<<" x "<<col<<endl; for(int i=0;i<row;i++) { for (int j=0; j<col;j++) cout<<setw(3)<<*(pm+i*10+j); cout<<endl; } } void main () { int m1,n1, m2,n2; cout<<"Enter M & N : row and columns 1"<<<"Enter M & N : row and columns 1"<<<"Invalid row and column value"<< Program No : 15 : Write a program in C++ to print the product of two M x N Matrix ? #include<iostream.h> #include<conio.h> #include<iomanip.h> class matrixone { protected : int a[10][10];

515
int row,col; void read(); void print(); }; class matrixtwo { protected : int a[10][10]; int row,col; void read(); void print(); }; class product:protected matrixone, matrixtwo { private : int pro[10][10]; public : product(int m1,int n1, int m2, int n2); int row, col; void read(); void productofmatrix(); void print(); }; product::product(int m1,int n1, int m2, int n2) { matrixone::row=m1; matrixone::col=n1; matrixtwo::row=m2; matrixtwo::col=n2; row=m1; col=n2;

} void matrixone::read() { int *p=&a[0][0]; cout<<"Enter Matrix one : "<<row<<" x "<<col<<endl; for(int i=0;i<row; i++) for(int j=0; j<col; j++) cin>>*(p+i*10+j); } void matrixone::print()

516
{ int *p=&a[0][0]; cout<<"given Matrix one : "<<row<<" x "<<col<<endl; for(int i=0;i<row;i++) { for (int j=0; j<col;j++) cout<<setw(3)<<*(p+i*10+j); cout<<endl; } } void matrixtwo::read() { int *p=&a[0][0]; cout<<"Enter Matrix Two : "<<row<<" x "<<col<<endl; for(int i=0;i<row; i++) for(int j=0; j<col; j++) cin>>*(p+i*10+j); } void matrixtwo::print() { int *p=&a[0][0]; cout<<"given Matrix two : "<<row<<" x "<<col<<endl; for(int i=0;i<row;i++) { for (int j=0; j<col;j++) cout<<setw(3)<<*(p+i*10+j); cout<<endl; } } void product::productofmatrix() { int *p1=&matrixone::a[0][0]; int *p2=&matrixtwo::a[0][0]; int *pm=&pro[0][0]; for(int i=0;i<row; i++) for(int j=0; j<col; j++) { *(pm+i*10+j)=0; for(int k=0; k<matrixone::col; k++) *(pm+i*10+j) += *(p1+i*10+k) * *(p2+k*10+j); } } void product::read() {

517
matrixone::read(); matrixtwo::read(); } void product::print() { int *pm=&pro[0][0]; matrixone::print(); matrixtwo::print(); cout<<"Product Matrix "<<row<<" x "<<col<<endl; for(int i=0;i<row;i++) { for (int j=0; j<col;j++) cout<<setw(3)<<*(pm+i*10+j); cout<<endl; } } void main () { int m1,n1, m2,n2; cout<<"Enter M & N : row and columns 1"<<<"Enter M & N : row and columns 2"<<<"Invalid row and column value"<< Program No : 16 : Write a program in C++ to count the upper, lower, number and special character of given text ? #include<iostream.h> #include<conio.h> class text { public : char ch[100]; void readtext(); void printtext(); }; class count : public text { private : int upp, low, num, spe; public : void counttext(); void printresult();

518
}; void text::readtext() { for(char *p=ch; (*p=cin.get())!='$'; p++); *p='\0'; } void text::printtext() { cout<<"Given text"<<endl; for(char *p=ch; *p ; cout.put(*p), p++); cout<<endl; } void count::counttext() { upp=low=spe=num=0; for(char *p=ch; *p ; p++) { if(*p>='A' && *p <='Z') upp++; else if(*p>='a' && *p<='z') low++; else if(*p>='0' && *p<='9') num++; else spe++; } } void count::printresult() { printtext(); cout<<"upper : "<<upp<<endl; cout<<"lower : "<<low<<endl; cout<<"number : "<<num<<endl; cout<<"special : "<<spe<<endl; } void main (void) { count *tp, text ; tp=&text; cout<<"Enter String"<<endl; tp->readtext(); tp->counttext(); cout<<"Result"<<endl;

519
tp->printresult(); getch(); } Program No : 17 : Write a program in C++ to count the no. of lines, words & characters of given text ?

#include<iostream.h> #include<conio.h> class text { public : char ch[100]; void readtext(); void printtext(); }; class count : public text { private : int lines, words, chars; public : void counttext(); void printresult(); }; void text::readtext() { for(char *p=ch; (*p=cin.get())!='$'; p++); *p='\0'; } void text::printtext() { cout<<"Given text"<<endl; for(char *p=ch; *p ; cout.put(*p), p++); cout<<endl; } void count::counttext() { lines=words=chars=0; for(char *p=ch; *p ; p++)

520
{ if( (*p==' ' && *(p+1) !=' ') || (*p=='\t' && *(p+1) !='\t') || (*p=='\n' && *(p+1) !='\n') ) words++; if (*p=='\n') lines++; } chars=p-ch; } void count::printresult() { printtext(); cout<<"lines : "<<lines<<endl; cout<<"words : "<<words<<endl; cout<<"chars : "<<chars<<endl; } void main (void) { count *tp, text ; tp=&text; cout<<"Enter String"<<endl; tp->readtext(); tp->counttext(); cout<<"Result"<<endl; tp->printresult(); getch(); } Program No : 18 : Write a program in C++ to copy one array to another array ?

#include<iostream.h> #include<conio.h> class text { public : char ch[100]; void readtext(); void printtext(); };

521
class copy : public text { private : char ch[100]; public : void copytext(); void printresult(); }; void text::readtext() { for(char *p=ch; (*p=cin.get())!='$'; p++); *p='\0'; } void text::printtext() { cout<<"Given text"<<endl; for(char *p=ch; *p ; cout.put(*p), p++); cout<<endl; } void copy::copytext() { for(char *p=ch, *q=text::ch; *q ; q++,p++) { *p=*q; } *p='\0'; } void copy::printresult() { cout<<"Copied text"<<endl; for(char *p=ch; *p ; cout.put(*p), p++); cout<<endl; } void main (void) { copy *tp, tex; tp=&tex; cout<<"Enter String"<<endl; tp->readtext(); tp->copytext();

522
cout<<"Result"<<endl; tp->printresult(); getch(); } Program No : 19 : Write a program in C++ to count the no of patterns exist on given string ?

#include<iostream.h> #include<conio.h> class text { public : char ch[100]; void readtext(); void printtext(); }; class pattern : public text { private : int count; public : void countpattern(char *); void printresult(); }; void text::readtext() { for(char *p=ch; (*p=cin.get())!='$'; p++); *p='\0'; } void text::printtext() { cout<<"Given text"<<endl; for(char *p=ch; *p ; cout.put(*p), p++); cout<<endl; } void pattern::countpattern(char *pat) { count=0;

523
for(char *p=ch; *p ; p++) { for(char *pt=pat, *tp=p; *tp==*pt && *pt!='\0'; tp++, pt++); if(*pt=='\0') count++; } } void pattern::printresult() { cout<<"No of of patterns "<<count<<endl; } void main (void) { pattern *tp, tex; tp=&tex; char pat[20]; cout<<"Enter String"<<endl; tp->readtext(); cout<<"Enter Pattern"<<endl; cin>>pat; tp->countpattern(pat); cout<<"Result"<<endl; tp->printresult(); getch(); } Program No : 20 : Write a program in C++ to delete the pattern ?

#include<iostream.h> #include<conio.h> class text { public : char ch[100]; void readtext(); void printtext(); }; class pattern : public text { private : int count; public :

524
void deletepattern(char *, char *); void printresult(); }; void text::readtext() { for(char *p=ch; (*p=cin.get())!='$'; p++); *p='\0'; } void text::printtext() { cout<<"Given text"<<endl; for(char *p=ch; *p ; cout.put(*p), p++); cout<<endl; } void pattern::deletepattern(char *pat, char *res) { count=0; char *pr=res; for(char *p=ch; *p ; p++) { for(char *pt=pat, *tp=p; *tp==*pt && *pt!='\0'; tp++, pt++); if(*pt=='\0') { count++; p=tp-1; } else *pr++=*p; } *pr='\0'; } void pattern::printresult() { cout<<"No of of patterns "<<count<<endl; } void main (void) { pattern *tp, tex; tp=&tex; char pat[20],res[100]; cout<<"Enter String"<<endl;

525
tp->readtext(); cout<<"Enter Pattern"<<endl; cin>>pat; tp->deletepattern(pat,res); cout<<"Result"<<endl; cout<<res<<endl; getch(); } Program No : 21 : Write a program in C++ to read the bio data and height, weight info of empolyee ?

#include<iostream.h> #include<conio.h> #include<stdio.h> #include<fstream.h> class biodata { protected : char name[20]; int age; float salary; char address[20]; void readbio(); void printbio(); }; class info : protected biodata { private : float height, weight; public : void readinfo(); void printinfo(); }; void biodata::readbio() { cout<<"Enter name, age, sal, add"<<endl; cin>>name>>age>>salary>>address; } void biodata::printbio()

526
{ cout<<"Given name, age, sal, add"<<endl; cout<<name<<" "<<age<<" "<<salary<<" "<<address<<endl; } void info::readinfo() { readbio(); cout<<"Enter height and weight of emp"<<endl; cin>>height>>weight; } void info::printinfo() { printbio(); cout<<"Height and weight of emp"<<endl; cout<<height<<" "<<weight<<endl; } void main(int argc, char *argv[]) { info emp, *p; p=&emp; p->readinfo(); p->printinfo(); getch(); } Program No : 22 : Write a program in C++ to read the bio data and height, weight info of 10 empolyees ?

#include<iostream.h> #include<conio.h> #include<stdio.h> #include<fstream.h> class biodata { protected : char name[20]; int age; float salary; char address[20]; void readbio(); void printbio(); };

527
class info : protected biodata { private : float height, weight; public : void readinfo(); void printinfo(); }; void biodata::readbio() { cout<<"Enter name, age, sal, add"<<endl; cin>>name>>age>>salary>>address; } void biodata::printbio() { cout<<"Given name, age, sal, add"<<endl; cout<<name<<" "<<age<<" "<<salary<<" "<<address<<endl; } void info::readinfo() { readbio(); cout<<"Enter height and weight of emp"<<endl; cin>>height>>weight; } void info::printinfo() { printbio(); cout<<"Height and weight of emp"<<endl; cout<<height<<" "<<weight<<endl; } void main(int argc, char *argv[]) { info emp[10], *p; p=&emp[0]; for(int i=0; i<10; i++) { cout<<i+1<<" info"<<endl; p->readinfo(); p++; } cout<<"All informations are readed"<<endl; getch();

528
p=&emp[0]; for(i=0; i<10; i++) { cout<<i+1<<" info"<<endl; p->printinfo(); p++; } cout<<"All informations are printed"<<endl; getch(); } Program No : 23 : Write a program in C++ to read the bio data and height, weight info 10 empolyees and store into file ?

#include<iostream.h> #include<conio.h> #include<stdio.h> #include<fstream.h> class biodata { protected : char name[20]; int age; float salary; char address[20]; void readbio(); void printbio(); }; class info : protected biodata { private : float height, weight; public : void readinfo(); void printinfo(); }; void biodata::readbio() { cout<<"Enter name, age, sal, add"<<endl; cin>>name>>age>>salary>>address; }

529
void biodata::printbio() { cout<<"Given name, age, sal, add"<<endl; cout<<name<<" "<<age<<" "<<salary<<" "<<address<<endl; } void info::readinfo() { readbio(); cout<<"Enter height and weight of emp"<<endl; cin>>height>>weight; } void info::printinfo() { printbio(); cout<<"Height and weight of emp"<<endl; cout<<height<<" "<<weight<<endl; } void main(int argc, char *argv[]) { fstream *in=new fstream; if(argc!=2) { cout<<"Invaild arguments"<<endl; getch(); return; } in->open(argv[1], ios::out); if(in->fail()) { cout<<"File opeing error"<<endl; getch(); return; } info emp, *p; p=&emp; for(int i=0; i<10; i++) { cout<<i+1<<" info"<<endl; p->readinfo(); in->write((char *)p, sizeof(info)); } cout<<"All informations are stored"<<endl; in->close(); getch();

530
} Program No : 24 : Write a program in C++ to read the bio-data from file ? #include<iostream.h> #include<conio.h> #include<stdio.h> #include<fstream.h> class biodata { protected : char name[20]; int age; float salary; char address[20]; void readbio(); void printbio(); }; class info : protected biodata { private : float height, weight; public : void readinfo(); void printinfo(); }; void biodata::readbio() { cout<<"Enter name, age, sal, add"<<endl; cin>>name>>age>>salary>>address; } void biodata::printbio() { cout<<"Given name, age, sal, add"<<endl; cout<<name<<" "<<age<<" "<<salary<<" "<<address<<endl; } void info::readinfo() { readbio(); cout<<"Enter height and weight of emp"<<endl;

531
cin>>height>>weight; } void info::printinfo() { printbio(); cout<<"Height and weight of emp"<<endl; cout<<height<<" "<<weight<<endl; } void main(int argc, char *argv[]) { fstream *in=new fstream; if(argc!=2) { cout<<"Invaild arguments"<<endl; getch(); return; } in->open(argv[1], ios::in); if(in->fail()) { cout<<"File opeing error"<<endl; getch(); return; } info emp, *p; p=&emp; for(int i=0; i<10; i++) { cout<<i+1<<" info"<<endl; in->read((char *)p, sizeof(info)); p->printinfo(); } cout<<"All informations are displayed"<<endl; in->close(); getch(); } Program No : 25 : Write a program in C++ to read the bio-data from file and calculate hra and da ? #include<iostream.h> #include<conio.h> #include<stdio.h> #include<fstream.h>

532
class biodata { protected : char name[20]; int age; float salary; char address[20]; void readbio(); void printbio(); }; class info : protected biodata { private : float height, weight; float hra, da, tax,net; public : void readinfo(); void calculate(); void printinfo(); }; void biodata::readbio() { cout<<"Enter name, age, sal, add"<<endl; cin>>name>>age>>salary>>address; } void biodata::printbio() { cout<<"Given name, age, sal, add"<<endl; cout<<name<<" "<<age<<" "<<salary<<" "<<address<<endl; } void info::readinfo() { readbio(); cout<<"Enter height and weight of emp"<<endl; cin>>height>>weight; } void info::printinfo() { printbio(); cout<<"Height and weight of emp"<<endl; cout<<height<<" "<<weight<<endl; cout<<"da : "<<da<<endl; cout<<"hra : "<<hra<<endl;

533
cout<<"tax : "<<tax<<endl; cout<<"net : "<<net<<endl; } void info::calculate() { hra=salary * 0.10; tax=salary * 0.10; da=250; net=salary+hra+da-tax; } void main(int argc, char *argv[]) { fstream *in=new fstream; if(argc!=2) { cout<<"Invaild arguments"<<endl; getch(); return; } in->open(argv[1], ios::in); if(in->fail()) { cout<<"File opeing error"<<endl; getch(); return; } info emp, *p; p=&emp; for(int i=0; i<10; i++) { cout<<i+1<<" info"<<endl; in->read((char *)p, sizeof(info)); p->calculate(); p->printinfo(); } cout<<"All informations are displayed"< Program No : 26 : Write a program in C++ to find the area of triangle without input ?

#include #include

534
class base { public : base() { cout<<"Base class created"<<<"Derived class created"< Program No : 27 : Write a program in C++ to demonstrate destructor function ?

#include<iostream.h> #include<conio.h> class base { public : base() { cout<<"Base class created"<<endl; }; ~base() { cout<<"Base class deleted"<<endl; }; }; class derived : public base { public : derived() { cout<<"Derived class created"<<endl; } ~derived() { cout<<"Derived class deleted"<<endl; } }; void main () {

535
void demo(); demo(); getch(); } void demo() { derived *obj=new derived; getch(); delete obj; } Program No : 28 : Write a program in C++ to demonstration on inherits one direct base class. ?

#include<iostream.h> #include<conio.h> class baseA { public : int a; void read() { cin>>a; } void print() { cout<<a<<endl; } }; class derivedA : public baseA { public : int b; void read() { baseA::read(); cin>>b; } void print() { baseA::print(); cout<<b<<endl;

536
} }; void main (void) { derivedA obj, *p; p=&obj; p->read(); p->print(); getch(); } Program No : 29 : Write a program in C++ to demonstration on inherits two direct base class ?

#include<iostream.h> #include<conio.h> class baseA { public : int a; void read() { cin>>a; } void print() { cout<<a<<endl; } }; class baseB { public : int a; void read() { cin>>a; } void print() { cout<<a<<endl; }

537
}; // here baseA and baseB are direct base classes class derivedA : public baseA, public baseB { public : int b; void read() { baseA::read(); baseB::read(); cin>>b; } void print() { baseA::print(); baseB::print(); cout<<b<<endl; } }; void main (void) { derivedA obj, *p; p=&obj; p->read(); p->print(); getch(); } Program No : 30 : Write a program in C++ to demonstration on inherits one indirect base class ?

#include<iostream.h> #include<conio.h> class baseA { public : int a; void read() { cout<<"Input of baseA : "; cin>>a; } void print()

538
{ cout<<"baseA : "<<a<<endl; } }; class baseB : public baseA { public : int b; void read() { baseA::read(); cout<<"Input of baseB : "; cin>>b; } void print() { baseA::print(); cout<<"baseB : "<<b<<endl; } }; class derivedC : public baseB { public : int c; void read() { baseB::read(); cout<<"Input of DeriC : "; cin>>c; } void print() { baseB::print(); cout<<"deriC : "<<c<<endl; } }; void main (void) { derivedC obj, *p; p=&obj; obj->read(); obj->print(); getch(); }

539
Program No : 31 : Write a program in C++ to demonstration on inherits two indirect base class ? #include<iostream.h> #include<conio.h> class baseA { public : int a; void read() { cout<<"Input of baseA : "; cin>>a; } void print() { cout<<"baseA : "<<a<<endl; } }; class baseB : public baseA { public : int b; void read() { baseA::read(); cout<<"Input of baseB : "; cin>>b; } void print() { baseA::print(); cout<<"baseB : "<<b<<endl; } }; class baseC : public baseA { public : int c; void read() { baseA::read();

540
cout<<"Input of baseC : "; cin>>c; } void print() { baseA::print(); cout<<"baseC : "<<c<<endl; } }; class derivedD : public baseB, baseC { public : int d; void read() { baseB::read(); baseC::read(); cout<<"Input of DeriD : "; cin>>d; } void print() { baseB::print(); baseC::print(); cout<<"deriD : "<<d<<endl; } }; void main (void) { derivedD obj, *p; p=&obj; obj->read(); obj->print(); getch(); } Program No : 32 : Write a program in C++ to demonstration of public inheritance ?

#include<iostream.h> #include<conio.h> class baseA

541
{ private : int x; protected : int y; public : int z; void print() { cout<<"x : "<<x<<endl; //Garbase value printed cout<<"y : "<<y<<endl; cout<<"z : "<<z<<endl; } }; class derivedB : public baseA { private : int a; protected : int b; public : int c; void assign() { // x = 10 ; cannot access private member declared in class 'baseA' y = 20 ; z = 30 ; a = 40 ; b = 50 ; c = 60 ; } void print() { baseA::print(); cout<<"a : "<<a<<endl; cout<<"b : "<<b<<endl; cout<<"c : "<<c<<endl; } }; void main (void) { derivedB obj, *p; p=&obj; p->assign(); p->print();

542
derivedB newobj; p=&newobj; // p->x=1; cannot access private member declared in class 'baseA' // p->y=2; cannot access protected member declared in class 'baseA' p->z=3; // p->a=4; cannot access private member declared in class 'derivedB' // p->b=5; cannot access protected member declared in class 'derivedB' p->c=6; p->print(); getch(); } Program No : 33 : Write a program in C++ to demonstration of protected inheritance ? #include<iostream.h> #include<conio.h> class baseA { private : int x; protected : int y; public : int z; void print() { cout<<"x : "<<x<<endl; //Garbase value printed cout<<"y : "<<y<<endl; cout<<"z : "<<z<<endl; } }; class derivedB : protected baseA { private : int a; protected : int b; public : int c; void assign() { // x = 10 ; cannot access private member declared in class 'baseA' y = 20 ; z = 30 ; a = 40 ;

543
b = 50 ; c = 60 ; } void print() { baseA::print(); cout<<"a : "<<a<<endl; cout<<"b : "<<b<<endl; cout<<"c : "<<c<<endl; } }; void main (void) { derivedB obj, *p; p=&obj; p->assign(); p->print(); derivedB newobj; p=&newobj; // p->x=1; cannot access private member declared in class 'baseA' // p->y=2; cannot access protected member declared in class 'baseA' // p->z=3; cannot access public member declared in class 'baseA' // p->a=4; cannot access private member declared in class 'derivedB' // p->b=5; cannot access protected member declared in class 'derivedB' p->c=6; p->print(); getch(); } Program No : 34 : Write a program in C++ to demonstration of private inheritance ? #include<iostream.h> #include<conio.h> class baseA { private : int x; protected : int y; public : int z; void print() {

544
cout<<"x : "<<x<<endl; //Garbase value printed cout<<"y : "<<y<<endl; cout<<"z : "<<z<<endl; } }; class derivedB : private baseA { private : int a; protected : int b; public : int c; void assign() { // x = 10 ; cannot access private member declared in class 'baseA' y = 20 ; z = 30 ; a = 40 ; b = 50 ; c = 60 ; } void print() { baseA::print(); cout<<"a : "<<a<<endl; cout<<"b : "<<b<<endl; cout<<"c : "<<c<<endl; } }; void main (void) { derivedB obj, *p; p=&obj; p->assign(); p->print(); derivedB newobj; p=&newobj; // p->x=1; // p->y=2; // p->z=3; // p->a=4; // p->b=5; p->c=6; cannot access private member declared in class 'baseA' cannot access protected member declared in class 'baseA' cannot access public member declared in class 'baseA' cannot access private member declared in class 'derivedB' cannot access protected member declared in class 'derivedB'

545
p->print(); getch(); } Program No : 35 : Write a program in C++ to demonstration of Container Class ? #include<iostream.h> #include<conio.h> class baseA { public : int a; void print() { cout<<"a : "<<a<<endl; } }; class baseB { public : int b; void print() { cout<<"b : "<<b<<endl; } }; class ContainerC { public : baseA objA; baseB objB; int c; void assign() { objA.a = 40 ; objB.b = 50 ; c = 60 ; } void print() { objA.print(); objB.print(); cout<<"c : "<<c<<endl;

546
} }; void main (void) { ContainerC obj, *p; P=&obj; obj->assign(); obj->print(); ContainerC newobj, *pc; pc=&newobj; pc->objA.a=10; pc->objB.b=20; pc->c=30; pc->print(); getch(); }

using overloading
Program No : 1 : Write a program in C++ to interchange of two values of all data types ?

#include<iostream.h> #include<conio.h> void swap(int *, int *); void swap(float *, float *); void swap(char *, char *); void main(void) { int a, b; float fa, fb; char ca, cb; cout<<"Enter integers"<<endl; cin>>a>>b;

547
cout<<"Enter floats"<<endl; cin>>fa>>fb; cout<<"Enter chars"<<endl; cin>>ca>>cb; cout<<"Integers Before swaping"<<endl; cout<<a<<" "<<b<<endl; swap(&a,&b); cout<<"Integers After swaping"<<endl; cout<<a<<" "<<b<<endl; cout<<"Floats Before swaping"<<endl; cout<<fa<<" "<<fb<<endl; swap(&fa,&fb); cout<<"Floats After swaping"<<endl; cout<<fa<<" "<<fb<<endl; cout<<"Chars Before swaping"<<endl; cout<<ca<<" "<<cb<<endl; swap(&ca,&cb); cout<<"Chars After swaping"<<endl; cout<<ca<<" "<<cb< Program No : 2 : Write a program in C++ to find the sum of two numbers of all data types ?

#include<iostream.h> #include<conio.h> int sum(int *, int *); float sum(float *, float *); double sum(double *, double *); void main(void) { int a, b; float fa, fb; double ca, cb;

548
cout<<"Enter integers"<<endl; cin>>a>>b; cout<<"Enter floats"<<endl; cin>>fa>>fb; cout<<"Enter doubles"<<endl; cin>>ca>>cb; cout<<"sum integ : "<<sum(&a,&b)<<endl; cout<<"sum float : "<<sum(&fa,&fb)<<endl; cout<<"sum double: "<<sum(&ca,&cb)<<endl; getch(); } int sum(int *a, int *b) { return (*a+*b); } float sum(float *a, float *b) { return (*a+*b); } double sum(double *a, double *b) { return (*a+*b); } Program No : 3 : Write a program in C++ to read and print the value of all data types ?

#include<iostream.h> #include<conio.h> void read(int *); void read(float *); void read(double *); void read(char *); void print(int *); void print(float *); void print(double *); void print(char *);

549
void main(void) { int a; float fa; double da; char ca; read(&a); read(&fa); read(&da); read(&ca); print(&a); print(&fa); print(&da); print(&ca); getch(); } void read(int *x) { cout<<"Enter Integer"<<endl; cin>>*x; } void read(float *x) { cout<<"Enter float"<<endl; cin>>*x; } void read(double *x) { cout<<"Enter double"<<endl; cin>>*x; } void read(char *x) { cout<<"Enter char"<<endl; cin>>*x; } void print(int *x) { cout<<"Integer : "; cout<<*x<<endl; }

550
void print(float *x) { cout<<"float : "; cout<<*x<<endl; } void print(double *x) { cout<<"double : "; cout<<*x<<endl; } void print(char *x) { cout<<"char : "; cout<<*x<<endl; } Program No : 4 : Write a program in C++ to find the square value of different data types ? #include<iostream.h> #include<conio.h> int square(int *); float square(float *); double square(double *); void main(void) { int a; float fa; double da; cout<<"Enter Numbers "<<endl; cin>>a>>fa>>da; cout<<" Square : "<<square(&a)<<endl; cout<<" Square : "<<square(&fa)<<endl; cout<<" Square : "<<square(&da)<<endl; getch(); } int square(int *x) { return (*x * *x);

551
} float square(float *x) { return (*x * *x); } double square(double *x) { return (*x * *x); } Program No : 5 : Write a program in C++ to find the sum of elements of 3 x 3 matrix of int and double numbers ?

#include<iostream.h> #include<conio.h> int sum(int *, int n); double sum(double *, int n); void main(void) { int isum; double dsum; static int a[][3]= { {1,2,3}, {4,5,6}, {7,8,9} }; static double b[][3]= { {1.1,2.1,3.1}, {4.1,5.1,6.1}, {7.1, 8.1, 9.1} }; isum=sum(&a[0][0],3); dsum=sum(&b[0][0],3); cout<<"Integer Matrix sum : "<<isum<<endl; cout<<"Double Matrix sum : "<<dsum<<endl; getch(); } int sum(int *mat, int n) { int temp=0; for(int i=0; i<n; i++) for(int j=0; j<n; j++) temp += *(mat+i*3+j);

552
return temp; } double sum(double *mat, int n) { double temp=0.0; for(int i=0; i<n; i++) for(int j=0; j<n; j++) temp += *(mat+i*3+j); return temp; } Program No : 6 : Write a program in C++ to transpose of 3x3 matrix of int and double numbers ?

#include<iostream.h> #include<conio.h> #include<iomanip.h> void transpose(int *, int *); void transpose(double *, double *); void print(int *); void print(double *); void main(void) { int ta[3][3]; double tb[3][3]; static int a[3][3]= { {1,2,3}, {4,5,6}, {7,8,9} }; static double b[3][3]= { {1.1,2.1,3.1}, {4.1,5.1,6.1}, {7.1, 8.1, 9.1} }; transpose(&a[0][0],&ta[0][0]); cout<<"Given Matrix of A "<<endl; print(&a[0][0]); cout<<"Transpose of A "<<endl; print(&ta[0][0]); transpose(&b[0][0],&tb[0][0]); cout<<"Given Matrix of B "<<endl; print(&b[0][0]);

553
cout<<"Transpose of B "<<endl; print(&tb[0][0]); getch(); } void transpose(double *mat, double *tr) { for(int i=0; i<3; i++) for(int j=0; j<3; j++) *(tr+j*3+i)= *(mat+i*3+j); } void transpose(int *mat, int *tr) { for(int i=0; i<3; i++) for(int j=0; j<3; j++) *(tr+j*3+i)= *(mat+i*3+j); } void print(int *mat) { for(int i=0; i<3; i++) { for(int j=0; j<3; j++) cout<<setw(4)<<*(mat+i*3+j); cout<<endl; } } void print(double *mat) { for(int i=0; i<3; i++) { for(int j=0; j<3; j++) cout<<setw(4)<<*(mat+i*3+j); cout<<endl; } } Program No : 7 : Write a program in C++ to find the sum of different no of arguments ?

#include<iostream.h> #include<conio.h>

554
int sum(); int sum(int*); int sum(int*, int*); int sum(int*, int*, int*); void main(void) { int a, b, c; cout<<"sum of 0 arguments : "<<sum()<<endl; cout<<"Enter Number a "<<endl; cin>>a; cout<<"sum of 1 arguments : "<<sum(&a)<<endl; cout<<"Enter Number a & b "<<endl; cin>>a>>b; cout<<"sum of 2 arguments : "<<sum(&a,&b)<<endl; cout<<"Enter Number a, b, & c "<<endl; cin>>a>>b>>c; cout<<"sum of 3 arguments : "<<sum(&a,&b,&c)<<endl; getch(); } int sum() { return (0); } int sum(int *x) { return (*x); } int sum(int *x, int *y) { return (*x+*y); } int sum(int *x, int *y, int *z) { return (*x+*y+*z); } Program No : 8 : Write a program in C++ to find the sum of square of given different no of arguments ?

555
#include<iostream.h> #include<conio.h> int sumofsquare(int *); int sumofsquare(int *, int *); int sumofsquare(int *, int *, int *); void main(void) { int a, b, c; cout<<"Enter Number a, b, & c "<<endl; cin>>a>>b>>c; cout<<"sum of square of 1 arguments : "<<sumofsquare(&a)<<endl; cout<<"sum of square of 2 arguments : "<<sumofsquare(&a,&b)<<endl; cout<<"sum of square of 3 arguments : "<<sumofsquare(&a,&b,&c)<<endl; getch(); } int sumofsquare(int *x) { return (*x * *x); } int sumofsquare(int *x, int *y) { int temp = *x+ *y; return (temp * temp); } int sumofsquare(int *x, int *y, int *z) { int temp = *x+*y+*z; return (temp * temp); } Program No : 9 : Write a program in C++ to read and print the given different no of arguments ? #include<iostream.h> #include<conio.h> void read(int *); void read(int *, int *);

556
void read(int *, int *, int *); void print(int *); void print(int *, int *); void print(int *, int *, int *); void main(void) { int x, y, z; read(&x); print(&x); read(&x,&y); print(&x,&y); read(&x,&y,&z); print(&x,&y,&z); getch(); } void read(int *x) { cout<<"Enter One Number"<<endl; cin>>*x; } void read(int *x, int *y) { cout<<"Enter Two numbers"<<endl; cin>>*x>>*y; } void read(int *x, int *y, int *z) { cout<<"Enter three Numbers"<<endl; cin>>*x>>*y>>*z; } void print(int *x) { cout<<"X : "; cout<<*x<<endl; } void print(int *x, int *y) { cout<<"X : "<<*x<<" Y : "<<*y<<endl; }

557
void print(int *x, int *y, int *z) { cout<<"X : "<<*x<<" Y : "<<*y<<" Z : "<<*z<<endl; } Program No : 10 : Write a program in C++ to overloading assignment operator. (Clone objects) ? #include<iostream.h> #include<conio.h> class sample { private : int a, b; public : sample(int, int); void operator=(sample ); void display(); }; sample::sample(int x, int y) { a=x; b=y; } void sample::operator=(sample temp) { a=temp.a; b=temp.b; } void sample::display() { cout<<"a = "<<a<<endl; cout<<"b = "<<b<<endl; } void main(void) { sample obj1(10,20), *p1; sample obj2(156,78), *p2; p1=&obj1; p2=&obj2;

558
cout<<"Value of Obj1 "<<endl; p1->display(); cout<<"Value of Obj2 "<<endl; p2->display(); *p2=*p1; // ob2=obj1&& obj2.operator=(obj1); here both statements are same. cout<<"Value of Obj2 after assigning"<<endl; p2->display(); getch(); } Program No : 11 : Write a program in C++ to find the sum of two objects ? #include<iostream.h> #include<conio.h> class sample { private : int value; public : sample(){}; sample(int); sample operator+(sample ); void display(); }; sample::sample(int x) { value=x; } sample sample::operator+(sample tr) { sample temp; temp.value=value+tr.value; return temp; } void sample::display() { cout<<"Value = "<<value<<endl; }

559
void main(void) { sample obj1(10); sample obj2(20); sample objsum; sample *p1,*p2,*pm; p1=&obj1; p2=&obj2; pm=&objsum; cout<<"Value of Obj1 "<<endl; p1->display(); cout<<"Value of Obj2 "<<endl; p2->display(); *pr=*p1+*p2; cout<<"Sum of objects "<<endl; pm->display(); getch(); } Program No : 12 : Write a program in C++ to perform all arithmetic operations ? #include<iostream.h> #include<conio.h> class sample { private : int value; public : sample(){}; sample(int); sample operator+(sample); sample operator-(sample); sample operator*(sample); sample operator/(sample); sample operator%(sample); void display(); }; sample::sample(int x) { value=x; }

560
sample sample::operator+(sample tr) { sample temp; temp.value=value+tr.value; return temp; } sample sample::operator-(sample tr) { sample temp; temp.value=value-tr.value; return temp; } sample sample::operator*(sample tr) { sample temp; temp.value=value*tr.value; return temp; } sample sample::operator/(sample tr) { sample temp; temp.value=value/tr.value; return temp; } sample sample::operator%(sample tr) { sample temp; temp.value=value%tr.value; return temp; } void sample::display() { cout<<"Value = "<<value<<endl; } void main(void) { sample obj1(10); sample obj2(20); sample objres; sample *p1, *p2, *pr;

561
p1=&obj1; cout<<"Value of Obj1 "<<endl; p1->display(); p2=&obj2; cout<<"Value of Obj2 "<<endl; p2->display(); pr=&objres; *pr=*p1+(*p2); cout<<"Sum of objects "<<endl; pr->display(); *pr=*p1/(*p2); cout<<"Diff of objects "<<endl; pr->display(); *pr=*p1*(*p2); cout<<"Mul of objects "<<endl; pr->display(); *pr=*p1/(*p2); cout<<"Quot of objects "<<endl; pr->display(); *pr=*p1%(*p2); cout<<"Remainder of objects "<<endl; pr->display(); getch(); }

Program No : 13 : Write a program in C++ to perform all arithmetic operations of two complex numbers ?

#include<iostream.h> #include<conio.h>

562
class complex { private : float real, image; public : complex(){}; complex(float, float); complex operator+(complex); complex operator-(complex); complex operator*(complex); complex operator/(complex); complex operator%(complex); void display(); }; complex::complex(float x, float y) { real=x; image=y; } complex complex::operator+(complex tr) { complex temp; temp.real=real+tr.real; temp.image=image+tr.image; return temp; } complex complex::operator-(complex tr) { complex temp; temp.real=real-tr.real; temp.image=image-tr.image; return temp; } complex complex::operator*(complex tr) { complex temp; temp.real=real*tr.real - image*tr.image; temp.image=real*tr.image + image*tr.real; return temp; } complex complex::operator/(complex tr) { complex temp; float tp=real*tr.real + image*tr.image; temp.real = (real*tr.real + image*tr.image)/tp;

563
temp.image = (image*tr.real - real*tr.image)/tp; return temp; } void complex::display() { cout<<"Complex Value : "; cout<<real<<" "; if(image>=0) cout<<"+"; cout<<image<<"i"<<endl; } void main(void) { complex obj1(1,2); complex obj2(2,3); complex objres; complex *p1, *p2, *pr; p1=&obj1; cout<<"Value of Obj1 "<<endl; p1->display(); p2=&obj2; cout<<"Value of Obj2 "<<endl; p2->display(); pr=&objres; *pr=*p1+(*p2); cout<<"Sum of objects "<<endl; pr->display(); *pr=*p1-(*p2); cout<<"Diff of objects "<<endl; pr->display(); *pr=*p1*(*p2); cout<<"Mul of objects "<<endl; pr->display(); *pr=*p1/(*p2); cout<<"Div of objects "<<endl; pr->display();

564
getch(); } Program No : 14 : Write a program in C++ to perform all logical operations of two objects. ? #include<iostream.h> #include<conio.h> class sample { private : int value; public : sample(){}; sample(int); int operator<(sample); int operator<=(sample); int operator>(sample); int operator>=(sample); int operator==(sample); int operator!=(sample); void display(); }; sample::sample(int x) { value=x; } int sample::operator<(sample tr) { return (value<tr.value); } int sample::operator<=(sample tr) { return (value<=tr.value); } int sample::operator>(sample tr) { return (value>tr.value); }

565
int sample::operator>=(sample tr) { return (value>=tr.value); } int sample::operator==(sample tr) { return (value==tr.value); } int sample::operator!=(sample tr) { return (value!=tr.value); } void sample::display() { cout<<"Value = "<<value<<endl; } void main(void) { sample obj1(10), *p1; sample obj2(10), *p2; p1=&obj1; p2=&obj2; cout<<"Value of Obj1 "<<endl; p1->display(); cout<<"Value of Obj2 "<<endl; p2->display(); cout<<"Comparision of these two objects"<<endl; cout<<" < "<<(*p1<*p2)<<endl; cout<<" <= "<<(*p1<=*p2)<<endl; cout<<" > "<<(*p1>*p2)<<endl; cout<<" >= "<<(*p1>=*p2)<<endl; cout<<" == "<<(*p1==*p2)<<endl; cout<<" != "<<(*p1!=*p2)<<endl; getch(); } Program No : 15 : Write a program in C++ to print the numbers between 1 to 100 and 100 to 1. ? #include<iostream.h>

566
#include<conio.h> #include<iomanip.h> class sample { private : int value; public : sample(int); void operator++(); void operator--(); int operator<=(int); int operator>=(int); void display(); }; sample::sample(int x) { value=x; } void sample::operator++() { value++; } void sample::operator--() { value--; } int sample::operator<=(int n) { return(value<=n) ; } int sample::operator>=(int n) { return(value>=n) ; } void sample::display() { cout<<setw(4)<<value; }

567
void main(void) { sample obj1(1), *p1; sample obj2(100), *p2; p1=&obj1; p2=&obj2; cout<<"Numbers between 1 to 100"<<endl; while(*p1<=100) { p1->display(); (*p1)++; } cout<<"Numbers between 100 to 1"<<endl; while(*p2>=1) { p2->display(); (*p2)--; } cout<<endl; getch(); } Program No : 16 : Write a program in C++ to generate the Fibonacci series (length : 20) ? #include<iostream.h> #include<conio.h> #include<iomanip.h> class fibonacci { private : int f0, f1, fib; public : fibonacci(); void operator++(); void display(); }; fibonacci::fibonacci() { f0=0; f1=1;

568
fib=f0+f1; } void fibonacci::operator++() { f0=f1; f1=fib; fib=f0+f1; } void fibonacci::display() { cout<<fib<<endl; } void main(void) { fibonacci obj, *p; p=&obj; cout<<"fibonacci Serils length : 20"<<endl; for(int i=1; i<=20; i++) { p->display(); (*p)++; } cout<<endl; getch(); }

using dynamic memory allocation


Program No : 1 : Write a program in C++ to read and display integer, float, char, double number ?

#include<iostream.h> #include<conio.h> void main() { int *pi=new int;

569
float *pf=new float; double *pd=new double; char *pc=new char; cout<<"Enter int.."<<endl; cin>>*pi; cout<<"Enter float.."<<endl; cin>>*pf; cout<<"Enter double.."<<endl; cin>>*pd; cout<<"Enter char.."<<endl; cin>>*pc; cout<<"int :.."<<*pi<<endl; cout<<"float:.."<<*pf<<endl; cout<<"double:."<<*pd<<endl; cout<<"char :.."<<*pc<<endl; delete pi; delete pf; delete pd; delete pc; getch(); } Program No : 2 : Write a program in C++ to sum of two integer numbers ?

#include<iostream.h> #include<conio.h> void main() { int *p1=new int; int *p2=new int; int *sum=new int; cout<<"Enter numbers.."<<endl; cin>>*p1>>*p2; *sum=*p1+*p2; cout<<"sum : "<<*sum<<endl;

570
delete p1; delete p2; delete sum; getch(); } Program No : 3 : Write a program in C++ to biggest of two integer numbers ?

#include<iostream.h> #include<conio.h> void main() { int *p1=new int; int *p2=new int; int *big=new int; cout<<"Enter numbers.."<<endl; cin>>*p1>>*p2; *big = (*p1 > *p2) ? *p1 : *p2; cout<<"Big : "<<*big<<endl; delete p1; delete p2; delete big; getch(); } Program No : 4 : Write a program in C++ to read and display 10 integer numbers ?

#include<iostream.h> #include<conio.h> void main() { int *ptr=new int[10]; cout<<"Enter numbers.."<<endl; for(int i=0; i<10; i++)

571
cin>>*(ptr+i); cout<<"Given numbers.."<<endl; for(i=0; i<10; i++) cout<<*(ptr+i)<<'\t'; delete [] ptr; cout<<endl; getch(); } Program No : 5 : Write a program in C++ to read and display N integer numbers ? #include<iostream.h> #include<conio.h> void main() { int *ptr; int *n=new int; cout<<"Enter N"<<endl; cin>>*n; ptr=new int[*n]; cout<<"Enter numbers.."<<endl; for(int i=0; i<*n; i++) cin>>*(ptr+i); cout<<"Given numbers.."<<endl; for(i=0; i<*n; i++) cout<<*(ptr+i)<<'\t'; delete []ptr; cout<<endl; getch(); } Program No : 6 : Write a program in C++ to read and display text using dynamic allocation ? #include<iostream.h> #include<conio.h>

572
void main() { char *ptr=new char[100]; char *temp=ptr; cout<<"Enter String"<<endl; for(*ptr=cin.get(); *ptr!='$'; ptr++, *ptr=cin.get()); *ptr='\0'; cout<<"Given String"<<endl; ptr=temp; cout<<ptr<<endl;; delete []ptr; getch(); } Program No : 7 : Write a program in C++ to read N characters ? #include<iostream.h> #include<conio.h> void main() { char *ptr, *temp; int *n=new int; cout<<"Enter N"<<endl; cin>>*n; ptr=new char[*n]; temp=ptr; cout<<"Enter String"<<endl; for(*ptr=cin.get(); *ptr!='$'; ptr++, *ptr=cin.get()); *ptr='\0'; cout<<"Given String"<<endl; ptr=temp; cout<<ptr<<endl; delete n; delete []ptr; getch(); }

573
Program No : 8 : Write a program in C++ to copy one array to another array ?

#include<iostream.h> #include<conio.h> void main() { char *ptr, *tp, *t1, *t2; int *n=new int; cout<<"Enter N"<<endl; cin>>*n; ptr=new char[*n]; t1=ptr; cout<<"Enter String"<<endl; for(*ptr=cin.get(); *ptr!='$'; ptr++, *ptr=cin.get()); *ptr='\0'; ptr=t1; tp=new char[strlen(ptr)+1]; for(t2=tp; *ptr; ptr++, tp++) *tp=*ptr; *tp='\0'; ptr=t1; tp=t2; cout<<"Copied String"<<endl; cout<<tp<<endl; delete n; delete []ptr; delete []tp; getch(); }

574
Program No : 9 : Write a program in C++ to merge two arrays ?

#include<iostream.h> #include<conio.h> void main() { char *ptr1, *ptr2, *tp, *t1, *t2, *t3; int *n=new int; cout<<"Enter String length of first"<<endl; cin>>*n; ptr1=new char[*n]; t1=ptr1; cout<<"Enter String length of second"<<endl; cin>>*n; ptr2=new char[*n]; t2=ptr2; cout<<"Enter String one"<<endl; for(*ptr1=cin.get(); *ptr1!='$'; ptr1++, *ptr1=cin.get()); *ptr1='\0'; cout<<"Enter String Two"<<endl; for(*ptr2=cin.get(); *ptr2!='$'; ptr2++, *ptr2=cin.get()); *ptr2='\0'; tp=new char[strlen(ptr1)+strlen(ptr2)+1]; for(t3=tp, ptr1=t1; *ptr1; ptr1++, tp++) *tp=*ptr1; for(ptr2=t2; *ptr2; ptr2++, tp++) *tp=*ptr2; *tp='\0'; ptr1=t1; ptr2=t2; tp=t3; cout<<"Merged String"<<endl; cout<<tp<<endl; delete n;

575
delete []ptr1; delete []ptr2; delete []tp; getch(); }

Program No : 10 : Write a program in C++ to read and display biodata using structures ? #include<iostream.h> #include<conio.h> void main() { struct bio { char name[20]; int age; float sal; char add[20]; }; struct bio *pa=new bio; cout<<"Enter biodata..\n"; cin>>pa->name>>pa->age>>pa->sal>>pa->add; cout<<"Given biodata..\n"; cout<<pa->name<<endl; cout<<pa->age<<endl; cout<<pa->sal<<endl; cout<<pa->add<<endl; delete pa; getch(); } Program No : 11 : Write a program in C++ to find the area of circle using structures ?

#include<iostream.h> #include<conio.h>

576
void main() { struct area { float radius,ar; }; struct area *ptr=new area; cout<<"Enter Radius"<<endl; cin>>ptr->radius; ptr->ar=3.1415 * ptr->radius * ptr->radius; cout<<"Area : "<<ptr->ar<<endl; delete ptr; getch(); }

Program No : 12 : Write a program in C++ to find the biggest of three numbers using structures ? #include<iostream.h> #include<conio.h> void main() { struct biggest { int a, b, c, big; }; struct biggest *pa=new biggest; cout<<"Enter Numbers"<<endl; cin>>pa->a>>pa->b>>pa->c; pa->big= (pa->a>pa->b) ? ((pa->a>pa->c) ? pa->a : pa->c ) : ((pa->b>pa->c) ? pa->b : pa->c); cout<<"Big : "<<pa->big<<endl; delete pa; getch(); }

577
Program No : 13 : Write a program in C++ to read and display text using functions ? #include<iostream.h> #include<conio.h> void readtext(char*); void printtext(char*); void main() { char *ptr=new char[100]; cout<<"Enter String"<<endl; readtext(ptr); cout<<"Given String"<<endl; printtext(ptr); delete []ptr; getch(); } void readtext(char *pt) { for(*pt=cin.get(); *pt!='$'; pt++, *pt=cin.get()); *pt='\0'; } void printtext(char *pt) { while(*pt) cout.put(*pt++); cout<<endl; } Program No : 14 : Write a program in C++ to read and display integer array using functions ? #include<iostream.h> #include<conio.h> void readnum(int*,int); void printnum(int*, int);

578
void main() { int *ptr; int *n=new int; cout<<"Enter N"<<endl; cin>>*n; ptr=new int[*n]; cout<<"Enter Numbers"<<endl; readnum(ptr,*n); cout<<"Given numbers"<<endl; printnum(ptr,*n); delete []ptr; delete n; getch(); } void readnum(int *pt, int n) { for(int i=0; i<n; i++) cin>>*(pt+i); } void printnum(int *pt, int n) { for(int i=0; i<n; i++) cout<<*(pt+i)<<'\t'; cout<<endl; } Program No : 15 : Write a program in C++ to read and display biodata using functions ?

#include<iostream.h> #include<conio.h> struct bio { char name[20]; int age; float sal; char add[20];

579
}; void readbio(struct bio *); void printbio(struct bio *); void main() { struct bio *pa=new bio; cout<<"Enter biodata..\n"; readbio(pa); cout<<"Given biodata..\n"; printbio(pa); delete pa; getch(); } void readbio(struct bio *ptr) { cin>>ptr->name>>ptr->age>>ptr->sal>>ptr->add; } void printbio(struct bio *ptr) { cout<<ptr->name<<endl; cout<<ptr->age<<endl; cout<<ptr->sal<<endl; cout<<ptr->add<<endl; } Program No : 16 : Write a program in C++ to read and display a 3 x 3 Matrix ? #include<iostream.h> #include<conio.h> void readmatrix(int (*)[3]); void printmatrix(int (*)[3]); void main (void) { int (*ptr)[3]=new int[3][3]; cout<<"Enter 3 x 3 Matrix"<<endl; readmatrix(ptr);

580
cout<<"Given 3 x 3 Matrix"<<endl; printmatrix(ptr); getch(); } void readmatrix(int (*m)[3]) { for(int i=0;i<3;i++) for (int j=0; j<3;j++) cin>>m[i][j]; } void printmatrix(int (*m)[3]) { for(int i=0;i<3;i++) { for (int j=0; j<3;j++) cout<<m[i][j]<<" "; cout<<endl; } } Program No : 17 : Write a program in C to transpose the 3 x 3 Matrix ? #include<stdio.h> #include<conio.h> main () { int *a, *b; a=(int *) malloc(3 * sizeof(int)); a[0]=(int *) malloc(3 * sizeof(int)); a[1]=(int *) malloc(3 * sizeof(int)); a[2]=(int *) malloc(3 * sizeof(int)); b=(int *) malloc(3 * sizeof(int)); b[0]=(int *) malloc(3 * sizeof(int)); b[1]=(int *) malloc(3 * sizeof(int)); b[2]=(int *) malloc(3 * sizeof(int)); readmatrix(a); transpose(a,b); printmatrix(b);

581
getch(); } readmatrix(m) int *m; { int i, j; for(i=0;i<3;i++) for (j=0; j<3;j++) scanf("%d", m+i*3+j); } transpose(m,n) int *m, *n; { int i,j; for(i=0;i<3;i++) for (j=0; j<3;j++) *(n+j*3+i) =*(m+i*3+j); } printmatrix(m) int *m; { int i,j; for(i=0;i<3;i++) { for (j=0; j<3;j++) printf("%d ", *(m+i*3+j)); printf("\n"); } } Program No : 18 : Write a program in C to print the sum of two 3 x 3 Matrix. ?

#include<stdio.h> #include<conio.h> main () {

582
int *a, *b, *sum; a=(int *) malloc(3 * sizeof(int)); a[0]=(int *) malloc(3 * sizeof(int)); a[1]=(int *) malloc(3 * sizeof(int)); a[2]=(int *) malloc(3 * sizeof(int)); b=(int *) malloc(3 * sizeof(int)); b[0]=(int *) malloc(3 * sizeof(int)); b[1]=(int *) malloc(3 * sizeof(int)); b[2]=(int *) malloc(3 * sizeof(int)); sum=(int *) malloc(3 * sizeof(int)); sum[0]=(int *) malloc(3 * sizeof(int)); sum[1]=(int *) malloc(3 * sizeof(int)); sum[2]=(int *) malloc(3 * sizeof(int)); printf("Enter matrix 1..\n"); readmatrix(a); printf("Enter matrix 2..\n"); readmatrix(b); summat(a,b,sum); printmatrix(sum); getch(); } readmatrix(m) int *m; { int i, j; for(i=0;i<3;i++) for (j=0; j<3;j++) scanf("%d", m+i*3+j); } summat(m,n,sum) int *m, *n,*sum; { int i,j; for(i=0;i<3;i++) for (j=0; j<3;j++) *(sum+i*3+j) = *(m+i*3+j) + *(n+i*3+j);

583
}

printmatrix(m) int *m; { int i,j; for(i=0;i<3;i++) { for (j=0; j<3;j++) printf("%d ", *(m+i*3+j)); printf("\n"); } }

Program No : 19 : Write a program in C to print the product of two 3 x 3 Matrix ? #include<stdio.h> #include<conio.h> main () { int *a, *b, *pro; a=(int *) malloc(3 * sizeof(int)); a[0]=(int *) malloc(3 * sizeof(int)); a[1]=(int *) malloc(3 * sizeof(int)); a[2]=(int *) malloc(3 * sizeof(int)); b=(int *) malloc(3 * sizeof(int)); b[0]=(int *) malloc(3 * sizeof(int)); b[1]=(int *) malloc(3 * sizeof(int)); b[2]=(int *) malloc(3 * sizeof(int)); pro=(int *) malloc(3 * sizeof(int)); pro[0]=(int *) malloc(3 * sizeof(int)); pro[1]=(int *) malloc(3 * sizeof(int)); pro[2]=(int *) malloc(3 * sizeof(int));

584
printf("Enter matrix 1..\n"); readmatrix(a); printf("Enter matrix 2..\n"); readmatrix(b); promat(a,b,pro); printmatrix(pro); getch(); } readmatrix(m) int *m; { int i, j; for(i=0;i<3;i++) for (j=0; j<3;j++) scanf("%d", m+i*3+j); } promat(mat1,mat2,pro) int *mat1, *mat2,*pro; { int i,j,k; for(i=0;i<3;i++) for (j=0; j<3;j++) { *(pro+i*3+j)=0; for(k=0; k<3;k++) *(pro+i*3+j) += *(mat1+i*3+k) * *(mat2+k*3+j); } } printmatrix(m) int *m; { int i,j; for(i=0;i<3;i++) { for (j=0; j<3;j++)

585
printf("%d ", *(m+i*3+j)); printf("\n"); } } Program No : 20 : Write a program in C to read and display a N x N Matrix ?

#include<stdio.h> #include<conio.h> main () { int *a; int n,i; printf("N value : \n"); scanf("%d", &n); a=(int *)malloc( n* sizeof(int)); for (i=0; i<n; i++) a[i]=(int *)malloc( n* sizeof(int)); printf("Enter Matrix %d x %d\n", n,n); readmatrix(a,n); printf("Given %d x %d Matrix\n",n,n); printmatrix(a,n); getch(); } readmatrix(mat,n) int *mat, n; { int i, j; for(i=0;i<n;i++) for (j=0; j<n;j++) scanf("%d", mat+i*n+j);

586
} printmatrix(mat,n) int *mat,n; { int i,j; for(i=0;i<n;i++) { for (j=0; j<n;j++) printf("%d ", *(mat+i*n+j)); printf("\n"); } } Program No : 21 : Write a program in C to transpose the N x N Matrix ? #include<stdio.h> #include<conio.h> main () { int *a, *b; int n,i; printf("N value : \n"); scanf("%d", &n); a=(int *)malloc( n* sizeof(int)); b=(int *)malloc( n* sizeof(int)); for (i=0; i<n; i++) { a[i]=(int *)malloc( n* sizeof(int)); b[i]=(int *)malloc( n* sizeof(int)); } printf("Enter Matrix %d x %d\n", n,n); readmatrix(a,n); transpose(a,b,n); printf("Given %d x %d Matrix\n",n,n);

587
printmatrix(b,n); getch(); } readmatrix(mat,n) int *mat, n; { int i, j; for(i=0;i<n;i++) for (j=0; j<n;j++) scanf("%d", mat+i*n+j); } transpose(mat,trans,n) int *mat,*trans,n; { int i,j; for(i=0;i<n;i++) { for (j=0; j<n;j++) *(trans+j*n+i) = *(mat+i*n+j); } } printmatrix(mat,n) int *mat,n; { int i,j; for(i=0;i<n;i++) { for (j=0; j<n;j++) printf("%d ", *(mat+i*n+j)); printf("\n"); } } Program No : 22 : Write a program in C to print the sum of two N x N Matrix ?

#include<stdio.h> #include<conio.h> main ()

588
{ int *a, *b, *sum; int n,i; printf("N value : \n"); scanf("%d", &n); a=(int *)malloc( n* sizeof(int)); b=(int *)malloc( n* sizeof(int)); sum=(int *)malloc( n* sizeof(int)); for (i=0; i<n; i++) { a[i]=(int *)malloc( n* sizeof(int)); b[i]=(int *)malloc( n* sizeof(int)); sum[i]=(int *)malloc( n* sizeof(int)); } printf("Enter Matrix %d x %d\n", n,n); readmatrix(a,n); printf("Enter Matrix %d x %d\n", n,n); readmatrix(b,n); summatrix(a,b,sum,n); printf("sum %d x %d Matrix\n",n,n); printmatrix(sum,n); getch(); } readmatrix(mat,n) int *mat, n; { int i, j; for(i=0;i<n;i++) for (j=0; j<n;j++) scanf("%d", mat+i*n+j); } summatrix(mat1,mat2,sum,n)

589
int *mat1,*mat2,*sum,n; { int i,j; for(i=0;i<n;i++) { for (j=0; j<n;j++) *(sum+i*n+j) = *(mat1+i*n+j) + *(mat2+i*n+j); } } printmatrix(mat,n) int *mat,n; { int i,j; for(i=0;i<n;i++) { for (j=0; j<n;j++) printf("%d ", *(mat+i*n+j)); printf("\n"); } } Program No : 23 : Write a program in C to print the product of two N x N Matrix ? #include<stdio.h> #include<conio.h> main () { int *a, *b, *pro; int n,i; printf("N value : \n"); scanf("%d", &n); a=(int *)malloc( n* sizeof(int)); b=(int *)malloc( n* sizeof(int)); pro=(int *)malloc( n* sizeof(int)); for (i=0; i<n; i++) { a[i]=(int *)malloc( n* sizeof(int)); b[i]=(int *)malloc( n* sizeof(int));

590
pro[i]=(int *)malloc( n* sizeof(int)); } printf("Enter Matrix %d x %d\n", n,n); readmatrix(a,n); printf("Enter Matrix %d x %d\n", n,n); readmatrix(b,n); promatrix(a,b,pro,n); printf("pro %d x %d Matrix\n",n,n); printmatrix(pro,n); getch(); } readmatrix(mat,n) int *mat, n; { int i, j; for(i=0;i<n;i++) for (j=0; j<n;j++) scanf("%d", mat+i*n+j); } promatrix(mat1,mat2,pro,n) int *mat1,*mat2,*pro,n; { int i,j,k=0;; for(i=0;i<n;i++) { for (j=0; j<n;j++) { *(pro+i*n+j)=0; for(k=0;k<n;k++) *(pro+i*n+j) += *(mat1+i*n+k) * *(mat2+k*n+j); } } } printmatrix(mat,n) int *mat,n; {

591
int i,j; for(i=0;i<n;i++) { for (j=0; j<n;j++) printf("%d ", *(mat+i*n+j)); printf("\n"); } } Program No : 24 : Write a program in C to read and display a M x N Matrix ?

#include<stdio.h> #include<conio.h> main () { int *a, i; int row,col; printf("Enter row and column Size\n"); scanf("%d%d", &row,&col); a=(int *) malloc(row * sizeof(int)); for (i=0; i<row; i++) a[i]=(int *) malloc(col * sizeof(int)); printf("\nEnter %d x %d Matrix\n",row,col); readmatrix(a,row,col); printf("output Matrix\n"); printmatrix(a,row,col); getch(); } readmatrix(mat,row,col) int *mat, row,col; { int i, j;

592
for(i=0;i<row;i++) for (j=0; j<col;j++) scanf("%d", mat+i*col+j); } printmatrix(mat,row,col) int *mat,row,col; { int i,j; for(i=0;i<row;i++) { for (j=0; j<col;j++) printf("%d ", *(mat+i*col+j)); printf("\n"); } } Program No : 25 : Write a program in C to transpose of M x N Matrix ? #include<stdio.h> #include<conio.h> main () { int *a, *b, i; int row,col; printf("Enter row and column Size\n"); scanf("%d%d", &row,&col); a=(int *) malloc(row * sizeof(int)); for (i=0; i<row; i++) a[i]=(int *) malloc(col * sizeof(int)); b=(int *) malloc(col * sizeof(int)); for (i=0; i<col; i++) a[i]=(int *) malloc(row * sizeof(int)); printf("\nEnter %d x %d Matrix\n",row,col); readmatrix(a,row,col);

593
transpose(a,b,row, col); printf("transpose Matrix\n"); printmatrix(b,col,row); getch(); } readmatrix(mat,row,col) int *mat, row,col; { int i, j; for(i=0;i<row;i++) for (j=0; j<col;j++) scanf("%d", mat+i*col+j); } transpose(mat,trans, row,col) int *mat, *trans, row,col; { int i, j; for(i=0;i<row;i++) for (j=0; j<col;j++) *(trans+j*row+i) = *(mat+i*col+j); } printmatrix(mat,row,col) int *mat,row,col; { int i,j; for(i=0;i<row;i++) { for (j=0; j<col;j++) printf("%d ", *(mat+i*col+j)); printf("\n"); } } Program No : 26 : Write a program in C to print the sum of two M x N Matrix ?

594
#include<stdio.h> #include<conio.h> main () { int *a, *b, *sum, i; int row,col; printf("Enter row and column Size\n"); scanf("%d%d", &row,&col); a=(int *) malloc(row * sizeof(int)); b=(int *) malloc(row * sizeof(int)); sum=(int *) malloc(row * sizeof(int)); for (i=0; i<row; i++) { a[i]=(int *) malloc(col * sizeof(int)); b[i]=(int *) malloc(col * sizeof(int)); sum[i]=(int *) malloc(col * sizeof(int)); } printf("\nEnter %d x %d Matrix\n",row,col); readmatrix(a,row,col); printf("\nEnter %d x %d Matrix\n",row,col); readmatrix(b,row,col); summatrix(a,b,sum,row, col); printf("sum Matrix\n"); printmatrix(sum,row,col); getch(); } readmatrix(mat,row,col) int *mat, row,col; { int i, j; for(i=0;i<row;i++) for (j=0; j<col;j++) scanf("%d", mat+i*col+j);

595
} summatrix(mat1,mat2,sum, row,col) int *mat1,*mat2, *sum,row,col; { int i, j; for(i=0;i<row;i++) for (j=0; j<col;j++) *(sum+i*col+j) = *(mat1+i*col+j) + *(mat2+i*col+j); } printmatrix(mat,row,col) int *mat,row,col; { int i,j; for(i=0;i<row;i++) { for (j=0; j<col;j++) printf("%d ", *(mat+i*col+j)); printf("\n"); } } Program No : 27 : Write a program in C to print the sum of two M x N Matrix ?

#include<stdio.h> #include<conio.h> main () { int *a,*b,*pro; int row1,col1, row2, col2,i; printf("Enter row and column Size of matrix one\n"); scanf("%d%d", &row1,&col1); printf("Enter row and column Size of matrix second\n"); scanf("%d%d", &row2,&col2);

596
if(col1!=row2) { printf("Row and Column sizes does not match for product"); getch(); return 0; } a=(int *)malloc(row1 * sizeof(int)); for(i=0; i < row1; i++) a[i]=(int *)malloc(col1 * sizeof(int)); b=(int *)malloc(row2 * sizeof(int)); for(i=0; i < row2; i++) b[i]=(int *)malloc(col2 * sizeof(int)); pro=(int *)malloc(row1 * sizeof(int)); for(i=0; i < row1; i++) pro[i]=(int *)malloc(col2 * sizeof(int)); printf("\nEnter %d x %d Matrix 1 \n",row1,col1); readmatrix(a,row1,col1); printf("\nEnter %d x %d Matrix 2 \n",row2,col2); readmatrix(b,row2,col2); promatrix(a,b,pro,row1,col1,row2,col2); printf("output Matrix\n"); printmatrix(pro,row1,col2); getch(); } readmatrix(mat,row,col) int *mat, row,col; { int i, j; for(i=0;i<row;i++) for (j=0; j<col;j++) scanf("%d", mat+i*col+j);

597
} promatrix(mat1,mat2,pro,row1,col1,row2,col2) int *mat1,*mat2, *pro,row1,col1,row2,col2; { int i, j, k; for(i=0;i<row1;i++) for (j=0; j<col2;j++) { *(pro+i*col2+j)=0; for(k=0; k<col1; k++) *(pro+i*col2+j) += *(mat1+i*col1+k) * *(mat2+k*col2+j); } } printmatrix(mat,row,col) int *mat, row,col; { int i,j; for(i=0;i<row;i++) { for (j=0; j<col;j++) printf("%d ", *(mat+i*col+j)); printf("\n"); } } Program No : 28 : Write a program in C++ to read and display biodata using class ?

#include<iostream.h> #include<conio.h> class biodata { private : char name[20]; int age; float sal; char add[20]; public : void readbio(); void printbio(); }; void biodata::readbio()

598
{ cin>>name>>age>>sal>>add; } void biodata::printbio() { cout<<name<<endl; cout<<age<<endl; cout<<sal<<endl; cout<<add<<endl; } void main() { biodata *pa=new biodata; cout<<"Enter biodata..\n"; pa->readbio(); cout<<"Given biodata..\n"; pa->printbio(); delete pa; getch(); } Program No : 29 : Write a program in C++ to find the area of circle using structures ?

#include<iostream.h> #include<conio.h> class area { private : float rad, ar; public : void read(); float calculatearea(); }; void area::read() { cin>>rad; } float area::calculatearea() {

599
ar=3.1415 * rad * rad; return ar; } void main() { area *pa=new area; cout<<"Enter Rad..\n"; pa->read(); cout<<"Area : "<<pa->calculatearea()<<endl; delete pa; getch(); } Program No : 30 : Write a program in C++ to find the sum of two numbers using function pointers ?

#include<iostream.h> #include<conio.h> void main(void) { int a, b, tot, sum(int,int), (*pt)(int,int); pt=sum; cout<<"Enter numbers"<<endl; cin>>a>>b; tot=(*pt)(a,b); cout<<"Sum : "<<tot<<endl; getch(); } int sum(int a, int b) { return (a+b); } Program No : 31 : Write a program in C++ to find the area of circle using function pointers ?

600
#include<iostream.h> #include<conio.h> void main(void) { float rad, ar, area(float), (*pt)(float); pt=area; cout<<"Enter Radius"<<endl; cin>>rad; ar=(*pt)(rad); cout<<"AREA : "<<ar<<endl; getch(); } float area(float r) { return (3.1415 * r * r); } Program No : 32 : Write a program in C++ to find the a given number is even or odd using function pointers ? #include<iostream.h> #include<conio.h> void main(void) { int num ; void even(void), odd(void), (*pt)(void); cout<<"Enter number"<<endl; cin>>num; if(num%2==0) pt=even; else pt=odd;

601
(*pt)(); getch(); } void even(void) { cout<<"this is even number"<<endl; } void odd(void) { cout<<"this is odd number"<<endl; }

Anda mungkin juga menyukai