You are on page 1of 34

Introduction

C++ was built as an extension to C and gave programmers a high control over memory and
system resources. If you know any other programming language, C++ will be easy to learn. Even
otherwise, C++ is a friendly language, and you can learn it through some hands-on projects and
practice.

* What is C++?

C++ is an OOPs based programming language, much suitable for building


high-performance applications. C++ finds its use in applications that need high speed and
accuracy, for example, operating systems, gaming applications, Graphical User Interface (GUI).
Some other popular IDEs are Eclipse, Code::Blocks. Dev C++ IDEs for new programer who are
new in C++. These IDEs(Intregrated Development Enviroments) are used for write code very
easily and also very much helpful in compile that code also

Some salient features of C++ are:

(1) Object-oriented

(2) Simple to code and understand

(3) Rich set of libraries

(4) Efficient memory management

(5) Powerful and fast

* What is Objects in programming ?

Object-oriented programming shifts the focus of attention to the objects.Objects combine


data (properties) and functions (capacities). A class defines a certain object type by defining
both the properties and the capacities of the objects of that type. Objects communicate by
sending each other “messages,” which in turn activate another object’s capacities.

Advantages of OOP :-

■ Reduced susceptibility to errors. ■ Easy to re-use. ■ Low maintenance requirement.

History of C++
C++ was developed by Bjarne Stroustrup, C++ has become one of the most popular
pro-gramming languages in the world. Originally, it was designed as an improvement upon the
C language, which was developed by Bell Labs.

Stroustrup began with the idea that object oriented programming would be an important
addition to C, and created C with Classes. In 1983, Stroustrup’s contributions officially became
known as C++, its name stemming from C and adding the ++ (increment) operator. It wasn’t
until 1998 that the international standard for C++ was established. Since then, most changes
have been minor. In 2005, a report was released by the ISO on features that were intended to be
included in the next version of C++.The early versions of this became known as C++0x, until
2011, when the C++11 standard was released by the ISO. The latest c++ version on 2021 is
C++20. "C makes it easy to shoot yourself in the foot. C++ makes it harder, but when you do,
you blow away your whole leg!" —Bjarne Stroustrup.

Page no. 1
Examples of C++ programming language.

Hint: Text in red colors are codes. And, text written in blue color in a programs are
comments or hints of that code. Comments aren't mandatory for wrote in your
program.

1) A Program for display a simple message in c++


Code:
#include<iostream>
using namespace std ;
int main()
{
cout<<"Hello! World! ";
return 0 ;
}
2) A Program for display a multiple message in c++
'#' means preprocessor, 'include' means tells the computer to insert another file
(i.e. header file),
'iostream' is a header file which contains declaration that are needed by the 'cout'
identifier. '<<' is a insertion operator.
'cout' means console output( tells the computer to print after '<<' sign).
'int main()' means that our function needs to return some integer at the end of the
execution (our program start) and we do so by returning 0 at the end of the
program by 'return o ;'
Code:
#include<iostream>
using namespace std ;
int main()
{
cout<<"Welcome to c++" ;
cout<<"It is easy to learn." ;
return 0 ;
}

Page no. 2
3) A program for display a multiple message by the help of '\n' and 'endl'
'\n' = new line character , 'endl' = end line
Code;
#include<iostream>
using namespace std;
int main()
{
cout<<"Hello, World. \n" ;
cout<<"My name is Jhon."<<endl;
cout<<"Let's learn C++";
return 0;
}
4) A program to Demonstration of inserting a comments in c++ program
Comments are ignores by compiler in the process of compile source code.
Code:
#include<iostream> //comment start with double slash and ended with end of the line.
using namespace std; /* comment is started (multi line comment)
and it is ended with this sign */
int main()
{
cout<<"Welcome to c++ program."<<endl;
cout<<"It is high level programing language.";
cout<<"\n It is used in a variety of application like operating system,
game."<<endl;
return 0;
}
5) A program for display simple message without use of 'using namespace std'
'std::' - stands for standard output stream.
Code:
#include<iostream>
Page no. 3
int main()
{
std::cout<<"Welcome to Vintage Academy of IT & Management Studies ";
std::cout<<"\n Codeing is easy to learn"; /* if we didn't use 'using namespace
std ' in our source code then we need to be insert 'std::' before 'cout' in every time
when we use 'cout' */
return 0;
}
6) A Program for add two number by the help of integer variable.
Code:
#include<iostream>
using namespace std; //it is the replacement of using ' std:: '
int main()
{
int a=5;
int b=42;
int c=a+b;
cout<<"The addition is "<<c;
cout<<"\n Press any key for exit....";
return 0;
}
7) A program for find out the size of variable in your system( int, char, float,
double).
Variables has a symbolic name and can be given a variety of values. It has not
have fixed value. We can assign the value of variables
In programming, some e.g. of variables are int,char,float,double,long,etc.
Code:
#include <iostream>
using namespace std;
int main()
{
Page no. 4
cout << "Size of char(character): " << sizeof(char) << " byte" << endl;
//char stored the Alphabets and here sizeof() is a function to find out the size
cout << "Size of int(integer) : " << sizeof(int) << " bytes" << endl;
// int stored all real numbers
cout << "Size of float: " << sizeof(float) << " bytes" << endl;
// float is used to stored real number and decimal numbers(i.e.2.5)
cout << "Size of double: " << sizeof(double) << " bytes" << endl;
//it is the another integer which stored more decimal digits
cout<<"Size of long: "<<sizeof(long)<<"bytes"<<endl;
//it is used to used to store more decimal than double.
return 0;
}
8) A program for add two number(during run time).
cin= console input.
Code:
#include<iostream>
using namespace std;
int main()
{
int a;
int b;
int sum;
cout<<"Enter 1st Number : ";
cin>>a;
cout<<" Enter 2nd Number : ";
cin>>b;
sum=a+b;
cout<<"\n\t The addition of entered two number is: "<<sum;
cout<<"\n\t\t Thank you for using. Press any key for exit...";

Page no. 5
return 0;
}
9) A program to demonstrate for convert fahrenheit value to celsius value of
temprature.
Hint: we knows that, 212 oF= 100 oC
String is a collection of letters and words. In simple words, it's called a sentence.
Code:
#include<iostream>
using namespace std;
int main()
{
int Ftemp;
cout<<"\t Enter the value of Fahrenheit: ";
cin>>Ftemp; //cin collect data from user and stores that data.
int Ctemp=(Ftemp-32)*5/9; //its calculate the value of Ctemp.
cout<<"\n\t Your entered Fahrenheit value is: "<<Ftemp<<" F";
cout<<"\n\t\t "<<Ftemp<<" F is Equal to: "<<Ctemp<<" C"<<endl;
return 0;
}
10) A program to find out the area of circle using the float variable.
Code:
#include<iostream>
using namespace std;
int main()
{
float rad;
const float pi=3.14159F; //'const float' means constant float value.
cout<<"Enter the radius of the circle: ";
cin>>rad;
float area=pi*rad*rad;
Page no. 6
cout<<"\t The area of circle which radius "<<rad<<" is = "<<area;
return 0;
}
11) A program to find out the square root of any value.
Code:
#include<iostream>
#include<math.h>
using namespace std;
int main()
{
double number,answer;
cout<<"Enter any Number: "; cin>>number;
answer=sqrt(number); //the declaration about sqrt() is provided in 'math.h'
cout<<"Square root of "<<number<<" is : "<<answer;
return 0;
}
12) A program to display the square of numbers from 1 to 14 using for loop:
Loop is a infinite process whichone break by some condition. Here we used "for
loop".
Code:
#include<iostream>
using namespace std;
int main()
{
int n;
cout<<"The series of square numbers from 1-14 is :\n";
for(n=1;n<15;++n)
cout<<n*n<<" , ";
return 0;
}
Page no. 7
13) A program for find out factors of a number.
code:
#include <iostream>
using namespace std;
int main()
{
int n, i;
cout << "Enter a positive integer: ";
cin >> n;
cout << "Factors of " << n << " are: ";
for(i = 1; i <= n; ++i)
{
if(n % i == 0)
cout << i << " ";
}
return 0;
}
14) A program to display the divident,divisor and remainder of the value using do
loop
Code:
#include<iostream>
using namespace std;
int main()
{
long dividend,divisor;
char ch;
do
{
cout<<"Enter divident value: "; cin>>dividend;
cout<<"Enter divisor value: "; cin>>divisor;
Page no. 8
cout<<"Remainder is : "<<dividend%divisor;
cout<<"\n Do another? (y/n): "; cin>>ch;
}while(ch!='n' || ch!='N'); //This loop is break if ch==n and ch==N.
// '!=' means not equal
cout<<"\t\t\t\tThanks for using. press any key for exit.....";
return 0;
}
15)A program to demonstrate multiple statement in the if body.
Code:
#include<iostream>
using namespace std;
int main()
{
int x;
cout<<"Enter any number: ";
cin>>x;
if(x>100)
{
cout<<"Your entered number is "<<x<<"\n";
cout<<x<<" is greater than 100\n";
}
else
cout<<x<<" is smaller than 100\n";
return 0;
}
16) A program to display a series upto ‘n’ numbers.
Code:
#include<iostream>
using namespace std;

Page no. 9
int main()
{
int i,n;
cout<<"Enter the value of n: ";
cin>>n;
for(i=1;i<=n;i++) //Here i++ means the value of i is increasing as 1, 2, 3, etc.
cout<<"\t The series is :"<<i<<endl;
return 0;
}
17) A program for find out ASCII code:
Code:
#include <iostream>
using namespace std;
int main()
{
char c;
cout << "Enter a single character: ";
cin >> c;
cout << "ASCII Value of " << c << " is " << int(c);
return 0;
}
18) A program for create simple calculator with do...while loop and switch
Code:
#include<iostream>
using namespace std;
int main()
{
float numOne, numTwo, res;
int choice;

Page no. 10
do
{
cout<<"\n Enter any two number :";
cin>>numOne>>numTwo;
cout<<"1. Addition\n";
cout<<"2. Subtraction\n";
cout<<"3. Multiplication\n";
cout<<"4. Division\n";
cout<<"5. Exit\n\n";
cout<<"Enter Your Choice(1-5): ";
cin>>choice;
switch(choice)
{
case 1:
res = numOne+numTwo;
cout<<"\nResult = "<<res;
break;
case 2:
res = numOne-numTwo;
cout<<"\nResult = "<<res;
break;
case 3:
res = numOne*numTwo;
cout<<"\nResult = "<<res;
break;
case 4:
res = numOne/numTwo;
cout<<"\nResult = "<<res;
break;

Page no. 11
case 5:
return 0;
default:
cout<<"\nWrong Choice!";
break;
}
cout<<"\n------------------------\n";
}while(choice!=5);
cout<<endl;
return 0;
}
19)Declare a class worker with the following specification.
Private member:
Wno of int item, wname of 45 character, wtime of int type, rate of int type, pay of
int type( pay= wtime*rate)
total() function to calculate pay
public member:
construction to initialize wno= 1, wname=” RAM KUMAR SHARMA”.
Code:
#include<iostream>
using namespace std;
class worker
{
private:
int wno;
char wname[45];
int wtime;
int rate;
int pay;
void total()
Page no. 12
{
pay=(wtime*rate);
}
public:
worker()
{
wno=1;
strcpy(wname,"RAM KUMAR SHARMA");
wtime=3;
rate=50;
}
void getdata()
{
cout<<"Enter the worker no.: "; cin>>wno;
cout<<"Enter the worker name: "; cin>>wname;
cout<<"Enter the working time: "; cin>>wtime;
cout<<"Enter rate of working in hours: Rs."; cin>>rate;
total();
}
void showdata()
{
cout<<"\n Worker no is: "<<wno;
cout<<"\n Worker name is: "<<wname;
cout<<"\n Worker working time is: "<<wtime;
cout<<"\n Rate of working in hrs is: Rs. "<<rate;
cout<<"\n Pay to "<<wname<<" of Rs. "<<pay;
}
};
int main()

Page no. 13
{
worker w;
w.getdata();
w.showdata();
return 0;
}
20) A program for find out LCM and GCD of two number(during run time)
LCM-LEAST COMMON MULTIPLE , GCD-GREATEST COMMON DIVISOR

Code:
#include<iostream>
using namespace std;
int main()
{
//variable declaration
int num1, num2, gcd = 1;
//take input from user
cout << "Enter two numbers : ";
cin >> num1;
cin >> num2;
//logic to calculate gcd and lcm
for (int i = 1; i <= num1*num2; ++i)
{
if ((num1 % i == 0) && (num2 % i == 0))
{
gcd = i;
}
}
//print calculated value of gcd and lcm
cout << "\nGCD of two number is : " << gcd;

Page no. 14
cout << "\nLCM of two number is : " << (num1 * num2) / gcd;
return 0;
}
21) A program for generate random number (in every time)
Code:
#include<iostream>
#include<cstdlib> //for random number store
#include<ctime> //for time store and display
using namespace std;
int main()
{
srand(time(0)); //it seed time with rand() function.
int random=rand(); //it stores the random number.
cout<<"Random number is "<<random<<endl;
cout<<"Random number of 1 digit is "<<random%10+1;
return 0;
}
22)A program to add two numbers using user defined function.
Code:
#include<iostream>
using namespace std;
int main()
{
void ss(); //Here ss() function is declared by void key word.
ss(); //Here we used ss() function
return 0;
}
void ss() //Codes inside the ss() function is written below.
{

Page no. 15
int a,b,sum;
cout<<"Enter the value of a and b : ";
cin>>a>>b;
sum=a+b;
cout<<"The sum of "<<a<<" and "<<b<<" is : "<<sum;
}
23) Write a program to find out the biggest of five numbers.(using user define
function)
Code:
#include<iostream>
using namespace std;
int main()
{
void bno(); // function is defined by void function.
bno();
return 0;
}
void bno() // codes in a bno() function
{
int a,b,c,d,e,big;
cout<<"Enter any five numbers : ";
cin>>a>>b>>c>>d>>e;
big=a;
if(b>big)
big=b;
if(c>big)
big=c;
if(d>big)
big=d;
if(e>big)
Page no. 16
big=e;
cout<<"\t\t\tThe bigger number is "<<big;
}
24) Write a program of copy constructor.
Code:
#include<iostream>
using namespace std;
class sample
{
int x,y;
public:
sample(int a,int b)
{
x=a;
y=b;
}
sample(sample&p)
{
x=p.x;
y=p.y;
}
void show()
{
cout<<"X is : "<<x<<endl;
cout<<"y is : "<<y<<endl;
}

};
int main()

Page no. 17
{
sample p1(10,20);
sample p2(p1);
p1.show();
p2.show();
return 0;
}
25)Declare a class students with the following specification:
Private members:
roll no, age of integer type, name of 30 character
public member:
read() function to read data as input of roll no, name, age.
show() function to display roll no, name, age.
Code:
#include<iostream>
using namespace std;
class students
{
private:
int rollno;
char name[30];
int age;
public:
void read()
{
cout<<"Enter the roll no: ";
cin>>rollno;
cout<<"Enter student name : ";
cin>>name;

Page no. 18
cout<<"Enter the age of student : ";
cin>>age;
}
void show()
{
cout<<"\n\t Roll no. is : "<<rollno;
cout<<"\n\t Student name is : "<<name;
cout<<"\n\t Student age is : "<<age;
}
};
int main()
{
students s;
s.read();
s.show();
return 0;
}
26)declare a class book with the following specification.
private member:
bookno of integer type, bookname and author of 20 character, price of float type.
public member:
get() --> function to enter the details of book.
show() --> function to display the details of a book.
code:
#include<iostream>
using namespace std;
class book
{
private:

Page no. 19
int bookno;
char bookname[20];
char author[20];
float price;
public:
void get()
{
cout<<"Enter the book number : "; cin>>bookno;
cout<<"Enter the book name : "; cin>>bookname;
cout<<"Enter the author name of the "<<bookname<<" : "; cin>>author;
cout<<"Enter the price of the "<<bookname<<" book : "; cin>>price;
}
void show()
{
cout<<"\n\t Book number is "<<bookno;
cout<<"\n\t Book name is "<<bookname;
cout<<"\n\t Author name is "<<author;
cout<<"\n\t Price of this book is "<<price;
}
};
int main()
{
book b;
b.get();
b.show();
return 0;
}

Page no. 20
27)Declare a class employee with the following specification.
private member:
employee number of integer type; employee name of 30 characters, salary of float
type.
private member:
enter() --> function to enter.
display() --> function to display employee number, employee name, salary.
code:
#include<iostream>
using namespace std;
class employee
{
private:
int e_no;
char eName[30];
float salary;
public:
void enter()
{
cout<<"Enter the employee serial number : "; cin>>e_no;
cout<<"Enter the employee name : "; cin>>eName;
cout<<"Enter the salary of "<<eName<<" : "; cin>>salary;
}
void display()
{
cout<<"\n\t\t Employee number : "<<e_no;
cout<<"\n\t\t Employee name is : "<<eName;
cout<<"\n\t\t Salary of "<<eName<<" is : "<<salary;
}
};
Page no. 21
int main()
{
employee e;
e.enter();
e.display();
return 0;
}
28)A program to calculate simple interest and total payable loan amount by the
help of class.
code:
#include<iostream>
using namespace std;
class si
{
private:
float p;
int t;
float r;
float interest;
float total;
void calculate()
{
interest=(p*t*r)/100;
total=p+interest;
}
public:
void get()
{
cout<<" Enter the Principal amount(Loan amount) : Rs."; cin>>p;
cout<<" Enter the time period in year : "; cin>>t;
Page no. 22
cout<<" Enter the rate of interest per year : @ "; cin>>r;
calculate();
}
void show()
{
cout<<"\n\t\t\t Principal amount is : Rs. "<<p;
cout<<"\n\t\t\t Time period is : "<<t<<" Years";
cout<<"\n\t\t\t Rate of interest per year is : @ "<<r<<" %";
cout<<"\n\t\t\t The interest amount of Rs. “<<p<<" is : Rs. "<<interest;
cout<<"\n\t\t\t Total payable amount with interest is Rs. "<<total;
}
};
int main()
{
si s;
s.get();
s.show();
return 0;
}
29)Declare a class subject with the following specification and find out average
and total mark of all subject.
private member:
math, eng, sci, sst, Mil of integer type, average of float type, total of integer type.
calculate() function to calculate total and average.
public member:
get() function to enter the number.
show() function to display the calculation result and entered details.
code:
#include<iostream>
using namespace std;
Page no. 23
class subject
{
private:
int math,eng,sci,sst,Mil;
int total;
float average;
void calculate()
{
total=math+eng+sci+sst+Mil;
average=total/5;
}
public:
void get()
{
cout<<"Enter the mark of Math : "; cin>>math;
cout<<"Enter the mark of English : "; cin>>eng;
cout<<"Enter the mark of Science : "; cin>>sci;
cout<<"Enter the mark of Social studies : "; cin>>sst;
cout<<”Enter the mark of MIL subject : "; cin>>Mil;
calculate();
}
void show()
{
cout<<"Marks of all subjects are :";
cout<<"\n\t Math= "<<math;
cout<<"\n\t English= "<<eng;
cout<<"\n\t Science= "<<sci;
cout<<"\n\t Social Studies= "<<sst;
cout<<"\n\t MIL= "<<Mil;

Page no. 24
cout<<"\n Total mark of all subject is : "<<total;
cout<<"\n The average of all subject is : "<<average;
}
};
int main()
{
subject s;
s.get();
s.show();
return 0;
}
30)Find the area and parameter of rectangle using class rectangle.
Hints: Area of rectangle= l*b
Parameter of rectangle = 2 * ( l + b )
code:
#include<iostream>
using namespace std;
class rectangle
{
private:
float l, b;
double area,para;
void calculate()
{
area=l*b;
para=2*(l+b);
}
public:
void get()

Page no. 25
{
cout<<"\tEnter the length value of rectangle(c.m.) : ";
cin>>l;
cout<<"\t\t Enter the bredth value of rectangle(c.m.) : ";
cin>>b;
calculate();
}
void show()
{
system("cls"); //It is written for clear the above data from the program.
cout<<"The length is : "<<l;
cout<<"\nThe breadth is : "<<b;
cout<<"\n-------------------------------------------------------------------------------------------------";
cout<<"\n\n\t\t The area of rectangle is : "<<area;
cout<<"\n\t\t The parameter of rectangle is : "<<para;
}
};
int main()
{
rectangle r;
r.get();
r.show();
return 0;
}
31) Find the area of tringle using class with the following formula.
Area of Tringle = 0.5*b*h
code:
#include<iostream>
using namespace std;

Page no. 26
class tringle
{
private:
float b,h;
float area;
void calculate() //declaration of calculate function.
{
area=0.5*b*h;
}
public:
void get()
{
cout<<"\t Enter the value of b(base) : "; cin>>b;
cout<<"\t Enter the value of h(height) : "; cin>>h;
calculate(); //using of calculation function.
}
void show()
{
system("cls");
cout<<"\n\t\t Value of b is : "<<b;
cout<<"\n\t\t Value of h is : "<<h;
cout<<"\n------------------------------------------------";
cout<<"\n\t The area of tringle is : "<<area; //formula was given in line 10
}
};
int main()
{
tringle t;
t.get();

Page no. 27
t.show();
return 0;
}
32)Declare a class sample with the following specification.
private member:
item no. of integer type, item name of string type, item price of float type.
public member:
read() means read data and show() means display all.
code:
#include<iostream>
#include<string> /* for use strings in our code but without inserting it program is
also run because its declaration is given in iostream header file.*/
using namespace std;
class sample
{
private:
int itemno;
string itemname;
float itemprice;
public:
void read()
{
cout<<"Enter item number : "; cin>>itemno;
cout<<"Enter item name : "; cin>>itemname;
cout<<"Enter the price of item : "; cin>>itemprice;
}
void show()
{
system("cls");
cout<<"\t Item no. is : "<<itemno;
Page no. 28
cout<<"\t item name is : "<<itemname;
cout<<"\t Item price is : "<<itemprice;
}
};
int main()
{
sample s;
s.read();
s.show();
return 0;
}
33)A program for count number of word, characters and spaces in a sentence
Code:
#include <iostream>
using namespace std;
int main()
{
char str[100]; //declare and initialize char array
int i;
int words=1,characters=0,space=0;
cout<<"Please enter the sentence \n";
gets(str);
for(i=0; str[i] != '\0'; i++)
{
if(str[i]!=' ')
{
//check characters
characters++;
}

Page no. 29
else if(str[i]==' ' || str[i] != '\n' || str[i] != '\t')
{
words++; //check words
}
}
cout<<"\nTotal words: "<<words; //display total numbers of words
cout<<"\nTotal characters: "<<characters; //display total characters
cout<<"\nSpace: "<<(words-1); //display total numbers of space
return 0;
}
34) A program to display the factorial number using for loop.
Code:
#include <iostream>
using namespace std;
int main()
{
unsigned int a; //Unsigned Integers are always non-negative (zero or positive).
unsigned long fact=1;
cout << "Enter any number ";
cin>>a;
for(int j=a;j>0;j--)
fact=fact*j;
cout<<"factorial is "<<fact;
return 0;
}
34) A program to add two number using pointer and class.
A pointer is a variable, with the address of another variable as its value. In
C++, pointers help us to make certain task easier to perform other task, such as
dynamic memory allocation, can not be performed without using pointers. All
pointer share the same data type - a long hexadecimal number that represents a
memory address. That address can be accessed using the ampersand (&)
Page no. 30
operator (also called the address-of operator), which denotes an address in
memory. It is a variable, and like any other variable, it must be declared before you
can work with it. The asterisk (*) sign is used to declare a pointer.
There are two operators for pointers : (a) Address-of operator (&) : It help us to
returns the memory address of its operand.
(b) Contents-of (or dereference) operator (*) : It help us to returns the value of
the variable located at the address specified by its operand.
Note: A variable or a value involved in an operation is called an operand.
Dynamic memory allocation in C/C++ refers to performing memory allocation
manually by programmer. Dynamic memory allocation should be used when the
size of an array is not know at compile time. For e.g when you want to allocate an
array based on user input. Dynamic memory allocation helps you to allocate the
exact number of needed bytes.
Code:
#include<iostream>
using namespace std;
class pointer // Class declared as pointer
{ public:
int a,b,c;
int *x,*y; //Pointer is declared
void read()
{
cout<<"Enter 1st number : "; cin>>a;
cout<<"Enter 2nd number : "; cin>>b;
x=&a; //The memory address of variable a is accessed and stored in x
y=&b; //The memory address of variable b is accessed and stored in y
c=*x+*y; // It is also equal with c=a+b
}
void show()
{
cout<<"\n\t x = "<<x; // variable memory location of a
cout<<"\n\t y = "<<y; // variable memory location of b
cout<<"\n x = "<<*x; // *x display the value of a
Page no. 31
cout<<"\n y = "<<*y; // *y display the value of b
cout<<"\n The sum of two number is : "<<c;
}
};
int main()
{
pointer p;
p.read();
p.show();
return 0;
}
35) A program for casino number guseeing game(It is not for exam point of view.)
Code:
#include<iostream> //casino number guseeing game
#include<string>//for use string in program
#include<cstdlib>//it is for use random number
#include<ctime>
using namespace std;
void rules();
int main()
{
string playername;
int balance; //it is for store player balance
int bettingamount; //it is for store betting amount
int guess; //it is for guess number store
int dice; //stores the random number
char choice;
srand(time(0)); //seed the random generator with time
cout<<"\n\t========welcome to casino world==========\n\n"

Page no. 32
<<"------------------------------------------------------------";
cout<<"\n\n What is your name? ";
getline(cin,playername);
cout<<"\n\n Enter the starting balance to play game: Rs.";
cin>>balance;
do
{
rules();
cout<<"\n\n Your balance is Rs."<<balance<<'\n';
do //get player betting balance
{
cout<<"hey,"<<playername<<", Enter amount to bet: Rs. ";
cin>>bettingamount;
if(bettingamount>balance)
cout<<"betting balance can't be more than current balance!!\n"
<<"\n Re-enter balance\n";
} while(bettingamount > balance);
do //get player's numbers
{
cout<<"Guess any betting number between 1 to 10 : ";
cin>>guess;
if(guess<=0 || guess>10)
cout<<"\n Numeber should be between 1 to 10.\n"
<<"re-enter number : \n";
} while(guess<=0||guess>10);
dice=rand()%10+1;
if(dice==guess)
{
cout<<"\n\n you are in luck!! you have won
Rs."<<bettingamount*10;
Page no. 33
balance=balance+bettingamount*10;
}
else
{
cout<<"Ooops, better luck next time!!, you lost
Rs."<<bettingamount<<"\n";
balance=balance-bettingamount;
}
cout<<"\n"<<playername<<", you have a balance of
Rs."<<balance<<"\n";
if(balance==0)
{
cout<<"You have no money to play";
break;
}
cout<<"\n\n--->do you want to play again(y/n) ?";
cin>>choice;
} while(choice!='n');
cout<<"\n Thanks you for playing the game. your balance is
Rs."<<balance<<"\n";
return 0;
}
void rules()
{
system("cls");
cout<<"\t\t=====Casino number Guessing rules!===\n";
cout<<"\t 1.Choice a number between 1 to 10.\n";
cout<<"\t 2.winner gets 10 times of money bet\n";
cout<<"\t 3.wrong bet,and you lose the amount you bet\n\n";
}

Page no. 34

You might also like