Showing posts with label c++. Show all posts
Showing posts with label c++. Show all posts

Monday, 20 October 2014

Program Of Get Manipulator In C++

#include<iostream.h>
#include<conio.h>
 main()
 {
 clrscr();
 const int max=40;
 char str[max];
 cout<<"\n enter string=";
 cout<<"string is="<<str;
 getch();
 }

Sunday, 6 May 2012

Program Of Error Handling In C++

#include <iostream.h>
#include<conio.h>
main()

{
    clrscr();

     char FirstName[30], LastName[30];

    int Age;

    char FileName[20];

    cout << "Enter the name of the file you want to open: ";

    cin >> FileName;


    if(Students.bad())    // error handling

    {

    cout<<"\n Error in File";

    }

    Students >> FirstName >> LastName >> Age;

    cout << "\nFirst Name: " << FirstName;

    cout << "\nLast Name:  " << LastName;

    cout << "\nEnter Age:  " << Age;

    cout << "\n\n";


getch();

}


Wednesday, 2 May 2012

Program Of IF Else Statement With Class In C++

#include<iostream.h>
#include<conio.h>
{
private:
int a;
public:
void getdata()
{
cout<<"Enter Marks=";
cin>>a;
}
void show()
{
if(a>=40)
{
if(a>=60)
{
if(a>=80)
{
}
else
{
  cout<<"\n First";
}
}
else
{
if(a>=50)
{
cout<<"\n Good Second";
}
else
{
  cout<<"\n Second";
}
}
}
else
{
{
cout<<"\n Poor";
}
else
{
  cout<<"\n V.poor";
}
}

}


};
main()
{
test t1;
clrscr();
t1.show();
getch();
}

Tuesday, 1 May 2012

Program Of Call By Value Using C

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


main()

{

int x,y,z;

clrscr();

printf("Enter First NUmber=");

scanf("%d",&x);

printf("Enter Second NUmber=");

scanf("%d",&y);

z=sum(x,y);

printf("\n Answer=%d",z);

getch();

}


{

int c;

c=a+b;

return c;
}

Program Of Call By Address Using c

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

main()

{

int x,y,z;

clrscr();

printf("ENter First NUmber=");

scanf("%d",&x);

printf("ENter Second NUmber=");

scanf("%d",&y);

z=sum(&x,&y);

printf("\n Answer=%d",z);

getch();

}

{
int c;

c= *a + *b;

return c;

}

Program Of Call By Refrence in c

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


main()

{

int x,y,z;

clrscr();

printf("ENter First NUmber=");

scanf("%d",&x);

printf("ENter Second NUmber=");

scanf("%d",&y);

z=sum(x,y);

printf("\n Answer=%d",z);

getch();

}


{

int c;

c= a + b;

return c;
}

Monday, 30 April 2012

Program To Print Salary Using C

#include<stdio.h>
#include<conio.h>
main()
{
clrscr();
printf("enter salary");
scanf("%d",&salary);
if(salary>2000)
{
if(salary>5000)
{
if(salary>10000)
{
printf("A");
}
else
{
printf("B");
}
}
else
{
{
printf("C");
}
else
{
printf("D");
}
}
}
else
{
if(salary>1000)
{
if(salary>1500)
{
printf("E");
}
else
{
printf("F");
}
}
else
{
if(salary>500)
{
printf("G");
}
else
{
printf("H");
}
}
}
getch();
}

Sunday, 29 April 2012

Program Of Recursion In C++


#include<iostream.h>
#include<conio.h>
int fact(int a);
main()
{
clrscr();
int x,n,y;
cin>>x;
y=fact(x);
cout<<"\n Answer="<<y;
getch();
}
{
int f=1;
if(a==1)
{
return 1;
}
else
{
f=a*fact(a-1);
return f;
}
}
------------------------------------------------
Define Recursion

Simply put, recursion is when a function calls itself. That is, in the course of the function definitiSimply put, recursion is when a function calls itself. That is, in the course of the function definition there is a call to that very same function. At first this may seem like a never ending loop, or like a dog chasing its tail. It can never catch it. So too it seems our function will never finish. This might be true is some cases, but in practice we can check to see if a certain condition is true and in that case exit (return from) our function. The case in which we end our recursion is called a base case . Additionally, just as in a loop, we must change some value and incremently advance closer to our base case.
Consider this function. 


void myFunction( int counter)
{
if(counter == 0)
     return;
else
       {
       cout <<counter<<endl;
       myFunction(--counter);
       return;
       }
}

This recursion is not infinite, assuming the function is passed a positive integer value. What will the output be? 

Consider this function:
void myFunction( int counter)

{
if(counter == 0)
     return;
else
       {
       cout<<"hello"<<counter<<endl;
       myFunction(--counter);
       cout<<counter<<endl;
       return;
       }
If the function is called with the value 4, what will the output be? Explain. 
The above recursion is essentially a loop like a for loop or a while loop. When do we prefer recursion to an iterative loop? We use recursion when we can see that our problem can be reduced to a simpler problem that can be solved after further reduction.

Every recursion should have the following characteristics.
  1. --->A simple base case which we have a solution for and a return value. Sometimes there are more than one base cases.
  2. ----->A way of getting our problem closer to the base case. I.e. a way to chop out part of the problem to get a somewhat simpler problem.
  3. --->A recursive call which passes the simpler problem back into the function.

  4. view source:- http://danzig.jct.ac.il/cpp/recursion.html

Saturday, 28 April 2012

Program Of Bitwise Operator In C++

#include<iostream.h>
#include<conio.h>
main()
{
clrscr();
int a,b,c,d,e;
a=25;
b=15;
                                              c=a&b;        // &-->And Operator
                                                d=a|b;           // |--> Or Operator
                                               e=~a;           // ~a Negation of a
cout<<" a&b="<< c;
cout<<"\n  a|b="<< d;
cout<<"\n  ~a="<< e;
getch();
}
------------------------------------------------  

Define Bitwise Operator 

Program Of Arithmetic Operators In C++

#include<iostream.h>
#include<conio.h>
main()
{
clrscr();
int a;
                                a=20;      //Assigment
 a+=10; // Assignment Operator    // a= a+10;
 cout<<"ASsigment For Addition="<< a<<endl;

  a-=10; // Assignment Operator    // a= a+10;
   cout<<"ASsigment For Subtraction="<< a<<endl;

   a*=10; // Assignment Operator    // a= a+10;
    cout<<"ASsigment For Multiplication="<< a<<endl;

    a/=10; // Assignment Operator    // a= a+10;
     cout<<"ASsigment For Division="<< a<<endl;


getch();
}
-----------------------------------------

Here are the most common arithmetic operators


*, / and % will be performed before + or - in any expression. Brackets can be used to force a different order of evaluation to this. Where division is performed between two integers, the result will be an integer, with remainder discarded. Modulo reduction is only meaningful between integers. If a program is ever required to divide a number by zero, this will cause an error, usually causing the program to crash.

Friday, 27 April 2012

Program Of Virtual Function In C++

 #include< iostream.h>
#include< conio.h>
class abc
{
public:
void display()
{
cout<< "\nInside base class.";
}
};
class xyz:public abc
{
public:
void display()
{
cout<< "\nInside 1st derived class.";
}
};
class mn:public abc
{
public:
void display()
{
cout<< "\nInside 2nd derived class.";
}
};
{
clrscr();
xyz ob;
mn ob1;
abc *ptr;
ptr=&ob;
cout<< "\nFor object of 1st derived class."<< endl;
ptr->display();
ptr=&ob1;
cout<< "\nFor object of 2nd derived class."<< endl;
ptr->display();
getch();
}

Find Length Of String Program IN C


#include<stdio.h>
#include<conio.h>
#include<string.h>
main()
{
char a[20];
int l;
printf("Enter String =");
scanf("%s",a);
l=strlen(a);
printf("length of String=%d",l);
getch();
}

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

Program Of File Structure In C++


#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<string.h>
struct emp
{
char name[40];
int age;
float bs;
};
main()
{
clrscr();
FILE *fp;
struct emp e;
char s[80];
char another='y';
fp=fopen("vEmployee.txt","w");
if(fp==NULL)
{
printf("Can't Open File");
exit(1);
}
printf("\n File  TEXT Is=");
printf("\n");
while(another=='y')
{
printf("\n Enter Name,AGE,Basic salary");
scanf("%s",e.name);
scanf("%d",&e.age);
scanf("%f",&e.bs);
fprintf(fp,"%s%d%f",e.name,e.age,e.bs);

printf("Add Another Record(y/n)");
fflush(stdin);
another=getche();
//scanf("%c",&another);

}
fclose(fp);a
getch();
}

Saturday, 11 February 2012

Program Of Macro In C


#include<stdio.h>
#include<conio.h>
main()
{
int a,b,c;
clrscr();
printf("Enter Numbers=");
scanf("%d%d",&a,&b);
c=sum(a,b);
printf(" Answer= %d",c);
getch();
}

Program Of FTELL In C


#include <stdio.h>
#include<conio.h>
main ()
{
  long size;
  pFile = fopen ("z123Exam.txt","w");
  fseek (pFile, 100, SEEK_END);
  size=ftell (pFile);
   fclose (pFile);
   printf ("Size of myfile.txt: %ld bytes.\n",size);

  getch();
}