c++ basic programs examples

c++ basic programs examples

c basic programs examples

c++ basic programs examples is collection of cpp program to learn the basic concept of cpp programming.

//Written by Adarsh Patel

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

void main()
{
clrscr();

cout << “MCA DEPARTMENT”;
cout << “\b\b\b Bhavanagar “;
cout << “\n”;
cout << “\”Mca Department\”” << endl;
cout << “Bhavnagar \a”;
cout << “\n\’University\'”;
cout << “\rRam\n”;

getch();
}
/*

<< Insersion Operator
>> Extraction Operator

cout is a Predefined Object not function

\t        Horizontal Tab
\n        New Line
\”        Double Quote
\’        Single Quote
\b        Back Space
\r        Carriage Return at same line (Like Home Key)
\a        Beep

*/

//Written by Adarsh Patel

/*
Set Precision
*/
#include <iostream.h>
#include <iomanip.h>
#include <conio.h>

#define PI 3.1415

class area
{
private :
int r;
public :
void getdata()
{
cout << “Enter Value of R : “;
cin >> r;
cal_display();
}
private :
void cal_display()
{
float area;
area = PI * r * r;
cout << “Area of the Circle is : ” << setprecision(4) << area;
}
};

void main()
{
clrscr();
area obj;
obj.getdata();

getch();
}

//Written by Adarsh Patel

/*
Setw
Find total and percentage of student (mark1 and mark2)
*/

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

class student
{
private :
int no,mark1,mark2,total;
float per;
char name[10];

public :
void getdata();
void caldata();
void displaydata();
};

void student :: getdata()
{
clrscr();
cout << “Enter No :”;
cin >> no;
cout << “Enter Name : “;
cin >> name;
cout << “Mark 1 : “;
cin >> mark1;
cout << “Mark 2 : “;
cin >> mark2;
}
void student :: caldata()
{
total = mark1 + mark2;
per = total/2.0;
}
void student :: displaydata()
{
cout << “No ” << setw(15) << “Name” << setw(15) << “Total” << setw(15) << “Percentage” << endl;
cout << no << setw(15) << name << setw(15) << total << setw(15) << per << endl;
}

//========================================================
//    main function
//========================================================
void main()
{
clrscr();
student obj;

obj.getdata();
obj.caldata();
obj.displaydata();

getch();
}

/*

setw()     sets width
*/

//Written by Adarsh Patel

/*
Write a program to findout maximum value from the given array
pass value to object
and return value
*/

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

class sort
{
private :
int value[100];
public :
void getvalue(int);
int maxvalue(int);
};
void sort :: getvalue(int n)
{
for(int i=0;i<n;i++)
{
cout << “Enter Value [” << i<< “] : “;
cin >> value[i];
}
}
int sort :: maxvalue(int n)
{
int max=value[0];

for(int i=1;i<n;i++)
{
if(max<value[i])
max = value[i];
}
return(max);
}

//========================================================
//    main function
//========================================================
void main()
{
clrscr();
int n;
sort obj;

cout << “Enter the number of terms : ” ;
cin >> n;

obj.getvalue(n);
cout << “Maximum value is : ” << obj.maxvalue(n);

getch();
}

//Written by Adarsh Patel

/*
Basic 05, for creating 2 class
*/
#include <iostream.h>
#include <iomanip.h>
#include <conio.h>

class A
{
private:
int i;
float j;
public:
A()
{
cout << “A Constructor is called” << endl;
i = 1000;
j = 2.134;
}
void display()
{
cout << “i = ” << i << setw(10) << “j = ” <<  j << endl;
}
};

class B
{
private:
int c;
char ch;
double f;
public:
void display()
{
cout << “c = ” << c << setw(10) << “ch = ” <<  ch
<< setw(10) << “f = ” <<  f
<< endl;
}
};

//=================================================
//    Void Main
//=================================================
void main()
{
clrscr();
A obj1;
B obj2;

obj1.display();
obj2.display();

getch();
}

//Written by Adarsh Patel

/*    Sum of Rupees 1 + Paise 1 and Rupees 2 + Paisa 2
Conversion from Paisa to Rupees                    */

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

class Money
{
private:
int rupees,paisa;
public:
void getdata(int,int);
void sum(Money,Money);
inline void putdata();
};

//=================================================
//    GetData member function
//=================================================
void Money :: getdata(int r, int p)
{
rupees = r;
paisa = p;
}

//=================================================
//  Sum member function
//=================================================
void Money :: sum(Money m1, Money m2)
{
paisa = m1.paisa + m2.paisa;
rupees = paisa / 100;
paisa = paisa % 100;
rupees = rupees + m1.rupees + m2.rupees;
}

//=================================================
//  Putdata member function
//=================================================
void inline Money :: putdata()
{
cout << “Rupees : ” << rupees << setw(10) << “Paisa  : ” << paisa << endl;
}

//=================================================
//    Main Function
//=================================================
void main()
{
clrscr();
Money m1,m2,m3;
m1.getdata(5,84);
m2.getdata(8,38);
m3.sum(m1,m2);

cout << “m1 amount is  “;
m1.putdata();

cout << “m2 amount is  “;
m2.putdata();

cout << “m3 amount is  “;
m3.putdata();

getch();
}

C Programming Tutorial

Adarsh Patel

My Self Adarsh Suryakant Patel. Currently I am working as Assistant Professor at Parul Instiute of Engineering and Technology, MCA Department, Limda, Vadodara. Major Responsibility : * Lectures * Organizing Training and Expert Sessions for * Placement and Recruitment * Faculty Representative