#include<iostream.h>
#include<conio.h>
void main()
{
int j,i,k,l;
char c;
clrscr();
do
{
cout<<"Enter the number of stars in the base..";
cin>>k;
for (i=1;i<=k;i++)
{
for (j=1;j<=i;j++)
cout<<"* ";
cout<<endl;
}
cout<<"\nWant to try more..(y/n)\n";
cin>>c;
}
while(c=='y');
getch();
}
C++ PROGRAMS
A blog containing C++ programs based on Classes, Objects and related concepts like Inheritance, Polymorphism etc.
100 Contemporary Living Room Designs
Monday, December 7, 2020
C++ program to print an lower triangle using stars.
Monday, November 9, 2020
Program to check if the given Value is of type Int in CPP
#include <iostream>
using namespace std;
bool isNumber(string s)
{
for (int i = 0; i < s.length(); i++)
if (isdigit(s[i]) == false)
return false;
return true;
}
int main() {
// Saving the input in a string
string str = "";
cout << "Enter Value.\n";
cin >> str;
// Function returns 1 if all elements
// are in range '0-9'
if (isNumber(str))
cout << "Entered Value is Integer";
// Function returns 0 if the input is
// not an integer
else
cout << "Entered Value is String";
return 0;
}
Wednesday, December 19, 2018
C++ PROGRAM TO DISPLAY ODD NUMBERS FROM 1 TO N
#include <iostream>
using namespace std;
int main()
{
int n;
cout<<"Enter the Limit\n";
cin>>n;
cout<<"Odd numbers between 1 and "<<n<<" are\n";
for(int i=1;i<=n;i++)
{
if(i%2 != 0)
cout<<i<<"\n";
}
return 0;
}
Friday, March 29, 2013
C++ File Handling Program
Write a program that shows the use of read() and write() to handle file I/O involving objects.
#include<iostream.h>
#include<fstream.h>
#include<iomanip.h>
class INVENTORY
{
char name[10];
int code;
float cost;
public:
void readdata(void);
void writedata(void);
};
void INVENTORY :: readdata(void)
{
cout<<"\n Enter name:";cin>>name;
cout<<"\n Enter code:";cin>>code;
cout<<"\n Enter cost:";cin>>cost;
}
void INVENTORY :: writedata(void)
{
cout<<setiosflags(ios::left)
<<setw(10)<<name
<<setiosflags(ios::right)
<<setw(10<<code)
<<setprecision(2)
<<setw(10)<<cost<<endl;
}
int main()
{
INVENTORY item[3];
fstream file;
file.open("Stock.dat",ios::in|ios::out);
cout<<"\n Enter details for three items \n";
for(int i=0;i<3;i++)
{
item[i].readdata();
file.write((char *) & item[i],sizeof(item[i]));
}
file.seekg(0);
cout<<"\nOutput\n";
for(i=0;i<3;i++)
{
file.readdata((char *) &item[i],sizeof(item[i]));
item[i].writedata();
}
file.close();
return 0;
}
#include<fstream.h>
#include<iomanip.h>
class INVENTORY
{
char name[10];
int code;
float cost;
public:
void readdata(void);
void writedata(void);
};
void INVENTORY :: readdata(void)
{
cout<<"\n Enter name:";cin>>name;
cout<<"\n Enter code:";cin>>code;
cout<<"\n Enter cost:";cin>>cost;
}
void INVENTORY :: writedata(void)
{
cout<<setiosflags(ios::left)
<<setw(10)<<name
<<setiosflags(ios::right)
<<setw(10<<code)
<<setprecision(2)
<<setw(10)<<cost<<endl;
}
int main()
{
INVENTORY item[3];
fstream file;
file.open("Stock.dat",ios::in|ios::out);
cout<<"\n Enter details for three items \n";
for(int i=0;i<3;i++)
{
item[i].readdata();
file.write((char *) & item[i],sizeof(item[i]));
}
file.seekg(0);
cout<<"\nOutput\n";
for(i=0;i<3;i++)
{
file.readdata((char *) &item[i],sizeof(item[i]));
item[i].writedata();
}
file.close();
return 0;
}
C++ PROGRAM TO DO ELECTRICITY BILL CALCULATION.
C++ PROGRAM TO READ AND DISPLAY STUDENT DETAILS USING INHERITANCE.
C++ program to overload the unary minus operator using friend function.
Write a program to overload the unary minus operator using friend function.
#include<iostream>
using
namespace std;
class
space{
int x;
int y;
int z;
public:
void getdata(int a,int b,int c);
void display(void);
friend void operator-(space &s);
};
void
space :: getdata(int a,int b,int c){
x=a;
y=b;
z=c;
}
void
space :: display(void){
cout<<x<<" ";
cout<<y<<" ";
cout<<z<<"\n";
}
void
operator-( space &s){
s.x=-s.x;
s.y=-s.y;
s.z=-s.z;
}
int
main(){
space s;
s.getdata(10,-20,30);
cout<<"S : ";
s.display();
-s;
cout<<"S :";
s.display();
return 0;
}
You might also like:
C++ PROGRAM TO DO ELECTRICITY BILL CALCULATION.
C++ PROGRAM TO READ AND DISPLAY STUDENT DETAILS USING INHERITANCE.
OUTPUT
Template Function
Write a template function that swaps the values of two arguments passed to it. In main( ), use the function with integers and characters.
#include<iostream.h>
using namespace std;
template <class T>
void swap(T &a,T &b)
{
T temp=a;
a=b;
b=temp;
}
void func(int x,int y,char w,char z)
{
cout<<"x and y before swap: "<<x<<y<<"\n";
swap(x,y)
cout<<"x and y after swap: "<<x<<y<<"\n";
cout<<"w and z before swap: "<<w<<z<<"\n";
swap(w,z)
cout<<"w and z after swap: "<<w<<z<<"\n";
}
int main()
{
fun(10,20,’s’,’S’);
return 0;
}
You might also like:-
#include<iostream.h>
using namespace std;
template <class T>
void swap(T &a,T &b)
{
T temp=a;
a=b;
b=temp;
}
void func(int x,int y,char w,char z)
{
cout<<"x and y before swap: "<<x<<y<<"\n";
swap(x,y)
cout<<"x and y after swap: "<<x<<y<<"\n";
cout<<"w and z before swap: "<<w<<z<<"\n";
swap(w,z)
cout<<"w and z after swap: "<<w<<z<<"\n";
}
int main()
{
fun(10,20,’s’,’S’);
return 0;
}
You might also like:-
- PROGRAM TO PRINT A DIAMOND IN A BOX - 2
- LARGEST IN AN ARRAY
- Operator Overloading-Matrix
- C++ PROGRAM TO DO ELECTRICITY BILL CALCULATION
Saturday, December 8, 2012
C++ PROGRAM - FACTORIAL USING RECURSION.
Write a C++ program which calculates the factorial of a given number using recursion.
#include<conio.h>
#include<iostream.h>
long factorial (long a)
{
if (a > 1)
return (a * factorial (a-1));
else
return (1);
}
int main ()
{
long ln;
clrscr();
cout << "Enter a number: ";
cin >> ln;
cout << ln << "!" << " = " << factorial (ln);
getch();
return 0;
}
OUTPUT
You might also like:-
#include<conio.h>
#include<iostream.h>
long factorial (long a)
{
if (a > 1)
return (a * factorial (a-1));
else
return (1);
}
int main ()
{
long ln;
clrscr();
cout << "Enter a number: ";
cin >> ln;
cout << ln << "!" << " = " << factorial (ln);
getch();
return 0;
}
OUTPUT
You might also like:-
Subscribe to:
Posts (Atom)