Welcome

Hi! This blog is for beginners of c++ and java .You can check and pick codes for your programming assignments. This will also help you to understand the concepts of these programming languages. Its all about programming.

Simple code for ATM - C++

Tuesday, July 26, 2011


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

using namespace std;
class atm
{
   private:
      float balance;
   public:
      atm()
      {
         balance=0;
      }
      void deposit(float a)
      {
         cout<<"Enter amount you want to deposit"<<endl;
         cin>>a;
         balance=balance+a;
      }
      void withdraw(float b)
      {
         cout<<"Enter money you want to withdraw"<<endl;
         cin>>b;
         if(b>balance)
            cout<<"Your balance is not enough to continue transaction"<<endl;
         else
            balance=balance-b;
      }
      float showBalance()
      {
         return balance;
      }
};

void main()
{
   atm ob;
   float y=0,z=0;
   char choice;
   string password="";
   char a;
   cout<<"Enter your password:"<<endl;
   do
   {
      a=_getch();
      switch(a)
      {
         case '\b':     // "\b" is used for BACKSPACE
            if(password.length()>0)
               {
                  password.erase(password.length()-1);
                  cout<<'\b'<<" "<<'\b';
                  break;
               }
         default:
            if(a>31&&a<127)        // 31 to 127 are ASCII code for keyboard keys    
               {
                  password.push_back(a);
                  cout<<"*";
               }
     }
   }
   while(a!='\r');           // "\r" is used for ENTER
   if(password=="tzt")
      {
         cout << "\n^^^^Access granted^^^^ \n";
         cout<<"Your current account balance is 0.\n"<<endl;
         do
         {
            int opt;
            cout<<"Enter 1 if you want to deposit money."<<endl;
            cout<<"Enter 2 if you want to withdraw money."<<endl;
            cin>>opt;
            switch(opt)
               {
                  case 1:
                        ob.deposit(y);
                        break;
                  case 2:
                        ob.withdraw(z);
                        break;
                  default:
                        cout<<"Wrong entry!"<<endl;
                        break;
               }
           cout<<"Your account balance:"<<ob.showBalance()<<endl;
           cout<<"******PRESS******\ny to continue\nn to terminate\n";
           cin>>choice;
        }
       while(choice!='n');
     }
   else
   {
      cout << "\nAccess denied...\n";
   }
   cout<<"*********GOOD BYE*********"<<endl;
   _getch();  
}

Sunday, July 17, 2011

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace simple
{
    class Program
    {
        static void Main(string[] args)
        {
            int a,b,c;
            Console.WriteLine("enter 1st integer\n");
            a = Convert.ToInt16(Console.ReadLine());
            Console.WriteLine("enter 2nd integer\n");
            b = Convert.ToInt16(Console.ReadLine());
            c = a + b;
            Console.WriteLine("sum of {0} and {1} is{2}", a, b, c);
            Console.WriteLine("difference of {0} {1} is {2}", a, b, a - b);
            Console.WriteLine("multiplication of {0} and {1} is {2}", a, b, a * b);
            Console.WriteLine("division of {0} and {1} is {2}", a, b, a / b);
            Console.ReadKey();
        }
    }
}
//TikroSoft
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace chash_test_2
{
    class Program
    {
        static void Main(string[] args)
        {
            int a;
            Console.WriteLine("enter a no");
            a = Convert.ToInt16(Console.ReadLine());
            string myString = (a < 10) ? "less than 10" : "greater than 10";//The Ternary Operator
            Console.WriteLine(myString);
            Console.ReadKey();
        }
    }
}
//TikroSoft

ifelse

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace chash_test_2
{
    class Program
    {
        static void Main(string[] args)
        {
            int a, b;
            string Comp;
            Console.WriteLine("cout 2 no");
            a = Convert.ToInt16(Console.ReadLine());
            b= Convert.ToInt16(Console.ReadLine());
            if (a > b)
            {
                Comp = "is greater than";
                    Console.WriteLine("{0}is {1} {2}",a,Comp,b);
            }else
            {
                Comp = "is less than";
                    Console.WriteLine("{0}is {1} {2}",a,Comp,b);
            }

            Console.ReadKey();

           
        }
    }
}
//TikroSofts

Character Pointer Usng As String

Friday, July 1, 2011

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

int main(){
   const int aSize=25;
   char arr[aSize];
   char* pChar;
   int   i = 0;
   int   nameSize;
  
   pChar = arr;
   cout << "Enter a name: ";
  
  for (i=0; i<aSize; i++){
     *(pChar+i)=getch();

     // --- If Enter is pressed, stop copying and break out of the for loop
     if (arr[i] == '\r'){
        cout << endl;
        nameSize=i;    // storing the size of the name
        break;
     } // end if
     cout << arr[i];
  }  // end for - Array arr
  
  // Displaying Array
  cout << endl;
  for (i=0; i<nameSize; i++)
      cout << arr[i];
     
return 0;
} // end main()

Calculator Using Pointers & Parameter pass by Refrence.

#include<iostream.h>

void add(float* , float*);        // function declaration
void sub(float* , float*);        // function declaration
void mul(float* , float*);        // function declaration
void div(float* , float*);        // function declaration

int main(){
    char option;
    float a;
    float b;     
   
    cout << "Choose from the following options: " << endl;
    cout << "+ : Plus" << endl;
    cout << "- : Minus" << endl;
    cout << "* : Multiply" << endl;
    cout << "/ : Divide" << endl;
    cin >> option;
   
    cout << "Enter first number: ";  cin >> a;
    cout << "Enter second number: "; cin >> b;
   
    switch (option){
      case '+':
         add(&a, &b);
         break;
     
      case '-':
         sub(&a, &b);
         break;
     
      case '*':
         mul(&a, &b);
         break;
     
      case '/':
         div(&a, &b);
         break;
     
      default:
         cout << "Wrong option. Run again.";
    } // end switch
   
return 0;
} // end main()

// ========= () ================
void add(float* x, float* y){
   cout << *x << " + " << *y << " = " << ((*x) + (*y));
return;


// ========= () ================
void sub(float* x , float* y) {
   cout << *x << " - " << *y << " = " << ((*x) - (*y));
return;   
}

// ========= () ================
void mul(float* x, float* y){
   cout << *x << " * " << *y << " = " << ((*x) * (*y));
return;


// ========= () ================
void div(float* x, float* y){
   cout << *x << " / " << *y << " = " << ((*x) / (*y));
return;

Using Of Pointers in Functions

/*
  Write a program using pointers to input any number
  and then pass that number to the function which will
  increment the value pointed by that pointer by 1
*/
#include<iostream.h>

void increment(int* p);        // function declaration
int main(){
   int a;
  
   int* p=&a;    // declaring pointer p and intializing it to address of integer a
  
   cout << " Enter a number: ";
   cin >> a;
  
   cout << endl << "You have enetered: " << a << endl;
  
   increment(p);
  
   cout << endl << "Value after incrementing is: " << a << endl;
  
   // Another way to call for function is to send the address like below
  
   increment(&a);
  
   cout << endl << "Value after second increment is: " << a << endl;
         
return 0;
} // end main()

// ========= increment() ================
void increment(int* p1){
   (*p1)++;
return;
} // end increment()

Adding of Two Integers Using Pointers

#include<iostream.h>

int main(){
   int a = 55;
   int b = 33;
  
   int* p1;
   int* p2;
  
   p1 = &a;    // initializing p1 to address of integer a  
   p2 = &b;    // initializing p1 to address of integer b  
  
   // Adding using integers.
  
   cout << a << " + " << b << " = " << (*p1 + * p2);
        
return 0;
} // end main()