Friday, 25 March 2016
Wednesday, 27 January 2016
menu driven program for the use of structure
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
#include<process.h>
struct student
{
int rno;
char name[20];
int marks;
};
void main()
{
clrscr();
student s[3];
char ans='y';
int ch;
do
{
clrscr();
cout<<"Options are"<<endl;
cout<<"1. Insert records"<<endl;
cout<<"2.Display all records"<<endl;
cout<<"3.Display records on the basis of roll number"<<endl;
cout<<"4.Display records in ascending order of roll number"<<endl;
cout<<"5.Display records in descending order of marks"<<endl;
cout<<"6.Display student with top marks"<<endl;
cout<<"7.Display average marks"<<endl;
cout<<"8.Exit"<<endl;
cout<<"Enter Your Choice"<<endl;
cin>>ch;
switch(ch)
{
case 1:
int i;
for(i=0;i<3;i++)
{
cout<<"Enter the rollno name and marks of a student"<<endl;
cin>>s[i].rno;
gets(s[i].name);
cin>>s[i].marks;
}
break;
case 2:
cout<<"The data of the students is"<<endl;
for(i=0;i<3;i++)
{
cout<<s[i].rno<<"\t";
cout<<s[i].name<<"\t";
cout<<s[i].marks<<endl;
}
break;
case 3:
int rn;
cout<<"Enter roll number to search"<<endl;
cin>>rn;
for(i=0;i<3;i++)
{
if(s[i].rno==rn)
{
cout<<s[i].rno<<"\t";
cout<<s[i].name<<"\t";
cout<<s[i].marks<<endl;
}
}
break;
case 4:
student S;
for(i=0;i<3;i++)
{
for(int j=0;j<3-i-1;j++)
{
if(s[j].rno>s[j+1].rno)
{
S=s[j];
s[j]=s[j+1];
s[j+1]=S;
}
}
}
cout<<"The data of the students in ascending order of roll no is"<<endl;
for(i=0;i<3;i++)
{
cout<<s[i].rno<<"\t";
cout<<s[i].name<<"\t";
cout<<s[i].marks<<endl;
}
break;
case 5:
for(i=0;i<3;i++)
{
for(int j=0;j<3-i-1;j++)
{
if(s[j].marks<s[j+1].marks)
{
S=s[j];
s[j]=s[j+1];
s[j+1]=S;
}
}
}
cout<<"The data of the students in descending order of marks is"<<endl;
for(i=0;i<3;i++)
{
cout<<s[i].rno<<"\t";
cout<<s[i].name<<"\t";
cout<<s[i].marks<<endl;
}
break;
case 6:
for(i=0;i<3;i++)
{
for(int j=0;j<3-i-1;j++)
{
if(s[j].marks<s[j+1].marks)
{
S=s[j];
s[j]=s[j+1];
s[j+1]=S;
}
}
}
cout<<"The details of the students securing top marks is"<<endl;
cout<<s[0].rno<<"\t";
cout<<s[0].name<<"\t";
cout<<s[0].marks<<endl;
break;
case 7:
int sum=0;
cout<<"The average marks are"<<endl;
for(i=0;i<3;i++)
{
sum=sum+s[i].marks;
}
cout<<sum/3<<endl;
break;
case 8:
cout<<"Press any key to exit.................";
getch();
exit(0);
default:
cout<<"Enter from the given choice "<<endl;
cout<<"Press any key to exit................."<<endl;
getch();
break;
}
cout<<"return to main menu y/n"<<endl;
cin>>ans;
}while((ans=='y')||(ans=='Y'));
getch();
}
This program is made for entry of three students at a time . You can change the number of students by changing the size of the array of structure and the value where ever loops are used
Hope this program will clear a lot of concepts like searching and sorting .
Make new programs always
#include<conio.h>
#include<stdio.h>
#include<process.h>
struct student
{
int rno;
char name[20];
int marks;
};
void main()
{
clrscr();
student s[3];
char ans='y';
int ch;
do
{
clrscr();
cout<<"Options are"<<endl;
cout<<"1. Insert records"<<endl;
cout<<"2.Display all records"<<endl;
cout<<"3.Display records on the basis of roll number"<<endl;
cout<<"4.Display records in ascending order of roll number"<<endl;
cout<<"5.Display records in descending order of marks"<<endl;
cout<<"6.Display student with top marks"<<endl;
cout<<"7.Display average marks"<<endl;
cout<<"8.Exit"<<endl;
cout<<"Enter Your Choice"<<endl;
cin>>ch;
switch(ch)
{
case 1:
int i;
for(i=0;i<3;i++)
{
cout<<"Enter the rollno name and marks of a student"<<endl;
cin>>s[i].rno;
gets(s[i].name);
cin>>s[i].marks;
}
break;
case 2:
cout<<"The data of the students is"<<endl;
for(i=0;i<3;i++)
{
cout<<s[i].rno<<"\t";
cout<<s[i].name<<"\t";
cout<<s[i].marks<<endl;
}
break;
case 3:
int rn;
cout<<"Enter roll number to search"<<endl;
cin>>rn;
for(i=0;i<3;i++)
{
if(s[i].rno==rn)
{
cout<<s[i].rno<<"\t";
cout<<s[i].name<<"\t";
cout<<s[i].marks<<endl;
}
}
break;
case 4:
student S;
for(i=0;i<3;i++)
{
for(int j=0;j<3-i-1;j++)
{
if(s[j].rno>s[j+1].rno)
{
S=s[j];
s[j]=s[j+1];
s[j+1]=S;
}
}
}
cout<<"The data of the students in ascending order of roll no is"<<endl;
for(i=0;i<3;i++)
{
cout<<s[i].rno<<"\t";
cout<<s[i].name<<"\t";
cout<<s[i].marks<<endl;
}
break;
case 5:
for(i=0;i<3;i++)
{
for(int j=0;j<3-i-1;j++)
{
if(s[j].marks<s[j+1].marks)
{
S=s[j];
s[j]=s[j+1];
s[j+1]=S;
}
}
}
cout<<"The data of the students in descending order of marks is"<<endl;
for(i=0;i<3;i++)
{
cout<<s[i].rno<<"\t";
cout<<s[i].name<<"\t";
cout<<s[i].marks<<endl;
}
break;
case 6:
for(i=0;i<3;i++)
{
for(int j=0;j<3-i-1;j++)
{
if(s[j].marks<s[j+1].marks)
{
S=s[j];
s[j]=s[j+1];
s[j+1]=S;
}
}
}
cout<<"The details of the students securing top marks is"<<endl;
cout<<s[0].rno<<"\t";
cout<<s[0].name<<"\t";
cout<<s[0].marks<<endl;
break;
case 7:
int sum=0;
cout<<"The average marks are"<<endl;
for(i=0;i<3;i++)
{
sum=sum+s[i].marks;
}
cout<<sum/3<<endl;
break;
case 8:
cout<<"Press any key to exit.................";
getch();
exit(0);
default:
cout<<"Enter from the given choice "<<endl;
cout<<"Press any key to exit................."<<endl;
getch();
break;
}
cout<<"return to main menu y/n"<<endl;
cin>>ans;
}while((ans=='y')||(ans=='Y'));
getch();
}
This program is made for entry of three students at a time . You can change the number of students by changing the size of the array of structure and the value where ever loops are used
Hope this program will clear a lot of concepts like searching and sorting .
Make new programs always
Sunday, 23 November 2014
Difference between call by value and call by reference
1. In call by value we pass a copy of actual arguments to formal arguments however in call by reference we pass the original values through reference variable.
2.In call by value if we make any change in formal argument the same changes will not take place in actual argument, whereas in call by reference the changes will take place in call by reference.
Example of call by value
#include<iostream.h>
#include<conio.h>
void swap(int,int);
void main()
{
clrscr();
int x,y;
cout<<"enter two numbers "<<endl;
cin>>x>>y;
swap(x,y); // actual arguments
cout<<"After Swapping:-"<<endl;
cout<<"The value of x is "<<x<<endl;
cout<<"The value of y is "<<y<<endl;
getch();
}
void swap(int a,int b) // Formal arguments
{
int c;
c=a;
a=b;
b=c;
cout<<"In swap function :-"<<endl;
cout<<"The value of a is "<<a<<endl;
cout<<"The value of b is "<<b<<endl;
}
1. In call by value we pass a copy of actual arguments to formal arguments however in call by reference we pass the original values through reference variable.
2.In call by value if we make any change in formal argument the same changes will not take place in actual argument, whereas in call by reference the changes will take place in call by reference.
Example of call by value
#include<iostream.h>
#include<conio.h>
void swap(int,int);
void main()
{
clrscr();
int x,y;
cout<<"enter two numbers "<<endl;
cin>>x>>y;
swap(x,y); // actual arguments
cout<<"After Swapping:-"<<endl;
cout<<"The value of x is "<<x<<endl;
cout<<"The value of y is "<<y<<endl;
getch();
}
void swap(int a,int b) // Formal arguments
{
int c;
c=a;
a=b;
b=c;
cout<<"In swap function :-"<<endl;
cout<<"The value of a is "<<a<<endl;
cout<<"The value of b is "<<b<<endl;
}
Output :
Enter two number
10
20
In Swap function:-
The value of a is 20
The value of b is 10
In main function:-
The value of x is 10
The value of y is 20
Example of call by reference
#include<iostream.h>
#include<conio.h>
void swap(int & ,int &);
void main()
{
clrscr();
int x,y;
cout<<"enter two numbers "<<endl;
cin>>x>>y;
swap(x,y); // actual arguments
cout<<"After Swapping:-"<<endl;
cout<<"The value of x is "<<x<<endl;
cout<<"The value of y is "<<y<<endl;
getch();
}
void swap(int &a,int &b) // Formal arguments
{
int c;
c=a;
a=b;
b=c;
cout<<"in swap function :-"<<endl;
cout<<"The value of a is "<<a<<endl;
cout<<"The value of b is "<<b<<endl;
}
#include<iostream.h>
#include<conio.h>
void swap(int & ,int &);
void main()
{
clrscr();
int x,y;
cout<<"enter two numbers "<<endl;
cin>>x>>y;
swap(x,y); // actual arguments
cout<<"After Swapping:-"<<endl;
cout<<"The value of x is "<<x<<endl;
cout<<"The value of y is "<<y<<endl;
getch();
}
void swap(int &a,int &b) // Formal arguments
{
int c;
c=a;
a=b;
b=c;
cout<<"in swap function :-"<<endl;
cout<<"The value of a is "<<a<<endl;
cout<<"The value of b is "<<b<<endl;
}
Output :
Enter two number
10
20
In Swap function:-
The value of a is 20
The value of b is 10
In main function:-
The value of x is 20
The value of y is 10
Monday, 14 July 2014
More programs on Class Concept
Program 1
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
class stock
{
int icode,qty;
char des[20];
float price,discount;
void finddisc()
{
if(qty<=50)
discount=0;
else if(qty<=100)
discount=5;
else
discount=10;
}
public:
void buy()
{
cout<<"Enter icode,name,price,quantity"<<endl;
cin>>icode;
gets(des);
cin>>price>>qty;
finddisc();
}
void showall()
{
cout<<"The item code is"<<icode<<endl;
cout<<"The item description is"<<des<<endl;
cout<<"The Price is"<<price<<endl;
cout<<"The quantity is"<<qty<<endl;
cout<<"The discount is"<<discount<<endl;
}
};
void main()
{
stock s;
clrscr();
s.buy();
s.showall();
getch();
}
Program 2
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
class resort
{
int rno;
char name[20];
float charges,amount;
int days;
float compute()
{
if((days*charges)>11000)
{
return 1.02*days*charges;
}
else
{
return days * charges;
}
}
public:
void getinfo()
{
cout<<"enter room no , name , charges and days"<<endl;
cin>>rno;
gets(name);
cin>>charges>>days;
}
void dispinfo()
{
cout<<"Room No is: " <<rno<<endl;
cout<<"Customer name is :" <<name<<endl;
cout<<"Amount is :"<<compute();
}
};
void main()
{
clrscr();
resort r;
r.getinfo();
r.dispinfo();
getch();
}
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
class stock
{
int icode,qty;
char des[20];
float price,discount;
void finddisc()
{
if(qty<=50)
discount=0;
else if(qty<=100)
discount=5;
else
discount=10;
}
public:
void buy()
{
cout<<"Enter icode,name,price,quantity"<<endl;
cin>>icode;
gets(des);
cin>>price>>qty;
finddisc();
}
void showall()
{
cout<<"The item code is"<<icode<<endl;
cout<<"The item description is"<<des<<endl;
cout<<"The Price is"<<price<<endl;
cout<<"The quantity is"<<qty<<endl;
cout<<"The discount is"<<discount<<endl;
}
};
void main()
{
stock s;
clrscr();
s.buy();
s.showall();
getch();
}
Program 2
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
class resort
{
int rno;
char name[20];
float charges,amount;
int days;
float compute()
{
if((days*charges)>11000)
{
return 1.02*days*charges;
}
else
{
return days * charges;
}
}
public:
void getinfo()
{
cout<<"enter room no , name , charges and days"<<endl;
cin>>rno;
gets(name);
cin>>charges>>days;
}
void dispinfo()
{
cout<<"Room No is: " <<rno<<endl;
cout<<"Customer name is :" <<name<<endl;
cout<<"Amount is :"<<compute();
}
};
void main()
{
clrscr();
resort r;
r.getinfo();
r.dispinfo();
getch();
}
Friday, 2 May 2014
Classes & Objects
A Class is a collection of data and functions working on that data. The class follows the concept of encapsulation.
An object is an instance of class containing real world data.
In a class we simply declare member data and define member functions that work on the data but the value of the data is in correspondence to the object.
#include<iostream.h>
#include<conio.h>
class student
{
int rno;
char name[20];
float fees;
public:
void accept()
{
cout<<"Enter the details of the student"<<endl;
cin>>rno
gets(name);
cin>>fees;
}
void disp()
{
cout<<"The rollno is '<<rno<<endl;
cout<<"The Nmae is"<<name<<endl;
cout<<"The Fees is "<<fees;
}
};
void main()
{
clrscfr();
student s;
s.accept();
s.disp();
getch();
}
A Class is a collection of data and functions working on that data. The class follows the concept of encapsulation.
An object is an instance of class containing real world data.
In a class we simply declare member data and define member functions that work on the data but the value of the data is in correspondence to the object.
#include<iostream.h>
#include<conio.h>
class student
{
int rno;
char name[20];
float fees;
public:
void accept()
{
cout<<"Enter the details of the student"<<endl;
cin>>rno
gets(name);
cin>>fees;
}
void disp()
{
cout<<"The rollno is '<<rno<<endl;
cout<<"The Nmae is"<<name<<endl;
cout<<"The Fees is "<<fees;
}
};
void main()
{
clrscfr();
student s;
s.accept();
s.disp();
getch();
}
Friday, 21 September 2012
On the demand of the students I am uploading some basic programs.Hope this will benefit you all.
// WRITE A PROGRAM TO ENTER TEMP IN FAHRENHIET AND CONVER IT IN CELCIUS
#include<iostream.h>
#include<conio.h>
void main()
{
float f,c;
cout<<"enter temp in fahrenheit"<<endl;
cin>>f;
c=5*(f-32)/9;
cout<<"The temp in celcius is:"<<c<<endl;
getch();
}
// WRITE A PROGRAM TO ENTER TEMP IN FAHRENHIET AND CONVER IT IN CELCIUS
#include<iostream.h>
#include<conio.h>
void main()
{
float f,c;
cout<<"enter temp in fahrenheit"<<endl;
cin>>f;
c=5*(f-32)/9;
cout<<"The temp in celcius is:"<<c<<endl;
getch();
}
// WRITE A PROGRAM TO ENTER TWO NUMBERS AND SWAP THEM
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int x,y,z;
cout<<"Enter two numbers"<<endl;
cin>>x>>y;
z=x;
x=y;
y=z ;
cout<<"The numbers after swapping:"<<endl;
cout<<"x is :="<<x<<endl<<"y is :="<<y<<endl;
getch();
}
// WRITE A PROGRAM TO ENTER TWO NUMBERS AND SWAP THEM WITHOUT USING THIRD VARIABLE
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int x,y,z;
cout<<"Enter two numbers"<<endl;
cin>>x>>y;
x=x+y;
x=x-y;
y=x-y;
cout<<"The numbers after swapping:"<<endl;
cout<<"x is :="<<x<<endl<<"y is :="<<y<<endl;
getch();
}
//enter a four digit number and find the sum of first and last
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int num,first,last,sum;
cout<<"enter a four digit number"<<endl;
cin>>num;
last=num%10;
first=num/1000;
sum=first+last;
cout<<"the sum of first and last digit is ="<<sum<<endl;
getch();
}
Wednesday, 19 September 2012
Linked Stack and Linked Queue
//PROGRAM TO IMPLEMENT LINKED STACK
#include<iostream.h>
#include<conio.h>
struct node
{
int data;
node * next;
};
class stack
{
node *top;
public:
stack()
{
top=NULL;
}
void stackpush();
void stackpop();
void displaystack();
};
void stack::stackpush()
{
node *ptr;
ptr=new node;
cout<<"Enter the element to be pushed"<<endl;
cin>>ptr->data;
if(top==NULL)
ptr->next=NULL;
else
ptr->next=top;
top=ptr;
}
void stack ::stackpop()
{
node *ptr;
ptr=new node;
ptr=top;
cout<<"The popped element is "<<ptr->data;
top=top->next;
delete ptr;
}
void stack :: displaystack()
{
node *ptr;
ptr=new node;
ptr=top;
cout<<"The stack is "<<endl;
while(ptr!=NULL)
{
cout<<ptr->data<<endl;
ptr=ptr->next;
}
}
void main()
{
clrscr();
char ans;
stack s1;
do
{
s1.stackpush();
cout<<"wish to continue "<<endl;
cin>>ans;
}while(ans=='y');
s1.displaystack();
cout<<"Press any key to pop an element"<<endl;
getch();
s1.stackpop();
getch();
}
//PROGRAM TO IMPLEMENT LINKED QUEUE
#include<iostream.h>
#include<conio.h>
struct node
{
int data;
node * next;
};
class queue
{
node *front,*rear;
public:
queue()
{
rear=front=NULL;
}
void insqueue();
void delqueue();
void dispqueue();
};
void queue::insqueue()
{
node *ptr;
ptr=new node;
cout<<"Enter the element to be insert"<<endl;
cin>>ptr->data;
ptr->next=NULL;
if(rear==NULL)
front=rear=ptr;
else
{
rear->next=ptr;
rear=ptr;
}
}
void queue ::delqueue()
{
node *ptr;
ptr=new node;
ptr=front;
cout<<"The deleted element is "<<ptr->data;
if(front==rear)
front=rear=NULL;
else
front=front->next;
delete ptr;
}
void queue :: dispqueue()
{
node *ptr;
ptr=new node;
ptr=front;
cout<<"The queue is "<<endl;
while(ptr!=NULL)
{
cout<<ptr->data<<endl;
ptr=ptr->next;
}
}
void main()
{
clrscr();
char ans;
queue q1;
do
{
q1.insqueue();
cout<<"wish to continue "<<endl;
cin>>ans;
}while(ans=='y');
q1.dispqueue();
cout<<"Press any key to delete an element ...."<<endl;
getch();
q1.delqueue();
getch();
}
Friday, 14 September 2012
Array in C++
This is a new post about the important concept of C++ Programming : - ARRAY
//WRITE A PROGRAM TO ENTER TEN NUMBERS IN AN ARRAY AND DISPLAY THEM
#include<iostream.h>
#include<conio.h>
void main()
{
int arr[10],i;
cout<<"Enter Ten numbers in an array"<<endl;
for(i=0;i<10;i++)
{
cin>>arr[i];
}
cout<<"The array elements are "<<endl;
for(i=0;i<10;i++)
{
cout<<arr[i]<<endl;
}
getch();
}
//WRITE A PROGRAM TO ENTER TEN NUMBERS IN AN ARRAY AND DISPLAY THEM
#include<iostream.h>
#include<conio.h>
void main()
{
int arr[10],i;
cout<<"Enter Ten numbers in an array"<<endl;
for(i=0;i<10;i++)
{
cin>>arr[i];
}
cout<<"The array elements are "<<endl;
for(i=0;i<10;i++)
{
cout<<arr[i]<<endl;
}
getch();
}
//WRITE A PROGRAM TO ENTER TEN NUMBERS IN AN ARRAY AND DISPLAY THEIR SUM
#include<iostream.h>
#include<conio.h>
void main()
{
int arr[10],i,sum=0;
cout<<"Enter Ten numbers in an array"<<endl;
for(i=0;i<10;i++)
{
cin>>arr[i];
sum=sum+arr[i];
}
cout<<"The sum is "<<sum<<endl;
getch();
}
//WRITE A PROGRAM TO ENTER TEN NUMBERS IN AN ARRAY AND DISPLAY THE BIGGEST OF THEM
#include<iostream.h>
#include<conio.h>
void main()
{
int arr[10],i,big=0;
cout<<"Enter Ten numbers in an array"<<endl;
for(i=0;i<10;i++)
{
cin>>arr[i];
if(arr[i]>big)
big=arr[i];
}
cout<<"The biggest number is "<<big<<endl;
getch();
}
//WRITE A PROGRAM TO ENTER TEN NUMBERS IN AN ARRAY AND DISPLAY THE SMALLEST OF THEM
#include<iostream.h>
#include<conio.h>
void main()
{
int arr[10],i,small;
small=arr[0];
cout<<"Enter Ten numbers in an array"<<endl;
for(i=1;i<10;i++)
{
cin>>arr[i];
if(arr[i]<small)
small=arr[i];
}
cout<<"The smallest number is "<<small<<endl;
getch();
}
Monday, 10 September 2012
Stack using structure
#include<iostream.h>
#include<conio.h>
#define MAX 100
struct stack
{
int arr[MAX];
int top;
}s;
void push(stack *,int);
int pop(stack *);
void disp(stack *);
void main()
{
int ch;
s.top=-1;
clrscr();
char ans='y';
while(ans=='y'||ans=='Y')
{
clrscr();
cout<<"Operations on Stack"<<endl;
cout<<"1. Push"<<endl;
cout<<"2. Pop"<<endl;
cout<<"3.Display"<<endl;
cout<<"Enter the choice"<<endl;
cin>>ch;
switch(ch)
{
case 1:
int val;
cout<<"enter a number to push"<<endl;
cin>>val;
push(&s,val);
break;
case 2:
int x;
x=pop(&s);
cout<<"The popped element is "<<x<<endl;
break;
case 3:
disp(&s);
break;
default:
cout<<"invalid choice"<<endl;
}
cout<<"do u wish to continue y/n"<<endl;
cin>>ans;
}
getch();
}
void push(stack *s1,int x)
{
if(s1->top==-1)
{
s1->top=0;
}
else if(s1->top==(MAX-1))
{
cout<<"Stack is full"<<endl;
}
else
{
s1->top++;
}
s1->arr[s1->top]=x;
}
int pop(stack *s1)
{
int temp;
if(s1->top==-1)
{
cout<<"can't pop elements"<<endl;
}
else
{
temp=s1->arr[s1->top];
s1->top--;
return temp;
}
}
void disp(stack *s1)
{
int i;
for(i=s1->top;i>=0;i--)
{
cout<<s1->arr[i]<<endl;
}
}
Program of Stack using array
This Program is displaying all the main functions of a stack like Push, Pop and display
#include<iostream.h>
#include<conio.h>
#include<process.h>
#define MAX 10
void push(int[],int);
int pop(int[]);
void disp(int[]);
int top=-1;
void main()
{
int arr[MAX];
int i;
char ans='y';
while(ans=='y')
{
clrscr();
cout<<"Operations on stacks are "<<endl;
cout<<"1. Push "<<endl;
cout<<"2. Pop "<<endl;
cout<<"3. Display "<<endl;
cout<<"Enter your choice "<<endl;
int ch;
cin>>ch;
switch(ch)
{
case 1:
cout<<"Enter an element to push"<<endl;
int x;
cin>>x;
push(arr,x);
break;
case 2:
int y;
y=pop(arr);
cout<<"The element deleted is"<<y<<endl;
break;
case 3:
disp(arr);
break;
}
cout<<"Do u wish to cont y/n"<<endl;
cin>>ans;
}
getch();
}
void push(int a[],int x)
{
if(top==(MAX-1))
{
cout<<"The stack is full"<<endl;
cout<<"Press any key to exit............"<<endl;
getch();
exit(0);
}
else
{
if(top==-1)
{
top=0;
}
else
{
top++;
}
a[top]=x;
}
}
int pop(int a[])
{
int z=a[top];
top--;
return z;
}
void disp(int a[])
{
int i;
for(i=top;i>=0;i--)
{
cout<<a[i]<<endl;
}
}
This Program is displaying all the main functions of a stack like Push, Pop and display
#include<iostream.h>
#include<conio.h>
#include<process.h>
#define MAX 10
void push(int[],int);
int pop(int[]);
void disp(int[]);
int top=-1;
void main()
{
int arr[MAX];
int i;
char ans='y';
while(ans=='y')
{
clrscr();
cout<<"Operations on stacks are "<<endl;
cout<<"1. Push "<<endl;
cout<<"2. Pop "<<endl;
cout<<"3. Display "<<endl;
cout<<"Enter your choice "<<endl;
int ch;
cin>>ch;
switch(ch)
{
case 1:
cout<<"Enter an element to push"<<endl;
int x;
cin>>x;
push(arr,x);
break;
case 2:
int y;
y=pop(arr);
cout<<"The element deleted is"<<y<<endl;
break;
case 3:
disp(arr);
break;
}
cout<<"Do u wish to cont y/n"<<endl;
cin>>ans;
}
getch();
}
void push(int a[],int x)
{
if(top==(MAX-1))
{
cout<<"The stack is full"<<endl;
cout<<"Press any key to exit............"<<endl;
getch();
exit(0);
}
else
{
if(top==-1)
{
top=0;
}
else
{
top++;
}
a[top]=x;
}
}
int pop(int a[])
{
int z=a[top];
top--;
return z;
}
void disp(int a[])
{
int i;
for(i=top;i>=0;i--)
{
cout<<a[i]<<endl;
}
}
Tuesday, 4 September 2012
Loops Continued
//PROGRAM TO ENTER TEN NUMBERS AND DISPLAY THEIR SUM
#include<iostream.h>
#include<conio.h>
void main()
{
int num,i,sum=0;
clrscr();
cout<<"Enter Ten numbers "<<endl;
for(i=1;i<=10;i++)
{
cin>>num;
sum=sum+num;
}
cout<<"The sum is " <<sum<<endl;
getch();
}
//PROGRAM TO ENTER TEN NUMBERS AND DISPLAY THE BIGGEST NUMBER
#include<iostream.h>
#include<conio.h>
void main()
{
int num,i,big=0;
clrscr();
cout<<"Enter Ten numbers "<<endl;
for(i=1;i<=10;i++)
{
cin>>num;
if(num>big)
{
big=num;
}
}
cout<<"The biggest number is " <<big<<endl;
getch();
}
In the program given above you can make some modification to get the smallest of ten numbers also
//PROGRAM TO ENTER A NUMBER AND DISPLAY THE SUM OF ITS DIGITS
#include<iostream.h>
#include<conio.h>
void main()
{
int num,i,dig,sum=0;
clrscr();
cout<<"Enter a number "<<endl;
cin>>num;
for(i=num;i>0;i=i/10)
{
dig=i%10;
sum=sum+dig;
}
cout<<"The sum of all the digits is " <<sum<<endl;
getch();
}
//PROGRAM TO ENTER A NUMBER AND DISPLAY if it is an armstrong number is or not
#include<iostream.h>
#include<conio.h>
void main()
{
int num,i,dig,cube,sum=0;
clrscr();
cout<<"Enter a number "<<endl;
cin>>num;
for(i=num;i>0;i=i/10)
{
dig=i%10;
cube=dig*dig*dig
sum=sum+cube;
}
if(sum==num)
cout<<"The number is an armstrong number " <<endl;
else
cout<<"The number is not an armstrong number"<<endl;
getch();
}
//PROGRAM TO ENTER A NUMBER AND DISPLAY ITS REVERSE NUMBER
#include<iostream.h>
#include<conio.h>
void main()
{
int num,i,dig,rev=0;
clrscr();
cout<<"Enter a number "<<endl;
cin>>num;
for(i=num;i>0;i=i/10)
{
dig=i%10;
rev=rev*10+dig;
}
cout<<"The reverse number is " <<rev<<endl;
getch();
}
//PROGRAM TO ENTER A NUMBER AND CHECK IF IT IS A PALINDROME NUMBER OR NOT
#include<iostream.h>
#include<conio.h>
void main()
{
int num,i,dig,rev=0;
clrscr();
cout<<"Enter a number "<<endl;
cin>>num;
for(i=num;i>0;i=i/10)
{
dig=i%10;
rev=rev*10+dig;
}
cout<<"The reverse number is " <<rev<<endl;
if(rev==num)
{
cout<<"Palindrome Number "<<endl;
}
else
{
cout<<"Not a Palindrome number"<<endl;
}
getch();
}
// WRITE A PROGRAM TO ENTER A NUMBER AND DISPLAY IF IT IS APRIME NUMBER OR NOT
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int num,i;
cout<<"Enter the number "<<endl;
cin>>num;
for(i=2;i<=num-1;i++)
{
if(num%i==0)
{
cout<<"Not a prime number"<<endl;
break;
}
}
if(i==num)
{
cout<<"The number is a prime number"<<endl;
}
getch();
}
Monday, 3 September 2012
Today i am starting a series of programs of loops. Now i wish that i would be regularly updating this blog so that all of my followers can be benefitted. thanks in believing in me
//WRITE A PROGRAM TO DISPLAY 1 TO 10 USING WHILE LOOP
#include<iostream.h>
#include<conio.h
void main()
{
int i=1;
while(i<=10)
{
cout<<i<<endl;
i++;
}
getch();
}
//WRITE A PROGRAM TO DISPLAY FIRST 10 EVEN NUMBERS USING WHILE LOOP
#include<iostream.h>
#include<conio.h>
void main()
{
int i=2;
while(i<=20)
{
cout<<i<<endl;
i=i+2;
}
getch();
}
//WRITE A PROGRAM TO DISPLAY THE SUM OF 1 TO 10 USING WHILE LOOP
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int i=1,sum=0;
while(i<=10)
{
sum=sum+i;
i++;
}
cout<<"The sum of 1 to 10 is "<<sum;
getch();
}
//WRITE A PROGRAM TO DISPLAY 1 TO 10 USING WHILE LOOP
#include<iostream.h>
#include<conio.h
void main()
{
int i=1;
while(i<=10)
{
cout<<i<<endl;
i++;
}
getch();
}
//WRITE A PROGRAM TO DISPLAY FIRST 10 EVEN NUMBERS USING WHILE LOOP
#include<iostream.h>
#include<conio.h>
void main()
{
int i=2;
while(i<=20)
{
cout<<i<<endl;
i=i+2;
}
getch();
}
//WRITE A PROGRAM TO DISPLAY THE SUM OF 1 TO 10 USING WHILE LOOP
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int i=1,sum=0;
while(i<=10)
{
sum=sum+i;
i++;
}
cout<<"The sum of 1 to 10 is "<<sum;
getch();
}
Friday, 20 January 2012
string concatenation without strcat()
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
void main()
{
clrscr();
char str[20],str1[40];
cout<<"Enter source string"<<endl;
gets(str);
cout<<"Enter target string"<<endl;
gets(str1);
int l=0;
for(int i=0;str1[i]!='\0';i++)
{
l++;
}
int j;
for(i=0,j=l;str[i]!='\0';i++,j++)
{
str1[j]=str[i];
}
str1[j]='\0';
cout<<"The concatenated string is "<<str1;
getch();
}
Tuesday, 17 January 2012
multiplication of matrices
#include<iostream.h>
#include<conio.h>
void main()
{
int mat1[3][3],mat2[3][3],mat3[3][3],i,j,k;
clrscr();
cout<<"Enter the elements in the first matrix"<<endl;
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
cin>>mat1[i][j];
}
}
cout<<"Enter the elements in the second matrix"<<endl;
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
cin>>mat2[i][j];
}
}
//loop to multiply
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
mat3[i][j]=0;
for(k=0;k<3;k++)
{
mat3[i][j]=mat3[i][j]+(mat1[i][k]*mat2[k][j]);
}
}
}
//loop to display the final matrix
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
cout<<mat3[i][j]<<"\t";
}
cout<<endl;
}
getch();
}
sum of two matrices
// program to enter two 2D arrays and display their sum in matrix form
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int mat1[3][3],mat2[3][3],sum[3][3],i,j;
cout<<"Enter elements in first matrix"<<endl;
for(i=0;i<3;i++) //Row
{
for(j=0;j<3;j++) //Column
{
cin>>mat1[i][j];
}
}
cout<<"Enter elements in second matrix"<<endl;
for(i=0;i<3;i++) //Row
{
for(j=0;j<3;j++) //Column
{
cin>>mat2[i][j];
}
}
// loop to sum two matrices
for(i=0;i<3;i++) //Row
{
for(j=0;j<3;j++) //Column
{
sum[i][j]=mat1[i][j]+mat2[i][j];
}
}
//loop to display sum in matrix form
cout<<"The sum of two matrices is "<<endl;
for(i=0;i<3;i++) //Row
{
for(j=0;j<3;j++) //Column
{
cout<<sum[i][j]<<"\t";
}
cout<<endl;
}
getch();
}
matrix transpose
#include<iostream.h>
#include<conio.h>
void main()
{
int mat[3][3],matt[3][3],i,j;
clrscr();
cout<<"Enter the elements in the matrix"<<endl;
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
cin>>mat[i][j];
}
}
//loop to transpose a matrix
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
matt[i][j]=mat[j][i];
}
}
cout<<"The Transpose matrix is "<<endl;
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
cout<<matt[i][j]<<"\t";
}
cout<<endl;
}
getch();
}
binary search method
//program to search an element using binary search
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int arr[10],i,j,temp,flag=0;
cout<<"Enter ten numbers"<<endl;
for(i=0;i<10;i++)
{
cin>>arr[i];
}
//loop to sort array
for(i=0;i<10;i++)
{
for(j=0;j<10-i-1;j++)
{
if(arr[j]>arr[j+1])
{
temp=arr[j];
arr[j]=arr[j+1];
arr[j+1]=temp;
}
}
}
cout<<"The number in sorted form are"<<endl;
for(i=0;i<10;i++)
{
cout<<arr[i]<<endl;
}
cout<<"Enter a number to search"<<endl;
int num;
cin>>num;
int l=0,h=9,mid;
while(l<=h)
{
mid =(l+h)/2;
if (num>arr[mid])
l=mid+1;
else if (num<arr[mid])
h=mid-1;
else
{
flag=1;
cout<<"The number is found at "<<mid+1<<" position"<<endl;
break;
}
}
if(flag==0)
{
cout<<"The number is not found in the list"<<endl;
}
getch();
}
linear search method
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int arr[10],i,num,flag=0;
cout<<"Enter ten numbers in the array"<<endl;
for(i=0;i<10;i++)
{
cin>>arr[i];
}
cout<<"Enter the number to search"<<endl;
cin>>num;
cout<<"The array elements are"<<endl;
for(i=0;i<10;i++)
{
cout<<arr[i]<<endl;
}
// loop to search the number
for(i=0;i<10;i++)
{
if(num==arr[i])
{
flag=1;
cout<<"The number is found at "<<i+1<<" location"<<endl;
break;
}
}
if(flag==0)
{
cout<<"The number is not found"<<endl;
}
getch();
}
bubble sort method
#include<iostream.h>
#include<conio.h>
void main()
{
int arr[10],i,j,temp;
cout<<"Enter ten numbers"<<endl;
for(i=0;i<10;i++)
{
cin>>arr[i];
}
//loop to sort array using bubble sort method
for(i=0;i<10;i++)
{
for(j=0;j<10-i-1;j++)
{
if(arr[j]>arr[j+1])
{
temp=arr[j];
arr[j]=arr[j+1];
arr[j+1]=temp;
}
}
}
cout<<"the array elements after sorting are"<<endl;
for(i=0;i<10;i++)
{
cout<<arr[i]<<endl;
}
getch();
}
Subscribe to:
Posts (Atom)

