Saturday, February 26, 2011

Files


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

void main()
{
fstream ob;

ob.open("stud1.dat",ios::out|ios::binary);

int rollno,mark,entry;

clrscr();

cout<<"\n\n\n\n\t\t\tEnter the serial no[0 to exit]:";
cin>>entry;

 while(entry!=0)
 {
 cout<<endl<<"\n\n\t\tEnter the stud no: ";
 cin>>rollno;

 cout<<"\n\n\t\tEnter the mark: ";
 cin>>mark;

 cout<<endl;

 ob<<setw(0)<<rollno<<setw(10) <<mark<<endl;

 cout<<"\n\n\n\t\t\tEnter serial no[0 to exit]";
 cin>>entry;

 }

ob.close();

getch();

}

Friday, February 25, 2011

Inheritance


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

class shape
{
public:
float a;
};

class triangle: public shape
{
int a,b,c;
float s;

public:
 triangle(int x,int y,int z)
 {
 a=x;b=y;c=z;
 s=(a+b+c)/2.0;
 }

 void area()
 {
 a=sqrt(s*(s-a)*(s-b)*(s-c));
 }

 void show()
 {
 cout<<"\nThe Area of the Triangle is : "<<a;
 }

};

class rectangle: public shape
{
int l,b;

public:
 rectangle(int x,int y)
 {
 l=x;
 b=y;
 }

 void area()
 {
 a=l*b;
 }

 void show()
 {
 cout<<"\n"<<"The Area of the Rectangle is : "<<a;
 }

};

class circle: public shape
{
int r;
public:

 circle(int x)
 {
 r=x;
 }

 void area()
 {
 a=3.14*r*r;
 }

 void show()
 {
 cout<<"\n"<<"Area of the Circle is : "<<a;
 }

};

void main()
{
clrscr();

triangle ob(20,20,20);
ob.area();
ob.show();

rectangle ob1(6,5);
ob1.area();
ob1.show();

circle ob2(7);
ob2.area();
ob2.show();

getch();
}



Thursday, February 24, 2011

Constructors and Destructors


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

class fixed
{
int a,b;
public:

 fixed()
 {
 cout<<"\nThe Constructor has Zero Arguments\n";
 cout<<"\n";
 }

 fixed(int x)
 {
 a=x;
 cout<<"The Constructor has One Argument"<<"\n\n";
 cout<<a<<"\n\n";
 }

 fixed(int x,int y)
 {
 a=x;
 b=y;
 cout<<"This Constructor Has Two Arguments:"<<"\n\n";
 cout<<a<<"\n"<<b;
 }

 ~fixed()
 {
 cout<<"\n Objects Destroyed";
 cout<<"\n";
 }

};

void main()
{
clrscr();

fixed ob;
fixed ob1(10);
fixed ob2(20);
fixed ob3(10,20);

getch();
}

Default Arguments


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

class find
{
public:
int a,b;

 void get(int x,int y=20)
 {
 a=x;
 b=y;
 }

 void display()
 {
 cout<<"\n A : " <<a<<"\n";
 cout<<"\n B : " <<b<<"\n";
 }

};

void main()
{
clrscr();

find ob;

ob.get(10);

ob.display();
find ob1;

ob1.get(20,30);

ob1.display();

getch();

}


Wednesday, February 23, 2011

Classes and Objects


CPP Programs. Banking Program

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

class banking
{
public:
char name[25];
char type[30];
char number[20];
long int balance;
long int dep,with;

 void create()
 {
 cout<<"\n\t\tEnter name: ";
 cin>>name;
 cout<<"\n\t\tEnter account type: ";
 cin>>type;
 cout<<"\n\t\tEnter account number: ";
 cin>>number;
 cout<<"\n\t\tInitial balance: ";
 cin>>balance;
 }

 void deposit()
 {
 cout<<"\n\t\tEnter the amount to be deposited: ";
 cin>>dep;
 balance=balance+dep;
 cout<<"\n\t\tCurrent balance: "<<balance;
 }

 void withdraw()
 {
 cout<<"\n\t\tEnter the amount to be withdrawn: ";
 cin>>with;
 balance=balance-with;
 cout<<"\n\t\tCurrent Balance: "<<balance;
 }

 void display()
 {
 cout<<"\n\t\tName of the customer: "<<name;
 cout<<endl;
 cout<<"\n\t\tAccount Number: "<<number;
 cout<<endl;
 cout<<"\n\t\tType of account: "<<type;
 cout<<endl;
 cout<<"\n\t\tBalance: "<<balance;
 }

};

void main()
{
banking ob;
int x;

clrscr();


 do
 {
 cout<<endl;
 cout<<"\n\n\n\t\t\tWelcome to State Bank  Of India ";
 cout<<"\n\n\t\t 1.Create a new account";
 cout<<"\n\n\t\t 2.Deposit amount";
 cout<<"\n\n\t\t 3.Withdraw amount";
 cout<<"\n\n\t\t 4.Display";
 cout<<"\n\n\t\t 5.Exit";
 cout<<"\n\n\t\t Select your choice: ";
 cin>>x;
 
  switch(x)
  {
  case 1:ob.create();break;
  case 2:ob.deposit();break;
  case 3:ob.withdraw();break;
  case 4:ob.display();break;
  case 5:break;
  default:cout<<"Enter any value b/w 1-5";
  }

 }while(x!=5);

}



Tuesday, February 22, 2011

Palindrome Program!!

Palindrome is a word which reads the same from both sides. Example is MADAM. This program uses the built-in functions from the directory 'string.h' , to determine whether the given string is a palindrome or not. 

The command 'strrev' reverses the string and 'strcpy' copies one string to another for comparision.


PROGRAM:
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char a[80],b[80];
clrscr();
printf("\nEnter the String:");
gets(a);
strcpy(b,a);
strrev(b);
if(strcmp(a,b)==0)
printf("\nThe String is Palindrome");
else
printf("\nThe String is Non-Palindrome");
getch();
}


String Reverse Program!!

This program uses the built-in function to reverse the given string. It is defined in the header file 'string.h' with the command 'strrev( )'

PROGRAM:
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char a[80];
int l;
clrscr();
printf("\nEnter the String:");
gets(a);
l=strlen(a);
printf("\nThe Length of %s is %d",a,l);
getch();
} 


String Lenth Program!!

The programs given below calculate the lenth of the given string. (a) is the program for calculating the length without using the built-in functions , whereas (b) uses the built-in function from the header file <string.h>


(a)
The length of the string is the number of spaces it occupies. It is calculated using the for control structure. When the sring ends the system assingns '\0' for the next space.

For example, Let us take the string 'an' , a[0]='a' ; a[1]='n' ; a[2]='\0'.
 Hence by finding the ith position in which a[i]='\0', the string length is determined.


PROGRAM:
#include<stdio.h>
#include<conio.h>
void main()
{
char a[80];
int l;
clrscr();
printf("\nEnter the String:");
gets(a);
l=sl(a);
printf("\nLength of String A is %d",l);
getch();
}

int sl(char str[])
{
int i;
  for (i=0;i<=80;i++)
   {
     if (str[i]=='\0')
     return(i);
   }
}


Matrix Program!!

This Program helps us to add a 3*3 matrix. The concept of 'arrays'is used here. For loops are used to get the inputs and print them. To add matrix of order n*n change all the 3 to n.

PROGRAM:
#include <stdio.h>
#include<conio.h>
void main()
 {
  int a[3][3],b[3][3],c[3][3];
  int i,j;
  clrscr();
  printf("\nEnter the first matrix:");
  for(i=0;i<3;i++)
    for(j=0;j<3;j++)
      scanf("%d",&a[i][j]);
  printf("\nThe first matrix is");
  for(i=0;i<3;i++)
   {
    printf("\n");
    for(j=0;j<3;j++)
     printf ("\t%d",a[i][j]);
   }
  printf ("\nEnter the second matrix is");
  for(i=0;i<3;i++)
    for(j=0;j<3;j++)
      scanf("\t%d",&b[i][j]);
  printf("\nThe Second Matrix is:");
  for(i=0;i<3;i++)
   {
    printf("\n");
    for(j=0;j<3;j++)
     printf ("\t%d",b[i][j]);
   }
  for(i=0;i<3;i++)
    for(j=0;j<3;j++)
      c[i][j]=a[i][j]+b[i][j];
  printf("\nThe Resultant matrix is:");
  for(i=0;i<3;i++)
   {
    printf("\n");
    for(j=0;j<3;j++)
     printf ("\t%d",c[i][j]);
   }
  getch();
}

Monday, February 21, 2011

Functions Program!!

This Program illustrates the use of  'functions'. The first function adds while the second subtracts the given 2 numbers

PROGRAM:
#include<stdio.h>
#include<conio.h>
void main()
 {
  int a,b;
  clrscr();
  printf("Enter A:");
  scanf("%d",&a);
  printf("Enter B:");
  scanf("%d",&b);
  sum(a,b);
  diff(a,b);
  getch();
 }

sum(int x,int y)
 {
  int ad;
  ad=x+y;
  printf("Sum=%d",ad);
 }

diff(int x,int y)
 {
  int su;
  su=x-y;
  printf("Difference=%d",su);
 } 


Fibonacci Series Program!!

This program generates the fibonacci series upto the number of terms which we desire. 'for' Control Structure is used.

LOGIC:
Fibonacci Series=Fn  
were the seed values are
 

PROGRAM:
#include<stdio.h>
#include<conio.h>
void main()
{
  int i,j,k,n,x;
  clrscr();
  i=0;
  j=1;
  printf("Enter the number of terms:");
  scanf("%d", &x);
  printf("%d    %d    ",i,j);
  for(n=0;n<=x;n++)
  {
    k=i+j;
    i=j;
    j=k;
    printf("%d    ",k);
  }
getch();
}

Factorial Program!!

This program computes the factorial of the given number and displays it. The control structure 'for' is used to determine the factorial. When there is a number of iteration is carried out till a desired output or condition is solved , we go for the 'for' statement.

LOGIC:
 Factroial of 'n' is denoted as 'n!' 

 

PROGRAM:
#include<stdio.h>
#include<conio.h>
void main()
{
int i,j,n;
j=1;
clrscr();
printf("Enter the n value:");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
j=j*i;
}
printf("\n Factorial of %d is :%d",n,j);
getch();
}

Choices Program!!

This Program mainly illustrates the use of 'Switch" control structure. When there are varied number of options and when we have to select 1 option , 'switch' is used.

We first get the values of 2 numbers and enter our choice to add,subtract,multiply or divde the numbers.

Important points to remember while using 'switch':
  • The value that is being switched must be an integer [x in this program].
  • There must be a space between the 'case' ant the no. [case 1:]
  • Every caes must end up with the 'break'.
  • 'default' is mandatory. 'break' is not necessary for default.
 PROGRAM:
#include<stdio.h>
#include<conio.h>
void main()
 {
  int a,b,x;
  float c;
  clrscr();
  printf("Enter A:");
  scanf("%d",&a);
  printf("Enter B:");
  scanf("%d",&b);
  printf("\n");
  printf("1 - Add\n2 - Subtract\n3 - Multiply\n4 - Divide\n");
  printf("Enter your choice:");
  scanf("%d",& x);
  switch(x)
   {
    case 1: c=a+b;
        printf("The Sum is:%f",c);
        break;
    case 2: c=a-b;
        printf("The difference is:%f", c);
        break;
    case 3: c=a*b;
        printf("The Product is:%f", c);
        break;
    case 4: c=a/b;
        printf("The Quotient is:%f",c);
        break;
    default: printf ("Invalid option");
   }
  getch();
 }
 


Sunday, February 20, 2011

Leap Year Program!!

This program determines whether the given year is a leap year or not.This problem also illustrates the use of the control statemen if-else!!

LOGIC:
If the year is divisible by 4 it is a leap year.
If the year is divisible by 100 it is not a leap year.
If the year is divisible by 400 it is a leap year.

PROGRAM:
#include<stdio.h>
#include<conio.h>
void main()
{
 int year;
 clrscr();
 printf("Enter the year:");
 scanf("%d",&year);
 if(year%100==0)
 {
   if(year%400==0)
  {
   printf("%d Is a leap year",year);
   }
 else
  {
   printf("%d Is a non-leap year",year);
   }
   }
  else
  {
   if(year%4==0)
  {
   printf("%d: It is a leap year",year);
   }
   else
  {
   printf("%d: It is not a leap year",year);
   }
  }
 printf("\n~!Anyway have a nice year!~");
 getch();
}

Thank You!!

I take this oppurtunity to thank my dearest and beloved C & CPP guruji Bala Venkatraman!! I was bascically a Biology Student turned completely into computer science now!! I am now able to write some programs. Thank God!

I have created this blog mainly to put up a few simple programs for each and every common mans use. I like the users to clarify and rectify me whenever necessary!! If u have some intresting programs to share, u r welcome to do so.

"Experience the joy of programming , with undrestanding" as Bala sir usually says.