0% found this document useful (0 votes)
350 views

#Include Void Main

The document contains 23 code snippets showing C++ programs that demonstrate various programming concepts like input/output using cin and cout, data types, operators, control structures like if-else, switch case, loops, functions, arrays, structures etc. The programs range from basic programs to read and display data to more complex programs to check palindrome, calculate sum of digits, find leap years etc.

Uploaded by

Deepak Gour
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
350 views

#Include Void Main

The document contains 23 code snippets showing C++ programs that demonstrate various programming concepts like input/output using cin and cout, data types, operators, control structures like if-else, switch case, loops, functions, arrays, structures etc. The programs range from basic programs to read and display data to more complex programs to check palindrome, calculate sum of digits, find leap years etc.

Uploaded by

Deepak Gour
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 29

1.

Write a program to accept string through the keyboard and display id


on the screen. Use cin and cout statement.

#include<iostream.h>
#include<conio.h>
Void main ()
{
char name[15];
clarscr();
cout<<”Enter your name:”;
cin>>name;
cout<<”Your name is”<<name;
return 0;
}

2. Write a program to display data using cout statements.


#include<iostream.h>
#include<conio.h>
Void main ()
{
clrscr();
cout<<”============================”;
cout<<endl;
cout<<”Hello”;
cout<<”\n”;
cout<<123;
cout<<endl<<3.134;
cout<<endl<<”NUMBER:”<<”\t”<<435;
cout<<”\n=========== The End===============”;
}

error
3. Write a program to display interger, float, char and string using cout
statement.
#include<iostream.h>
#include<conio.h>
Void main ()
{
int x=5;
float y=5.3;
char z=’z’;
char city[]=”Nanded”;
Clrscr();

cout<<”x=”<<x<<”y”<<y<<”z”<<z<<endl;
cout<<”city=”<<city;

4. Write a program to use different formats of typecasting and display the


converted values.
#include<iostream.h>
#include<conio.h>
Void main ()
{
int a=66;
float f=2.5;
double d=85.22;
char c=’k’;
clrscr();
cout<<”\n int in char format :”<<(char)a;
cout<<”\n float in int format :”<<(int)f;
cout<<”\n double in char format :”<<(char)d;
cout<<”\n char in int format”<<(int)c;
}
5. Write a program to display A to Z alphabets using ASCII values.
#include<iostream.h>
#include<conio.h>
Void main ()
{
int j;
Clrscr();

for(j=65;j<91;j++)
{
cout<<(char)j<<” “;
cout<<endl;
for(j=65;j<91;j++)
{
printf(“%c”,j);
return 0;
}
}
}
6. Write a program to display addresses of variables in hexadecimal and
unsigned integer format.
#include<iostream.h>
#include<conio.h>
Void main ()
{
int x=77;
float y=5.1243;
int z=78;
Clrscr();

cout<<”Address of x=”<<&x<<endl;
cout<<”Address of y=”<<&y<<endl;
cout<<”Address of z=”<<(unsigned)&z<<endl;
}
7. Write a program to display the character on the screen using put()
function.
#include<iostream.h>
#include<conio.h>
Void main ()
{
Clrscr();
cout.put(‘C’);
cout.put(‘+’);
cout.put(‘+’);
}
8. Write a program to read character using get() and display it using
put().
#include<iostream.h>
#include<conio.h>
Void main ()
{
char ch;
Clrscr();
cout<<”\n Enter a character:”;
cin.get(ch);
cout<<”\n Entered character was:”
cout.put(ch);
}

9. Write a program to read and display the string. Use get() and put()
function .
#include<iostream.h>
#include<conio.h>
Void main ()
{
Char j=0;
Char x[20];
Clrscr();

Cout<<”\n Enter a string :”;


While(x[j++]!=’\n’)
Cin.get(x[j]);
J=0;
Cout<<”\n Enterd string :”;
Cout.put(x[j]);
Return 0;
}

10. Write a program in c++ to demonstrate dynamic initialization.


#include<iostream.h>
#include<conio.h>
Void main ()
{
Clrscr();
Cout<<”\n Enter radius”;
Int r;
Cin>>r;
Float area=3.14*r*r;
Cout<<”\n Area=”<<area;
}

11. Write a program to declare, initialize an array. Display the elements of


an array with their addresses.
#include<iostream.h>
#include<conio.h>
Void main ()
{
Int b[4]={2,4,3,7};
Clrscr();

Cout<<”b[0] value=”<<b[0]<<”Address:”<<(unsigned)&b[0]<<endl;
Cout<<”b[1] value=”<<b[1]<<”Address:”<<(unsigned)&b[1]<<endl;
Cout<<”b[2] value=”<<b[2]<<”Address:”<<(unsigned)&b[2]<<endl;
Cout<<”b[3] value=”<<b[3]<<”Address:”<<(unsigned)&b[3]<<endl;

}
12. Write a program to declare struct object ,initialize it and display the
contents.
#include<iostream.h>
#include<conio.h>
Void main ()
{
Clrscr();
Struct my_friend
{
Char *fname;
Int phone,age,height;
}A;
A.fname=”Bharat”;
A.phone=2765;
A.age=22;
A.height=4.5;
Cout<<”\n Contents of object A”;
Cout<<”\n Name :”<<A.fname;
Cout<<”\n Phone :”<<A.phone;
Cout<<”\n Age :”<<A.age;
Cout<<”\n Height :”<<A.height;

13. Write a program to allocate memory using new operator.


#include<iostream.h>
#include<conio.h>
Void main ()
{
Int *p=new int[3],k;
Clrscr();

For(k=0;k<3;k++)
{
Cout<<”\nEnter a number :”;
Cin>>*p;
p++;

}
p-=3;
cout<<”\n Entered numbers with their addresses are:\n”;
for(k=0;k<3;k++)
{
Cout<<”\n\t”<<*p<<”\t”<<(unsigned)p;
p++;
}
p-=3;
delete p;
}
14. Write a program to use comma operator in if..else structure as scope
indicator.
#include<iostream.h>
#include<conio.h>
Void main ()
{
Int x;
Clrscr();

x=10;
if(x==10)
cout<<x<<endl;
cout<<x+1<<endl;
cout<<x+2<<endl;
cout<<”end of if block”;
else
cout<<”False”;
cout<<”\n End”;
}
15. Write a program to enter age and display message whether the user is
eligible for voting or not. Using if…else statement.
#include<iostream.h>
#include<conio.h>
#include<constream.h>
#include<string.h>
Void main ()
{
Int age;
Clrscr();

Cout<<”Enter Your Age”;


Cin>>age;
If(age>=18)
{
Cout<<”You are eligible for voting”
}
Else
{
Cout<<”Yor are noneligible for voting”<<endl;
Cout<<”Wait for”<<18-age<<”Year(s).”;
}

16. Write a program to enter two characters and display the larger
character. Use nested if—else statement.
#include<iostream.h>
#include<conio.h>
Void main ()
{
Char j,k;
Clrscr();

Cout<<”\n Enter a character”;


j=getche();
cout<<”\n Enter another character:”;
k=getche();
cout<<endl;
if(j>k)
{
Cout<<j<<”is larger than”<<k;

}
Else if(k>j)
{
Cout<<k<<”is larger than”<<j;
}
Else
{
Cout<<”Both the characters are same”;
}
17. Write a program to execute if statement without any expression.
#include<iostream.h>
#include<conio.h>
Void main ()
{
Int score;
Clrscr();

Cout<<”\n Enter Sachin’s score:”;


Cin>>score;
If(score>=50)
{
If(score>=100)
Cout<<”Sachin scored a century and more runs”;
Else
{
Cout<<”\nSachin scored more than half century”;
Cout<<”\n Cross your fingers and pray he completes
century”;
}

}
Else
{
If(score==0)
Cout<<”Oh my God”;
If(score>0)
Cout<<”Not in form today”;
}
Return 0;
}
18. Write a program to demonstrate the use of goto statement.
#include<iostream.h>
#include<conio.h>
int main ()
{
Clrscr();
Int x;
Cout<<”Enter a Number :”;
cin>>x;
If(x%2==0)
goto even;
else
goto odd;
even:cout<<x<<”is Even Number”;
return;
odd:cout<<x<<”is odd Number”;
}
19. Write a program to enter a month number of year 2002 and display the
number of days present in that month.
#include<iostream.h>
#include<conio.h>
int main ()
{
Int month,days;
Clrscr();

Cout<<”Enter a month of year 2002”;


Cin>>month;
Switch(month)
{
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
days=31;
break;
case 2:
case 1:
days=28;
break;
case 4:
case 6:
case 9:
case 11:
days=30;
break;
}
Cout<<”\n Number of days in this month are”<<days;
}
20. Write a program to display all leap years from 1900 to 2002.
#include<iostream.h>
#include<conio.h>
int main ()
{
int i=1900;
Clrscr();

cout<<”\n Program to print all the leap years from 1900 to


2002\n\n”;
for(;i++<=2002;)
{
If(i%4==0&&i%100!=0)
Cout<<i<<” ”;
Else if(i%100==0&&i%400==0)
Cout<<i<<” “;
}
}
21. Write a program to add 10 consecutive numbers starting from 1. use
the while loop.
#include<iostream.h>
#include<conio.h>
int main ()
{
int a=1,sum=0;
clrscr();
while(a<=10)
{
Cout<<” ”<<a;
Sum=sum+1;
a++
}
Cout<<”\n sum of 10 numbers ”<<sum;
}
22. Write a program to calculate the sum of individual digits of an entered
number.
#include<iostream.h>
#include<conio.h>
int main ()
{
int num,t;
clrscr();
cout<<”\n enter a number”;
cin>>num;
t=num;
int sum=0;
while(num)
{
Sum=sum+num%10;
Num=num/10;
}
Cout<<”\n sum of the individual digits of the
number”<<t<<”is=”<<sum;
}
23. Write a program to check whether the entered number is palindrome or
not
#include<iostream.h>
#include<conio.h>
int main ()
{
int num;
clrscr();
cout<<”\n Enter the number”;
cin>>num;
int b=0,a=num;
while(a)
{
b=b*10+a%10;
a=a/10;
}
If(num==b)
Cout<<”\n The given number is palindrome”;
Else
Cout<<”\nThe given number is not palindrome”;
}
24. Write a program using do-while loop to print numbers and their cubes
up to 10
#include<iostream.h>
#include<conio.h>
int main ()
{
int y,x=1;
clrscr();
cout<<”\n\tNumber and their cubes\n”;
do
{
Y=pow(x,3)
Cout<<”\t”<<x<<”\t\t”<<y<<”\n”;
x++;
}while(x<=10);
}
25. Write a program to declare prototype of function sum(), define the
function sum() exactly similar to its prototype.
#include<iostream.h>
#include<conio.h>
int main ()
{
Clrscr();
Float sum(int,float);
Int a=20;
Float s,b=2.5;
s=sum(a,b);
cout<<”Sum=”<<s;
return 0;
}
Float sum(int x,float y)
{
Return(x+y);
}

26. Write a program to demonstrate call by value.


#include<iostream.h>
#include<conio.h>
int main ()
{
Clrscr();
Int x,y;
Void change(int,int);
Cout<<”\n Enter values of x &y:”;
Cin>>x>>y;
Change(x,y);
Cout<<”\n\n In function main()”;
Cout<<”\n Values X=”<<x<<”and Y=”<<y;
Cout<<”\n Address X=”<<(unsigned)&x<<”and
Y=”<<(unsigned)&y;
}
Void change(int a,int b)
{
Int k;
K=a;a=b;b=k;
Cout<<”\n In Function change()”;
Cout<<”\n Values X=”<<a<<”and Y=”<<b;
Cout<<”\n Address X=”<<(unsigned)&a<<”and
Y=”<<(unsigned)&b;
}

27. Write a program to demonstrate pass by address.


#include<iostream.h>
#include<conio.h>
int main ()
{
Clrscr();
Int x,y;
Void change(int *,int*)
{
Cout<<”\n Enter Values of x&y”;
Cin>>x>>y;
Change(&x,&y);
Cout<<”\nIn main()”;
Cout<<”\n Values X=”<<x<<”and Y=”<<y;
Cout<<”\nAddresses X=”<<(unsigned)&x<<”and
Y=”(unsigned)&y;

}
Void change(int *a,int *b)
{
Int *k;
*k=*a;
*a=*b;
*b=*k;
Cout<<”\n in change ()”;
Cout<<”\n Values X=”<<*a<<”And Y=”<<*b;
Cout<<”\n Address X=”<<(unsigned)a<<”And
Y=”<<(unsigned)b;
}

28. Write a program to declare all members of a class as public. Access


the elements using objects.
#include<iostream.h>
#include<conio.h>
Class item
{
Public:
Int codeno;
Float prize;
Int qty;
};
int main ()
{
Clrscr();
Item one;
one.codeno=123;
one.prize=123.33;
one.qty=150;
cout<<”\n Codeno=”<<one.codeno;
cout<<”\n Prize=”<<one.prize;
cout<<”\nQuantity=”<<one.qty;
}

29. Write a program to access private members of a class using member


function.
#include<iostream.h>
#include<conio.h>
Struct item
{
Private:
Int codeno;
Float price;
Int qty;
Public:

Void show()
{
cdeno=124;
price=198;
qty=300;
cout<<”\n Codeno=”<<codeno;
cout<<”\nPrice=<<price;
cout<<”\nQuntity=”<<qty;
}
};
int main ()
{
Clrscr();
Item one;
one.show();
return 0;
}
30. Write a program to declare private member function and access it
using public member function.
#include<iostream.h>
#include<conio.h>
struct item
{
Private:
Int codeno;
Float price;
Int qty;
Void values()
{
Codeno=123;
Price=145;
Qty=200;
}
Public:
Void show()
{
Values();
Cout<<”\n Codeno=”<<codeno;
Cout<<”\n Price=”<<price;
Cout<<”\n Quantity=”<<qty;
}
};
int main ()
{
Clrscr();
Item one;
one..values();
one.show();
return 0;
}
31. Write a program to define member function of class outside the class.
#include<iostream.h>
#include<conio.h>
Class item
{
Private:
Int codeno;
Float price;
Int qty;
Public:
Void show(void);

};
Void item::show()
{
Codeno=101;
Price=3545;
Qty=133;
Cout<<”\n Codeno=”<<codeno;
Cout<<”\n Price=”<<price;
Cout<<”\nQuantity=”<<qty;
}
int main ()
{
Clrscr();
Item one;
one.show();
return 0;
}
32. Write a program to declare outside function inline.
#include<iostream.h>
#include<conio.h>
Class item
{
Private:
Int codeno;
Float price;
Int qty;
Public:
Void show(void);
};
Inline void item::show()
{
Codeno=213;
Price=2022;
Qty=150;
Cout<<”\ncodeno=”<<codeno;
Cout<<”\nPrice=”<<price;
Cout<<”\n Quantity=”<<price;

int main ()
{
Clrscr();
Item one;
one.show();
return 0;
}
33. Write a program to calculate simple interst. Hide the data elements of
the class using private keyword.
#include<iostream.h>
#include<conio.h>
Class interest
{
Private:
Float p_amount;
Float rate;
Float period;
Float interest;
Float t_amout;
Public:
Void in()
{
Cout<<”Principle Amount:”;
Cin>>p_amount;
Cout<<”\nRate of Interest:”;
Cin>>rate;
Cout<<”\nNumber of years:”;
Cin>>period;
Interest=(p_amout*period*rate)/100;
t_amout=interest+p_amout;
}
Void show()
{
Cout<<”\n Principle Amout:”<<p_amount;
Cout<<”\n Rate of Interest:”<<rate;
Cout<<”\nNumber of years:”<<period;
Cout<<”\nInterest”<<interest;
Cout<<”\n Total Amount:”<<t_amount;
}
};
int main ()
{
Clrscr();
Interest r;
r.in();
r.show();
return 0;
}
34. Write a program to declare static data member. Display the value of
static data member.
#include<iostream.h>
#include<conio.h>
Class number
{
Static int c;
Public:
Void count()
{
` ++c;
Cout<<”\nc=”<<c;
}
};
Int number::c=0;

int main ()
{
Clrscr();
Number a,b,c;
a.count();
b.count();
c.count();
return 0;
}
35. Write a program to access private data using non-member function.
Use friend function.
#include<iostream.h>
#include<conio.h>
Class ac
{
Private :
Char name[15];
Int acno
Float bal;
Public:
Void read()
{
Cout<<”\n Name :”;
Cin>>name;
Cout<<”\nA/C No. :”;
Cin>>acno;
Cout<<”\n Balance:”;
Cin>>bal;
}
Friend void showbal(ac);
};
Void showbal(ac a)
{
Cout<<”\n Balance of A/c no.”<<a.acno<<”is Rs.”<<a.bal;
}
int main ()
{
Clrscr();
Ac k;
k.read();
showbal(k);
return 0;
}

36. Write a program to show that for each object constructor is called
separately.
#include<iostream.h>
#include<conio.h>
Class num
{
Private:
Int a,b,c;
Public:
Int x;
Num(void);
Void show()
{
Cout<<”\n x=”<<x<<”a=”<<a<<”b=”<<b<<”c=”<<c;
}
};
Num::num(void)
{
Cout<<”\n Constructor called”;
X=5;a=0;b=1;c=2;
}
int main ()
{
Clrscr();
Num x;
x.show();
return 0;
}

37. Write a program to read values through the keyboard. Use constructor.
#include<iostream.h>
#include<conio.h>
Class num
{
Private:
Int a,b,c;
Public:
Num(void);
Void show()
{
Cout<<”\n”<<”a=”<<a<<”b=”<<b<<”c=”<<c;
}
};
Num::num(void)
{
Cout<<”\nConstructor called”;
Cout<<”\n Enter Values for a,b and c:”;
Cin>>a>>b>>c;
}

int main ()
{
Clrscr();
Class num x;
x.show();
return 0;
}
38. Write a program to create constructor with arguments and pass the
arguments to the constructor.
#include<iostream.h>
#include<conio.h>
Class num
{
Private:
Int a,b,c;
Public:
Num(int m,int j,int k);
Void show()
{
Cout<<”\n a=”<<a<<”b=”<<b<<”c=”<<c;
}
};
Num::num(int m,int j,int k)
{
a=m;
b=j;
c=k;
}
int main ()
{
Clrscr();
Num=x=num(4,3,3);
Num y(2,4,6);
x.show();
y.show();
return 0;
}
39. Write a program with multiple constructors for the single class.
#include<iostream.h>
#include<conio.h>
Class num
{
Private:
Int a;
Float b;
Char c;
Public:
Num(int m,float j,char k);
Num(int m,float j);
Num();
Void show()
{
Cout<<”\n\t a=”<<a<<”b=”<<”c=”<<c;

}
};
Num::num(int m,float j,char k)
{
Cout<<”\n Constructor with three arguments”;
a=m;
b=j;
c=k;
}
Num::num(int m,float j)
{
Cout<<”\n Constructor with tow arguments”;
a=m;
b=j;
c=’’;
num::num()
{
Cout<<”\n Constructor without arguments”;
a=b=c=NULL;
}
}
int main ()
{
Clrscr();
Class num x(4,4.5,’A’);
x.show();
class num y(1,2.3);
y.show();
class num z;
z.show();
return 0;
}
40. Write a program to use new and delete operators with constructor and
destructor. Allocate memory for given number of integers. Also
release the memory.
#include<iostream.h>
#include<conio.h>
Int number(void)
Class num
{
Int *x;
Int s;
Public:
Num()
{
s=number();
x=new int[s];
}
~num(){delete x;}
Void input();
Void sum();
};
Void num::input()
{
For(int h=0;h<s;h++)
{
Cout<<”Enter number[”<<h+1<<”]:”;
Cin>>x[h];
}
}
Void num::sum()
{
Int adi=0;
For(int h=0;h<s;h++)
Adi+=x[h];
Cout<<”sum of elements =”<<adi;
}
Number()
{
Clrscr();
Int n;
Cout<<”How many numbers:”;
Cin>>n;
Return n;
}
int main ()
{
Clrscr();
Num n1;
n1.input();
n1.sum();
}
41. Write a program to perform addition o two objects and store the result
in third object. Display contents of all the three objects.
#include<iostream.h>
#include<conio.h>
Class number
{
Public:
Int x;
Int y;
Number(){}
Number(int j,int k)
{
x=j;
y=k;

}
Void show()
{
Cout<<”\n x=”<<x<<”y=”<<y;
}
};

int main ()
{
Clrscr();
Number A(2,3),B(4,5),C;
A.show();
B.show();
C.x=A.x+B.x;
C.y=A.y+B.y;
c.show();
}
42. Write a program to derive a class publicly from base class. Declare the
base class with its member under public section.
#include<iostream.h>
#include<conio.h>
Class A
{
Public:
Int x;
};
Class B:public A
{
Public :
Int y;
};
int main ()
{
Clrscr();
B b;
b.x=20;
b.y=49;
cout<<”\n Member of A:”<<b.x;
cout<<”\n Member of B:”<<b.y;
}
43. Write a program to show single inheritance between two classes.
#include<iostream.h>
#include<conio.h>
Class ABC
{
Protected:
Char name[15];
Int age;

};
Class abc:public ABC
{
Float height;
Float weight;
Public:
Void getdata()
{
Cout<<”\n Enter Name and Age ”;
Cin>>name>>age;
Cout<<”\n Enter Height and Weight:”;
Cin>>height>>weight;
}
Void show()
{
Cout<<”\n Name:”<<name<<”\n Age”<<age<<”Years”;
Cout<<”\n Height:”<<height<<”Feets”<<”\n Weight”<<weight<<”Kg.”;

}
};

Void main ()
{
abc x;
x.getdata();
x.show();
}
44. Write a program to create multilevel inheritance. Create classes A1,A2
and A3.
#include<iostream.h>
#include<conio.h>
Class A1
{
Protected:
Char name[15];
Int age;

};
Class A2:public A1
{
Protected :
Float height;
Float weight;

};
Class A3:public A2
{
Protected:
Char sex;
Public:
Void get()
{
Cout<<”Name :”;cin>>name;
Cout<<”Age :”;cin>>age;
Cout<<”Sex :”;cin>>sex;
Cout<<”Height :”;cin>>height;
Cout<<”weight :”;cin>>weight;

}
Void show()
{
Cout<<”\nName:”<<name;
Cout<<”\nAge”<<age<<”years”;
Cout<<”\nSex”<<sex;
Cout<<”\nHeight”<<height<<”Feets”;
Cout<<”\nWeight”<<weight<<”Kg.”;

}
};
Void main ()
{
Clrscr();
A3 x;
x.get();
x.show();
}
45. Write a program to invoke function using scope access operator.
#include<iostream.h>
#include<conio.h>
Class first
{
Int b;
Public:
Fisrt(){b=10;}
Void display(){cout<<”\n b=”<<b;}

};
Class second:public first
{
Int d;
Public:
Second(){d=20;}
Void display(){cout<<”\n d=”<<d;}
};
Int main()
{
Clrscr();
Second s;
s.fist::display();
s.display();
return 0;
}
46. Write a program to invoke member functions of base and derived
classes using pointer of base class.
#include<iostream.h>
#include<conio.h>
Class first
{
Public :
First(){b=10;}
Void display(){cout<<”\n b=”<<b;
};
Class second:public first
{
Int d;
Public:
First(){b=10;}
Void display(){cout<<”\nb=”<<b;}

};
Class second:public first
{
Int d;
Public:
Second(){d=20;}
Void display(){cout<<”\n d=”<<d;}
};

Void main ()
{
Clrscr();
First f,*p;
Second s;
p=&f;
p->display();
p=&s;
p->display();
return 0;
}
47. Write a program to call virtual function through constructor.
#include<iostream.h>
#include<conio.h>
Class B
{
Int k;
Public:
B(int 1){k=1;}
Virtual void show(){count<<endl<<”k=”<<k;}

};
Class D:public B
{
Int h;
Public:
D(int m,int n):B(m)
{
h=n;
B *b;
b=this;
b->show();
}
Void show()
{
Cout<<endl<<”h=”<<h;
}
};
Void main ()
{
Clrscr();
B b(4);
D d(4,5);
}
48. Write a program to open an output file using fstream class.
#include<iostream.h>
#include<fstream.h>
#include<conio.h>

int main ()
{
Clrscr();
Ofstream out(“text”);
Out<<”One two three four\n”;
Out<<”1 2 3 4”;
Out<<”\n**The End**”;
Return 0;
}
49. Write a program to check whether the file in successfully opened or
not.
#include<iostream.h>
#include<conio.h>
#include<fstream.h>
Void main ()
{

Clrscr();
Ifstream in(“text”);
If(!in)cerr<<”\n File is not opened”<<in;
Else cerr<<”\n file is opened ”<<in;
}
50. Write a program to detect end of file using function eof().Desplay the
values returned by the eof() function.
#include<iostream.h>
#include<conio.h>
#include<fstream.h>
Void main ()
{
Clrscr();
Char c;
Ifstream in(“text”);
While(in.eof()==0)
{
In.get(c);
Cout<<in.eof();
}
Return 0;
}
51. Write a program to open a file in binary mode. Write and read the data
#include<iostream.h>
#include<conio.h>
#include<fstream.h>
Void main ()
{
Ofstream out;
Char data[32];
Out.open(“text”,ios::out|ios::binary);
Cout<<”\n Enter text”<<endl;
Cin.getline(data,32);
Out<<data;
Out.close();
Ifstream in;
In.open(“text”,ios::in|ios::binary);
Cout<<endl<<”Contents of the file\n”;
Char ch;
While(in.eof()==0)
{
Ch=in.get();
Cout<<ch;
}
Return 0;
}
52. Write a program to append a file.
#include<iostream.h>
#include<conio.h>
#include<fstream.h>
int main ()
{
Ofstream out;
Char data[25];
Out.open(“text”,ios::out);
Cout<<”\n Enter text”<<endl;
Cin.getline(data,25);
Out<<data;
Out.close();
Out.open(“text”,ios::app);
Cout<<”\n Again Enter text”;<<endl;
Cin.getline(data,25);
Out<<data;
Out.close();
Ifstream in;
In.open(“text”,ios::in);
Cout<<endl<<”Contents of the file \n”;
While(in.eof()==0)
{
In>>data;
Cout<<data;
}
Return 0;
}

You might also like