-
8/16/2019 100+ C++ Programs with Output For Students & Professionals - Aniket Pataskar
1/309
-
8/16/2019 100+ C++ Programs with Output For Students & Professionals - Aniket Pataskar
2/309
C++ 100+ Programs with Output
By Aniket Pataskar
Preface
Copyright 2016 By Author
All rights reserved. No part of this book may be reproduced, stored in a retrieval system,or transmitted in any form or by any means, without the prior written permission of theauthor.
Every effort has been made in the preparation of this book to ensure the accuracy of theinformation presented. However the information contained in this book is sold withoutwarranty, either express or implied. Neither the author, nor distributor will be held liablefor any damages caused or alleged to be caused directly or indirectly by this book.
First Published : Jan 2016
Email : [email protected]
Contact : +91 9423448273
-
8/16/2019 100+ C++ Programs with Output For Students & Professionals - Aniket Pataskar
3/309
Cover image by Aniket Pataskar
About The Author
Aniket O. Pataskar is originally from Aurangabad, India. He is an IT Software engineerwith working experience on Programming in various languages & Mac OSX Systems.Along with that he has interest in various social things. After the continuous efforts, he hascompleted this book.
Author also has skills in software development, iOS (iPhone, iPad & iPod) &AndroidApplication Development, Multimedia such as - Animation, Digital film making,
Video editing, visual effects & Sound Editing. He also has his online learning course inUdemy Market.
-
8/16/2019 100+ C++ Programs with Output For Students & Professionals - Aniket Pataskar
4/309
This book is dedicated to :
My family for their continued love and support, and for always believing in me. I alsowant to dedicate this book to the God who always inspires me to do things.
This book would not have been possible without your love and understanding.
Thank you from the bottom of my heart.
Index
Program Name Page No.
-
8/16/2019 100+ C++ Programs with Output For Students & Professionals - Aniket Pataskar
5/309
1. Hello World C++ Example Program 12
2. if Statement Example Program in C++ 13
3. if..else Statement Example Program In C++ 14
4. For Loop Example Program In C++ 16
5. While Loop Example Program In C++ 17
6. Do While Loop Example Program In C++ 18
7. Simple Example Program For Constructor In C++ 19
8. Simple Example Program For Parameterized
Constructor In C++ 20
9. Simple Example Program For Constructor
Overloading In C++ 23
10. Simple Constructor Example Program For Find Prime
Number In C++ 25
11. Simple Example Program For Copy
Constructor In C++ 28
12. Simple Class Example Program In C++ 30
-
8/16/2019 100+ C++ Programs with Output For Students & Professionals - Aniket Pataskar
6/309
13. Simple Class Example Program For Find Prime
Number In C++ 32
14. Simple Example Program For Namespace In C++ 34
15. Find Prime Number Example Program In C++ 35
16. Find Prime Number by another method Example
Program In C++ 37
17. Fibonacci series Example Program In C++ 39
18. Factorial Using Recursion Example Program In C++ 41
19. Factorial Using Function Example Program In C++ 43
20. Factorial Using Loop Example Program In C++ 45
21. Simple Example Program for Function In C++ 46
22. Simple Example Program for Function Find
Smallest Number In C++ 47
23. Simple Example Program for Function to
Find Factorial In C++ 48
24. Simple Example Program for Inline Function
Using C++ Programming 50
-
8/16/2019 100+ C++ Programs with Output For Students & Professionals - Aniket Pataskar
7/309
25. Simple Addition in C++ Binary Operator
Overloading Using C++ Programming 51
26. Simple Program for Multiple Inheritance
Using C++ Programming 53
27. Simple Program for Inline Function Using
C++ Programming 56
28. Simple Program for Function Template
Using C++ Programming 58
29. Simple Program for Function Overloading
Using C++ Programming 60
30. Simple Program for Friend Function
Using C++ Programming 63
31. Simple Program for Exception Handling with
Multiple Catch Using C++ Programming 65
32. Simple Program for Exception Handling Divide
by zero Using C++ Programming 67
33. Simple Program for Constructor Using
C++ Programming 69
-
8/16/2019 100+ C++ Programs with Output For Students & Professionals - Aniket Pataskar
8/309
34. Simple Program for Binary Operator
Overloading Using C++ Programming 71
35. Simple Program Book Entry Using structure
Variable in C++ Programming 74
36. Simple Program for Write File Operation
Using C++ Programming 76
37. Simple Program for Virtual FunctionsUsing C++ Programming 77
38. Simple Program for Virtual Base Class
Using C++ Programming 79
39. Simple Program for Unary Operator Overloading
Using C++ Programming 83
40. Simple Program for Static Data and Member
Function Using C++ Programming 86
41. Simple Program for Single Inheritance
Using C++ Programming 88
42. Simple Program for Copy Constructor
Using C++ Programming 91
43. Simple Program for Read File Operation
-
8/16/2019 100+ C++ Programs with Output For Students & Professionals - Aniket Pataskar
9/309
Using C++ Programming 93
44. Simple Program for Read & Write File Operation
(Convert lowercase to uppercase) Using C++ Programming 95
45. C++ program which adds two integers 97
46. C++ Program to output an integer, a floating
point number and a character 98
47. C++ Program to find the sum, difference, product
and quotient of two integers 99
48. Program to enter two integers and find their sum
and average 101
49. Program to enter velocity, acceleration and time
and print final velocity using the formula : v = u + a * t 102
50. Program to enter your age and print if you should
be in grade 10 103
51. Program to enter an integer and print if it is greater
or less than 100 105
52. Program to enter an integer and output its 15 multiples 106
53. Program to enter two integers and print the quotient
-
8/16/2019 100+ C++ Programs with Output For Students & Professionals - Aniket Pataskar
10/309
and remainder 108
54. Program to enter an integer and find out if it is
even or odd 109
55.Program to change the foreground colors and draw
circles on the screen 110
56. Program to draw 2 rectangles and fill 1 of them 112
57. Program to enter three integers and output the
biggest integer 114
58. Program to draw circles 115
59. Program to change the background colors
on the screen 117
60. Program to enter three integers and output
the biggest integer using IF 119
61. Program to enter an integer and print out
its successor 120
62. Program to enter an integer and output the cube
of that integer 121
63. Program to enter a string and find its length 122
-
8/16/2019 100+ C++ Programs with Output For Students & Professionals - Aniket Pataskar
11/309
64. Program to switch between different cases 123
65. Program to enter the principal, rate & time and
print the simple interest 125
66. Program to enter an integer and print if it is
prime or composite 127
67. Program to enter the sale value and print the
agent’s commission 129
68. Program to enter the unit reading and output the
customer’s telephone bill 131
69. Program to find the total days in the year till date 133
70. Program to compute the fibonacci series 134
71. Program to enter an integer and print its total
value based on the formula
‘x - 1/3!x^3 + 1/5!x^5 - 1/7!x^7 + 1/9!x^9’ 136
72. Program to convert days into years and weeks 138
73. Program to find the roots of a quadratic equation 139
74. Program to enter an integer and output it in the
-
8/16/2019 100+ C++ Programs with Output For Students & Professionals - Aniket Pataskar
12/309
reversed form 141
75. Program to count the number of words and characters
in a sentence 142
76. write program to take the salary of the employee
as a screen input from the user. 144
77. Program to find the sum of each row & column
of a matrix of size n x m and 146if matrix is square, find the sum of the diagonals also.
78. Program to convert 2-digit octal number into binary
number and print it 149
79. Program to identify if an input is a symbol,
digit or character 151
80. Program to enter three integers and output the
smallest integer using IF 152
81. Program to enter a sentence and output the
number of uppercase & lowercase consonants, uppercase
& lowercase vowels in sentence. 153
82. Program to print the first 10 lines of pascal’s triangle 155
83. Program to enter 10 integers in a single-dimension
-
8/16/2019 100+ C++ Programs with Output For Students & Professionals - Aniket Pataskar
13/309
array and then print out the array in ascending order 157
84. Program to find the sum of either of the diagonals
of a 4 x 4 matrix 159
85. Program to convert temperatures from Celsius to
Fahrenheit and vice versa 161
86. Program to enter a character and output
its ASCII code 163
87. Program to enter a letter and output the next 2 letters 164
88. Print 1 to 100 in C++, without loop and recursion 165
89. simple C++ program to show working of getline. 167
90. Print 2D matrix in different lines and without curly
braces in C/C++? 170
91. A simple and complete C++ program to demonstrate
friend Class 171
92. A simple and complete C++ program to demonstrate
friend function of another class. 172
93. A simple and complete C++ program to demonstrate
global friend 173
-
8/16/2019 100+ C++ Programs with Output For Students & Professionals - Aniket Pataskar
14/309
94. Write a programs to show Constructors in C++ 174
95. Write a program on sorting. 175
96. Write a program to demonstrate Binary Search
Algorithms in C++ 176
97. Can namespaces be nested in C++?Write the program. 178
98. Write a program to find the prime numbers from
2 to 100 by using nested for loop. 180
99. Write a program using nested if statement. 181
100. Write a program to demonstrate nested
switch statement. 182
101. Predict the output of following C++ program.
(1 to 15 Programs) 183
-
8/16/2019 100+ C++ Programs with Output For Students & Professionals - Aniket Pataskar
15/309
1. Hello World C++ Example Program
#include
#include
//Standard namespace declaration
using namespace std;
//Main Function
int main(){
//Standard Ouput Statement
cout
-
8/16/2019 100+ C++ Programs with Output For Students & Professionals - Aniket Pataskar
16/309
-
8/16/2019 100+ C++ Programs with Output For Students & Professionals - Aniket Pataskar
17/309
2. if Statement Example Program in C++
#include
#include
using namespace std;
int main()
{
// Variable Declaration int a;
//Get Input Value
couta;
//If Condition Check
if(a > 10)
{
// Block For Condition Success
cout
-
8/16/2019 100+ C++ Programs with Output For Students & Professionals - Aniket Pataskar
18/309
15 Is Greater than 10
-
8/16/2019 100+ C++ Programs with Output For Students & Professionals - Aniket Pataskar
19/309
3. if..else Statement Example Program In C++
#include
#include
using namespace std;
int main()
{
// Variable Declaration
int a;
// Get Input Value
couta;
//If Condition Check
if(a > 10)
{
// Block For Condition Success
cout
-
8/16/2019 100+ C++ Programs with Output For Students & Professionals - Aniket Pataskar
20/309
return 0;
}
Output 1:
Enter the Number :20
20 Is Greater than 10
Output 2:
Enter the Number :5
5 Is Less than/Equal 10
-
8/16/2019 100+ C++ Programs with Output For Students & Professionals - Aniket Pataskar
21/309
-
8/16/2019 100+ C++ Programs with Output For Students & Professionals - Aniket Pataskar
22/309
4. For Loop Example Program In C++
#include
#include
using namespace std;
int main()
{
// Variable Declaration
int a;
// Get Input Value
couta;
//for Loop Block
for (int counter = 1; counter
-
8/16/2019 100+ C++ Programs with Output For Students & Professionals - Aniket Pataskar
23/309
Execute 1 time
Execute 2 time
Execute 3 time
Execute 4 time
Execute 5 time
-
8/16/2019 100+ C++ Programs with Output For Students & Professionals - Aniket Pataskar
24/309
5. While Loop Example Program In C++
#include
#include
using namespace std;
int main()
{
// Variable Declaration int a;
// Get Input Value
couta;
int counter = 1;
//while Loop Block
while (counter
-
8/16/2019 100+ C++ Programs with Output For Students & Professionals - Aniket Pataskar
25/309
Enter the Number :4
Execute While 1 time
Execute While 2 time
Execute While 3 time
Execute While 4 time
-
8/16/2019 100+ C++ Programs with Output For Students & Professionals - Aniket Pataskar
26/309
6. Do While Loop Example Program In C++
#include
#include
using namespace std;
int main()
{
// Variable Declaration int a;
// Get Input Value
couta;
int counter = 1;
//Do while Loop Block
do
{
cout
-
8/16/2019 100+ C++ Programs with Output For Students & Professionals - Aniket Pataskar
27/309
Execute Do While 1 time
Execute Do While 2 time
Execute Do While 3 time
Execute Do While 4 time
Execute Do While 5 time
Execute Do While 6 time
-
8/16/2019 100+ C++ Programs with Output For Students & Professionals - Aniket Pataskar
28/309
7. Simple Example Program For Constructor
In C++
#include
#include
using namespace std;
class Example {
// Variable Declaration
int a,b;
public:
//Constructor
Example() {
// Assign Values In Constructor a=10;
b=20;
cout
-
8/16/2019 100+ C++ Programs with Output For Students & Professionals - Aniket Pataskar
29/309
// Wait For Output Screen
getch();
return 0;
}
Output :
Im Constructor
Values :10 20
-
8/16/2019 100+ C++ Programs with Output For Students & Professionals - Aniket Pataskar
30/309
-
8/16/2019 100+ C++ Programs with Output For Students & Professionals - Aniket Pataskar
31/309
8. Simple Example Program For
Parameterized Constructor In C++
#include
#include
using namespace std;
class Example {
// Variable Declaration
int a,b;
public:
//Constructor
Example(int x,int y) {
// Assign Values In Constructor a=x;
b=y;
cout
-
8/16/2019 100+ C++ Programs with Output For Students & Professionals - Aniket Pataskar
32/309
// Wait For Output Screen
getch();
return 0;
}
Output :
Im Constructor
Values :10 20
-
8/16/2019 100+ C++ Programs with Output For Students & Professionals - Aniket Pataskar
33/309
-
8/16/2019 100+ C++ Programs with Output For Students & Professionals - Aniket Pataskar
34/309
9. Simple Example Program For Constructor
Overloading In C++
#include
#include
using namespace std;
class Example {
// Variable Declaration
int a,b;
public:
//Constructor wuithout Argument
Example() {
// Assign Values In Constructor a=50;
b=100;
cout
-
8/16/2019 100+ C++ Programs with Output For Students & Professionals - Aniket Pataskar
35/309
cout
-
8/16/2019 100+ C++ Programs with Output For Students & Professionals - Aniket Pataskar
36/309
-
8/16/2019 100+ C++ Programs with Output For Students & Professionals - Aniket Pataskar
37/309
10. Simple Constructor Example Program
For Find Prime Number In C++
Algorithm:
STEP 1: Start the program.
STEP 2: Declare the class as Prime with data members,
Member functions.
STEP 3: Consider the argument constructor Prime() with integer
Argument.
STEP 4: To cal the function calculate() and do the following steps.
STEP 5: For i=2 to a/2 do
STEP 6: Check if a%i==0 then set k=0 and break.
STEP 7: Else set k value as 1.
STEP 8: Increment the value i as 1.
STEP 9: Check whether the k value is 1 or 0.
STEP 10:If it is 1 then display the value is a prime number.STEP 11:Else display the value is not prime.
STEP 12:Stop the program.
#include
#include
using namespace std;
// Class Declaration
class prime
{
//Member Varibale Declaration
int a,k,i;
public:
-
8/16/2019 100+ C++ Programs with Output For Students & Professionals - Aniket Pataskar
38/309
prime(int x)
{
a=x;
k=1;
{
for(i=2;i
-
8/16/2019 100+ C++ Programs with Output For Students & Professionals - Aniket Pataskar
39/309
int a;
couta;
// Object Creation For Class
prime obj(a);
// Call Member Functions
obj.show();
getch();
return 0;
}
Output :
Enter the Number:7
7 is Prime Number.
-
8/16/2019 100+ C++ Programs with Output For Students & Professionals - Aniket Pataskar
40/309
-
8/16/2019 100+ C++ Programs with Output For Students & Professionals - Aniket Pataskar
41/309
11. Simple Example Program For Copy
Constructor In C++
#include
#include
using namespace std;
class Example {
// Variable Declaration
int a,b;
public:
//Constructor with Argument
Example(int x,int y) {
// Assign Values In Constructor a=x;
b=y;
cout
-
8/16/2019 100+ C++ Programs with Output For Students & Professionals - Aniket Pataskar
42/309
Example Object2=Object;
// Constructor invoked.
Object.Display();
Object2.Display();
// Wait For Output Screen
getch();
return 0;
}
Output :
Im Constructor
Values :10 20
Values :10 20
-
8/16/2019 100+ C++ Programs with Output For Students & Professionals - Aniket Pataskar
43/309
-
8/16/2019 100+ C++ Programs with Output For Students & Professionals - Aniket Pataskar
44/309
12. Simple Class Example Program In C++
#include
#include
using namespace std;
// Class Declaration
class person
{
//Access - Specifierpublic:
//Varibale Declaration
string name;
int number;
};
//Main Function
int main()
{
// Object Creation For Class
person obj;
//Get Input Values For Object Varibales
coutobj.name;
coutobj.number;
//Show the Output
-
8/16/2019 100+ C++ Programs with Output For Students & Professionals - Aniket Pataskar
45/309
cout
-
8/16/2019 100+ C++ Programs with Output For Students & Professionals - Aniket Pataskar
46/309
-
8/16/2019 100+ C++ Programs with Output For Students & Professionals - Aniket Pataskar
47/309
13. Simple Class Example Program For Find
Prime Number In C++
#include
#include
using namespace std;
// Class Declaration
class prime
{
//Member Varibale Declaration
int a,k,i;
public:
prime(int x) {
a=x;
}
// Object Creation For Class
void calculate()
{
k=1;
{
for(i=2;i
-
8/16/2019 100+ C++ Programs with Output For Students & Professionals - Aniket Pataskar
48/309
}
else
{
k=1;
}
}
}
void show()
{
if(k==1)
cout
-
8/16/2019 100+ C++ Programs with Output For Students & Professionals - Aniket Pataskar
49/309
}
Output:
Enter the Number:10
10 is Not Prime Numbers.
Enter the Number:7
7 is Prime Number.
-
8/16/2019 100+ C++ Programs with Output For Students & Professionals - Aniket Pataskar
50/309
14. Simple Example Program For Namespace
In C++
#include
using namespace std;
//Namespace namespacefirst
namespace namespacefirst
{
int value = 5;
}
//Namespace namespacesecond
namespace namespacesecond
{
double value = 3.1416;}
int main () {
//Namespace namespacefirst Varibale Usage
cout
-
8/16/2019 100+ C++ Programs with Output For Students & Professionals - Aniket Pataskar
51/309
namespacefirst value : 5
namespacesecond value : 3.1416
-
8/16/2019 100+ C++ Programs with Output For Students & Professionals - Aniket Pataskar
52/309
15. Find Prime Number Example Program In
C++
#include
#include
#include // Math.h For sqrt function
using namespace std;
int main()
{
// Variable Declaration
int n;
// Get Input Value coutn;
cout
-
8/16/2019 100+ C++ Programs with Output For Students & Professionals - Aniket Pataskar
53/309
}
}
// Wait For Output Screen
getch();
return 0;
}
Output:
Enter the Number :50
List Of Prime Numbers Below 50
5
7
11
13
17
19
23
29
31
37
41
4347
-
8/16/2019 100+ C++ Programs with Output For Students & Professionals - Aniket Pataskar
54/309
-
8/16/2019 100+ C++ Programs with Output For Students & Professionals - Aniket Pataskar
55/309
16. Find Prime Number by another method
Example Program In C++
#include
#include
#include // Math.h For sqrt function
using namespace std;
int main()
{
// Variable Declaration
int n;
// Get Input Value
coutn;
cout
-
8/16/2019 100+ C++ Programs with Output For Students & Professionals - Aniket Pataskar
56/309
prime=false;
break;
}
}
if(prime) cout
-
8/16/2019 100+ C++ Programs with Output For Students & Professionals - Aniket Pataskar
57/309
-
8/16/2019 100+ C++ Programs with Output For Students & Professionals - Aniket Pataskar
58/309
17. Fibonacci series Example Program In
C++
#include
#include
using namespace std;
int main()
{
// Variable Declaration
int counter, n;
long last=1,next=0,sum;
// Get Input Value
coutn;
//Fibonacci Series Calculation
while(next
-
8/16/2019 100+ C++ Programs with Output For Students & Professionals - Aniket Pataskar
59/309
}
Output :
Enter the Number :300
1
1
2
3
5
8
13
21
34
55
89
144
233
-
8/16/2019 100+ C++ Programs with Output For Students & Professionals - Aniket Pataskar
60/309
-
8/16/2019 100+ C++ Programs with Output For Students & Professionals - Aniket Pataskar
61/309
18. Factorial Using Recursion Example
Program In C++
#include
#include
using namespace std;
//Function
long factorial(int);
int main()
{
// Variable Declaration
int counter, n;
// Get Input Value
coutn;
// Factorial Function Call
cout
-
8/16/2019 100+ C++ Programs with Output For Students & Professionals - Aniket Pataskar
62/309
long factorial(int n)
{
if (n == 0)
return 1;
else
return(n * factorial(n-1));
}
Output :
Enter the Number :6
6 Factorial Value Is 720
-
8/16/2019 100+ C++ Programs with Output For Students & Professionals - Aniket Pataskar
63/309
-
8/16/2019 100+ C++ Programs with Output For Students & Professionals - Aniket Pataskar
64/309
19. Factorial Using Function Example
Program In C++
#include
#include
using namespace std;
//Function
long factorial(int);
int main()
{
// Variable Declaration
int counter, n;
// Get Input Value
coutn;
// Factorial Function Call
cout
-
8/16/2019 100+ C++ Programs with Output For Students & Professionals - Aniket Pataskar
65/309
long factorial(int n)
{
int counter;
long fact = 1;
//for Loop Block
for (int counter = 1; counter
-
8/16/2019 100+ C++ Programs with Output For Students & Professionals - Aniket Pataskar
66/309
-
8/16/2019 100+ C++ Programs with Output For Students & Professionals - Aniket Pataskar
67/309
20. Factorial Using Loop Example Program
In C++
#include
#include
using namespace std;
int main()
{
// Variable Declaration
int counter, n, fact = 1;
// Get Input Value
coutn;
//for Loop Block
for (int counter = 1; counter
-
8/16/2019 100+ C++ Programs with Output For Students & Professionals - Aniket Pataskar
68/309
Output :
Enter the Number :6
6 Factorial Value Is 720
-
8/16/2019 100+ C++ Programs with Output For Students & Professionals - Aniket Pataskar
69/309
21. Simple Example Program for Function In
C++
#include
#include
using namespace std;
// Simple Function
void printmessage ()
{
cout
-
8/16/2019 100+ C++ Programs with Output For Students & Professionals - Aniket Pataskar
70/309
-
8/16/2019 100+ C++ Programs with Output For Students & Professionals - Aniket Pataskar
71/309
22. Simple Example Program for Function
Find Smallest Number In C++
#include
#include
using namespace std;
// Simple Function
int compare( int a, int b )
{
return (a+4 < b)? a : b;
}
int main() {
cout
-
8/16/2019 100+ C++ Programs with Output For Students & Professionals - Aniket Pataskar
72/309
-
8/16/2019 100+ C++ Programs with Output For Students & Professionals - Aniket Pataskar
73/309
23. Simple Example Program for Function to
Find Factorial In C++
#include
#include
using namespace std;
// Simple factorial Function
int factorial(int var)
{
int fact=1;
for(int i=1;i
-
8/16/2019 100+ C++ Programs with Output For Students & Professionals - Aniket Pataskar
74/309
-
8/16/2019 100+ C++ Programs with Output For Students & Professionals - Aniket Pataskar
75/309
24. Simple Example Program for Inline
Function Using C++ Programming
ALGORITHM:
Step 1: Start the pogram.
Step 2: Declare the class.
Step 3: Declare and define the inline function for multiplication and cube.
Step 4: Declare the class object and variables.
Step 5: Read two values.
Step 6: Call the multiplication and cubic functions using class objects.
Step 7: Return the values.
Step 8: Display.
Step 9: Stop the program.
Example Program Of Inline Function
#include#include
class line
{
public:
inline float mul(float x,float y)
{
return(x*y);
}
inline float cube(float x)
{
return(x*x*x);
}
};
void main()
-
8/16/2019 100+ C++ Programs with Output For Students & Professionals - Aniket Pataskar
76/309
{
line obj;
float val1,val2;
clrscr();
coutval1>>val2;
cout
-
8/16/2019 100+ C++ Programs with Output For Students & Professionals - Aniket Pataskar
77/309
-
8/16/2019 100+ C++ Programs with Output For Students & Professionals - Aniket Pataskar
78/309
25. Simple Addition in C++ Binary Operator
Overloading Using C++ Programming
// Header Files
#include
#include
//Standard namespace declaration
using namespace std;
class overloading
{
int value;
public:
void setValue(int temp)
{
value = temp; }
overloading operator+(overloading ob)
{
overloading t;
t.value=value+ob.value;
return(t);
}
void display()
{
cout
-
8/16/2019 100+ C++ Programs with Output For Students & Professionals - Aniket Pataskar
79/309
int main()
{
overloading obj1,obj2,result;
int a,b;
couta>>b;
obj1.setValue(a);
obj2.setValue(b);
result = obj1+obj2;
cout
-
8/16/2019 100+ C++ Programs with Output For Students & Professionals - Aniket Pataskar
80/309
-
8/16/2019 100+ C++ Programs with Output For Students & Professionals - Aniket Pataskar
81/309
26. Simple Program for Multiple Inheritance
Using C++ Programming
To find out the student details using multiple inheritance.
ALGORITHM:
Step 1: Start the program.
Step 2: Declare the base class student.
Step 3: Declare and define the function get() to get the student details.
Step 4: Declare the other class sports.
Step 5: Declare and define the function getsm() to read the sports mark.
Step 6: Create the class statement derived from student and sports.
Step 7: Declare and define the function display() to find out the total and average.
Step 8: Declare the derived class object,call the functions get(),getsm() and display().
Step 9: Stop the program.
PROGRAM:#include
#include
class student
{
protected:
int rno,m1,m2;
public:
void get()
{
coutrno;
coutm1>>m2;
-
8/16/2019 100+ C++ Programs with Output For Students & Professionals - Aniket Pataskar
82/309
}
};
class sports
{
protected:
int sm; // sm = Sports mark
public:
void getsm()
{
coutsm;
}
};
class statement:public student,public sports
{
int tot,avg;
public:
void display()
{
tot=(m1+m2+sm);
avg=tot/3;
cout
-
8/16/2019 100+ C++ Programs with Output For Students & Professionals - Aniket Pataskar
83/309
obj.display();
getch();
}
Output:
Enter the Roll no: 100
Enter two marks
90
80
Enter the Sports Mark: 90
Roll No: 100 Total : 260
Average: 86.66
-
8/16/2019 100+ C++ Programs with Output For Students & Professionals - Aniket Pataskar
84/309
-
8/16/2019 100+ C++ Programs with Output For Students & Professionals - Aniket Pataskar
85/309
27. Simple Program for Inline Function Using
C++ Programming
ALGORITHM:
Step 1: Start the pogram.
Step 2: Declare the class.
Step 3: Declare and define the inline function for multiplication and cube.
Step 4: Declare the class object and variables.
Step 5: Read two values.
Step 6: Call the multiplication and cubic functions using class objects.
Step 7: Return the values.
Step 8: Display.
Step 9: Stop the program.
PROGRAM:
#include#include
class line
{
public:
inline float mul(float x,float y)
{
return(x*y);
}
inline float cube(float x)
{
return(x*x*x);
}
};
-
8/16/2019 100+ C++ Programs with Output For Students & Professionals - Aniket Pataskar
86/309
void main()
{
line obj;
float val1,val2;
clrscr();
coutval1>>val2;
cout
-
8/16/2019 100+ C++ Programs with Output For Students & Professionals - Aniket Pataskar
87/309
-
8/16/2019 100+ C++ Programs with Output For Students & Professionals - Aniket Pataskar
88/309
28. Simple Program for Function Template
Using C++ Programming
To swap the numbers using the concept of function template.
ALGORITHM:
STEP 1: Start the program.
STEP 2: Declare the template class.
STEP 3: Declare and define the functions to swap the values.
STEP 4: Declare and define the functions to get the values.
STEP 5: Read the values and call the corresponding functions.
STEP6: Display the results.
STEP 7: Stop the program.
PROGRAM:
#include
#include
template
void swap(t &x,t &y)
{
t temp=x;
x=y;
y=temp;
}
void fun(int a,int b,float c,float d)
{
cout
-
8/16/2019 100+ C++ Programs with Output For Students & Professionals - Aniket Pataskar
89/309
cout
-
8/16/2019 100+ C++ Programs with Output For Students & Professionals - Aniket Pataskar
90/309
-
8/16/2019 100+ C++ Programs with Output For Students & Professionals - Aniket Pataskar
91/309
29. Simple Program for Function
Overloading Using C++ Programming
ALGORITHM:
STEP 1: Start the program.
STEP 2: Declare the class name as fn with data members and member functions.
STEP 3: Read the choice from the user.
STEP 4: Choice=1 then go to the step 5.
STEP 5: The function area() to find area of circle with one integer argument.
STEP 6: Choice=2 then go to the step 7.
STEP 7: The function area() to find area of rectangle with two integer argument.
STEP 8: Choice=3 then go to the step 9.
STEP 9: The function area() to find area of triangle with three arguments, two as Integerand one as float.
STEP 10: Choice=4 then stop the program.
PROGRAM:
#include
#include
#include
#define pi 3.14
class fn
{
public:
void area(int); //circle
void area(int,int); //rectangle
void area(float ,int,int); //triangle
};
void fn::area(int a)
{
-
8/16/2019 100+ C++ Programs with Output For Students & Professionals - Aniket Pataskar
92/309
cout
-
8/16/2019 100+ C++ Programs with Output For Students & Professionals - Aniket Pataskar
93/309
cin>>a>>b;
obj.area(a,b);
break;
case 3:
couta>>b;
obj.area(0.5,a,b);
break;
case 4:
exit(0);
}
getch();
}
Output:
Function Overloading
1. Area of Circle
2. Area of Rectangle
3. Area of Triangle
4. Exit
Enter Your Choice: 2
Enter the Sides of the Rectangle: 5 5
Area of Rectangle is: 25
1. Area of Circle
2. Area of Rectangle
3. Area of Triangle
4. Exit
Enter Your Choice: 4
-
8/16/2019 100+ C++ Programs with Output For Students & Professionals - Aniket Pataskar
94/309
-
8/16/2019 100+ C++ Programs with Output For Students & Professionals - Aniket Pataskar
95/309
30. Simple Program for Friend Function
Using C++ Programming
ALGORITHM:
STEP 1: Start the program.
STEP 2: Declare the class name as Base with data members and member functions.
STEP 3: The function get() is used to read the 2 inputs from the user.
STEP 4: Declare the friend function mean(base ob) inside the class.
STEP 5: Outside the class to define the friend function and do the following.
STEP 6: Return the mean value (ob.val1+ob.val2)/2 as a float.
STEP 7: Stop the program.
PROGRAM:
#include
#include
class base{
int val1,val2;
public:
void get()
{
coutval1>>val2;
}
friend float mean(base ob);
};
float mean(base ob)
{
return float(ob.val1+ob.val2)/2;
}
-
8/16/2019 100+ C++ Programs with Output For Students & Professionals - Aniket Pataskar
96/309
void main()
{
clrscr();
base obj;
obj.get();
cout
-
8/16/2019 100+ C++ Programs with Output For Students & Professionals - Aniket Pataskar
97/309
-
8/16/2019 100+ C++ Programs with Output For Students & Professionals - Aniket Pataskar
98/309
31. Simple Program for Exception Handling
with Multiple Catch Using C++ Programming
ALGORITHM:
Step 1: Start the program.
Step 2: Declare and define the function test().
Step 3: Within the try block check whether the value is greater than zero or not.
a. if the value greater than zero throw the value and catch the corresponding exception.
b. Otherwise throw the character and catch the corresponding exception.
Step 4: Read the integer and character values for the function test().
Step 5: Stop the program.
PROGRAM:
#include
#include
void test(int x){
try
{
if(x>0)
throw x;
else
throw ‘x’;
}
catch(int x)
{
cout
-
8/16/2019 100+ C++ Programs with Output For Students & Professionals - Aniket Pataskar
99/309
catch(char x)
{
cout
-
8/16/2019 100+ C++ Programs with Output For Students & Professionals - Aniket Pataskar
100/309
-
8/16/2019 100+ C++ Programs with Output For Students & Professionals - Aniket Pataskar
101/309
32. Simple Program for Exception Handling
Divide by zero Using C++ Programming
ALGORITHM:
Step 1: Start the program.
Step 2: Declare the variables a,b,c.
Step 3: Read the values a,b,c,.
Step 4: Inside the try block check the condition.
a. if(a-b!=0) then calculate the value of d and display.
b. otherwise throw the exception.
Step 5: Catch the exception and display the appropriate message.
Step 6: Stop the program.
PROGRAM:
#include
#includevoid main()
{
int a,b,c;
float d;
clrscr();
couta;
coutb;
coutc;
try
{
if((a-b)!=0)
-
8/16/2019 100+ C++ Programs with Output For Students & Professionals - Aniket Pataskar
102/309
{
d=c/(a-b);
cout
-
8/16/2019 100+ C++ Programs with Output For Students & Professionals - Aniket Pataskar
103/309
-
8/16/2019 100+ C++ Programs with Output For Students & Professionals - Aniket Pataskar
104/309
33. Simple Program for Constructor Using
C++ Programming
ALGORITHM:
STEP 1: Start the program.
STEP 2: Declare the class as Prime with data members,
Member functions.
STEP 3: Consider the argument constructor Prime() with integer
Argument.
STEP 4: To cal the function calculate() and do the following steps.
STEP 5: For i=2 to a/2 do
STEP 6: Check if a%i==0 then set k=0 and break.
STEP 7: Else set k value as 1.
STEP 8: Increment the value i as 1.
STEP 9: Check whether the k value is 1 or 0.
STEP 10:If it is 1 then display the value is a prime number.STEP 11:Else display the value is not prime.
STEP 12:Stop the program.
#include
#include
class prime
{
int a,k,i;
public:
prime(int x)
{
a=x;
}
-
8/16/2019 100+ C++ Programs with Output For Students & Professionals - Aniket Pataskar
105/309
void calculate()
{
k=1;
{
for(i=2;i
-
8/16/2019 100+ C++ Programs with Output For Students & Professionals - Aniket Pataskar
106/309
cin>>a;
prime obj(a);
obj.calculate();
obj.show();
getch();
}
Output:
Enter the number: 7
Given number is Prime Number
-
8/16/2019 100+ C++ Programs with Output For Students & Professionals - Aniket Pataskar
107/309
34. Simple Program for Binary Operator
Overloading Using C++ Programming
ALGORITHM:
Step 1: Start the program.
Step 2: Declare the class.
Step 3: Declare the variables and its member function.
Step 4: Using the function getvalue() to get the two numbers.
Step 5: Define the function operator +() to add two complex numbers.
Step 6: Define the function operator –()to subtract two complex numbers.
Step 7: Define the display function.
Step 8: Declare the class objects obj1,obj2 and result.
Step 9: Call the function getvalue using obj1 and obj2
Step 10: Calculate the value for the object result by calling the function operator + andoperator -.
Step 11: Call the display function using obj1 and obj2 and result.
Step 12: Return the values.
Step 13: Stop the program.
PROGRAM:
#include
#include
class complex
{
int a,b;
public:
void getvalue()
{
couta>>b;
-
8/16/2019 100+ C++ Programs with Output For Students & Professionals - Aniket Pataskar
108/309
}
complex operator+(complex ob)
{
complex t;
t.a=a+ob.a;
t.b=b+ob.b;
return(t);
}
complex operator-(complex ob)
{
complex t;
t.a=a-ob.a;
t.b=b-ob.b;
return(t);
}
void display()
{
cout
-
8/16/2019 100+ C++ Programs with Output For Students & Professionals - Aniket Pataskar
109/309
result = obj1+obj2;
result1=obj1-obj2;
cout
-
8/16/2019 100+ C++ Programs with Output For Students & Professionals - Aniket Pataskar
110/309
-
8/16/2019 100+ C++ Programs with Output For Students & Professionals - Aniket Pataskar
111/309
35. Simple Program Book Entry Using
structure Variable in C++ Programming
#include
#include
struct books
{
char name[20],author[20];
}a[50];
int main()
{
int i,n;
coutn; cout
-
8/16/2019 100+ C++ Programs with Output For Students & Professionals - Aniket Pataskar
112/309
cout
-
8/16/2019 100+ C++ Programs with Output For Students & Professionals - Aniket Pataskar
113/309
-
8/16/2019 100+ C++ Programs with Output For Students & Professionals - Aniket Pataskar
114/309
36. Simple Program for Write File Operation
Using C++ Programming
ALGORITHM:
STEP 1: Start the program.
STEP 2: Declare the variables.
STEP 3: Read the file name.
STEP 4: open the file to write the contents.
STEP 5: writing the file contents up to reach a particular condition.
STEP 6: Stop the program.
PROGRAM:
#include
#include
#include
#includevoid main()
{
char c,fname[10];
ofstream out;
coutfname;
out.open(fname);
cout
-
8/16/2019 100+ C++ Programs with Output For Students & Professionals - Aniket Pataskar
115/309
}
Output:
Enter File name: one.txt
Enter contents to store in file (enter # at end)
Master of Computer Applications#
-
8/16/2019 100+ C++ Programs with Output For Students & Professionals - Aniket Pataskar
116/309
37. Simple Program for Virtual Functions
Using C++ Programming
ALGORITHM:
Step 1: Start the program.
Step 2: Declare the base class base.
Step 3: Declare and define the virtual function show().
Step 4: Declare and define the function display().
Step 5: Create the derived class from the base class.
Step 6: Declare and define the functions display() and show().
Step 7: Create the base class object and pointer variable.
Step 8: Call the functions display() and show() using the base class object and pointer.
Step 9: Create the derived class object and call the functions display() and show() usingthe derived class object and pointer.
Step 10: Stop the program.
PROGRAM:
#include
#include
class base
{
public:
virtual void show()
{
cout
-
8/16/2019 100+ C++ Programs with Output For Students & Professionals - Aniket Pataskar
117/309
class drive:public base
{
public:
void display()
{
cout
-
8/16/2019 100+ C++ Programs with Output For Students & Professionals - Aniket Pataskar
118/309
Output:
P points to Base
Base class display
Base class show
P points to Drive
Base class Display
Drive class Show
-
8/16/2019 100+ C++ Programs with Output For Students & Professionals - Aniket Pataskar
119/309
38. Simple Program for Virtual Base Class
Using C++ Programming
ALGORITHM:
Step 1: Start the program.
Step 2: Declare the base class student.
Step 3: Declare and define the functions getnumber() and putnumber().
Step 4: Create the derived class test virtually derived from the base class student.
Step 5: Declare and define the function getmarks() and putmarks().
Step 6: Create the derived class sports virtually derived from the base class student.
Step 7: Declare and define the function getscore() and putscore().
Step 8: Create the derived class result derived from the class test and sports.
Step 9: Declare and define the function display() to calculate the total.
Step 10: Create the derived class object obj.
Step 11: Call the function get number(),getmarks(),getscore() and display().
Step 12: Stop the program.
PROGRAM:
#include
#include
class student
{
int rno;
public:
void getnumber()
{
coutrno;
}
-
8/16/2019 100+ C++ Programs with Output For Students & Professionals - Aniket Pataskar
120/309
void putnumber()
{
cout
-
8/16/2019 100+ C++ Programs with Output For Students & Professionals - Aniket Pataskar
121/309
{
coutscore;
}
void putscore()
{
cout
-
8/16/2019 100+ C++ Programs with Output For Students & Professionals - Aniket Pataskar
122/309
getch();
}
Output:
Enter Roll No: 200
Enter Marks
Part1: 90
Part2: 80
Enter Sports Score: 80
Roll No: 200
Marks Obtained
Part1: 90
Part2: 80
Sports Score is: 80
Total Score is: 250
-
8/16/2019 100+ C++ Programs with Output For Students & Professionals - Aniket Pataskar
123/309
-
8/16/2019 100+ C++ Programs with Output For Students & Professionals - Aniket Pataskar
124/309
39. Simple Program for Unary Operator
Overloading Using C++ Programming
To write a program to find the complex numbers using unary operator overloading.
ALGORITHM:
Step 1: Start the program.
Step 2: Declare the class.
Step 3: Declare the variables and its member function.
Step 4: Using the function getvalue() to get the two numbers.
Step 5: Define the function operator ++ to increment the values
Step 6: Define the function operator - -to decrement the values.
Step 7: Define the display function.
Step 8: Declare the class object.
Step 9: Call the function getvalue
Step 10: Call the function operator ++() by incrementing the class object and call thefunction display.
Step 11: Call the function operator - -() by decrementing the class object and call thefunction display.
Step 12: Stop the program.
PROGRAM:
#include
#include
class complex
{
int a,b,c;
public: complex(){}
void getvalue()
-
8/16/2019 100+ C++ Programs with Output For Students & Professionals - Aniket Pataskar
125/309
{
couta>>b;
}
void operator++()
{
a=++a;
b=++b;
}
void operator—()
{
a=—a;
b=—b;
}
void display()
{
cout
-
8/16/2019 100+ C++ Programs with Output For Students & Professionals - Aniket Pataskar
126/309
obj.display();
obj—;
cout
-
8/16/2019 100+ C++ Programs with Output For Students & Professionals - Aniket Pataskar
127/309
-
8/16/2019 100+ C++ Programs with Output For Students & Professionals - Aniket Pataskar
128/309
40. Simple Program for Static Data and
Member Function Using C++ Programming
ALGORITHM:
STEP 1: Start the program.
STEP 2: Declare the class name as Stat with data member s and member functions.
STEP 3: The constructor Stat() which is used to increment the value of count as 1 to toassign the variable code.
STEP 4: The function showcode() to display the code value.STEP 5: The function showcount() to display the count value.
STEP 6: Stop the program.
PROGRAM:
#include
#include
class stat
{
int code;
static int count;
public:
stat()
{
code=++count;
}
void showcode()
{
cout
-
8/16/2019 100+ C++ Programs with Output For Students & Professionals - Aniket Pataskar
129/309
static void showcount()
{
cout
-
8/16/2019 100+ C++ Programs with Output For Students & Professionals - Aniket Pataskar
130/309
-
8/16/2019 100+ C++ Programs with Output For Students & Professionals - Aniket Pataskar
131/309
41. Simple Program for Single Inheritance
Using C++ Programming
ALGORITHM:
Step 1: Start the program.
Step 2: Declare the base class emp.
Step 3: Define and declare the function get() to get the employee details.
Step 4: Declare the derived class salary.
Step 5: Declare and define the function get1() to get the salary details.
Step 6: Define the function calculate() to find the net pay.
Step 7: Define the function display().
Step 8: Create the derived class object.
Step 9: Read the number of employees.
Step 10: Call the function get(),get1() and calculate() to each employees.
Step 11: Call the display().
Step 12: Stop the program.
PROGRAM:PAYROLL SYSTEM USING SINGLE INHERITANCE
#include
#include
class emp
{
public:
int eno;
char name[20],des[20];
void get()
{
couteno;
-
8/16/2019 100+ C++ Programs with Output For Students & Professionals - Aniket Pataskar
132/309
coutname;
coutdes;
}
};
class salary:public emp
{
float bp,hra,da,pf,np;
public:
void get1()
{
coutbp;
couthra;
coutda;
coutpf;
}
void calculate() {
np=bp+hra+da-pf;
}
void display()
{
cout
-
8/16/2019 100+ C++ Programs with Output For Students & Professionals - Aniket Pataskar
133/309
void main()
{
int i,n;
char ch;
salary s[10];
clrscr();
coutn;
for(i=0;i
-
8/16/2019 100+ C++ Programs with Output For Students & Professionals - Aniket Pataskar
134/309
Enter the Dearness allowance: 500
Enter the profitability Fund: 300
E.No E.name des BP HRA DA PF NP
150 ram Manager 5000 1000 500 300 6200
-
8/16/2019 100+ C++ Programs with Output For Students & Professionals - Aniket Pataskar
135/309
42. Simple Program for Copy Constructor
Using C++ Programming
ALGORITHM:
STEP 1: Start the program.
STEP 2: Declare the class name as Copy with data members and member functions.
STEP 3: The constructor Copy() with argument to assign the value.
STEP 4: To cal the function calculate() do the following steps.
STEP 5: For i=1 to var do
STEP 6: Calculate fact*i to assign to fact.
STEP 7: Increment the value as 1.
STEP 8: Return the value fact.
STEP 9: Print the result.
STEP 10: Stop the program.
PROGRAM:
#include
#include
class copy
{
int var,fact;
public:
copy(int temp)
{
var = temp;
}
double calculate()
{
fact=1;
-
8/16/2019 100+ C++ Programs with Output For Students & Professionals - Aniket Pataskar
136/309
for(int i=1;i
-
8/16/2019 100+ C++ Programs with Output For Students & Professionals - Aniket Pataskar
137/309
-
8/16/2019 100+ C++ Programs with Output For Students & Professionals - Aniket Pataskar
138/309
43. Simple Program for Read File Operation
Using C++ Programming
Program for read the content of a file.
ALGORITHM:
STEP 1: Start the program.
STEP 2: Declare the variables.
STEP 3: Get the file name to read.
STEP 4: Using ifstreamin(filename) check whether the file exist.
STEP 5: If the file exist then check for the end of file condition.
STEP 6: Read the contents of the file.
STEP 7: Print the contents of the file.
STEP 8: Stop the program.
PROGRAM:#include
#include
#include
void main()
{
char c,fname[10];
clrscr();
coutfname;
ifstream in(fname);
if(!in)
{
cout
-
8/16/2019 100+ C++ Programs with Output For Students & Professionals - Aniket Pataskar
139/309
return;
}
cout
-
8/16/2019 100+ C++ Programs with Output For Students & Professionals - Aniket Pataskar
140/309
-
8/16/2019 100+ C++ Programs with Output For Students & Professionals - Aniket Pataskar
141/309
44. Simple Program for Read & Write File
Operation (Convert lowercase to uppercase)
Using C++ Programming
ALGORITHM:
STEP 1: Start the program.
STEP 2: Declare the variables.
STEP 3: Read the file name.
STEP 4: open the file to write the contents.
STEP 5: writing the file contents up to reach a particular condition.
STEP6: write the file contents as uppercase.
STEP7: open the file to read the contents.
STEP 8: Stop the program.
PROGRAM:
#include
#include
#include
#include
#include
#include
void main(){
char c,u;
char fname[10];
clrscr();
ofstream out;
coutfname;
out.open(fname);
-
8/16/2019 100+ C++ Programs with Output For Students & Professionals - Aniket Pataskar
142/309
cout
-
8/16/2019 100+ C++ Programs with Output For Students & Professionals - Aniket Pataskar
143/309
-
8/16/2019 100+ C++ Programs with Output For Students & Professionals - Aniket Pataskar
144/309
45. C++ program which adds two integers
#include
main() {
// A simple C++ program
int x, y, sum;
cout x;
cout > y;
sum = x + y;
cout
-
8/16/2019 100+ C++ Programs with Output For Students & Professionals - Aniket Pataskar
145/309
-
8/16/2019 100+ C++ Programs with Output For Students & Professionals - Aniket Pataskar
146/309
46. C++ Program to output an integer, a
floating point number and a character
#include
#include
void main()
{
clrscr();
int x = 10;
float y = 10.1;
char z = ‘a’;
cout
-
8/16/2019 100+ C++ Programs with Output For Students & Professionals - Aniket Pataskar
147/309
z = a
-
8/16/2019 100+ C++ Programs with Output For Students & Professionals - Aniket Pataskar
148/309
47. C++ Program to find the sum, difference,
product and quotient of two integers
#include
#include
void main()
{
clrscr();
int x = 10;
int y = 2;
int sum, difference, product, quotient;
sum = x + y;
difference = x - y;
product = x * y;
quotient = x / y; cout
-
8/16/2019 100+ C++ Programs with Output For Students & Professionals - Aniket Pataskar
149/309
OUTPUT :
The sum of 10 & 2 is 12.
The difference of 10 & 2 is 8.
The product of 10 & 2 is 20.
The quotient of 10 & 2 is 5.
-
8/16/2019 100+ C++ Programs with Output For Students & Professionals - Aniket Pataskar
150/309
-
8/16/2019 100+ C++ Programs with Output For Students & Professionals - Aniket Pataskar
151/309
48. Program to enter two integers and find
their sum and average
#include
#include
#include
void main()
{
clrscr();
int x,y,sum;
float average;
cout x>>y;
sum=x+y;
average=sum/2;cout
-
8/16/2019 100+ C++ Programs with Output For Students & Professionals - Aniket Pataskar
152/309
The sum of 8 and 6 is 14.
The average of 8 and 6 is 7.
-
8/16/2019 100+ C++ Programs with Output For Students & Professionals - Aniket Pataskar
153/309
49. Program to enter velocity, acceleration
and time and print final velocity using the
formula : v = u + a * t
#include
#include
void main()
{
clrscr();
int v,u,a,t;
cout u>>a>>t;
v=u+a*t;
cout
-
8/16/2019 100+ C++ Programs with Output For Students & Professionals - Aniket Pataskar
154/309
-
8/16/2019 100+ C++ Programs with Output For Students & Professionals - Aniket Pataskar
155/309
50. Program to enter your age and print if
you should be in grade 10
#include
#include
void main()
{
clrscr();
int age;
cout age;
if(age==16)
{
cout
-
8/16/2019 100+ C++ Programs with Output For Students & Professionals - Aniket Pataskar
156/309
INPUT :
15
OUTPUT :
Your present age is 15 years.
You are not of the right age for joining grade 10 !
-
8/16/2019 100+ C++ Programs with Output For Students & Professionals - Aniket Pataskar
157/309
-
8/16/2019 100+ C++ Programs with Output For Students & Professionals - Aniket Pataskar
158/309
51. Program to enter an integer and print if it
is greater or less than 100
#include
#include
void main(){
clrscr();
int x;
cout x;
if(x>100)
{
cout
-
8/16/2019 100+ C++ Programs with Output For Students & Professionals - Aniket Pataskar
159/309
OUTPUT :
74 is less than 100.
-
8/16/2019 100+ C++ Programs with Output For Students & Professionals - Aniket Pataskar
160/309
52. Program to enter an integer and output its
15 multiples
#include
#include
int main()
{
clrscr();
int x;
cout >x;
cout
-
8/16/2019 100+ C++ Programs with Output For Students & Professionals - Aniket Pataskar
161/309
12x4=48
12x5=60
12x6=72
12x7=84
12x8=96
12x9=108
12x10=120
12x11=132
12x12=144
12x13=156
12x14=168
12x15=180
-
8/16/2019 100+ C++ Programs with Output For Students & Professionals - Aniket Pataskar
162/309
-
8/16/2019 100+ C++ Programs with Output For Students & Professionals - Aniket Pataskar
163/309
53. Program to enter two integers and print
the quotient and remainder
#include
#include
int main()
{
clrscr();
int x,y,quotient,remainder;
cout >x>>y;
quotient=x/y;
remainder=x-(quotient*y);
cout
-
8/16/2019 100+ C++ Programs with Output For Students & Professionals - Aniket Pataskar
164/309
-
8/16/2019 100+ C++ Programs with Output For Students & Professionals - Aniket Pataskar
165/309
54. Program to enter an integer and find out
if it is even or odd
#include
#include
void main()
{
clrscr();
int x;
cout >x;
if(x%2==0)
cout
-
8/16/2019 100+ C++ Programs with Output For Students & Professionals - Aniket Pataskar
166/309
-
8/16/2019 100+ C++ Programs with Output For Students & Professionals - Aniket Pataskar
167/309
55.Program to change the foreground colors
and draw circles on the screen
#include
#include
#include
#include
void main (int)
{
int gdriver=DETECT,gmode,errorcode; //Requesting auto-detection.
int midx,midy,x;
//Initializing graphics and local variables.
initgraph(&gdriver,&gmode,“d:\bc3\bgi”);
//Reading result of initialization.
errorcode=graphresult();if(errorcode!=grOk)
//An error occurred.
{
printf(“Graphics error occurred : %s \n”,grapherrormsg(errorcode));
printf(“Press any key to stop : “);
getch();
exit(1); //Terminate the program due to error.
}
/*Changing the foreground color.
Note : Press enter to exit the last screen as it is black and
it may appear as if the program has stopped running.*/
for(x=15;x>=0;x—)
{
setcolor(x);
-
8/16/2019 100+ C++ Programs with Output For Students & Professionals - Aniket Pataskar
168/309
circle(20+(x*40),200,15);/*Changing x-coordinate by 50 each time so that
the circles do not overlap.*/
getch();
}
cleardevice(); //Clearing the screen in the graphics mode.
circle(200,200,50);
getch();
closegraph();
}
This graphics program changes the foreground colors on the screen gradually from white
to black, in-turn drawing circles of that foreground color, using the ‘setcolor’ command.
-
8/16/2019 100+ C++ Programs with Output For Students & Professionals - Aniket Pataskar
169/309
-
8/16/2019 100+ C++ Programs with Output For Students & Professionals - Aniket Pataskar
170/309
56. Program to draw 2 rectangles and fill 1 of
them
#include
#include
#include
#include
#include
#include
void main()
{
clrscr();
int gd = DETECT,gm,errorcode; //Requesting auto-detection.
//Initializing graphics and local variables.
initgraph (&gd, &gm, “d:\bc3\bgi”); //Path where graphics drivers are installed
//Read result of initialization.
errorcode = graphresult();
//An error occured.
if (errorcode != grOk)
{
cout
-
8/16/2019 100+ C++ Programs with Output For Students & Professionals - Aniket Pataskar
171/309
/*Drawing a rectangle having top LHS vertex at (300, 300)
and bottom RHS vertex at (600, 400)*/
rectangle(300, 300, 600, 400);
rectangle(100, 100, 200, 200);
getch();
floodfill(120, 120, WHITE);
getch();
closegraph();
}
This graphics program draws two rectangles, but fills in only one of them with a whitecolor using the ‘floodfill’ command.
-
8/16/2019 100+ C++ Programs with Output For Students & Professionals - Aniket Pataskar
172/309
-
8/16/2019 100+ C++ Programs with Output For Students & Professionals - Aniket Pataskar
173/309
57. Program to enter three integers and
output the biggest integer
#include
#include
int main()
{
clrscr();
int x,y,z,biggest;
cout >x>>y>>z;
biggest=x>y?(x>z?x:z):(y>z?y:z);
cout
-
8/16/2019 100+ C++ Programs with Output For Students & Professionals - Aniket Pataskar
174/309
79
-
8/16/2019 100+ C++ Programs with Output For Students & Professionals - Aniket Pataskar
175/309
58. Program to draw circles
#include #include
#include
#include
#include
#include
void main()
{
clrscr();
int gd=DETECT,gm,errorcode; //Requesting auto-detection.
//Initializing graphics and local variables.
initgraph(&gd,&gm,“d:\bc3\bgi”); //Path where graphics drivers are installed
//Reading result of initialization.
errorcode=graphresult();
//An error occured.
if (errorcode!=grOk)
{
cout
-
8/16/2019 100+ C++ Programs with Output For Students & Professionals - Aniket Pataskar
176/309
getch();
closegraph();
}
Output :
This graphics program draws three circles on the screen.
-
8/16/2019 100+ C++ Programs with Output For Students & Professionals - Aniket Pataskar
177/309
-
8/16/2019 100+ C++ Programs with Output For Students & Professionals - Aniket Pataskar
178/309
59. Program to change the background colors
on the screen
#include
#include
#include
#include
void main (int)
{
int gdriver=DETECT,gmode,errorcode; //Requesting auto-detection.
int midx,midy,x;
//Initializing graphics and local variables.
initgraph(&gdriver,&gmode,“d:\bc3\bgi”);
//Reading result of initialization.errorcode=graphresult();
if(errorcode!=grOk)
//An error occurred.
{
printf(“Graphics error occurred : %s \n”,grapherrormsg(errorcode));
printf(“Press any key to stop : “);
getch();
exit(1); //Terminate the program due to error.
}
/*Changing the background color.
Note : Press enter to see the first screen as it is black and
it may appear as if the program has stopped running.*/
for(x=0;x
-
8/16/2019 100+ C++ Programs with Output For Students & Professionals - Aniket Pataskar
179/309
setbkcolor(x);
getch();
}
closegraph();
}
Output :
This graphics program changes the background colors on the screen gradually from black
to white using the ‘setbkcolor’ command.
-
8/16/2019 100+ C++ Programs with Output For Students & Professionals - Aniket Pataskar
180/309
-
8/16/2019 100+ C++ Programs with Output For Students & Professionals - Aniket Pataskar
181/309
60. Program to enter three integers and
output the biggest integer using IF
#include
#include
int main()
{
clrscr();
int x,y,z,biggest;
cout >x>>y>>z;
biggest=x;
if(y>biggest)
biggest=y;
if(z>biggest)biggest=z;
cout
-
8/16/2019 100+ C++ Programs with Output For Students & Professionals - Aniket Pataskar
182/309
OUTPUT :
The biggest integer out of the 3 integers you typed 32, 47 & 44 is :
47
-
8/16/2019 100+ C++ Programs with Output For Students & Professionals - Aniket Pataskar
183/309
61. Program to enter an integer and print out
its successor
#include
#include
void value(int);
void main()
{
clrscr();
int x;
cout >x;
cout
-
8/16/2019 100+ C++ Programs with Output For Students & Professionals - Aniket Pataskar
184/309
OUTPUT :
The successor of 49 is 50.
-
8/16/2019 100+ C++ Programs with Output For Students & Professionals - Aniket Pataskar
185/309
62. Program to enter an integer and output
the cube of that integer
#include
#include
int cube(int x); //The prototype.
void main()
{
clrscr();
int a;
cout >a;
cout
-
8/16/2019 100+ C++ Programs with Output For Students & Professionals - Aniket Pataskar
186/309
OUTPUT :
The cube of 8 is : 512
-
8/16/2019 100+ C++ Programs with Output For Students & Professionals - Aniket Pataskar
187/309
63. Program to enter a string and find its
length
#include
#include
#include
void main()
{
clrscr();
int slength;
char x[81]; //Allowing the user to input a maximum of 80 characters.
cout x;
slength=strlen(x);
cout
-
8/16/2019 100+ C++ Programs with Output For Students & Professionals - Aniket Pataskar
188/309
-
8/16/2019 100+ C++ Programs with Output For Students & Professionals - Aniket Pataskar
189/309
64. Program to switch between different cases
#include
#include
int main()
{
clrscr();
int choice;
cout
-
8/16/2019 100+ C++ Programs with Output For Students & Professionals - Aniket Pataskar
190/309
The appropriate message is then outputted using the ‘cout’ command.
INPUT :
3
OUTPUT :
You chose to play…playing too much everyday is bad.
-
8/16/2019 100+ C++ Programs with Output For Students & Professionals - Aniket Pataskar
191/309
-
8/16/2019 100+ C++ Programs with Output For Students & Professionals - Aniket Pataskar
192/309
-
8/16/2019 100+ C++ Programs with Output For Students & Professionals - Aniket Pataskar
193/309
INPUT :
1000 5 3
OUTPUT :
Principal = $1000
Rate = 5%
Time = 3 years
Simple Interest = $150
-
8/16/2019 100+ C++ Programs with Output For Students & Professionals - Aniket Pataskar
194/309
-
8/16/2019 100+ C++ Programs with Output For Students & Professionals - Aniket Pataskar
195/309
66. Program to enter an integer and print if it
is prime or composite
#include
#include
#include
void main()
{
clrscr();
int num1,x;
cout num1;
for(x=2;x
-
8/16/2019 100+ C++ Programs with Output For Students & Professionals - Aniket Pataskar
196/309
This program takes in an integer num1 as a screen input from the user.
It then determines whether the integer is prime or composite.
It finally outputs the approriate message by writing to the ‘cout’ stream.
INPUT :
23
OUTPUT :
23 is a prime number.
-
8/16/2019 100+ C++ Programs with Output For Students & Professionals - Aniket Pataskar
197/309
-
8/16/2019 100+ C++ Programs with Output For Students & Professionals - Aniket Pataskar
198/309
67. Program to enter the sale value and print
the agent’s commission
#include
#include
void main()
{
clrscr();
long int svalue;
float commission;
cout svalue;
if(svalue
-
8/16/2019 100+ C++ Programs with Output For Students & Professionals - Aniket Pataskar
199/309
cout
-
8/16/2019 100+ C++ Programs with Output For Students & Professionals - Aniket Pataskar
200/309
-
8/16/2019 100+ C++ Programs with Output For Students & Professionals - Aniket Pataskar
201/309
68. Program to enter the unit reading and
output the customer’s telephone bill
#include
#include
int main()
{
clrscr();
long int units,charge=0;
float total;
const int rent=25;
cout >units;
if(units>200)
charge=(units-200)*20+150*40+50*60;else if(units>50)
charge=(units-50)*40+50*60;
else
charge=units*60;
total=0.01*charge+rent;
cout
-
8/16/2019 100+ C++ Programs with Output For Students & Professionals - Aniket Pataskar
202/309
A compulsory fee of $25, plus
60 cents per unit for the first 50 units,
40 cents per unit for the next 150 units,
20 cents per unit for anything above 200 units.
It then outputs the bill using the ‘cout’ command.
INPUT :
250
OUTPUT :
You have used 250 units.
Your total bill is $125
-
8/16/2019 100+ C++ Programs with Output For Students & Professionals - Aniket Pataskar
203/309
-
8/16/2019 100+ C++ Programs with Output For Students & Professionals - Aniket Pataskar
204/309
69. Program to find the total days in the year
till date
#include
#include
void main()
{
clrscr();
int day,month,total;
int days_per_month[12]={31,28,31,30,31,30,31,31,30,31,30,31};
cout month;
cout day;total=day;
for(int x=0;x
-
8/16/2019 100+ C++ Programs with Output For Students & Professionals - Aniket Pataskar
205/309
OUTPUT :
The number of days in this year till date = 163
-
8/16/2019 100+ C++ Programs with Output For Students & Professionals - Aniket Pataskar
206/309
70. Program to compute the fibonacci series
#include
#include
void main()
{
clrscr();
int a,b,x,y,num1,ct;a=0;
b=1;
cout num1;
cout
-
8/16/2019 100+ C++ Programs with Output For Students & Professionals - Aniket Pataskar
207/309
INPUT :
12
OUTPUT :
0
1
1
2
3
5
8
13
21
34
55
89
-
8/16/2019 100+ C++ Programs with Output For Students & Professionals - Aniket Pataskar
208/309
-
8/16/2019 100+ C++ Programs with Output For Students & Professionals - Aniket Pataskar
209/309
71. Program to enter an integer and print its
total value based on the formula
-
8/16/2019 100+ C++ Programs with Output For Students & Professionals - Aniket Pataskar
210/309
‘x - 1/3!x^3 + 1/5!x^5 - 1/7!x^7 + 1/9!x^9’
#include
#include
#include
int main()
{
clrscr();
float factorial=1;float num,tot,term,total;
int i,n=20,index,j=1;
cout >num;
tot=num;
total=num;
for(i=2,index=3;i
-
8/16/2019 100+ C++ Programs with Output For Students & Professionals - Aniket Pataskar
211/309
1/7!x^7 + 1/9!x^9.
It then outputs the final answer using the ‘cout’ command.
INPUT :
3
OUTPUT :
Total = 0.14112
-
8/16/2019 100+ C++ Programs with Output For Students & Professionals - Aniket Pataskar
212/309
-
8/16/2019 100+ C++ Programs with Output For Students & Professionals - Aniket Pataskar
213/309
72. Program to convert days into years and
weeks
#include
#include
void main()
{
clrscr();
int days,years,weeks,num1;
cout days;
years=days/365;
num1=days-(years*365);
weeks=days/7;num1=days-(weeks*7);
cout
-
8/16/2019 100+ C++ Programs with Output For Students & Professionals - Aniket Pataskar
214/309
789 days =
112 weeks OR
2 years.
-
8/16/2019 100+ C++ Programs with Output For Students & Professionals - Aniket Pataskar
215/309
73. Program to find the roots of a quadratic
equation
#include
#include
#include
int main()
{
clrscr();
float a,b,c,d,root1,root2;
cout a>>b>>c;
if(!a){
if(!b)
cout
-
8/16/2019 100+ C++ Programs with Output For Students & Professionals - Aniket Pataskar
216/309
getch();
return 0;
}
This program takes in the values of the three coefficients a, b, and c as a screen input fromthe user.
It then determines the roots of the quadratic equation using the formula ax^2 + bx + c = 0.
The two roots are then outputted using the ‘cout’ command.
INPUT :
4 4 -3
OUTPUT :
The first root = 0.5
The second root = -1.5
-
8/16/2019 100+ C++ Programs with Output For Students & Professionals - Aniket Pataskar
217/309
-
8/16/2019 100+ C++ Programs with Output For Students & Professionals - Aniket Pataskar
218/309
74. Program to enter an integer and output it
in the reversed form
#include
#include
void main()
{
clrscr();
long int num1,num2,rnum=0;
cout num1;
num2=num1;
do
{rnum=rnum*10;
int digit=num1%10;
rnum+=digit;
num1/=10;
}
while(num1);
cout
-
8/16/2019 100+ C++ Programs with Output For Students & Professionals - Aniket Pataskar
219/309
987
OUTPUT :
The integer you typed is 987.
The reversed integer is 789.
-
8/16/2019 100+ C++ Programs with Output For Students & Professionals - Aniket Pataskar
220/309
75. Program to count the number of words
and characters in a sentence
#include
#include
void main()
{
clrscr();
int countch=0;
int countwd=1;
cout
-
8/16/2019 100+ C++ Programs with Output For Students & Professionals - Aniket Pataskar
221/309
INPUT :
this is a nice program
OUTPUT :
Words = 5
Characters = 18
-
8/16/2019 100+ C++ Programs with Output For Students & Professionals - Aniket Pataskar
222/309
-
8/16/2019 100+ C++ Programs with Output For Students & Professionals - Aniket Pataskar
223/309
76. write program to take the salary of the
employee as a screen input from the user.
-
8/16/2019 100+ C++ Programs with Output For Students & Professionals - Aniket Pataskar
224/309
It then deducts the income tax from the salary
on the following basis :
-
8/16/2019 100+ C++ Programs with Output For Students & Professionals - Aniket Pataskar
225/309
-
8/16/2019 100+ C++ Programs with Output For Students & Professionals - Aniket Pataskar
226/309
20% income tax if the salary is between $7000
and $15000.
-
8/16/2019 100+ C++ Programs with Output For Students & Professionals - Aniket Pataskar
227/309
10% income tax if the salary is below $7000.
#include
#include
void main()
{
clrscr();
int itrate;float salary,itax,nsalary=0;
cout >salary;
if(salary>15000)
{
itax=salary*30/100;
itrate=30;
}
else if(salary>=7000)
{
itax=salary*20/100;
itrate=20;
}
else
{
itax=salary*10/100;
itrate=10;
}
nsalary=salary-itax;
cout
-
8/16/2019 100+ C++ Programs with Output For Students & Professionals - Aniket Pataskar
228/309
cout
-
8/16/2019 100+ C++ Programs with Output For Students & Professionals - Aniket Pataskar
229/309
77. Program to find the sum of each row &
column of a matrix of size n x