Comp Project

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 30

COMPUTER PROJECT

CLASS XI A

ANKUSH SINGH ROLL NO. 21 2/21/2011

INDEX
3. PROGRAMS AND THEIR OUTPUTS . ............................................................................................ 1. ARR_INT 2. ASCII 3. CALL_BY_REFERENCE 4. PALINDROM 5. NUMBER OF PALINDROMS.. 6. NUMBER OF 3s B/W 1 AND 1000. 7. DIVISION UNTIL n PLACES 8. TRIANGLE 1.. 9. TRIANGLE 2 10. TRIANGLE 3 11. TRIANGLE 4 12. FLYING BALLONS.. 13. SHOOTING BALOONS 14. SOUND 15. STRCAT 16. TEXTCOLO 17. MODULUS 18. MIDMAT 19. FIBONACCI 20. GUESS ME .. .. .. .. .. .. . . .. .. .. .. .. .. .. .. . . . . . . .. .. .. .. .. .. .. . . . . .. .. . . .. .

[Type text] ANKUSH SINGH

Page 2

Program - Elements of an Array #include<iostream.h> #include<conio.h> #include<math.h> // STEPS TO TAKE // 1. COUNT THE NUMBER OF DIGITS IN NO // 2. SPLIT THE NUMBER INTO CONSTIUENT DIGITS // 3. PUT THE SPLIT DIGITS IN ARRAY BY RETURNING IN LOOP // FUNCTIONS TO MAKE // 1. COUNT // 2. SPLIT N' ASSSEMBLE int lnth(int); int split(int,int); void main() { clrscr(); int no,len=0,arr[10]; cout<<"Enter a number:"; cin>>no; endl; len=lnth(no); cout<<"The length of number is:"<<len<<endl; for(int i=0,j=len;i<len,j>0;i++,j--) arr[i]=split(no,j); cout<<"\n\nThe elements of the array are:\n"; for(i=0;i<len;i++) cout<<arr[i]<<endl; getch(); } int lnth(int no) [Type text] ANKUSH SINGH Page 3

{ int pwr=0,v1; while(1) { v1=pow10(pwr); if((no%v1)==no) break; else pwr++; } return pwr; } int split(int no,int i) { int v1,v2,splt; v1=pow10(i); v2=pow10(i-1); splt=((no%v1)-(no%v2))/v2;

return splt; } Output of Program Elements of an Array Enter a number:1234 The length of number is:4

The elements of the array are: 1 2 3 4

[Type text] ANKUSH SINGH

Page 4

Program - ASCII #include<iostream.h> #include<conio.h> void main() { clrscr(); char a[100]; cout<<"Enter a String:"; cin.getline(a,100); for(int len=0;a[len]!='\0';len++); int i; cout<<"The ASCII for the above Character(s) are:\n\n"; for(int j=0;j<len;j++) { i=a[j]; if(a[j]==' ') cout<<"For (space)="<<i<<"\n"; else cout<<"For "<<a[j]<<"="<<i<<"\n"; } getch(); } OUTPUT Enter a String: shubham roy The ASCII for the above Character(s) are: For s=115 For h=104 For u=117 For b=98 For h=104 For a=97 [Type text] ANKUSH SINGH Page 5

For m=109 For (space)=32 For r=114 For o=111 For y=121

Program - Call by Reference #include<iostream.h> #include<conio.h> void swap(int &,int &); void main() {

clrscr(); int a,b; int *a1,*a2;

//Decleration of Variables //Declaration of Pointers

a=3; //Initialization b=7; cout<<"original values:"<<a<<" "<<b<<"\n\n"; a1=&a; //Pointing of Variables a2=&b; cout<<a1<<" \n"<<a2<<"\n\n"; swap(a,b); cout<<"aftr swap()\n a="<<a<<" b= "<<b;

getch(); } void swap(int &x, int &y) { int temp,*a1,*a2; temp=x; x=y; y=temp; a1=&x; //Pointing address of Function Variables a2=&y; cout<<"swapped values\n a="<<x<<" b= "<<y<<"\n\n"; cout<<a1<<" \n"<<a2<<"\n\n"; }

[Type text] ANKUSH SINGH

Page 6

Output of Program Call by Reference original values:3 7 0x8f86fff4 0x8f86fff2 swapped values a=7 b= 3 0x8f86fff4 0x8f86fff2 aftr swap() a=7 b= 3

Program - Palindrome #include<iostream.h> #include<conio.h> #include<string.h> #include<stdio.h> void main() { char str[20]; int flag,i,j,len; cout<<"\nEnter a string:"; cin.getline(str,20); for(len=0;str[len]!='\0';len++) {} flag=0; for(i=0,j=len-1;i<len,j>=0;i++,j--) { if(str[i]==str[j]) flag++; else break; } if(flag==len) [Type text] ANKUSH SINGH Page 7

cout<<"yes"; else { cout<<"no"<<"\n\npalindrom till:"<<flag; } getch(); } Output of Program Palindrome Enter a string:roy no palindrom till:0 Enter a string:naman yes Enter a string:123421 no palindrom till:2

Program - Number of Palindromes #include<iostream.h> #include<conio.h> #include<math.h> // STEPS TO TAKE // 1. COUNT THE NUMBER OF DIGITS IN NO // 2. SPLIT THE NUMBER INTO CONSTIUENT DIGITS // 3. PUT THE SPLIT DIGITS IN ARRAY BY RETURNING IN LOOP // 4. PUT A LOOP TO ENTER DIGITS ONE BY ONE AND CHECK FOR PALINDROME // FUNCTIONS TO MAKE // 1. COUNT // 2. SPLIT N' ASSSEMBLE // 3. CHECK PALINDROM (IN MAIN) int lnth(int); int split(int,int); void main() [Type text] ANKUSH SINGH Page 8

{ clrscr(); int no,len=0,arr[10],flag,count=0; for(no=10;no<=30000;no++) { flag=0; len=lnth(no); for(int i=0,j=len;i<len,j>0;i++,j--) arr[i]=split(no,j); for(i=0,j=len-1;i<len,j>=0;i++,j--) { if(arr[i]==arr[j]) flag++; else break; } if(flag==len) count++; } cout<<"\nThe number of palindrom numbers between 1 and 30000 are:"; cout<<count; getch(); } int lnth(int no) { int pwr=0,v1; while(1) { v1=pow10(pwr); if((no%v1)==no) break; else pwr++; } return pwr; } int split(int no,int i) { int v1,v2,splt; v1=pow10(i); [Type text] ANKUSH SINGH Page 9

v2=pow10(i-1); splt=((no%v1)-(no%v2))/v2;

return splt; } Output of Program Number of Palindromes The number of palindrom numbers between 1 and 30000 are:389

Program - Number of 3s #include<iostream.h> #include<conio.h> #include<math.h> // STEPS TO TAKE // 1. COUNT THE NUMBER OF DIGITS IN NO // 2. SPLIT THE NUMBER INTO CONSTIUENT DIGITS // 3. PUT THE SPLIT DIGITS IN ARRAY BY RETURNING IN LOOP // 4. RUN THE CHECK IN EACH ARRAY FOR PRESENCE OF ANY NUMBER // FUNCTIONS TO MAKE // 1. COUNT // 2. SPLIT N' ASSSEMBLE // 3. RUN THE TEST FOR PRESENCE OF n IN EACH ARRAY(IN MAIN) int lnth(int); int split(int,int); void main() { clrscr(); int no,len=0,arr[10],count=0,u_limt,l_limt,n; cout<<"\nEnter the number whose count you want in a range:"; cin>>n; cout<<"\n\n\nEnter the lower limit:"; cin>>l_limt; cout<<"\n\nEnter the upper limit:"; cin>>u_limt;

[Type text] ANKUSH SINGH

Page 10

for(no=l_limt;no<=u_limt;no++) { len=lnth(no); for(int i=0,j=len;i<len,j>0;i++,j--) arr[i]=split(no,j); for(i=0;i<len;i++) { if(arr[i]==n) count++; } } cout<<"\n\n\nThe number of "<<3<<"s in the given range are:"<<count; getch(); } int lnth(int no) { int pwr=0,v1; while(1) { v1=pow10(pwr); if((no%v1)==no) break; else pwr++; } return pwr; } int split(int no,int i) { int v1,v2,splt; v1=pow10(i); v2=pow10(i-1); splt=((no%v1)-(no%v2))/v2;

return splt; } Output of Program Number of 3s Enter the number whose count you want in a range:3 [Type text] ANKUSH SINGH Page 11

Enter the lower limit:10

Enter the upper limit:100

The number of 3s in the given range are:19

Program Division upto n places #include<iostream.h> #include<conio.h> #include<process.h> void main() { clrscr(); int n1,n2; cout<<"\n\n"<<"Enter number to be divided:"; cin>>n1; cout<<endl<<"Enter number by which it has to be divided:"; cin>>n2; int n; cout<<endl<<"Enter the number of places you want to divide:"; cin>>n; int r5; if(n==0) { int r1; r1=n1%n2; r1=r1*10; if(r1==0) { cout<<"\n\nThe division of "<<n1<<" with "<<n2<<" gives: "<<n1/n2; cout<<"\n\n"<<n1<<" is perfectly divisible by "<<n2<<endl; } else { if((r1/n2)>=5) cout<<(n1/n2)+1; else [Type text] ANKUSH SINGH Page 12

cout<<n1/n2; } } else { cout<<"\n\nThe division upto "<<n<<" places is:\n"; int r=n1%n2; if(r==0) { cout<<"\n\nThe division of "<<n1<<" with "<<n2<<" gives: \n"<<n1/n2; cout<<"\n\n"<<n1<<" is perfectly divisible with "<<n2<<endl; } else { cout<<n1/n2<<"."; for(int i=0;i<n;i++) { r=r%n2; if(r==0) { cout<<"\n\n"<<n1<<" is divisible by "<<n2<<" till:\n"; if(i==1) cout<<i<<" place of decimal"; else cout<<i<<" places of decimal"; break; } else { r=r*10; if(i==(n-1)) { r5=(r%n2)*10; if((r5/n2)>=5) cout<<(r/n2)+1; else cout<<r/n2; } else cout<<r/n2; } } } }

[Type text] ANKUSH SINGH

Page 13

getch(); } Output of Program Division upto n places The division upto 10 places is: 3.25 13 is divisible by 4 till: 2 places of decimal Enter number to be divided:61 Enter number by which it has to be divided:3 Enter the number of places you want to divide:7

The division upto 7 places is: 20.3333333 Enter number to be divided:61 Enter number by which it has to be divided:9 Enter the number of places you want to divide:6

The division upto 6 places is: 6.777778

Programmes - Triangles #include<iostream.h> #include<conio.h> void main() { clrscr(); int m=9; for(int i=1;i<=5;i++) { for(int j=1;j<=i;j++) { cout<<"*"; } [Type text] ANKUSH SINGH Page 14

for(int k=1;k<=m;k++) { cout<<"."; } for(int l=1;l<=i;l++) { cout<<"*"; } cout<<"\n"; m-=2; } cout<<"***********"; getch(); } Output of Program Triangle 1 *.........* **.......** ***.....*** ****...**** *****.***** *********** #include<iostream.h> #include<conio.h> void main() { clrscr(); int n=1; int m=1; cout<<"***********"<<"\n"; for(int i=1;i<=5;i++) { for(int j=5;j>=n;j--) { cout<<"*"; } for(int k=1;k<=m;k++) { cout<<"."; } [Type text] ANKUSH SINGH Page 15

for(int l=5;l>=n;l--) { cout<<"*"; } n++; m+=2; cout<<"\n"; }

getch(); } Output of Program Triangle 2 *********** *****.***** ****...**** ***.....*** **.......** *.........*

#include<iostream.h> #include<conio.h> void main() { clrscr(); int k=4,l=1; for(int i=1;i<=5;i++) { for(int j=1;j<=k;j++) { cout<<" "; } for(int m=1;m<=l;m++) { cout<<"*"; } k--; l+=2; cout<<"\n"; } [Type text] ANKUSH SINGH Page 16

getch(); }

Output of Program Triangle 3 * *** ***** ******* ********* #include<iostream.h> #include<conio.h> void main() { clrscr(); int k=9; for(int i=1;i<=5;i++) { for(int j=1;j<=i-1;j++) { cout<<" "; } for(int l=k;l>=1;l--) { cout<<"*"; } cout<<"\n"; k-=2; } getch(); } Output of Program Triangle 4 ********* ******* ***** *** *

[Type text] ANKUSH SINGH

Page 17

Program - Flying balloons #include<dos.h> #include<graphics.h> #include<stdlib.h> #include<stdio.h> #include<conio.h> void main() { /* request autodetection */ int gdriver = EGA, gmode=EGAHI; int midx, midy, i; int xradius = 30, yradius = 40; /* initialize graphics and local variables */ initgraph(&gdriver, &gmode, "C:\\TC\\BGI");

midx = getmaxx() / 2; midy = getmaxy() / 2; setfillstyle(1,4); setcolor(4); for(int y=midy;y>=50;y--) { fillellipse(midx, y, xradius, yradius); delay(30); if(y!=50) cleardevice(); }

getch(); closegraph(); }

[Type text] ANKUSH SINGH

Page 18

Program - Shooting #include<dos.h> #include<graphics.h> #include<stdlib.h> #include<stdio.h> #include<conio.h> void main() { /* request autodetection */ int gdriver=EGA,gmode=EGAHI; int midx,midy; int xradius = 30, yradius = 40; char msg[50]; /* initialize graphics and local variables */ initgraph(&gdriver, &gmode, "C:\\TC\\BGI");

midx = getmaxx() / 2; midy = getmaxy() / 2; setbkcolor(2); setfillstyle(1,4); for(int y=midy+75,stxinc=10,endxinc=100;y>=50,stxinc<=(midx+60),endxinc<=(midx+150);y-,stxinc++,endxinc++) { setcolor(4); fillellipse(midx, y, xradius, yradius); setcolor(1); line(stxinc,60,endxinc,60); delay(30); cleardevice(); if(endxinc==midx-30) break; } cleardevice(); [Type text] ANKUSH SINGH Page 19

int stx_temp,edx_temp; line(stxinc,60,endxinc,60); for(stx_temp=stxinc,edx_temp=endxinc;stx_temp<=midx,edx_temp<=midx+90;stx_temp++,edx _temp++) { cleardevice(); line(stx_temp,60,edx_temp,60); delay(30); } for(stxinc=stx_temp;stxinc<=midx+90;stxinc++) { cleardevice(); line(stxinc,60,midx+90,60); delay(30); } cleardevice(); delay(1000); settextjustify(CENTER_TEXT,CENTER_TEXT); sprintf(msg,"The baloon is popped!!!",midx); outtextxy(midx,midy,msg);

getch(); closegraph(); }

Program SOUND #include<dos.h> #include<iostream.h> int main(int argc, char *argv[]) { unsigned int frequency = 0; cout << "This program makes pc speaker sound at entered frequency.." << endl; do { cout << endl << "Enter in frequency (0 to exit) : ";

[Type text] ANKUSH SINGH

Page 20

cin >> frequency; if (frequency > 0) sound(frequency); } while (frequency != 0); nosound(); return 0; }

Output of Program SOUND

This program makes pc speaker sound at entered frequency..

Enter in frequency (0 to exit) : 1000

SOUND GENERATED

Enter in frequency (0 to exit) : 2000 SOUND GENERATED Enter in frequency (0 to exit) : 100 Enter in frequency (0 to exit) : 0 SOUND GENERATED PROGRAM CLOSED

Program STRCAT #include<string.h> #include<iostream.h> int main(int argc, char *argv[]) { char firstname[40], middlename[40], lastname[40]; char fullname[120+1+1]; // 1+1 for spaces in between first middle and middle last.. cout << "This program concatenates strings to form 1 string.." << endl; cout << "Enter in First name : "; cin >> firstname; cout << "Enter in Middle name : "; cin >> middlename; [Type text] ANKUSH SINGH Page 21

cout << "Enter in Last name : "; cin >> lastname; // Initialize variable.. strcpy(fullname, ""); strcat(fullname, firstname); strcat(fullname, " "); strcat(fullname, middlename); strcat(fullname, " "); strcat(fullname, lastname); cout << endl << "Full name is \"" << fullname << "\"" << endl; return 0; } Output OF Program STRCAT

This program concatenates strings to form 1 string.. Enter in First name : Shubham Enter in Middle name : Roy Enter in Last name : Bhatnagar

Full name is "Shubham Roy Bhatnagar" Program TEXTCOLO

#include<conio.h> #include<iostream.h> int main(int argc, char *argv[]) { int colors = 0; for (colors = 0; colors < 256; colors++) { textattr(colors); cprintf("Hello World!!\n\r"); } return 0; [Type text] ANKUSH SINGH Page 22

} Output of Program TEXTCOLO

Graphical Output cannot be shown

[Type text] ANKUSH SINGH

Page 23

Program MODULUS #include <iostream.h> #include <conio.h> void swap(int a[],int n) { /*Programme to switch negative elements of an array to positive*/ int i=0,t; for(i=0;i<n&&a[i]!='\0';i++) { if(a[i]<0) { t=a[i]; a[i]=a[i]-(2*t); } } } void main() { clrscr(); int i, ar[100],n; cout<<"\n Enter the no. of elements:"; cin>>n; cout << "Enter array elements : "; for (i = 0; i < n; i++) { cout<<"\n Enter element no. "<<i+1<<":"; cin >> ar[i]; } ar[i]='\0'; swap(ar,n); cout << endl; for (i=0; i<n&&ar[i]!='\0'; i++) { cout<<"\n Element no. "<<i+1<<" of the array now is:"; cout << ar[i] << endl; } getch(); } Output of Program MODULUS Enter the no. of elements:2 Enter array elements : Enter element no. 1:-1 Enter element no. 2:-3 Element no. 1 of the array now is:1 Element no. 2 of the array now is:3 [Type text] ANKUSH SINGH Page 24

Program MIDMAT

// Filename: \\U2Chap09\ch09_217.CPP // Function to display the elements which lie on middle of row and column #include <stdio.h> #include <iostream.h> #include <conio.h> const M = 10; const N = 10; void display_RowCol(int Array[M][N], int r, int c) { clrscr(); int row = r / 2; int col = c / 2; // Finding the middle row cout << "Middle Row : "; for(int j=0; j<c; j++) cout << Array[row][j] << " "; cout << endl; // Finding the middle column cout << "Middle Column : "; for(j=0; j<c; j++) cout << Array[j][col] << " "; getch(); } void main() { clrscr(); int Array[M][N]; int i, j; int r, c; cout << "Enter total no. of rows: "; cin >> r; cout << "Enter total no. of columns: "; cin >> c; // Array to be a square matrix with odd dimension if ((r == c) && ((r%2==1) && (c%2==1))) { cout << "Input steps"; cout << "\n\Enter the element in the array\n"; for(i=0; i<r; i++) for(j=0; j<c; j++) { cin >> Array[i][j]; } } else [Type text] ANKUSH SINGH Page 25

{ cout << "Input row and column not valid"; getch(); return; } display_RowCol(Array, r, c); }

Output of Program MIDMAT

Enter total no. of rows: 3 Enter total no. of columns: 3 Input steps Enter the element in the array 1 2 3 4 5 6 7 8 9 Middle Row : 4 5 6 Middle Column : 2 5 8

[Type text] ANKUSH SINGH

Page 26

Program FABONACI

// Filename: \\U1Chap05\ch05_403.CPP // Program to generate a series #include<iostream.h> class Fabonnaci { int a, b, c, N, i; public: Fabonnaci() { a = 1; b = 1; N = 20; i = 2; } void display() { cout << "\n" << a; cout << "\n" << b; while(i<N) { c = a+ b; cout << "\n" << c; i++; a = b; b = c; } } }; void main() { Fabonnaci fab; fab.display(); }; Output of Program FABONACI 1 1 2 3 5 8 [Type text] ANKUSH SINGH Page 27

13 21 34 55 89 144 233 377 610 987 1597 2584 4181 6765

Program GUESSME #include<stdlib.h> #include<iostream.h> class MyChar { private: char mychar; char guesschar; void GenerateChar(void) { randomize(); // from ascii code of 'a' to ascii code of 'z' as 26 chars are available from a to z.. mychar = 'a' + random(26); } int GetAndCheckGuess(int guessno) { cout << endl << "Try #" << (guessno+1) << " : "; cin >> guesschar;

[Type text] ANKUSH SINGH

Page 28

if (guesschar == mychar) return 1; if (((int)mychar - (int)guesschar) < 0) cout << "My char is slightly to the left.."; else cout << "My char is slightly to right.."; return 0; } public: // nturns -> No. of turns to give to user.. void Start(int nturns) { GenerateChar(); cout << "My character is from lower case alphabets.." << endl << endl; for (int i = 0; i < nturns; i++) { if (GetAndCheckGuess(i) == 1) { cout << "Wah.. your intutions are really strong.." << endl; break; } } if (i == nturns) cout << "Nevermind, i should have known that my choice is tough for kids.." << endl << "Anyway, i chose \'" << mychar << "\'"; } }; int main(int argc, char *argv[]) { MyChar guess1; // 5 guesses allowed.. guess1.Start(5); return 0; }

[Type text] ANKUSH SINGH

Page 29

Output of Program GUESSME

Try #1 : s My char is slightly to right.. Try #2 : u My char is slightly to right.. Try #3 : w My char is slightly to right.. Try #4 : y My char is slightly to the left.. Try #5 : x Wah.. your intutions are really strong.. ********* ****** ***

[Type text] ANKUSH SINGH

Page 30

You might also like