37
 1 Program 1 //PROGRAM TO SHOW WORKING OF A BINARY FILE #include<fstream> #include<iostream> #include<cstdlib> using namespace std; int read_file(); int write_file(); class student{ int roll_no,reg_no; char name[50]; public: void input(); void output(); }; void student::input() { cout<<"Enter Registration Number: "; cin>>reg_no; cout<<"Enter Name: "; cin>>name; cout<<"Enter the Roll Number: "; cin>>roll_no; } void student::output() { cout<<"Registration Number: "<<reg_no<<endl; cout<<"Name: "<<name<<endl; cout<<"Roll Number: "<<roll_no<<endl; } int main() { write_file(); cout<<"The Content of the File is: "<<endl; read_file(); return 0; } int write_file() { student s1; int loop; char file_name[50]; cout<<"Enter the File Name: (without Spaces)"; cin>>file_name; fstream ifile(file_name,ios::binary|ios::out); cout<<"How Many Records to be entered? : "; cin>>loop; for (int i=1; i<=loop; i++) { s1.input(); ifile.write((char*)&s1,sizeof(s1)); if (i!=loop) { cout<<"Enter Next Record-----------------"<<endl<<endl; } } return 0;

c++ program list

  • Upload
    adwadaw

  • View
    220

  • Download
    0

Embed Size (px)

DESCRIPTION

List of C++ Programs

Citation preview

7/17/2019 c++ program list

http://slidepdf.com/reader/full/c-program-list-568c19b8cef8b 1/37

 1 

Program 1 //PROGRAM TO SHOW WORKING OF A BINARY FILE

#include<fstream>#include<iostream>#include<cstdlib>using namespace std;int read_file();int write_file();class student{

int roll_no,reg_no;char name[50];public:void input();void output();

};void student::input()

{ cout<<"Enter Registration Number: ";cin>>reg_no;cout<<"Enter Name: ";cin>>name;cout<<"Enter the Roll Number: ";cin>>roll_no;

}void student::output(){

cout<<"Registration Number: "<<reg_no<<endl;cout<<"Name: "<<name<<endl;

cout<<"Roll Number: "<<roll_no<<endl;}int main(){

write_file();cout<<"The Content of the File is: "<<endl;read_file();return 0;

}int write_file(){

student s1;

int loop;char file_name[50];cout<<"Enter the File Name: (without Spaces)";cin>>file_name;fstream ifile(file_name,ios::binary|ios::out);cout<<"How Many Records to be entered? : ";cin>>loop;for (int i=1; i<=loop; i++){

s1.input();ifile.write((char*)&s1,sizeof(s1));if (i!=loop)

{ cout<<"Enter Next Record-----------------"<<endl<<endl;}

}return 0;

7/17/2019 c++ program list

http://slidepdf.com/reader/full/c-program-list-568c19b8cef8b 2/37

 2 

}int read_file(){

system("cls");student s2;char file_name[50];

cout<<"Enter the name of file to be read:";cin>>file_name;fstream ofile(file_name,ios::binary|ios::in);while(ofile){

ofile.read((char*)&s2,sizeof(student));s2.output();

}return 0;

}

Output

Enter the File Name: (without Spaces)arfatHow Many Records to be entered? : 2

Enter Registration Number: 1000

Enter Name: arfat

Enter the Roll Number: 1

Enter Next Record-----------------

Enter Registration Number: 1001

Enter Name: salman

Enter the Roll Number: 2

The Content of the File is:

Enter the name of file to be read:arfatRegistration Number: 1000

Name: arfat

Roll Number: 1

Registration Number: 1001

Name: salman

Roll Number: 2

Registration Number: 1001

Name: salman

Roll Number: 2

Process returned 0 (0x0) execution time : 20.588 sPress any key to continue. 

7/17/2019 c++ program list

http://slidepdf.com/reader/full/c-program-list-568c19b8cef8b 3/37

 3 

Program 2//DELETING RECORD FROM A BINARY FILE

#include<fstream>#include<iostream>#include<cstdlib>using namespace std;int read_file();int write_file();int delRecord();int copyToFile();class student{

int roll_no,reg_no;char name[50];char status;public:student()

{ status = 'A';}void input();void output();char getStatus(){

return status;}void del(){

status = 'D';

}int getRegNumber(){

return reg_no;}

};void student::input(){

cout<<"Enter Registration Number: ";cin>>reg_no;cout<<"Enter Name: ";cin>>name;

cout<<"Enter the Roll Number: ";cin>>roll_no;}void student::output(){

cout<<"Registration Number: "<<reg_no<<endl;cout<<"Name: "<<name<<endl;cout<<"Roll Number: "<<roll_no<<endl;cout<<"Status: "<<status<<endl;

}int main(){

write_file();read_file();delRecord();read_file();return 0;

7/17/2019 c++ program list

http://slidepdf.com/reader/full/c-program-list-568c19b8cef8b 4/37

 4 

}int write_file(){

student s1;int loop;char file_name[50]="stu.dat";

fstream ifile(file_name,ios::binary|ios::out);cout<<"How Many Records to be entered? : ";cin>>loop;for (int i=1; i<=loop; i++){

s1.input();ifile.write((char*)&s1,sizeof(s1));if (i!=loop){

cout<<"\nEnter Next Record-----------------"<<endl<<endl;}

}

return 0;}int read_file(){

system("cls");student s2;char file_name[50]="stu.dat";fstream ofile(file_name,ios::binary|ios::in);while(ofile){

ofile.read((char*)&s2,sizeof(student));s2.output();

}return 0;

}int delRecord(){

student s2;int temp_reg,cursor,counter=0,nbr;char ch;cout<<"Enter The reg number whose record you want to delete: ";cin>>temp_reg;fstream rfile("stu.dat",ios::binary|ios::out|ios::in);rfile.seekg(0,ios::end);

nbr=(rfile.tellg())/sizeof(student);rfile.seekg(0);while(rfile){

++counter;rfile.read((char*)&s2,sizeof(student));if (s2.getRegNumber()==temp_reg){

cursor = (counter-1)*sizeof(student);s2.output();cout<<"\nDo you want to Delete this record [y/n]: ";cin>>ch;

if (ch=='y'||ch=='Y'){

rfile.seekp(cursor,ios::beg);s2.del();rfile.write((char*)&s2,sizeof(student));

7/17/2019 c++ program list

http://slidepdf.com/reader/full/c-program-list-568c19b8cef8b 5/37

 5 

rfile.close();copyToFile();break;

}else{

cout<<"\nRecord Was not deleted. ";}}else if (counter = nbr){

cout<<"\nRecord Does not Exist.\n";}

}return 0;

}int copyToFile(){

student s3;fstream stu("stu.dat",ios::binary|ios::in);fstream temp("temp.dat",ios::binary|ios::out);while(stu){

stu.read((char*)&s3,sizeof(student));if (s3.getStatus()=='A'){

temp.write((char*)&s3,sizeof(student));}

}stu.close();

temp.close();remove("stu.dat");rename("temp.dat","stu.dat");return 0;

}

Output

Enter the File Name: (without Spaces)arfat

How Many Records to be entered? : 2

Enter Registration Number: 1000

Enter Name: arfat

Enter the Roll Number: 1

Enter Next Record-----------------

Enter Registration Number: 1001

Enter Name: salman

Enter the Roll Number: 2

The Content of the File is:

Enter the name of file to be read:arfat

Registration Number: 1000

Name: arfat

Roll Number: 1

Registration Number: 1001

Name: salman

Roll Number: 2

Enter The reg number whose record you want to delete:1001

Record Deleted.

7/17/2019 c++ program list

http://slidepdf.com/reader/full/c-program-list-568c19b8cef8b 6/37

 6 

Program 3//PROGRAM TO SHOW THE WORKING OF A LINKED LIST AS A QUEUE

#include<iostream.h>#include<conio.h>#include<process.h>#include<stdio.h>#include<string.h>struct node{char name[20];int age;node *link;}*ptr=NULL,*save=NULL;

class queue{node *rear,*front;public:queue()

{rear=NULL;front=NULL;}

void queins();void quedel();void display();}q;

void queue::queins(){ptr=new node;if(ptr==NULL){cout<<"Queue overflow ";}

else{cout<<"Enter the name ";gets(ptr->name);cout<<"Enter the age ";cin>>ptr->age;ptr->link=NULL;if(rear==NULL){rear=ptr;front=ptr;}else{rear->link=ptr;

rear=ptr;}}

}void queue::quedel(){if(rear==NULL){cout<<"Queue underflow ";}else{if(front==rear){save=front;front=NULL;

rear=NULL;cout<<"Name ";puts(save->name);cout<<"Age "<<save->age;delete save;

7/17/2019 c++ program list

http://slidepdf.com/reader/full/c-program-list-568c19b8cef8b 7/37

 7 

}else{save=front;front=front->link;cout<<"Name ";puts(save->name);

cout<<"Age "<<save->age;delete save;}

}}

void queue::display(){if(rear==NULL){cout<<"No elements ";}else{ptr=front;while(ptr!=NULL)

{cout<<"Name ";puts(ptr->name);cout<<"Age "<<ptr->age;ptr=ptr->link;}

}}

int main(){clrscr();int ch;

X:cout<<"\nEnter your choice\n1.Insert\n2.Delete\n3.Display\n4.Exit\n";cin>>ch;switch(ch){case 1:q.queins();

goto X;case 2:q.quedel();

goto X;case 3:q.display();

goto X;

case 4:exit(0);

default:cout<<"Wrong choice ";goto X;

}getch();}

OUTPUT

1.Insert

2.Delete

3.Display

4.Exit1

Enter the name Arfat

Enter the age 15

7/17/2019 c++ program list

http://slidepdf.com/reader/full/c-program-list-568c19b8cef8b 8/37

 8 

Enter your choice

1.Insert

2.Delete

3.Display

4.Exit

1

Enter the name parikshitEnter the age 18

Enter your choice

1.Insert

2.Delete

3.Display

4.Exit

1

Enter the name shalabh

Enter the age 17

Enter your choice

1.Insert

2.Delete

3.Display

4.Exit

2

Name krishna

Age 15

Enter your choice

1.Insert

2.Delete3.Display

4.Exit

3

Name parikshit

Age 18Name shalabh

Age 17

7/17/2019 c++ program list

http://slidepdf.com/reader/full/c-program-list-568c19b8cef8b 9/37

 9 

Program 4//PROGRAM TO SHOW THE WORKING OF A LINKED LIST AS STACK

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

struct node{char name[20];int age;node *link;}*ptr=NULL,*save=NULL;

class stack{node *top;public:stack(){top=NULL;}

void stackpush();void stackpop();void display();}st;

void stack::stackpush(){ptr=new node;if(ptr==NULL){cout<<"Overflow ";}

else{cout<<"Enter the name ";gets(ptr->name);cout<<"Enter the age ";cin>>ptr->age;ptr->link=NULL;if(top==NULL){top=ptr;}else{ptr->link=top;top=ptr;

}}}

void stack::stackpop(){if(top==NULL){cout<<"Underflow ";}else{save=top;top=top->link;cout<<"Name ";

puts(save->name);cout<<"Age "<<save->age;delete save;}

}

7/17/2019 c++ program list

http://slidepdf.com/reader/full/c-program-list-568c19b8cef8b 10/37

 10 

void stack::display(){if(top==NULL){cout<<"No elements.."<<endl;}else

{ptr=top;while(ptr!=NULL){cout<<"\nName ";

puts(ptr->name);cout<<"Age "<<ptr->age;ptr=ptr->link;}

}}

void main(){clrscr();

int ch;X:cout<<"\nEnter your choice\n1.Insert\n2.Delete\n3.Display\n4.Exit\n";cin>>ch;switch(ch){case 1:st.stackpush();

goto X;case 2:st.stackpop();

goto X;case 3:st.display();

goto X;default:cout<<"Wrong choice ";

goto X;case 4:exit(0);}

getch();}

OUTPUT

Enter your choice

1.Insert

2.Delete

3.Display

4.Exit1

Enter the name krishna

Enter the age 15

Enter your choice

1.Insert

2.Delete

3.Display

4.Exit

1

Enter the name parikshit

Enter the age 18

Enter your choice

1.Insert

7/17/2019 c++ program list

http://slidepdf.com/reader/full/c-program-list-568c19b8cef8b 11/37

 11 

2.Delete

3.Display

4.Exit

1

Enter the name shalabh

Enter the age 17

Enter your choice

1.Insert

2.Delete

3.Display

4.Exit

2

Name shalabh

Age 17

Enter your choice

1.Insert

2.Delete3.Display

4.Exit

3

Name parikshit

Age 18

Name krishna

Age 15

Enter your choice

1.Insert

2.Delete

3.Display4.Exit

4

7/17/2019 c++ program list

http://slidepdf.com/reader/full/c-program-list-568c19b8cef8b 12/37

 12 

Program 5//Binary Search - Ascending Sorted Array

#include <iostream>#include <cstdlib>

using namespace std;

class binarySearch {int a[30],choice,data;public:int insert();int display();int search();

};int binarySearch::insert(){

cout<<"Enter the Number of Elements: ";cin>>choice;cout<<"Enter the "<<choice<<" elements in Ascending Order: ";for(int i=0; i<choice; i++){

cin>>a[i];}return 0;

}int binarySearch::display(){

system("cls");

cout<<"The Array is :";for(int i=0; i<choice;i++){

cout<<a[i]<<" ";}return 0;

}int binarySearch::search(){

int low=0,mid,high=choice-1;cout<<"Enter the Element to be Searched : ";cin>>data;

while (low<=high){mid=(low+high)/2;if (a[mid]==data)return mid;else if(data > a[mid])low=mid+1;elsehigh=mid-1;

}return -1;

}

int main(){binarySearch ob;ob.insert();ob.display();

7/17/2019 c++ program list

http://slidepdf.com/reader/full/c-program-list-568c19b8cef8b 13/37

 13 

int pos=ob.search();if (pos==-1)cout<<"Unsuccesful Search!!";elsecout<<"Element is at "<<pos;return 0;

}

Output

The Array is :2 3 4 5 6 Enter the Element to be Searched : 5

Element is at 3

7/17/2019 c++ program list

http://slidepdf.com/reader/full/c-program-list-568c19b8cef8b 14/37

 14 

Program 6//Linear Search in Unsorted Array

#include <iostream>#include <cstdlib>

using namespace std;int search(int[],int,int);int main(){

int a[30],limit=30,choice,data,pos=0;cout<<"Enter the Limit of the Array (<=30): ";cin>>choice;if (choice<=30)

{cout<<"Enter the "<<choice<<" elements of the Array: ";for(int i=0; i<choice; i++)

{ cin>>a[i];}

system("cls");cout<<"The Given Array is : "<<endl;for(int j=0; j<choice; j++)

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

}cout<<endl<<"Which element is to be searched: ";cin>>data;pos=search(a,choice,data);

if(pos==-1)cout<<"Search Unsuccessful!!";elsecout<<"The Element is at "<<pos;

}else

{cout<<"Array Overflow!!";

}return 0;

}int search(int a[],int limit,int data)

{ for (int i=0; i<limit;i++){

if(a[i]==data)return i;

}return -1;

}

Output

The Given Array is :

1 2 3 4 5

Which element is to be searched: 4

The Element is at 3

Process returned 0 (0x0) execution time : 16.691 s

Press any key to continue.

7/17/2019 c++ program list

http://slidepdf.com/reader/full/c-program-list-568c19b8cef8b 15/37

 15 

Program 7// List of Armstrong Number

#include <iostream>using namespace std;int main(){

cout<<"Generating Armstrong Number upto Given Limit\n";int num,sum=0,r,temp;cout<<"Enter the Limit\n";cin>>num;for(int i=0; i<=num; i++)

{temp=i;while(temp!=0){

r=temp%10;

sum=sum+(r*r*r);temp=temp/10;}if(sum==i)cout<<endl<<i<<" is armstrong";sum=0;

}return 0;

}

Output

Generating Armstrong Number upto Given Limit

Enter the Limit

500

0 is armstrong

1 is armstrong

153 is armstrong

370 is armstrong

371 is armstrong

407 is armstrong

Process returned 0 (0x0) execution time : 6.393 s

7/17/2019 c++ program list

http://slidepdf.com/reader/full/c-program-list-568c19b8cef8b 16/37

 16 

Program 8//Perfect Numbers#include <iostream>

using namespace std;

int main(){

int low,upp,temp,track=0,sum=0;float loop=0.0;cout<<"Program to find the Perfect Numbers Withing Given Limits\n";cout<<"Enter the Lower Limit: ";cin>>low;cout<<"Enter the upper Limit: ";cin>>upp;cout<<endl;for (int i=low;i<=upp;i++)

{ temp=i;loop++;for (int j=1; j<i; j++){

if (temp%j==0){

loop++;sum=sum+j;

}}if (sum==temp)

{ cout<<temp<<" is a perfect square\n";}sum=0;

}cout<<"\nThe Loop ran "<<loop<<" times\n";return 0;

}

Output

Program to find the Perfect Numbers Withing Given Limits

Enter the Lower Limit: 1

Enter the upper Limit: 200

6 is a perfect square

28 is a perfect square

The Loop ran 1098 times

Process returned 0 (0x0) execution time : 6.598 s

Press any key to continue.

7/17/2019 c++ program list

http://slidepdf.com/reader/full/c-program-list-568c19b8cef8b 17/37

 17 

Program 9//Prime Numbers#include <iostream>

using namespace std;

int main(){

int num,temp,flag=0,track=0;cout<<"Enter the Limit upto which Prime Numbers are to be found: ";cin>>num;for(int i=1; i<=num; i++)

{temp=i;for (int j=1; j<=i; j++){

if (temp%j==0)

{ flag++;}

}if (flag==2)cout<<endl<<"The "<<++track<<"th Prime Number is "<<i;flag=0;

}cout<<endl<<"\nThere are "<<track<<" Prime Numbers between 1 and

"<<num<<"\n";return 0;

}

OutputEnter the Limit upto which Prime Numbers are to be found: 500

The 1th Prime Number is 2

The 2th Prime Number is 3

The 3th Prime Number is 5

The 4th Prime Number is 7

The 5th Prime Number is 11

The 6th Prime Number is 13

The 7th Prime Number is 17

The 8th Prime Number is 19

The 9th Prime Number is 23The 10th Prime Number is 29

The 11th Prime Number is 31

The 12th Prime Number is 37

The 13th Prime Number is 41

The 14th Prime Number is 43

The 15th Prime Number is 47

The 16th Prime Number is 53

The 17th Prime Number is 59

The 18th Prime Number is 61

The 19th Prime Number is 67

The 20th Prime Number is 71The 21th Prime Number is 73

The 22th Prime Number is 79

The 23th Prime Number is 83

The 24th Prime Number is 89

7/17/2019 c++ program list

http://slidepdf.com/reader/full/c-program-list-568c19b8cef8b 18/37

 18 

The 25th Prime Number is 97

The 26th Prime Number is 101

The 27th Prime Number is 103

The 28th Prime Number is 107

The 29th Prime Number is 109

The 30th Prime Number is 113

The 31th Prime Number is 127The 32th Prime Number is 131

The 33th Prime Number is 137

The 34th Prime Number is 139

The 35th Prime Number is 149

The 36th Prime Number is 151

The 37th Prime Number is 157

The 38th Prime Number is 163

The 39th Prime Number is 167

The 40th Prime Number is 173

The 41th Prime Number is 179

The 42th Prime Number is 181The 43th Prime Number is 191

The 44th Prime Number is 193

The 45th Prime Number is 197

The 46th Prime Number is 199

The 47th Prime Number is 211

The 48th Prime Number is 223

The 49th Prime Number is 227

The 50th Prime Number is 229

The 51th Prime Number is 233

The 52th Prime Number is 239

The 53th Prime Number is 241The 54th Prime Number is 251

The 55th Prime Number is 257

The 56th Prime Number is 263

The 57th Prime Number is 269

The 58th Prime Number is 271

The 59th Prime Number is 277

The 60th Prime Number is 281

The 61th Prime Number is 283

The 62th Prime Number is 293

The 63th Prime Number is 307

The 64th Prime Number is 311The 65th Prime Number is 313

The 66th Prime Number is 317

The 67th Prime Number is 331

The 68th Prime Number is 337

The 69th Prime Number is 347

The 70th Prime Number is 349

The 71th Prime Number is 353

The 72th Prime Number is 359

The 73th Prime Number is 367

The 74th Prime Number is 373

The 75th Prime Number is 379The 76th Prime Number is 383

The 77th Prime Number is 389

The 78th Prime Number is 397

The 79th Prime Number is 401

7/17/2019 c++ program list

http://slidepdf.com/reader/full/c-program-list-568c19b8cef8b 19/37

 19 

The 80th Prime Number is 409

The 81th Prime Number is 419

The 82th Prime Number is 421

The 83th Prime Number is 431

The 84th Prime Number is 433

The 85th Prime Number is 439

The 86th Prime Number is 443The 87th Prime Number is 449

The 88th Prime Number is 457

The 89th Prime Number is 461

The 90th Prime Number is 463

The 91th Prime Number is 467

The 92th Prime Number is 479

The 93th Prime Number is 487

The 94th Prime Number is 491

The 95th Prime Number is 499

There are 95 Prime Numbers between 1 and 500

Process returned 0 (0x0) execution time : 5.190 s

Press any key to continue.

7/17/2019 c++ program list

http://slidepdf.com/reader/full/c-program-list-568c19b8cef8b 20/37

 20 

Program 10//Pythagorean Triplets#include <iostream>

using namespace std;

int main(){

int low,upp,temp,track=0;float loop=0.0;cout<<"Program to find the Pythagorean Triplets Withing Given Range\n";cout<<"Enter the Lower Limit: ";cin>>low;cout<<"Enter the upper Limit: ";cin>>upp;for(int i=low; i<=upp; i++)

{

temp=i;loop++;for (int j=1;j<i;j++){

loop++;for (int k=1;k<i;k++){

if ((i*i)==(j*j)+(k*k) && (i==(j+1)) && (j==(k+1))){

cout<<i<<'\t'<<j<<'\t'<<k<<endl;loop++;track++;

}

}}

}cout<<"there are "<<track<<" pythagorean triplets\n";cout<<"The loop ran "<<loop<<" times";

return 0;}

Output

Program to find the Pythagorean Triplets Withing Given Range

Enter the Lower Limit: 1Enter the upper Limit: 200

5 4 3

there are 1 pythagorean triplets

The loop ran 20101 times

Process returned 0 (0x0) execution time : 6.405 s

Press any key to continue.

7/17/2019 c++ program list

http://slidepdf.com/reader/full/c-program-list-568c19b8cef8b 21/37

 21 

Program 11//Prime Factors of an Integer#include <iostream>

using namespace std;

int main(){

int num,temp,flag=0,dummy,loop=0,count=0;cout<<"Enter the Number for Finding the Prime Factor: \n";cin>>num;dummy=num;for(int i=1; i<=num; i++)

{temp=i;loop++;for (int j=1; j<=i; j++)

{ if (temp%j==0){

flag++;loop++;

}}if (flag==2){while(dummy%i==0){

cout<<i<<'\n';

count++;loop++;dummy=dummy/i;

}}flag=0;

}cout<<"\nThere are "<<count<<" Prime Factors of "<<num<<"\n";cout<<"\nThe loop Ran "<<loop<<" times.";

return 0;}

Output

Enter the Number for Finding the Prime Factor:210

2

3

5

7

There are 4 Prime Factors of 210

The loop Ran 1380 times.

7/17/2019 c++ program list

http://slidepdf.com/reader/full/c-program-list-568c19b8cef8b 22/37

 22 

Program 12//Traversig an Array and find the sum of all in the Array.

#include <iostream>#include <cstdlib>

using namespace std;int sum(int[],int);int main(){

int a[30],limit=30,choice;cout<<"Enter the Limit of the Array (<=30): ";cin>>choice;if (choice<=30)

{cout<<"Enter the "<<choice<<" elements of the Array: ";for(int i=0; i<choice; i++)

{ cin>>a[i];}

system("cls");cout<<"The Given Array is : ";for(int j=0; j<choice; j++)

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

}cout<<endl<<"The Sum of the Array is : "<<sum(a,choice);

}else

{ cout<<"Array Overflow!!";}

return 0;}int sum(int a[],int limit){

int sum=0;for(int i=0; i<limit; i++){

sum+=a[i];}

return sum;}

Output

The Given Array is : 3 4 5 6 7

The Sum of the Array is : 25

Process returned 0 (0x0) execution time : 9.267 s

Press any key to continue.

7/17/2019 c++ program list

http://slidepdf.com/reader/full/c-program-list-568c19b8cef8b 23/37

 23 

Program 13//Linked List#include <iostream>using namespace std;

struct node{int info;node *next;

};class link_list{

private:node *start;

public:link_list(){

start=NULL;}

void add_data(int data){node *new_node=new node;new_node->info=data;new_node->next=start;start=new_node;

}void display(){

node *ptr=start;node *temp;cout<<"->";

while(ptr){cout<<ptr->info<<"->";temp=ptr;ptr=ptr->next;delete temp;

}}

};int main(){

link_list link;

int value;char choice;while(1){

cout<<"\nDo you wish to insert new node: [y/n]";cin>>choice;if (choice=='y' || choice=='Y'){

cout<<"\nEnter the info of node:";cin>>value;link.add_data(value);

}

elsebreak;}cout<<"\nThe Linked List is: ";link.display();

7/17/2019 c++ program list

http://slidepdf.com/reader/full/c-program-list-568c19b8cef8b 24/37

 24 

}

Output

Do you wish to insert new node: [y/n]y

Enter the info of node:4

Do you wish to insert new node: [y/n]y

Enter the info of node:3

Do you wish to insert new node: [y/n]y

Enter the info of node:3

Do you wish to insert new node: [y/n]n

The Linked List is: ->3->3->4->

Process returned 0 (0x0) execution time : 20.053 s

Press any key to continue.

7/17/2019 c++ program list

http://slidepdf.com/reader/full/c-program-list-568c19b8cef8b 25/37

 25 

Program 14//Palindrome Number#include <iostream>#include<stdio.h>#include<stdlib.h>using namespace std;

int main(){

int a,b,c,r=0;System("cls");cout<<"Enter the Number:";cin>>a;b=a;for(int i=0; b!=0; i++){

c=b%10;

r=(r*10)+c;b=b/10;

}if(r==a)

cout<<endl<<"The Number is Palindrome.";else

cout<<endl<<"The Number Is Not Palindrome.";

return 0;

}

OUTPUTEnter the Number: 262

The Number is Palindrome.

Enter the Number: 134

The Number Is Not Palindrome.

7/17/2019 c++ program list

http://slidepdf.com/reader/full/c-program-list-568c19b8cef8b 26/37

 26 

Program 15 

//Sum of N Natural Numbers

#include <iostream>#include<stdio.h>#include<stdlib.h>using namespace std;int sum(int,int);int main(){

system("cls");int start,end,num;cout<<endl<<"Program to Find the Sum of n Natural Numbers From a Given

Number.";cout<<endl<<"Enter the Starting Number: ";cin>>start;cout<<endl<<"Enter the Ending Number: ";

cin>>end;num=sum(start,end);cout<<endl<<"The Sum from "<<start<<" till "<<end<<" is "<<num;return 0;

}int sum(int a, int b){

int sum;sum=((b*(b+1))/2)-(((a-1)*a)/2);return sum;

}

OUTPUT

Program to Find the Sum of n Natural Numbers From a Given Number.Enter the Starting Number: 3

Enter the Ending Number: 12

The Sum from 3 till 12 is 75

7/17/2019 c++ program list

http://slidepdf.com/reader/full/c-program-list-568c19b8cef8b 27/37

 27 

Program 16 // Program to Count the Number of words Present in a Line

#include <iostream>#include<stdio.h>#include<stdio.h>using namespace std;

int count(char[]);int main(){

int a;char ch[80];cout<<endl<<"Program to Count the Number of words Present in a Line.";cout<<endl<<"Enter the String.";gets(ch);a=count(ch);

cout<<"The Number of Words are "<<a;return 0;}int count(char str[]){

int b=1;for(int i=0; str[i]!=NULL; i++){

if (str[i]==' ')b++;

}return b;

}OUTPUT

Program to Count the Number of words Present in a Line.

Enter the String. India is a Great country.

The Number of Words are 5

7/17/2019 c++ program list

http://slidepdf.com/reader/full/c-program-list-568c19b8cef8b 28/37

 28 

Program 17 //Interactive Pattern Printing

#include <iostream>#include<stdio.h>#include<stdlib.h>#include<iomanip>int main(){

cout<<"Interactive pattern Printing";int rows;char ch;cout<<endl;do{

cout<<"How Many Rows: ";cin>>rows;

system("cls");if (rows>=1 && rows<=24){

int a=1,b=2*rows-1;for(int i=1; i<=rows; i++){

for (int j=1; j<=i; j++){

cout<<setw(a)<<"*";++a;cout<<setw(b)<<"*";b-=2;

}cout<<endl;}a=rows,b=1;for(int k=1; k<=rows; k++){

for(int j=0; j<=k; j++){

cout<<setw(a)<<"*";--a;cout<<setw(b)<<"*";b+=2;

}cout<<endl;}cout<<"Want to continue:";cin>>ch;

}else{

cout<<endl<<"You have entered "<<rows<<" rows which is more thanthe screen can show";

cout<<endl<<"Press Y to Try Again";cin>>ch;

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

}

7/17/2019 c++ program list

http://slidepdf.com/reader/full/c-program-list-568c19b8cef8b 29/37

 29 

OUTPUT

How Many Rows:2

* *

*

* *

7/17/2019 c++ program list

http://slidepdf.com/reader/full/c-program-list-568c19b8cef8b 30/37

 30 

Program 18 //Position of A Character Finder

#include <iostream>#include<stdio.h>#include<stdlib.h>#include<iomanip>using namespace std;int main(){char a[10],ch;int z=0; b[5]={0,0,0,0,0},x=0;cout<<"Enter String";gets(a);cout<<endl<<"Enter the Character: ";cin>>ch;for(int i=0; a[i]!=NULL; i++)

{if (a[i]==int(ch)){b[z]=i;z++;}}cout<<"The Position of"<<ch<<" is at ";for(int k=0; k=z-2; k++){cout<<b[i]<<"th";++x;

}cout<<b[x]<<"th";cout<<"and "<<b[++x]<<"th position";return 0;}

OUTPUT

Enter String: Arfat Salman

Enter the Character: A

The Position of A is at 1th position.

7/17/2019 c++ program list

http://slidepdf.com/reader/full/c-program-list-568c19b8cef8b 31/37

 31 

Program 19 

//Vertical Sentence Printing#include <iostream>#include<stdio.h>#include<stdlib.h>#include<iomanip>using namespace std;int main(){

char a[100],ch;cout<<"Program to Print Vertical Words"<<endl;cout<<"Enter String:";gets(a);cout<<endl;

cout<<”Vertical Words Are:”; for(int i=0; a[i]!=NULL; i++){

if (a[i]==' ')cout<<endl;elsecout<<a[i];

}return 0;

}

OUTPUT

Program to Print Vertical Words

Enter String: India is a Great country

Vertical Words Are:

India

Is

A

Great

Country

7/17/2019 c++ program list

http://slidepdf.com/reader/full/c-program-list-568c19b8cef8b 32/37

 32 

Program 20 

//Demonstration of Function Overloading

#include <iostream>#include<stdio.h>#include<stdlib.h>#include<iomanip>using namespace std;int print(char,int);int print(int,int);int main(){

cout<<"Function Overloading";cout<<endl;print('*',5);print(10,5);return 0;

}int print(char a, int b){

for(int i=1; i<=b; i++)cout<<endl<<a;return 0;

}int print (int x, int y){

for(int i=1; i<=y; i++)cout<<endl<<x;return 0;

}OUTPUT

Function Overloading

*

*

*

*

*

10

1010

10

10

7/17/2019 c++ program list

http://slidepdf.com/reader/full/c-program-list-568c19b8cef8b 33/37

 33 

Program 21//Largest and Smallest Among the Arrays

#include <iostream>#include<stdio.h>#include<stdlib.h>#include<iomanip>using namespace std;int main(){

int v[50],large,small,i,n;cout<<endl<<"How Many Element";cin>>n;cout<<endl<<"Enter values";for( i=0; i<n; i++)cin>>v[i];large=v[1];

small=v[1];for (int j=0; j<n; j++){

if (v[j]>large)large=v[j];else if(v[j]<small)small=v[j];

}cout<<endl<<"The Largest Element is:"<<large;cout<<"The Smallest Element is:"<<small;return 0;

}OUTPUT

How Many Element5

Enter values

21

32

43

54

67

The Largest Element is:67

The Smallest Element is:21

7/17/2019 c++ program list

http://slidepdf.com/reader/full/c-program-list-568c19b8cef8b 34/37

 34 

Program 22 //Exponentiation#include <iostream>#include<stdio.h>#include<stdlib.h>#include<string.h>using namespace std;double power(double,double);int main(){

int a,b,c;double d=0;cout<<endl<<"Enter Base:";cin>>a;cout<<"Enter Exponent:";cin>>b;d=power(a,b);

cout<<a<<" raised to "<<b<<" is equal to "<<d;return 0;}double power(double base, double exp){

double c=1;for(int i=1; i<=exp; i++)c=c*base;return c;

}

OUTPUT

Enter Base: 5

Enter Exponent: 5

5 raised to 5 is equal to 3125.

7/17/2019 c++ program list

http://slidepdf.com/reader/full/c-program-list-568c19b8cef8b 35/37

 35 

Program 23 

//Factorial of A Number#include <iostream>#include<stdio.h>#include<stdlib.h>#include<string.h>using namespace std;int main(){

int a,b;double d=1;cout<<endl<<"Enter up which Number factorial has to Calculated:";cin>>a;for (int j=1; j<=a; j++)d=d*j;for (int i=1; i<=a; i++){

if (i==a)cout<<i;elsecout<<i<<"*";

}cout<<"="<<d;return 0;

}

OUTPUT

Enter up which Number factorial has to Calculated: 8

1*2*3*4*5*6*7*8=40320

7/17/2019 c++ program list

http://slidepdf.com/reader/full/c-program-list-568c19b8cef8b 36/37

 36 

Program 24 

// Dynamic Memory Allocation#include<iostream>using namespace std;int *empno=NULL;int *salary=NULL;int main(){

int size;cout<<"\nHow Many employees are there? :";cin>>size;empno=new int[size];salary=new int[size];for(int i=0; i<size; i++){

cout<<"Enter EMPNO and Salary: ";cin>>empno[i]>>salary[i];

}cout<<"You have Entered \n";cout<<"\tEmpno\t\tSalary\n\n";for(int j=0; j<size; j++){

cout<<"\t"<<empno[j]<<"\t\t"<<salary[j]<<"\n";}delete empno;delete salary;

}

Output

How Many employees are there? :3Enter EMPNO and Salary: 101

2000

Enter EMPNO and Salary: 102

3000

Enter EMPNO and Salary: 103

4000

You have Entered

Empno Salary

101 2000

102 3000103 4000

Process returned 0 (0x0) execution time : 13.304 s

Press any key to continue.

7/17/2019 c++ program list

http://slidepdf.com/reader/full/c-program-list-568c19b8cef8b 37/37

 37 

Program 25 

//PROGRAM FOR MAINTAINING A LIBRARY CLASS

#include<iostream.h>#include<conio.h>#include<stdio.h>class library { char bookname[30];

char author[30] ;int price;public: void input(void);void output(void);};

void library::input(){ cout<<"Enter bookname:" ;gets(bookname);cout<<"Enter author name:" ;gets(author);

cout<<"Enter price: Rs." ;cin>>price;}void library::output(void){ cout<<bookname;cout<<"\n"<<author;cout<<"\nRs. "<<price;

}void main(){ clrscr();library b1,b2,b3 ;// for(i=1;1<=num;i++)

// cout<<"Enter information for book1:"<<endl;// b1.input();cout<<"Enter information for book1:"<<endl;b1.input();cout<<"You entered for book1:"<<endl;b1.output();cout<<"\nEnter information for book2:"<<endl;b2.input();cout<<"You entered for book2:"<<endl;b2.output();getch();}

OUTPUT

Enter information for book1:

Enter bookname:India My pride

Enter author name:Arfat

Enter price: Rs.100

You entered for book1:

India My pride

Krishna

Rs. 100