Você está na página 1de 128

INDEX

1. Find if a number is an Armstrong number


2. Remove all duplicates from an array
3. Convert decimal to binary
4. Hangman Game
5. Hi-Low Game
6. LCM of 2 numbers
7. Pattern
8. Permutation and Combinations
9. Get a number of random numbers
10. To reverse an array of numbers
11. To reverse an array of letters
12. Sort an array
13. Starline pattern
14. Check palindrome
15. Sort a structure
16. Tic Tac Toe Game (2 players)
17. Linear search
18. Binary search
19. Insertion in sorted array
20. Deletion in sorted array
21. Traversal of array
22. Input and display 2D array in matrix form
23. Find row sum of matrix
24. Find column sum of a matrix
25. Find sum of diagonals of a matrix
26. Create a file
27. Append to a file
28. Read a File
29. Copy to another File
30. Search In A File
31. Modification of File
32. Deletion of record
33. Insertion of record
34. Sorting a file
35. Renaming a file
36. Link list as a stack
37. Link list as a queue

SQL Programs
1. Create a table of Students
2. Display those who have PCM
3. Display average marks of all students
4. Display info of those whose marks lie in 90 to
100
5. Display info of those who have Commerce
and have marks greater than 85
6. Display info of those who doesn’t have PCB
7. Display info of those whose name starts with
the letter K
8. Filter Names Having 6 letters
9. Add a Column of max marks having value 100
10. Sort the table according to marks
11. Find total number of students
12. Find total Number of streams
13. Find Maximum marks of all the streams
Armstrong Number
#include <iostream.h>

#include <conio.h>

#include <stdlib.h>

void main()

clrscr();

int i,l=0,num,s=0;

cout << "Enter A Number: ";

cin >> num;

i=num;

while (i>0)

l = i%10;

s = s+ (l*l*l);

i = i/10;

if (s==num)

cout << num << " Is An ARMSTRONG NUMBER!";

else

cout << num << " Is Not An Armstrong Number";

}
getch();

}
Remove Duplicates from array
#include <iostream.h>

#include <conio.h>

void main()

clrscr();

int a[50];

int n, i, j, count=0;

cout << "How Many Numbers Do You Want To Enter?(Max 50): ";

cin >> n;

for (i=0; i<n; i++)

cout << "Enter Number " << i+1 << ": ";

cin >> a[i];

cout << "\n\nArray Entered: ";

for (i=0; i<n; i++)

cout << a[i] << " ";

for (i=0; i<n-1; i++)

for (j=i+1; j<n; j++)

{
if (a[i] == a[j])

a[j] = 0;

j=0;

cout << "\n\nRemoved Duplicates: ";

for (i=0; i<n; i++)

cout << a[i] << " ";

for (i=0; i<n; i++)

if (a[i] == 0)

count++;

for (i=0; i<n; i++)

if (a[i] != 0)

a[j] = a[i];

j++;

}
for (i= (n - count); i<n; i++)

a[i] = 0;

cout << "\n\nShifting Zeroes To Right: ";

for (i=0; i<n; i++)

cout << a[i] << " ";

getch();

}
Convert Decimal TO Binary
#include <stdio.h>

#include<conio.h>

#include<iostream.h>

int main()

clrscr();

int n,p,c,k,count=0;

cout<<"Enter the number to find ";

cin>>n;

p=n;

while(p)

p=p/10;

count++;

cout<<"in binary number system is:";

if (count=1)

count=2;

c=count*count*count;

for (c; c >= 0; c--)

k = n >> c;

if (k & 1)

cout<<"1";

else

cout<<"0";
}

cout<<"\n";

getch();

return 0;

}
Hangman Game
#include <conio.h>

#include <iostream.h>

#include <string.h>

#include <stdlib.h>

#include <ctype.h>

char secret_name[15];

char name_entered[15];

int chnc = 5;

char countries[10][20] = {"INDIA", "CHINA", "JAPAN", "NEWZEALAND", "TANZANIA", "TURKEY", "TAJIKISTAN",


"MOZAMBIQUE", "LUXEMBOURG"};

void init_name()

for (int i=0; i<15; i++)

secret_name[i] = '\0';

};

void disp_screen()

int i;

gotoxy(3,12);

cout << secret_name;

gotoxy (24, 3);


cout << "Chances: " << chnc;

};

void main()

clrscr();

int i, lvl, l, n;

char ch;

cout << " WELCOME TO HANGMAN!" << endl;

cout << "\n\nThere Are Three Levels To Choose From.\nThen There Comes A Series Of Asteriks and You Have To
Uncover Them By Guessuing The Word Letter By Letter.If You Guess The Correct Letter The Asterik Will Be
Uncovered.\nYou HAve 5 Chances To Do That!.\nAll The Best!";

cout << "\n\nPlease Enter A Level (1 or 2 or 3): ";

cin >> lvl;

switch (lvl)

case 1: randomize();

n = random(10000) % 3;

break;

case 2: randomize();

n = random(10000) % 3 + 3;

break;

case 3: randomize();

n = random(10000) % 3 + 6;

break;

}
strcpy(name_entered, countries[n]);

l = strlen(name_entered);

for (i=0; i<l; i++)

secret_name[i] = '*';

while (strcmpi(name_entered, secret_name) != 0)

int count = 0;

clrscr();

disp_screen();

gotoxy (3, 13);

cout << "\nEnter A Guess: ";

cin >> ch;

for (i=0; i<l; i++)

if (toupper(ch) == name_entered[i])

secret_name[i] = toupper(ch);

count++;

if (count == 0)

chnc--;

}
if (chnc == 0)

cout << "Sorry You Have Been Hanged!";

cout << "\nThe Correct Word Was: " << name_entered;

gotoxy (24, 3);

cout << "Chances: 0";

goto finish;

disp_screen();

gotoxy(3, 14);

cout << "Congrats! You Figured It Out!";

finish: getch();

}
Chances: 5

HHHHHHHHUU

nter A Guess:

Chances: S

t1JZAMB IQUE

EnCongrats! You Figured It Out!


High-Low Game
#include <iostream.h>

#include <conio.h>

#include <stdlib.h>

#include <ctype.h>

void main()

clrscr();

int ch;

char choice;

int n, guess, count = 0;

cout << " Welcome To High Low Game!!!";

cout << "\n1. Rules" << "\n2. Play" << endl;

cout << "\nYour Choice: ";

cin >> ch;

play: switch (ch)

case 1: cout << "The Rules Are Simple:"

<< "\nThe Computer Generates A Number And You Have To Guess The Number 0 < n < 1000"

<< "\nThe Computer Will Then Tell You If Its Higher Or Lower Than The Actual Number"

<< "\nYou Have To Take Minimum Guesses to Find The Number";

cout << "\nWanna Play?(Y/N)";

cin >> choice;


if (toupper(choice) == 'Y')

ch = 2;

goto play;

exit(0);

case 2: clrscr();

count = 0;

randomize();

n = random(10000);

n = n % 1000;

cout << "The Computer Has Decided The Number!";

while (guess != n)

cout << "\nYour Guess: ";

cin >> guess;

if (guess > n)

cout << "The Number Is Lesser Than It" << endl;

count ++;

else if (guess < n)

cout << "The Number Is Greater Than It" << endl;


count++;

cout << "Congartulations! You Took " << count + 1 << " Number Of Guesses";

cout << "\nWanna Go Again?(Y/N): ";

cin >> choice;

if (toupper(choice) == 'Y')

ch = 2;

goto play;

break;

getch();

}
LCM of 2 Numbers
#include <iostream.h>

#include <conio.h>

void main()

clrscr();

int n, n1, n2, i=1;

cout << "Enter The First Number: ";

cin >> n1;

cout << "Enter The Second Number: ";

cin >> n2;

while (n % n1 != 0)

n = n2 * i;

i++;

cout << "\n\n\nThe LCM Of The 2 Numbers Is: " << n;

getch();

}
Pattern
#include <iostream.h>

#include <conio.h>

void main()

clrscr();

int i, j;

char a[6] = {'A','B','C','D','E','F'};

for (i = 0; i<6 ; i++)

for (j = 0; j<=i; j++)

cout << a[j] << " ";

cout << endl;

getch();

}
Permutation And Combination
#include <iostream.h>

#include <conio.h>

#include <math.h>

float fact(int a)

int i;

float ans=1;

for (i=a; i>0; i--)

ans = ans * i;

return ans;

float npr(int n, int r)

float ans;

ans = fact(n) / fact(n-r);

return ans;

float ncr(int n, int r)

float ans;
ans = fact(n) / (fact(n-r) * fact(r));

return ans;

void main()

clrscr();

int ch, n, r;

cout << " MENU";

cout << "\n\n1. Permutation";

cout << "\n2. Combination";

cout << "\n\nYour Choice: ";

cin >> ch;

switch (ch)

case 1: clrscr();

cout << "Enter n: ";

cin >> n;

cout << "Enter r: ";

cin >> r;

cout << "\nAnswer: " << npr(n,r);

break;

case 2: clrscr();

cout << "Enter n: ";


cin >> n;

cout << "Enter r: ";

cin >> r;

cout << "\nAnswer: " << ncr(n,r);

break;

getch();}
Get A Number Of Unique Random Numbers
#include <iostream.h>

#include <stdlib.h>

#include <conio.h>

void main()

clrscr();

int n, a[50], rand_no, num, i, j;

for (i=0; i<50; i++)

a[i] = 0;

cout << "How Many Random Numbers: ";

cin >> n;

cout << "Upto What Number: ";

cin >> num;

cout << "\nThe Random Numbers Generated Are:\n";

for (i=0; i<n; i++)

again: randomize();

rand_no = random(10000) % num;

for (j=0; j<n; j++)

{
if (a[j] == rand_no)

goto again;

a[i] = rand_no;

cout << rand_no << endl;

getch();

}
To reverse An Array Of Numbers
#include <iostream.h>

#include <conio.h>

void main()

clrscr();

int a[50], n, i;

cout << "Enter Number Of Elements: ";

cin >> n;

for (i=0; i<n; i++)

cout << "Enter Number " << i+1 << ": ";

cin >> a[n-i-1];

cout << "\nThe Reversed Array Is: \n";

for (i=0; i<n; i++)

cout << endl << a[i];

getch();

}
Enter Number Of Elements: 5
Enter Number 1: 1
Enter Number z: 3
Enter Number 3: 5
Enter Number 4: 7
Enter Number 5: 9

TI1e Reversed Array Is:

9
7
5
3
1
To Reverse An Array of Letters
#include <iostream.h>

#include <string.h>

#include <dos.h>

#include <conio.h>

void main()

clrscr();

char text[20];

int l;

int i;

cout << "Please Enter A String \n";

cin >> text;

l = strlen(text);

cout << "The reverse of your string is";

for (i = l; i >= 0 ; i -= 1)

cout << text[i];

cout << endl << endl << endl;

cout << "Press Any Key to exit";

getch();

}

1
Sorting An Array
#include <iostream.h>

#include <conio.h>

void main()

clrscr();

int n, num[50], i, j, temp;

cout << "Enter Number Of Elements: ";

cin >> n;

for (i=0; i<n; i++)

cout << "Enter Number " << i+1 << ": ";

cin >> num[i];

for (i=0; i< (n-1); i++)

for (j= (i+1); j<n; j++)

if (num[i] > num[j])

temp = num[i];

num[i] = num[j];

num[j] = temp;

}
cout << "\n\nThe Sorted Array Is: \n";

for (i=0; i<n; i++)

cout << num[i] << endl;

getch();

}
Starline Pattern
#include <iostream.h>

#include <conio.h>

void starline (char a, int n)

int i;

cout << "\n\n";

for (i=0; i<n; i++)

cout << a;

};

void main()

clrscr();

int n;

char a;

cout << "Enter Character: ";

cin >> a;

cout << "How Many Times Do You Want It To Repeat?: ";

cin >> n;

starline (a,n);
getch();

nter Character: f
ow Many Times Do You Want It To Repeat?: 20

fffffffffffffffffff
Check For Palindrome
#include <iostream.h>

#include <conio.h>

#include <string.h>

#include <stdio.h>

void main()

clrscr();

char a[20], a1[20];

cout << "Enter String: ";

cin >> a;

strcpy(a1,a);

strrev(a);

if (strcmpi(a, a1) == 0)

cout << "String Is A Palindrome!";

else

cout << "String Is Not A Palindrome";

getch();

}
Sorting Of Structure
#include <iostream.h>

#include <conio.h>

#include <stdio.h>

#include <string.h>

#include <iomanip.h>

struct stud

int roll;

char name[20];

int marks;

};

void main()

clrscr();

stud s[10];

stud temp;//Temp Will Be Of Student Type as we have to sort whole student data.Not OF Int!

int i, n, j;

cout << "How Many Students?(max 10): ";

cin >> n;

for (i=0; i<n; i++)

cout << "\nEnter Roll No of Student " << i+1 << ": ";

cin >> s[i].roll;


cout << "Enter Name of Student " << i+1 << ": ";

gets(s[i].name);

cout << "Enter Marks of Student " << i+1 << ": ";

cin >> s[i].marks;

for (i=0; i<n-1; i++)

for (j= (i+1); j<n; j++)

if (s[i].roll > s[j].roll)

temp = s[i];

s[i] = s[j];

s[j] = temp;

clrscr();

cout << "The Sorted List Is\n\n";

cout << setw(7) << "Roll No" << setw(20) << "Name" << setw(7) << "Marks" << endl;

for (i=0; i<n; i++)

cout << setw(7) << s[i].roll << setw(20) << s[i].name << setw(7) << s[i].marks << endl;

Getch();
TI1e Sorted List Is
Roll No Name Marks
6 Ishita 90
10 Parth 90
13 Shashwat 90
11 Tl.tshar 90
15 Utkarsh 90
Tic Tac Toe Game
#include <iostream.h>

#include <conio.h>

#include <dos.h>

#include <ctype.h>

char board[9] = {'1', '2', '3', '4', '5', '6', '7', '8', '9'};

char p[2][20];

void init_board()

board[0] = '1';

board[1] = '2';

board[2] = '3';

board[3] = '4';

board[4] = '5';

board[5] = '6';

board[6] = '7';

board[7] = '8';

board[8] = '9';

};

void disp_board ()

int i;

cout << " TIC TAC TOE" << endl

<< p[0] << " <x> " << p[1] << " <o>" << "\n\n";

for (i=0; i<9; i++)


{

if (i%3 == 0)

cout << "\n\n";

cout << board[i] << " ";

cout << "\n\n";

};

int chk_win ()

int i;

for (i=0; i<9; i+=3)

if (board[i] == board[i+1] && board[i+1] == board[i+2])

if (board[i] == 'x')

cout << p[0] << " WINS!!";

else if (board[i] == 'o')

cout << p[1] << " WINS!!";

return 1;

for (i=0; i<3; i++)

{
if (board[i] == board[i+3] && board[i+3] == board[i+6])

if (board[i] == 'x')

cout << p[0] << " WINS!!";

else if (board[i] == 'o')

cout << p[1] << " WINS!!";

return 1;

if (board[0] == board[4] && board[4] == board[8])

if (board[0] == 'x')

cout << p[0] << " WINS!!";

else if (board[i] == 'o')

cout << p[1] << " WINS!!";

return 1;

if (board[2] == board[4] && board[4] == board[6])

if (board[2] == 'x')

{
cout << p[0] << " WINS!!";

else if (board[i] == 'o')

cout << p[1] << " WINS!!";

return 1;

return 0;

};

void main()

again: clrscr();

int c, ch, i, count=0;

char a;

init_board();

cout << " Welcome To Tic Tac Toe" << endl

<< "\nPlayer 1 Please Enter Your Name: ";

cin >> p[0];

cout << "\nNice \nPlayer 2 Your Name: ";

cin >> p[1];

cout <<"\nOkay\nLaunching Game";

for (i=0; i<3; i++)


{

cout << ".";

delay (800);

clrscr();

disp_board();

for (i=0; i<9; i++)

c = i%2;

cout << p[c] << " Your Choice: ";

cin >> ch;

if (ch < 1 || ch > 9 || board[ch-1] == 'x' || board[ch-1] == 'o')

cout << "INVALID CHOICE!" << endl;

delay(600);

i--;

else

if (c == 0)

board[ch-1] = 'x';

else if (c == 1)

board[ch-1] = 'o';

clrscr();

disp_board();
if (chk_win() == 1)

break;

else

count ++;

if (count == 9)

cout << "THE GAME IS DRAWN!!";

cout << "\nWanna Go Again(Y/N): ";

cin >> a;

if (toupper(a) == 'Y')

goto again;

getch();

}
Linear Search
#include<iostream.h>

#include<conio.h>

void main()

clrscr();

int arr[15],i,num,pos=0;

cout<<"Enter numbers: \n";

for(i=0;i<10;i++)

cin>>arr[i];

cout<<"Enter the number to be searched:\n";

cin>>num;

for(i=0;i<10;i++)

if(num==arr[i])

pos=i+1;

cout<<num<<" found at position "<<pos;break;

if(pos==0)

cout<<"Number not found";

getch();
Binary Search
#include<iostream.h>

#include<conio.h>

void main()

clrscr();

int i,arr[10], beg=0, last=9, mid, pos=0,num;

mid=(beg+last)/2;

cout<<"Enter array in ascending order\n";

for(i=0;i<10;i++)

cin>>arr[i];

cout<<"Enter number to be searched\n";

cin>>num;

while(beg<last)

if(arr[mid]==num)

pos=mid+1;

cout<<"Value found at position "<<pos;break;

else if(arr[mid]<num)

beg=mid+1;
else if(arr[mid]>num) last=mid-

1;

mid=(beg+last)/2;

if(pos==0)

cout<<"Number not found";

getch();

}
Insertion In a Sorted Array
#include<iostream.h>

#include<conio.h>

int arr[15];

int findpos(int x)

int pos;

for(int i=0;i<10;i++)

if(arr[i]<=x && arr[i+1]>x)

pos=++i;

return pos;

void main()

clrscr();

int i,num,location;

cout<<"Enter numbers in ascending order\n";

for(i=0;i<10;i++)

cin>>arr[i];

cout<<"Enter new number to be added\n";

cin>>num;

location=findpos(num);
for(i=11;i>location;i--)

arr[i]=arr[i-1];

arr[location]=num;

cout<<"New array \n";

for(i=0;i<11;i++)

cout<<arr[i]<<" ";

getch();

}
Deletion from An Array
#include<iostream.h>

#include<conio.h>

int arr[10];

int findpos(int x)

int pos=0;

for(int i=0;i<10;i++)

if(arr[i]==x)

pos=++i;

return pos;

void main()

clrscr();

int i,num,location;

cout<<"Enter numbers \n";

for(i=0;i<10;i++)

cin>>arr[i];

cout<<"Enter number to be deleted\n";

cin>>num;

location=findpos(num);

if (location==0)

{cout<<"Number to be deleted not found\n";


cout<<"Array remains \n";

for(i=0;i<10;i++)

cout<<arr[i]<<" ";

else

for(i=location-1;i<9;i++)

arr[i]=arr[i+1];

cout<<"New array \n";

for(i=0;i<9;i++)

cout<<arr[i]<<" ";

getch();

}
Traversal In An Array
#include<iostream.h>

#include<conio.h>

void main()

clrscr();

int a[10],i;

cout<<"Enter elements of array\n";

for(i=0;i<10;i++)

cin>>a[i];

cout<<"The elements of array are\n";

for(i=0;i<10;i++)

cout<<a[i]<<" ";

getch();

}
Input And Display Members OF 2D array In
Matrix Form
#include<iostream.h>

#include<conio.h>

void main()

clrscr();

int a[3][3],i,j;

cout<<"Enter elements of 2D array\n";

for(i=0;i<3;i++)

for(j=0;j<3;j++)

cin>>a[i][j];

cout<<"The elements of 2D array in matrix form are\n";

for(i=0;i<3;i++)

for(j=0;j<3;j++)

cout<<a[i][j]<<" ";

if(j==2)

cout<<endl;

getch();

}
i
To Find Row Sum Of Matrix
#include<iostream.h>

#include<conio.h>

void main()

clrscr();

int a[3][3],i,j,r,sum=0;

cout<<"Enter elements of 2D array\n";

for(i=0;i<3;i++)

for(j=0;j<3;j++)

cin>>a[i][j];

cout<<"Enter the row number whose sum of elements is required";

cin>>r;

for(i=0;i<3;i++)

for(j=0;j<3;j++)

if(i==r-1)

sum+=a[i][j];

cout<<"Required sum is "<<sum;

getch();

}
sum
To Find Column Sum Of A Matrix
#include<iostream.h>

#include<conio.h>

void main()

clrscr();

int a[3][3],i,j,r,sum=0;

cout<<"Enter elements of 2D array\n";

for(i=0;i<3;i++)

for(j=0;j<3;j++)

cin>>a[i][j];

cout<<"Enter the column number whose sum of elements is required";

cin>>r;

for(i=0;i<3;i++)

for(j=0;j<3;j++)

if(j==r-1)

sum+=a[i][j];

cout<<"Required sum is "<<sum;

getch();
sum i
To Find Diagonal Sum Of A Matrix
#include<iostream.h>

#include<conio.h>

void main()

clrscr();

int a[3][3],i,j,r,sum1=0,sum2=0;

cout<<"Enter elements of 2D array\n";

for(i=0;i<3;i++)

for(j=0;j<3;j++)

cin>>a[i][j];

for(i=0;i<3;i++)

for(j=0;j<3;j++)

if(i==j)

sum+=a[i][j];

cout<<"Required sum is "<<sum;

getch();
Create A File
#include <fstream.h>

#include <conio.h>

#include <stdio.h>

#include <iomanip.h>

#include <string.h>

#include <process.h>

class student

public:

int roll;

char name[20];

int marks;

void input()

cout << "\nEnter Roll No: ";

cin >> roll;

cout << "Enter Name: ";

gets(name);

cout << "Enter Marks: ";

cin >> marks;

void display()

{
cout << setw (7) << roll << setw (10) << name << setw (8) << marks << endl;

};

void main()

clrscr();

char fname[20], str[50], ch, str1[20];

int n2;

student s1;

fstream f;

cout << "1. Text File"

<< "\n2. Binary File";

cout << "\n\nYour Choice: ";

cin >> n2;

switch (n2)

case 1: cout << "Enter Name Of File (Add '.txt' at end): ";

gets(fname);

f.open (fname, ios::out | ios::noreplace);


do

cout << "Enter Text: ";

gets (str);

f << str;

cout << "Want To Enter More? (Y/N): ";

cin >> ch;

} while (ch == 'Y' || ch == 'y');

f.close();

getch();

break;

case 2: cout << "Enter Name Of File (Add '.dat' at end): ";

gets(fname);

f.open (fname, ios::out | ios::binary | ios::noreplace);

do

s1.input();

f.write((char *) &s1, sizeof(s1));

cout << "\nWant To Add More? (Y/N): ";

cin >> ch;


} while (ch == 'y' || ch == 'Y');

f.close();

getch();

break;

}
Append a File
#include <fstream.h>

#include <conio.h>

#include <stdio.h>

#include <iomanip.h>

#include <string.h>

#include <dos.h>

#include <process.h>

class student

public:

int roll;

char name[20];

int marks;

void input()

cout << "\nEnter Roll No: ";

cin >> roll;

cout << "Enter Name: ";

gets(name);

cout << "Enter Marks: ";

cin >> marks;

void display()
{

cout << setw (7) << roll << setw (10) << name << setw (8) << marks << endl;

};

void main()

clrscr();

char fname[20], str[50], ch, str1[20];

int n2;

student s1;

fstream forg;

cout << "1. Text File"

<< "\n2. Binary File";

cout << "\n\nYour Choice: ";

cin >> n2;

switch (n2)

case 1: cout << "Enter Name Of File (Add '.txt' at end): ";

gets(fname);

forg.open (fname, ios::out | ios::app | ios::nocreate);


if (!forg)

cout << "File Doesnt Exist!!";

getch();

break;

do

cout << "Enter Text: ";

gets (str);

forg << str;

cout << "Want To Enter More? (Y/N): ";

cin >> ch;

} while (ch == 'Y' || ch == 'y');

forg.close();

getch();

break;

case 2: cout << "Enter Name Of File (Add '.dat' at end): ";

gets(fname);

forg.open (fname, ios::out | ios::binary | ios::app | ios::nocreate);

if (!forg)

{
cout << "File Doesnt Exist!!";

getch();

break;

do

s1.input();

forg.write((char *) &s1, sizeof(s1));

cout << "\nWant To Add More? (Y/N): ";

cin >> ch;

} while (ch == 'y' || ch == 'Y');

forg.close();

getch();

break;

}
Read A File
#include <fstream.h>

#include <conio.h>

#include <stdio.h>

#include <iomanip.h>

#include <string.h>

#include <dos.h>

#include <process.h>

class student

public:

int roll;

char name[20];

int marks;

void input()

cout << "\nEnter Roll No: ";

cin >> roll;

cout << "Enter Name: ";

gets(name);

cout << "Enter Marks: ";

cin >> marks;

void display()
{

cout << setw (7) << roll << setw (10) << name << setw (8) << marks << endl;

};

void main()

clrscr();

char fname[20], str[50], ch, str1[20];

int n2;

student s1;

fstream f;

cout << "Enter Name Of File You Want To Read From: ";

gets(fname);

f.open (fname , ios::in | ios::binary);

cout << setw (7) << "Roll No" << setw (10) << "Name" << setw (8) << "Marks" << endl;

while (f.read ((char *) &s1, sizeof(s1)))

s1.display();

getch();
Copy A File
#include <fstream.h>

#include <conio.h>

#include <stdio.h>

#include <iomanip.h>

#include <string.h>

#include <dos.h>

#include <process.h>

class student

public:

int roll;

char name[20];

int marks;

void input()

cout << "\nEnter Roll No: ";

cin >> roll;

cout << "Enter Name: ";

gets(name);

cout << "Enter Marks: ";

cin >> marks;

void display()
{

cout << setw (7) << roll << setw (10) << name << setw (8) << marks << endl;

};

void main()

clrscr();

char fname[20], str[50], ch, str1[20];

student s1;

fstream f, fnew;

cout << "Enter Name Of File You Want To Copy FROM: ";

gets(fname);

f.open (fname, ios::in | ios::binary);

if(!f)

cout << "File Doesnt Exists!!";

getch();

cout << "Enter Name Of File You Want To Copy TO: ";

gets(fname);
fnew.open (fname, ios::out | ios::binary | ios::noreplace);

if(!fnew)

cout << "File Name Already Exists!!";

getch();

while (f.read((char*) &s1, sizeof(s1)))

fnew.write ((char*) &s1, sizeof(s1));

f.close();

fnew.close();

getch();

}
Search In A File
#include <fstream.h>

#include <conio.h>

#include <stdio.h>

#include <iomanip.h>

#include <string.h>

#include <dos.h>

#include <process.h>

class student

public:

int roll;

char name[20];

int marks;

void input()

cout << "\nEnter Roll No: ";

cin >> roll;

cout << "Enter Name: ";

gets(name);

cout << "Enter Marks: ";

cin >> marks;

void display()
{

cout << setw (7) << roll << setw (10) << name << setw (8) << marks << endl;

};

void main()

clrscr();

char fname[20], str[50], ch, str1[20];

long pos;

int n1, n2, n, i=0;

student s1, s2, s3[11], temp;

fstream forg, fnew;

cout << "Enter The Name Of The File (Add '.dat'): ";

gets(fname);

forg.open (fname, ios::in | ios::binary);

if (!forg)

cout << "File Doesnt Exist!!";

getch();

break;

}
cout << "Okay!";

delay (500);

clrscr();

cout << "1. By Name"

<< "\n2. By Roll Number";

cout << "\n\nYour Choice: ";

cin >> n2;

switch (n2)

case 1: clrscr();

cout << "Enter Name You Want To Search: ";

gets (str1);

cout << setw (7) << "Roll No" << setw (10) << "Name" << setw (8) << "Marks" << endl;

while (forg.read ((char*) &s1, sizeof(s1)))

if (strcmpi (str1, s1.name) == 0)

s1.display();

break;

break;
case 2: clrscr();

cout << "Enter Roll Number You Want To Search: ";

cin >> n;

cout << setw (7) << "Roll No" << setw (10) << "Name" << setw (8) << "Marks" << endl;

while (forg.read ((char*) &s1, sizeof(s1)))

if (s1.roll == n)

s1.display();

break;

break;

forg.close();

getch();

}
Modification Of File
#include <fstream.h>

#include <conio.h>

#include <stdio.h>

#include <iomanip.h>

#include <string.h>

#include <dos.h>

#include <process.h>

class student

public:

int roll;

char name[20];

int marks;

void input()

cout << "\nEnter Roll No: ";

cin >> roll;

cout << "Enter Name: ";

gets(name);

cout << "Enter Marks: ";

cin >> marks;

void display()
{

cout << setw (7) << roll << setw (10) << name << setw (8) << marks << endl;

};

void main()

clrscr();

char fname[20], str[50], ch, str1[20];

long pos;

int n1, n2, n, i=0;

student s1, s2, s3[11], temp;

fstream forg, fnew;

cout << "Enter The Name Of The File (Add '.dat'): ";

gets(fname);

forg.open (fname, ios::in | ios::out | ios::binary);

cout << "Okay!";

delay (500);

forg.close();

clrscr();
cout << "1. By Name"

<< "\n2. By Roll Number"

<< "\n3. By Record Number";

cout << "\n\nYour Choice: ";

cin >> n2;

cout << "\n\n";

do

forg.open (fname, ios::in | ios::out | ios::binary);

switch (n2)

case 1: cout << "Enter Name You Want To Edit: ";

gets(str);

while (forg.read ((char *) &s1, sizeof(s1)))

if (strcmpi(s1.name, str) == 0)

pos = forg.tellg() - sizeof(s1);

break;

break;

case 2: cout << "Enter Roll number You Want To Edit: ";

cin >> n;
while (forg.read ((char *) &s1, sizeof(s1)))

if (s1.roll == n)

pos = forg.tellg() - sizeof(s1);

break;

break;

case 3: cout << "Enter Record Number You Want To Modify With: ";

cin >> n;

pos = (n-1) * sizeof(s1);

break;

forg.seekg(pos);

s1.input();

forg.write ((char*)&s1, sizeof(s1));

clrscr();

cout << "New Data: \n\n\n";

cout << setw (7) << "Roll No" << setw (10) << "Name" << setw (8) << "Marks" << endl;
forg.close();

forg.open (fname, ios::in | ios::binary);

while (forg.read ((char*) &s1, sizeof(s1)))

s1.display();

forg.close();

cout << "\n\nWanna Modify More: ";

cin >> ch;

} while (ch == 'y' || ch == 'Y');

getch();

}
Delete A Record
#include <fstream.h>

#include <conio.h>

#include <stdio.h>

#include <iomanip.h>

#include <string.h>

#include <dos.h>

#include <process.h>

class student

public:

int roll;

char name[20];

int marks;

void input()

cout << "\nEnter Roll No: ";

cin >> roll;

cout << "Enter Name: ";

gets(name);

cout << "Enter Marks: ";

cin >> marks;

void display()
{

cout << setw (7) << roll << setw (10) << name << setw (8) << marks << endl;

};

void main()

clrscr();

char fname[20], str[50], ch, str1[20];

long pos;

int n1, n2, n, i=0;

student s1, s2, s3[11], temp;

fstream forg, fnew;

cout << "Enter The Name Of The File (Add '.dat'): ";

gets(fname);

forg.open (fname, ios::in | ios::out | ios::binary);

fnew.open ("newstudent.dat", ios::out | ios::binary);

cout << "Okay!";

delay (500);

clrscr();

cout << "1. By Name"


<< "\n2. By Roll Number"

<< "\n3. By Record Number";

cout << "\n\nYour Choice: ";

cin >> n2;

cout << "\n\n";

switch (n2)

case 1: cout << "Enter Name You Want To Delete: ";

gets(str);

while (forg.read((char*) &s1, sizeof(s1)))

if (strcmpi(str, s1.name) != 0)

fnew.write ((char*) &s1, sizeof(s1));

break;

case 2: cout << "Enter Roll Number Of Record You Want To Delete: ";

cin >> n;

while (forg.read((char*) &s1, sizeof(s1)))

if (s1.roll != n)

fnew.write ((char*) &s1, sizeof(s1));

}
}

break;

case 3: cout << "Enter The Record Number You Want To Delete: ";

cin >> n;

while (forg.read((char*) &s1, sizeof(s1)))

i++;

if (i != n)

fnew.write ((char*) &s1, sizeof(s1));

break;

forg.close();

fnew.close();

remove (fname);

rename ("newstudent.dat" , fname);

cout << "\n\nNew Data:\n\n ";

cout << setw (7) << "Roll No" << setw (10) << "Name" << setw (8) << "Marks" << endl;
forg.open (fname, ios::in | ios::binary);

while (forg.read ((char*) &s1, sizeof(s1)))

s1.display();

forg.close();

getch();

}
Insert A Record
#include <fstream.h>

#include <conio.h>

#include <stdio.h>

#include <iomanip.h>

#include <string.h>

#include <dos.h>

#include <process.h>

class student

public:

int roll;

char name[20];

int marks;

void input()

cout << "\nEnter Roll No: ";

cin >> roll;

cout << "Enter Name: ";

gets(name);

cout << "Enter Marks: ";

cin >> marks;

void display()
{

cout << setw (7) << roll << setw (10) << name << setw (8) << marks << endl;

};

void main()

clrscr();

char fname[20], str[50], ch, str1[20];

long pos;

int n1, n2, n, i=0;

student s1, s2, s3[11], temp;

fstream forg, fnew;

cout << "Enter The Name Of The File (Add '.dat'): ";

gets(fname);

forg.open (fname, ios::in | ios::out | ios::binary);

fnew.open ("newstudent.dat", ios::out | ios::binary);

cout << "Enter The Roll Number After Which You Want To Add: ";

cin >> n;

while (forg.read ((char*) &s1, sizeof(s1)))

fnew.write ((char*)&s1, sizeof(s1));


if (s1.roll == n)

s2.input();

fnew.write ((char*) &s2, sizeof(s2));

forg.close();

fnew.close();

remove (fname);

rename ("newstudent.dat" , fname);

cout << "\n\nNew Data:\n\n ";

cout << setw (7) << "Roll No" << setw (10) << "Name" << setw (8) << "Marks" << endl;

forg.open (fname, ios::in | ios::binary);

while (forg.read ((char*) &s1, sizeof(s1)))

s1.display();

forg.close();

getch();

}
Sorting A File
#include <fstream.h>

#include <conio.h>

#include <stdio.h>

#include <iomanip.h>

#include <string.h>

#include <dos.h>

#include <process.h>

class student

public:

int roll;

char name[20];

int marks;

void input()

cout << "\nEnter Roll No: ";

cin >> roll;

cout << "Enter Name: ";

gets(name);

cout << "Enter Marks: ";

cin >> marks;

void display()
{

cout << setw (7) << roll << setw (10) << name << setw (8) << marks << endl;

};

void main()

clrscr();

char fname[20], str[50], ch, str1[20];

long pos;

int n1, n2, n, i=0;

student s1, s2, s3[11], temp;

fstream forg, fnew;

cout << "Enter The Name Of The File (Add '.dat'): ";

gets(fname);

forg.open (fname, ios::in | ios::out | ios::binary);

while (forg.read ((char *) &s1, sizeof(s1)))

s3[i] = s1;

i++;

for (int j=0; j < i; j++)


{

for (int k = 0; k < i+1; k++)

if (s3[j].roll < s3[k].roll)

temp = s3[k];

s3[k] = s3[j];

s3[j] = temp;

cout << setw (7) << "Roll No" << setw (10) << "Name" << setw (8) << "Marks" << endl;

forg.close();

forg.open (fname, ios::out | ios::binary);

for (j=0; j<i; j++)

forg.write ((char*) &s3[j], sizeof(s3[j]));

s3[j].display();

forg.close();

getch();

}
Rename A File
#include <fstream.h>

#include <conio.h>

#include <stdio.h>

void main()

Char str[20], str1[20];

clrscr();

cout << “Enter Original File Name: “;

gets(str);

cout << “Enter New Name: “;

gets(str1);

rename(str, str1);

getch();

}
Link List As A Stack
#include <iostream.h>

#include <conio.h>

struct node

int e;

node* link;

};

class stack

node *top, *ptr;

public:

stack()

top = NULL;

~stack()

while(ptr != NULL)

ptr = top;

top = top->link;

delete ptr;

}
void push();

void display();

void pop();

};

void stack::push()

ptr = new node; ptr-

>link = NULL;

cout << "Enter Element: ";

cin >> ptr->e;

if (top == NULL)

top = ptr;

else

ptr->link = top;

top = ptr;

void stack::display()

if (top == NULL)

cout << "Empty Stack!";

else

{
ptr = top;

while (ptr != NULL)

cout << ptr->e << " ";

ptr = ptr->link;

void stack::pop()

ptr = top;

if (top == NULL)

cout << "Empty Stack!\n";

cout << "Underflow";

else

cout << "Element: " << ptr->e;

top = top->link;

delete ptr;

cout << "\nDeleted!";

void main()

stack s1;
int n;

do

clrscr();

cout << " MAIN MENU\n";

cout << "\n1. Push";

cout << "\n2. Display";

cout << "\n3. Pop";

cout << "\n4. Exit";

cout << "\n\nYour Choice: ";

cin >> n;

switch(n)

case 1: clrscr();

s1.push();

break;

case 2: clrscr();

s1.display();

getch();

break;

case 3: clrscr();

s1.pop();

getch();

break;

default: cout << "\a";


}

}while (n!=4);

cout << "\n\nThanks For Using!!";

getch();

}
Link List As A Queue
#include <iostream.h>

#include <conio.h>

struct node

int e;

node* link;

};

class queue

node *front, *rear, *ptr;

public:

queue()

rear = NULL;

front = NULL;

void queins();

void quedel();

void quedisp();

};

void queue::queins()

ptr = new node; ptr-

>link = NULL;
cout << "Enter Element: ";

cin >> ptr->e;

if (rear == NULL)

front = rear = ptr;

else

rear->link = ptr;

rear = ptr;

void queue::quedisp()

if (front == NULL)

cout << "Empty Queue!";

else

ptr = front;

while (ptr!=NULL)

cout << ptr->e << " ";

ptr = ptr->link;

}
void queue::quedel()

ptr = front;

if (front == NULL)

cout << "Underflow";

else

cout << "Element: " << ptr->e;

front = front->link;

delete ptr;

cout << "\nDeleted";

if (front == NULL)

rear = NULL;

void main()

queue q1;

int n;

do

{
clrscr();

cout << " MAIN MENU\n";

cout << "\n1. Insert";

cout << "\n2. Display";

cout << "\n3. Delete";

cout << "\n4. Exit";

cout << "\n\nYour Choice: ";

cin >> n;

switch(n)

case 1: clrscr();

q1.queins();

break;

case 2: clrscr();

q1.quedisp();

getch();

break;

case 3: clrscr();

q1.quedel();

getch();

break;

default: cout << "\a";

}while (n!=4);

cout << "\n\nThanks For Using!!";


getch();

miN MENU

1. Insert
2. Display Enter Element: Zl
3. Delete
14. Exit
our Choice: 1

miN MENU

1. Insert
- Display
- Delete
!4. Exit
fe 21 22
our Choice: 2_

miN MENU

1. Insert
- Display
- Delete
lerrent: 20
14 . Exit
1 e leted-
our Choice: 3
Table For SQL Programs
CREATE A TABLE OF STUDENTS
Display those Having PCM
Display Average Marks Of All Students
Display Info Of those whose marks lie in
90 to 100
Display info of those who have commerce
and have marks greater than 85
Display Info Of those who doesn’t have
PCB
Display Info Of those whose name Starts
with “K”
Filter Names Having 6 Letters
Add A column Of Max Marks having value
100
Sort Table By Marks
Find Total Number Of Students
Find Total Number Of Streams
Find Maximum Marks OF All The Streams

Você também pode gostar