cout<<"Old";
else
cout<<"Young”;
}
5. Write a program to calculate division obtained by a student with his percentage
mark given.
Solution:
#include
void main()
{
mark;
cout<<"Enter mark”;
cin>>mark;
if(mark>=60)
cout<<" Ist Division”;
else if(mark>=50 && mark<60)
cout<<"2nd Division”;
else if(mark>=40 && mark<50)
cout<<"3rd Division”;
else
cout<<"Fail”;
}
6. Write a program to illustrate the use of switch case statements,
Solution:
#include
void main()
for(int
switch(i)
(
case 0:
cout<<"i is zero,
break;
case |
cout<<"i is one.”
break;
break;
default:
cout<<"i is greater than 3.”;
}
d7. Write a program to print the message “C+ is good” 10 times using while loop.
Solution:
#include
void main()
{
int n=0;
while (n <10)
{
cout<<"C+ is good”;
net;
}
}
8. Write a program to check whether a number is prime or not.
Solution:
#include
void main()
{
int n, i
cout<<"Enter a number:”;
cin>>
for(i=2; icn; i++)
{
if(n%i==0)
{
cout<<"Number is not Prime”;
cout<<"Number is Prime”;
}
9. The marks obtained by a student in 5 different subjects are input through
the keyboard. The student gets a nas per the following rules:
Percentage above or equal to 60 - First division
Percentage between 50 and 59 - Second division
Percentage between 40 and 49 - Third division
Percentage less than 40 — Fail. Write a program to calculate the division obtained by the
student.
Solution:
#include
void main( )
{
int m1, m2, m3, m4, m5, per ;
cout<< "Enter marks in five subjects " ;
cin>>m1>>m2>>m3>>m4>>m5 ;
per =(ml +m2+m3+m4+m5)/5;
if (per >= 60)
{cout<< "First division” ;
7
else if (( per >= 50 ) && ( per <60) )
{
cout<< "Second division" ;
}
else if (( per >=40 ) && ( per <50))
{
cout<<"Third division" ;
}
else
{
cout<< "Fail" ;
}
}
10. Write a program to find the roots of a quadratic equation ax?+bx+c=0, where the
values of coefficient a, b and c are given from the keyboard.
Solution:
#include
#include
void main()
{
int a, b,c, ds
float rl, 12, x, ys
cout<<“Enter the coefficients”;
cin>>a>>b>>¢;
d=(b*b) - (4*a*c);
if(d>0)
{
r1=(-b+pow(d,0.5))/2*a;
12=(-b- pow(d,0.5))/2"a;
cout<<"Roots are real and the roots are "<
void main()
{
int a, b, c;
cout<<“Enter 3 sides”;
cin>>a>>b>>¢;
Iflatb>c && b+e>a aét cHa>b)
{
if((a*a+b*b}
{
cout<<"I
1
else if( (a==b &&b!=c) || (b==c&&c!=a) |] (
{ cout<<“Isosceles”;
}
else iffa
{
cout<<“Equilateral”;
}
else
{
cout<<“Any Valid Triangle”;
}
}
else
{
cout<<"Cannot form a valid triangle”;
}
}
=c%e || (b*b4e*e)==a*a II (Cera
*b)
ight Angled”;
&&
12, Compute 142+3+4+....
Solution:
#include
void main()
..tn for a given value of n.
cout<<“Enter the value of n”;
cin>>n;
for(i=1; i<=n; i++)
{
sti;
}
couts< s;
i13. Write a program to check whether a no is palindrome number or not. (A number is
said to be palindrome number if it is equal
Solution:
#include
void main()
{
int a, n, n=O, b;
cout<<“Enter a number";
cin>>n;
while(n!=0)
{
a=n%10;
cout<<"palindrome”;
}
else
{
cout<<“not palindrome”;
}
}
14, Write a program to reverse a number.
#include
void main()
{
int a, n, m=0;
cout<<“Enter a number”;
cin>>n;
while(n!=0)
{
a=n%10;
m* 10+;
n=n/10;
}
cout<
void main()
{
int a, n.s=0,
cout<<“Enter a number”;cin>>n;
ben
while(n!=0)
: n%10;
sestataa;
n=n/10;
}
if(b==s)
{
cout<<“Armstrong”;
}
else
{
cout<<"Not Armstrong”;
16, Write a program to print all the prime numbers from 1 to 500.
Solution:
#include
void main( )
{
int n, i
for(n:
{
for(i=:
{
if(n%i==0)
{
break ;
}
}
if(i
{
cout<
nt, int);
swaptx, ys
coutc
void swap(int *, int *);
void main()
{
int x=5, y
swap(&x, &y);
cout<
using namespace std
class Test
C
private:
int datal:
float data2;
public:
void insertIntegerData(int d)
{
datal = d;
cout << "Number: " << datal;
}
float insertFloatData()
{
cout << "\nEnter dat:
cin >> data2;
return data2;
}
kint main()
{
Test ol, 02;
float secondDataOfObject2;
1 insertIntegerData( 12);
secondDataOfObject2 = 02.insertFloatData();
cout << "You entered " << secondDataOfObject2;
return 0;
}
19. Program to Enter Students Details and
Example:
#include
using namespace std;
lay it
class stud
{
publ
char name[30},clas{10};
int rol,age;
void enter()
{
cout<<"Enter Student Name: "; cin>>name;
cout<<"Enter Student Age: "; cin>>age;
cout<<"Enter Student Roll number: "; cin>>rol;
cout<<"Enter Student Class: "; cin>>clas;
}
void display)
{
cout<<"\n Age\tName\tR.No.\tClas
cout<<"\n"<
using namespace std1/ class definition
class Circle
{
public:
double radius;
double compute_area()
(
return 3.14*radius*radius;
I
bi
1/ main function
int main()
{
Circle obj;
// accessing public datamember out
cout << "Radius is: " << obj.radius << "\
cout << "Area is: " << obj.compute_area();
return 0;
}
21. // C++ program to demonstrate private access modifier
#include
using namespace std;
class Circle
{
1! private data member
private:
double radius;
1! public member function
double compute_area()
(// member function can access private
1/ data member radius
return 3.14#radius*radius;
}
bb
1/ main function
int main()
{
// creating object of the classCircle obj;
// trying to access private data member
// directly outside the class
obj.radius = 1.5;
cout << "Area is:" << obj.compute_area();
return 0;
J
22. // C++ program to demonstrate protected access modifier
#include
using namespace std;
J/ base class
class Parent
{
// protected data members
protected:
int id_protected;
hk
J/ sub class or derived class
class Child : public Parent
{
public:
void setfd(int id)
{
1 Child class is able to access the inherited
/ protected data members of base clas
id_protected = id;
}
void displayld,
{
cout << "id_protected is: " << id_protected << endl;
}
¥
1 main function
int maing) {
Child obj;// member function of the derived class can
// access the protected data members of the base class
obj setld(81);
obj] displayld0;
return 0;
}
23. Example: Multiply two matrices without using functions
#include
using namespace std;
main()
int a[10]{10}, b{10][10], mult[10][10}, rl, cl, 12, €2, i,j.
cout << "Enter rows and columns for first matrix: ";
cin >> rl >> cl;
cout << "Enter rows and columns for second matrix:
cin >> 12 >> 2;
/(If column of first matrix in not equal to row of second matrix,
/Task the user to enter the size of matrix again,
while (cl!=r2)
{
cout << "Error! column of first matrix not equal to row of second.
cout << "Enter rows and columns for first matrix: ";
cin >> rl >> cl;
cout << "Enter rows and columns for second matrix:
cin >> 12 >> ¢2;
J
1! Storing elements of first matrix,
cout << endl << "Enter elements of matrix 1:" << endl;
fori = 0; i< rl; +#i)
for(j = 0; j > afil{il;
}
/{ Storing elements of second matrix.
cout << endl << "Enter elements of matrix 2:" << endl;
for(i = 0; i < 12; ++i)
for(j = 0; j > bliJ[j}:
}
//Tnitializing elements of matrix mult to 0.
for(i =0;i
using namespace std;
int main()
{
intr, ¢, a[100][100}, b[100][100], sum[100]{100}, i, j;
cout << "Enter number of rows (between 1 and 100): ";
cin >> ry
cout << "Enter number of columns (between I and 100): "
cin >> ¢;
cout << endl << "Enter clements of Ist matrix: " << endl;
/{ Storing elements of first matrix entered by user.
for(i = 0; i > alillils
// Storing elements of second matrix entered by user.
cout << endl << "Enter elements of 2nd matrix: " << endl;
cout << "Enter element b" <> blillil:
}
1! Adding Two matrices
for(i = 0; i
using namespace std;
int main()
{
int al 10][ 10}, trans[10][10], r, ¢. i, js
cout << "Enter rows and columns of matrix:
cin >> 1 >>;
/! Storing element of matrix entered by user in array af][].
cout << endl << "Enter elements of matrix: " << endl;
for(i =O; i <0; 1 i)
for(j = 0; j> alli};}
// Displaying the matrix al]L]
cout << endl << "Entered Matrix: " << endl;
for(i =0; i <4; ++i)
for(j = 0; j
using namespace std;
void enterData(int firstMatrix[][10], int secondMatrix[][10], int rowFirst, int columnFirst, int
rowSecond, int columnSecond);
void multiplyMatrices(int firstMatrix{][10], int secondMatrix][10}, int multResult{]{10}, int
rowFirst, int columnFirst, int rowSecond, int columnSecond),
void display(int mult{][10}, int rowFirst, int columnSecond);
int main()
{
int firstMatrix{10][10}, secondMatrix{10]{10}, mult{ 10]{10}, rowFirst, columnFirst,
rowSecond, columnSecond, i, j,k;
cout << "Enter rows and column for first matrix: ";
cin >> rowFirst >> columnFirst;
cout << "Enter rows and column for second matrix:cin >> rowSecond >> columnSecond;
//1f colum of first matrix in not equal to row of second matrix, asking user to enter the
size of matrix again.
while (columnFirst != rowSecond)
{
cout << "Error! column of first matrix not equal to row of second.” << endl;
cout << "Enter rows and column for first matri
cin >> rowFirst >> columnFirst;
cout << "Enter rows and column for second matrix: ";
cin >> rowSecond >> columnSecond;
/{ Function to take matrices data
enterData(firstMatrix, secondMatrix, rowFirst, columnFirst, rowSecond,
columnSecond);
// Function to multiply two matrices.
multiplyMatrices(firstMatrix, secondMatrix, mult, rowFirst, columnFirst, rowSecond,
columnSecond);
1 Function to display resultant matrix after multiplication:
display(mult, rowFirst, columnSecond);
return 0;
}
void enterData(int firstMatrix{]{10], int secondMatrix{][ 10], int rowFirst, int columnFirst, int
rowSecond, int columnSecond)
{
inti, js
cout << endl << "Enter elements of matrix 1:" << endl;
for(i = 0; i < rowFirst; ++i)
{
for(j = 0; j < columnFirst; +4)
(
cout << "Enter elements a"<> firstMatrix(i]{j}s
}
cout << endl << "Enter elements of matrix 2:" << endl;
for(i = 0; i < rowSecond; +i)
{
for(j = 0; j < columnSecond; +4))
(
cout << "Enter elements b” << i+ 1 <> secondMatrix{i]{j};}
void multiplyMatrices(int firstMatrix{][10], int secondMatrixf][10], int mult{][10], int
rowFirst, int columnFirst, int rowSecond, int columnSecond)
{
inti, j, ks
/Hnitializing elements of matrix mult to 0.
for(i = 0; i < rowFirst; ++i)
{
for(j = 0; j < columnSecond; +4)
i
mult{i{j] =
}
}
// Multiplying matrix firstMatrix and secondMatrix and storing in array mult,
for(i = 0; i < rowFirst; ++i)
{
for(j = 0; j < columnSecond; +4)
(
for(k=0; k
#includeusing namespace std;
J/ Student Class Declaration
class StudentClass {
private://Access - Specifier
i/Member Variable Declaration
char name(20];
int regNo, subl, sub2, sub3;
float total, avg;
public://Access - Specifier
{/Member Functions read() and print() Declaration
void read() {
//Get Input Values For Object Variables
cout << "Enter Name :";
cin >> name;
cout << "Enter Registration Number :":
cin >> regNo;
cout << "Enter Marks for Subject 1,2 and 3 :";
cin >> sub] >> sub2>> sub3;
}
void sum() {
total = subl + sub2 + sub3;
avg = total / 3;
}
void print() {
//Show the Output
cout << "Name :" << name << en
cout << "Registration Number :" << regNo << endl;
cout << "Marks :" << subl <<" ," << sub2 <<", " << sub3 << endl;
cout << "Total :" << total << endl;
cout << "Average :" << avg << endl;
}
he
int main() {
1/ Object Creation For Class
StudentClass stul, stu2;
cout << "Read and Print Student Information Class Example Program In C+#\n";
cout << "\nStudentClass : Student 1" << endl;
stul.read();
stul.sum(;stul print);
cout << "\nStudentClass : Student 2" << endl;
stu2.readQ);
stu2.sum();
stu2.print();
getch();
return 0;
}
28. /* Example Program For Si
CH]
#include
#include
nple Class Example Program For Prime Number In
using namespace std
1) Class Declaration
class prime (
1/Member Variable Declaration
int a, kis
public:
primetint x) {
ax;
}
// Object Creation For Class
void calculate()
k=
void show() {
if (k== 1)
cout <<"\n" <>a;,
Object Creation For Class
prime obj(a):
// Call Member Funetions
obj.calculate();
obj.show(;
getch();
return 0;
}
29. /* Example Program For Simple Example Program Of Function
Number In C++#/
#include
#include
using namespace std;
// Find Smallest Number compare Function:
int compare(int a, int b) {
return (a
#include
using namespace std;
1/ Simple factorial Function
int factorial(int var) {
int fact = 1;
for (int i = 1; i <= var; i++)int main() {
cout << "5 Factorial Number :" << factorial(5);
getch();
return 0;
}
31. Inline Function Example Program
#include
#include
class line {
public:
inline float mul(float x, float y) {
return (x * y);
}
inline float eube(float x) {
return (x * x * x);
i }
void main() {
line obj;
float vall, val2;
elrser();
cout << "Enter two values:";
cin >> vall>>val2;
cout << "\Multiplication value is:" << obj.mul(vall, val2);
cout << "\n\nCube value is :" << obj.cube(vall) << "\t" << obj.cube(val2);
geich();
32. Simple Soring Example Program In C++
// Header Files
#include
#include
using namespace std;
#define ARRAY_SIZE 5
int main()
int numbers[ARRAY_SIZE]., i ,j temp;
cout<<"Simple C++ Example Program for Sorting (Asending Order) In Array\n";
// Read Inputfor (i = 0; i< ARRAY_SIZE; i++)
(
cout<<"Enter the Number : "<< (#1) <<" :"5
cin>>numbers[i];
}
1! Array Sorting - Asensding Order
for (i= 0; i< ARRAY_SIZE; ++i)
{
for j=1+ 1;)< ARRAY_SIZE; +4))
(
if (numbers[i] > numbers{j])
{
temp = numbersfi
numbersfi] = numbers{j];
numbers[j] = temp;
}
}
}
cout<<"Sorting Order Array: \n";
for (i = 0; i< ARRAY_SIZE; ++i)
cout<
void main ()
{
int x;
cout << "Please enter an integer value:
cin >>x;
cout <
void main ()
{
inva, b, pqs:
cout<