Showing posts with label C plus plus. Show all posts
Showing posts with label C plus plus. Show all posts

Saturday, 9 November 2013

Program to represent a bank account using classes and objects.

This code represents a bank account.
 We have defined a class named bank which has 4 different functions :-
 1.'enterdata' to enter the data.
2. 'deposit' to deposit an amount in bank.
3.'withdraw' to withdraw an amount from the bank
4.'dispdata' to display the final account status.

Code:

#include<iostream>
#include<stdlib.h>
#include<conio.h>
using namespace std;
class  bank
{
 private:
  char name[10],type[10];
  int acc_no,balance;
public:
 void enterdata(void)
 {
        cout<<"enter name of person";
        cin>>name;
        cout<<"enter type of account";
        cin>>type;
        cout<<"enter account no.";
        cin>>acc_no;
        cout<<"enter balance";
        cin>>balance;
 }
 void deposit(void)
 {
        int n;
        cout<<"enter amount to deposit";
        cin>>n;
        cout<<"total is:"<<balance+n;
        balance=balance+n;
 }
 void withdraw(void)
 {
        int w;
        cout<<"balance is:"<<balance;
        cin>>w;
        cout<<"balance is:"<<balance-w;
        balance=balance-w;
 }
 void dispdata(void)
 {
        cout<<"name:"<<name;
        cout<<"balance left:"<<balance;
 }
};
int main(void)
{
 bank b1,b2,b3,b4,b5,b6,b7,b8,b9,b10;
 int k,i,choice;
 while(k)
    {
        cout<<"\n1.To assign the initial values\n";
  cout<<"2.To deposit an amount\n";
  cout<<"3.To withdraw an amount\n";
  cout<<"4.To display name and balance\n";
  cout<<"5.Exit\n";
  cin>>choice;
  switch(choice)
  {
      case 1: b1.enterdata();
      break;
      case 2: b1.deposit();
      break;
      case 3: b1.withdraw();
         break;
            case 4: b1.dispdata();
         break;
         case 5: exit(0);
         break;
         default:
         cout<<"wrong choice";
         break;
  }
    }
 getch();
}

WAP to concatenate 2 strings using constructor & destructor ( dynamic constructor ).

This program:- 
1. Inputs 2 strings by user.
2.Concatenates them using dynamic constructor. 
We have defined a class String which has String constructor performing concatenation. Here we have input the 2 strings as 'Hello' & 'World'.

CODE:---

#include<iostream>
#include<string.h>
using namespace std;
class String
{
char *name;
int length;
public:
String()
{
length=0;
name= new char[length+1];
}
String(char *s)
{
length=strlen(s);
name= new char[length+1];
strcpy(name,s);
}
void display()
{
cout<<name<<endl;
}
void join(String &a,String &b)
{
length=a.length+b.length;
delete name;
name= new char[length+1];
strcpy(name,a.name);
strcat(name,b.name);
}
};
int main()
{
String name1("Hello"),name2("World"),s1;
s1.join(name1,name2);
name1.display();
name2.display();
s1.display();
}

Thursday, 31 October 2013

C++ Program To Print ASCII value of Digits,Uppercase and Lowercase Alphabetes

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

void main()
{
clrscr();
char ch,a[]={"Hello world This is me"};
int j=0;

cout<<"Uppercase Alphabetes\n\n";
for(int i=65;i<91;++i)
{
j++;
ch=i;
cout<<ch<<":"<<i<<"\t";
if(j==10)
{
cout<<"\n";
j=0;
}
}
j=0;

cout<<"\n\n\nLowercase Alphabetes\n\n";
for(i=97;i<123;++i)
{
j++;
ch=i;
cout<<ch<<":"<<i<<"\t";
if(j==10)
{
cout<<"\n";
j=0;
}
}

cout<<"\n\n\nDigits\n\n";
for(i=48;i<58;i++)
{
ch=i;
cout<<ch<<":"<<i<<"\t";
}

cout<<"\n\n\n\n\t\t";
for(i=0;a[i]!='\0';++i)
{
cout<<a[i];
sleep(1);
}
exit(0);
}

-------------------------------------------------------------------------------------------

C++ Program to find Compound Interest.

#include<iostream.h>
#include<conio.h>
#include<math.h>
void main()
{
float p,r,t,ci;
clrscr();

cout<<"Enter Principle,Rate and Time ";
cin>>p>>r>>t;
ci=pow(p*(1+r/100),t);

cout<<"\n"<<"Compound Interest = "<<ci<<"%";
getch();
}

--------------------------------------------------------------------------------------------------------------------------

Tuesday, 29 October 2013

C++ Program check whether a number is palindrome or not.

#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
unsigned long n,num,d,rev=0;
cout<<"Enter any number: ";
cin>>n;
num=n;
do
{
d=n%10;
rev=(rev*10)+d;
n=n/10;
}while(n!=0);
if(num==rev)
cout<<endl<<"Number is Palindrome";
else
cout<<endl<<"Number is not Palindrome";
getch();
}
----------------------------------------------------------------------

C++ Program to print table of any number

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

void main()
{
clrscr();
int i,n;
cout<<"Enter Number to Find Its Multiplication Table :";
cin>>n;
cout<<"\n\n";

for(i=1;i<=10;++i)
cout<<"\t"<<n<<"*"<<i<<"="<<n*i<<"\n";
getch();
}


 If u want more programs or any help contact me on Facebook , i will be happy if i could help u...  

Click on the name below :-
***** Nilov Manna *****

TIC - TAC -TOE GAME in C++

OUTPUT SNAPSHOT :-

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


char mat[3][3];
void table(void);            //function to print the table
void welcome(void);     //function for welcome screen

void main()
{
 welcome();
     A: clrscr();
 int i,j,m,n,sum=0;
 char ch;

 for(m=0;m<3;++m)
  for(n=0;n<3;++n)
   mat[m][n]='\0';
 table();

 while(sum<10)
 {
 //for player 1
 cout<<"Player 1 is'x'\nChoose the position:";
 cout<<"\nRow:";
 cin>>i;
 cout<<"Coloumn:";
 cin>>j;

//if position is wrong
 for(;i>3||i<1||j>3||j<1||('x'==mat[i-1][j-1]||'o'==mat[i-1][j-1]);)
 {
    cout<<"\nSorry!!!! wrong position,Choose the position again";
    cout<<"\nRow:";
    cin>>i;
    cout<<"Coloumn:";
    cin>>j;
 }
 mat[i-1][j-1]='x';
 sum++;

 //to check if player 1 wins or not
 if(mat[0][0]=='x'&&mat[0][0]==mat[0][1]&&mat[0][0]==mat[0][2])
 {
  table();
  cout<<"\nPlayer 1 wins.......!!!" ;
  cout<<"\nYou have played Great.....!!!";
  sleep(5);
  break;
 }

 if(mat[1][0]=='x'&&mat[1][0]==mat[1][1]&&mat[1][0]==mat[1][2])
 {
  table();
  cout<<"\nPlayer 1 wins.......!!!";
  cout<<"\nYou have played Great.....!!!";
  sleep(5);
  break;
 }

 if(mat[2][0]=='x'&&mat[2][0]==mat[2][1]&&mat[2][0]==mat[2][2])
 {
  table();
  cout<<"\nPlayer 1 wins.......!!!";
  cout<<"\nYou have played Great.....!!!";
  sleep(5);
  break;
 }

 if(mat[0][0]=='x'&&mat[0][0]==mat[1][0]&&mat[0][0]==mat[2][0])
 {
  table();
  cout<<"\nPlayer 1 wins.......!!!";
  cout<<"\nYou have played Great.....!!!";
  sleep(5);
  break;
 }

 if(mat[0][1]=='x'&&mat[0][1]==mat[1][1]&&mat[0][1]==mat[2][1])
 {
  table();
  cout<<"\nPlayer 1 wins.......!!!";
  cout<<"\nYou have played Great.....!!!";
  sleep(5);
  break;
 }

 if(mat[0][2]=='x'&&mat[0][2]==mat[1][2]&&mat[0][2]==mat[2][2])
 {
  table();
  cout<<"\nPlayer 1 wins.......!!!";
  cout<<"\nYou have played Great.....!!!";
  sleep(5);
  break;
 }

 if(mat[0][0]=='x'&&mat[0][0]==mat[1][1]&&mat[0][0]==mat[2][2])
 {
  table();
  cout<<"\nPlayer 1 wins.......!!!";
  cout<<"\nYou have played Great.....!!!";
  sleep(5);
  break;
 }

 if(mat[0][2]=='x'&&mat[0][2]==mat[1][1]&&mat[0][0]==mat[2][0])
 {
  table();
  cout<<"\nPlayer 1 wins.......!!!";
  cout<<"\nYou have played Great.....!!!";
  sleep(5);
  break;
 }

 if(sum==9) //to check the chances
 {
  table();
  cout<<"\n\tThe game is over.......no one wins...HaHaHa.....!!!";
  break;
 }
 //for player 2
 cout<<"\n\nPlayer 2 is'o'\nChoose the position:";
 cout<<"\nRow:";
 cin>>i;
 cout<<"Coloumn:";
 cin>>j;

 //if position is wrong
 for(;i>3||i<1||j>3||j<1||('x'==mat[i-1][j-1]||'o'==mat[i-1][j-1]);)
 {
    cout<<"\nSorry!!!! wrong position,Choose the position again";
    cout<<"\nRow:";
    cin>>i;
    cout<<"Coloumn:";
    cin>>j;
 }
 mat[i-1][j-1]='o';
 sum++;
 table();

 //to check player 2 wins or not
 if(mat[0][0]=='o'&&mat[0][0]==mat[0][1]&&mat[0][0]==mat[0][2])
 {
  cout<<"\nPlayer 2 wins.......!!!";
  cout<<"\nYou have played Great.....!!!";
  sleep(5);
  break;
 }

 if(mat[1][0]=='o'&&mat[1][0]==mat[1][1]&&mat[1][0]==mat[1][2])
 {
  cout<<"\nPlayer 2 wins.......!!!";
  cout<<"\nYou have played Great.....!!!";
  sleep(5);
  break;
 }

 if(mat[2][0]=='o'&&mat[2][0]==mat[2][1]&&mat[2][0]==mat[2][2])
 {
  cout<<"\nPlayer 2 wins.......!!!";
  cout<<"\nYou have played Great.....!!!";
  sleep(5);
  break;
 }

 if(mat[0][0]=='o'&&mat[0][0]==mat[1][0]&&mat[0][0]==mat[2][0])
 {
  cout<<"\nPlayer 2 wins.......!!!";
  cout<<"\nYou have played Great.....!!!";
  sleep(5);
  break;
 }

 if(mat[0][1]=='o'&&mat[0][1]==mat[1][1]&&mat[0][1]==mat[2][1])
 {
  cout<<"\nPlayer 2 wins.......!!!";
  cout<<"\nYou have played Great.....!!!";
  sleep(5);
  break;
 }

 if(mat[0][2]=='o'&&mat[0][2]==mat[1][2]&&mat[0][2]==mat[2][2])
 {
  cout<<"\nPlayer 2 wins.......!!!";
  cout<<"\nYou have played Great.....!!!";
  sleep(5);
  break;
 }

 if(mat[0][0]=='o'&&mat[0][0]==mat[1][1]&&mat[0][0]==mat[2][2])
 {
  cout<<"\nPlayer 2 wins.......!!!";
  cout<<"\nYou have played Great.....!!!";
  sleep(5);
  break;
 }

 if(mat[0][2]=='o'&&mat[0][2]==mat[1][1]&&mat[0][0]==mat[2][0])
 {
  cout<<"\nPlayer 2 wins.......!!!";
  cout<<"\nYou have played Great.....!!!";
  sleep(5);
  break;
 }
 }
 cout<<"\n\n\tWould you like to play more....?(y/n):";
 cin>>ch;
 if(ch=='y'||ch=='Y')
  goto A;
 else
 {
  cout<<"\n\n\tThanks for Playing.......:)";
  sleep(5);
  exit(0);
 }
}

void table()
{
 clrscr();
 cout<<"\n\n\t\t  1  2  3\n";
 cout<<"\t\t1  "<<mat[0][0]<<"|"<<mat[0][1]<<"|"<<mat[0][2];
 cout<<"\n\t\t  --|-|--";
 cout<<"\n\t\t2  "<<mat[1][0]<<"|"<<mat[1][1]<<"|"<<mat[1][2];
 cout<<"\n\t\t  --|-|--";
 cout<<"\n\t\t3  "<<mat[2][0]<<"|"<<mat[2][1]<<"|"<<mat[2][2]<<"\n\n";
}

void welcome()
{
 textmode(C80);
 textcolor(YELLOW);
 clrscr();

 cout<<"\n\n\n\n\n\t\t\t\tWelcome To";
 sleep(2);
 cout<<"\n\n\t\t\t\tTic-Tac-Toe";
 sleep(1);
 cout<<"\n\n\t\t\t\t   Game";
 sleep(1);
 cout<<"\n\n\n\n\n\n\n\n\n\t\t\t\t\tPress any key to continue.....!!";
 getch();
}


 If u want more programs or any help contact me on Facebook , i will be happy if i could help u...  

Click on the name below :-
***** Nilov Manna *****