QUESTION:1
Write a Program to Find Average of 3 number
#include <iostream>
using namespace std;
int main() {
int num_1;
cout << "Enter num_1 : ";
cin >> num_1;
int num_2;
cout << "Enter num_2 : ";
cin >> num_2;
int num_3;
cout << "Enter num_3 : ";
cin >> num_3;
float average = (num_1 + num_2 + num_3) / 3;
cout << "Average : " << average << endl;
}
Output
Enter num1 : 23
Enter num2 : 41
Enter num3 : 35
Average : 33
QUESTION:2
Write a Program to Find bigger among 3 numbers
OUTPUT:-
QUESTION:3
Write a Menu driven program ( Switch Case ) to perform arithmetic operations
OUTPUT:-
Write a Program to check whether entered number is Prime or not
Write a Program to check whether entered number is even or odd
#include <iostream>
using namespace std;
int main()
{
int n;
cout << "Enter an integer: ";
cin >> n;
if ( n % 2 == 0)
cout << n << " is even.";
else
cout << n << " is odd.";
return 0;
}
Output:-
Enter an integer: 23
23 is odd.
QUESTION:6
Write a Program for addition of Two Matrixes
#include <iostream>
using namespace std;
int main()
{
int r, c, a[100][100], b[100][100], sum[100][100], i, j;
cout << "Enter number of rows (between 1 and 100): ";
cin >> r;
cout << "Enter number of columns (between 1 and 100): ";
cin >> c;
cout << endl << "Enter elements of 1st matrix: " << endl;
// Storing elements of first matrix entered by user.
for(i = 0; i < r; ++i)
for(j = 0; j < c; ++j)
{
cout << "Enter element a" << i + 1 << j + 1 << " : ";
cin >> a[i][j];
}
// Storing elements of second matrix entered by user.
cout << endl << "Enter elements of 2nd matrix: " << endl;
for(i = 0; i < r; ++i)
for(j = 0; j < c; ++j)
{
cout << "Enter element b" << i + 1 << j + 1 << " : ";
cin >> b[i][j];
}
// Adding Two matrices
for(i = 0; i < r; ++i)
for(j = 0; j < c; ++j)
sum[i][j] = a[i][j] + b[i][j];
// Displaying the resultant sum matrix.
cout << endl << "Sum of two matrix is: " << endl;
for(i = 0; i < r; ++i)
for(j = 0; j < c; ++j)
{
cout << sum[i][j] << " ";
if(j == c - 1)
cout << endl;
}
return 0;
}
Output
Enter number of rows (between 1 and 100): 2
Enter number of columns (between 1 and 100): 2
Enter elements of 1st matrix:
Enter element a11: -4
Enter element a12: 5
Enter element a21: 6
Enter element a22: 8
Enter elements of 2nd matrix:
Enter element b11: 3
Enter element b12: -9
Enter element b21: 7
Enter element b22: 2
Sum of two matrix is:
-1 -4
13 10
Related searches
QUESTION:7
Write a Program for multiplication of Two Matrixes
#include <iostream>
using namespace std;
int main()
{
int a[10][10],b[10][10],mul[10][10],r,c,i,j,k;
cout<<"enter the number of row=";
cin>>r;
cout<<"enter the number of column=";
cin>>c;
cout<<"enter the first matrix element=\n";
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
cin>>a[i][j];
}
}
cout<<"enter the second matrix element=\n";
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
cin>>b[i][j];
}
}
cout<<"multiply of the matrix=\n";
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
mul[i][j]=0;
for(k=0;k<c;k++)
{
mul[i][j]+=a[i][k]*b[k][j];
}
}
}
//for printing result
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
cout<<mul[i][j]<<" ";
}
cout<<"\n";
}
return 0;
}
Output:
enter the number of row=3
enter the number of column=3
enter the first matrix element=
1 2 3
1 2 3
1 2 3
enter the second matrix element=
1 1 1
2 1 2
3 2 1
multiply of the matrix=
14 9 8
14 9 8
14 9 8
QUESTION:8
Write a Program to Find transpose of a matrix
#include <iostream>
using namespace std;
int main() {
int a[10][10], transpose[10][10], row, column, i, j;
cout << "Enter rows and columns of matrix: ";
cin >> row >> column;
cout << "\nEnter elements of matrix: " << endl;
// Storing matrix elements
for (int i = 0; i < row; ++i) {
for (int j = 0; j < column; ++j) {
cout << "Enter element a" << i + 1 << j + 1 << ": ";
cin >> a[i][j];
}
}
// Printing the a matrix
cout << "\nEntered Matrix: " << endl;
for (int i = 0; i < row; ++i) {
for (int j = 0; j < column; ++j) {
cout << " " << a[i][j];
if (j == column - 1)
cout << endl << endl;
}
}
// Computing transpose of the matrix
for (int i = 0; i < row; ++i)
for (int j = 0; j < column; ++j) {
transpose[j][i] = a[i][j];
}
// Printing the transpose
cout << "\nTranspose of Matrix: " << endl;
for (int i = 0; i < column; ++i)
for (int j = 0; j < row; ++j) {
cout << " " << transpose[i][j];
if (j == row - 1)
cout << endl << endl;
}
return 0;
}
Output
Enter rows and columns of matrix: 2
3
Enter elements of matrix:
Enter element a11: 1
Enter element a12: 2
Enter element a13: 9
Enter element a21: 0
Enter element a22: 4
Enter element a23: 7
Entered Matrix:
1 2 9
0 4 7
Transpose of Matrix:
1 0
2 4
9 7
SUBSCRIBE YOUTUBE CHANNEL CLICK HERE 🔵
QUESTION:9
Write a Program to Print
*
* *
* * *
* * * *
#include <iostream>
using namespace std;
int main()
{
int rows;
cout << "Enter number of rows: ";
cin >> rows;
for(int i = 1; i <= rows; ++i)
{
for(int j = 1; j <= i; ++j)
{
cout << "* ";
}
cout << "\n";
}
return 0;
}
Output:-
Enter number of rows : 4
*
* *
* * *
* * * *
QUESTION:10
Write a Program to Print
1
2 2
3 3 3
#include<iostream>
using namespace std;
int main( )
{
int input, number='1';
cout<<"Enter the uppercase number you want to print in the last row: ";
cin >> input;
for(int i = 1; i <= (input-'1'+1); ++i)
{
for(int j = 1; j <= i; ++j)
{
cout <<number << " ";
}
++number;
cout << endl;
}
return 0;
}
Output:-
1
2 2
3 3 3
QUESTION:11
Write a Program to Print
1
2 3
4 5 6
#include <iostream>
using namespace std;
int main()
{
int rows, number = 1;
cout << "Enter number of rows: ";
cin >> rows;
for(int i = 1; i <= rows; i++)
{
for(int j = 1; j <= i; ++j)
{
cout << number << " ";
++number;
}
cout << endl;
}
return 0;
}
Output:-
Enter number of rows: 3
1
2 3
4 5 6
QUESTION:12
Write a Program to Check Whether entered string is palindrome or not
#include <iostream>
using namespace std;
int main()
{
int n, num, digit, rev = 0;
cout << "Enter a positive number: ";
cin >> num;
n = num;
do
{
digit = num % 10;
rev = (rev * 10) + digit;
num = num / 10;
} while (num != 0);
cout << " The reverse of the number is: " << rev << endl;
if (n == rev)
cout << " The number is a palindrome.";
else
cout << " The number is not a palindrome.";
return 0;
}
Output
Enter a positive number: 12321
The reverse of the number is: 12321
The number is a palindrome.
Enter a positive number: 12331
The reverse of the number is: 13321
The number is not a palindrome.
QUESTION:13
Write a Program to Print fabonacci Series
#include <iostream>
using namespace std;
int main() {
int n, t1 = 0, t2 = 1, nextTerm = 0;
cout << "Enter the number of terms: ";
cin >> n;
cout << "Fibonacci Series: ";
for (int i = 1; i <= n; ++i) {
// Prints the first two terms.
if(i == 1) {
cout << t1 << ", ";
continue;
}
if(i == 2) {
cout << t2 << ", ";
continue;
}
nextTerm = t1 + t2;
t1 = t2;
t2 = nextTerm;
cout << nextTerm << ", ";
}
return 0;
}
OUTPUT:-
Enter the number of terms: 10
Fibonacci Series: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34,
QUESTION:14
Write a Program to Find factorial of a given Number
#include <iostream>
using namespace std;
int main()
{
int n;
unsigned long long factorial = 1;
cout << "Enter a positive integer: ";
cin >> n;
if (n < 0)
cout << "Error! Factorial of a negative number doesn't exist.";
else {
for(int i = 1; i <=n; ++i) {
factorial *= i;
}
cout << "Factorial of " << n << " = " << factorial;
}
return 0;
}
Output
Enter a positive integer: 12
Factorial of 12 = 47900160
QUESTION:15
Write a Program to demonstrate use of static data Member
#include <iostream>
#include<string.h>
using namespace std;
class Student {
private:
int rollNo;
char name[10];
int marks;
public:
static int objectCount;
Student() {
objectCount++;
}
void getdata() {
cout << "Enter roll number: "<<endl;
cin >> rollNo;
cout << "Enter name: "<<endl;
cin >> name;
cout << "Enter marks: "<<endl;
cin >> marks;
}
void putdata() {
cout<<"Roll Number = "<< rollNo <<endl;
cout<<"Name = "<< name <<endl;
cout<<"Marks = "<< marks <<endl;
cout<<endl;
}
};
int Student::objectCount = 0;
int main(void) {
Student s1;
s1.getdata();
s1.putdata();
Student s2;
s2.getdata();
s2.putdata();
Student s3;
s3.getdata();
s3.putdata();
cout << "Total objects created = " << Student::objectCount << endl;
return 0;
}
Output:-
Enter roll number: 1
Enter name: Mark
Enter marks: 78
Roll Number = 1
Name = Mark
Marks = 78
Enter roll number: 2
Enter name: Nancy
Enter marks: 55
Roll Number = 2
Name = Nancy
Marks = 55
Enter roll number: 3
Enter name: Susan
Enter marks: 90
Roll Number = 3
Name = Susan
Marks = 90
Total objects created = 3
QUESTION:16
Write a Program to Demonstrate use of static member function
#include <iostream>
using namespace std;
class Example{
static int Number;
int n;
public:
void set_n(){
n = ++Number;
}
void show_n(){
cout<<"value of n = "<<n<<endl;
}
static void show_Number(){
cout<<"value of Number = "<<Number<<endl;
}
};
int Example:: Number;
int main()
{
Example example1, example2;
example1.set_n();
example2.set_n();
example1.show_n();
example2.show_n();
Example::show_Number();
return 0;
}
OUTPUT:-
value of n=1
value of n=2
value of Number=2
QUESTION:17
Write a Program to create array of objects
#include <iostream>
class MyClass {
int x;
public:
void setX(int i) { x = i; }
int getX() { return x; }
};
void main()
{
MyClass obs[4];
int i;
for(i=0; i < 4; i++)
obs[i].setX(i);
for(i=0; i < 4; i++)
cout << "obs[" << i << "].getX(): " << obs[i].getX() << "\n";
getch();
}
Output:
obs[0].getX(): 0
obs[1].getX(): 1
obs[2].getX(): 2
obs[3].getX(): 3
QUESTION:18
Write a Program to demonstrate use of friend function
#include <iostream>
using namespace std;
class B; // forward declarartion.
class A
{
int x;
public:
void setdata(int i)
{
x=i;
}
friend void min(A,B); // friend function.
};
class B
{
int y;
public:
void setdata(int i)
{
y=i;
}
friend void min(A,B); // friend function
};
void min(A a,B b)
{
if(a.x<=b.y)
std::cout << a.x << std::endl;
else
std::cout << b.y << std::endl;
}
int main()
{
A a;
B b;
a.setdata(10);
b.setdata(20);
min(a,b);
return 0;
}
Output:
10
QUESTION:19
Write a Program to illustrate use of Copy Constructor
#include<iostream>
using namespace std;
class Point
{
private:
int x, y;
public:
Point(int x1, int y1) { x = x1; y = y1; }
// Copy constructor
Point(const Point &p1) {x = p1.x; y = p1.y; }
int getX() { return x; }
int getY() { return y; }
};
int main()
{
Point p1(10, 15); // Normal constructor is called here
Point p2 = p1; // Copy constructor is called here
// Let us access values assigned by constructors
cout << "p1.x = " << p1.getX() << ", p1.y = " << p1.getY();
cout << "\np2.x = " << p2.getX() << ", p2.y = " << p2.getY();
return 0;
}
Output:
p1.x = 10, p1.y = 15
p2.x = 10, p2.y = 15
QUESTION:20
Write a Program to illustrate use of destructors
#include<iostream>
using namespace std;
class Demo {
private:
int num1, num2;
public:
Demo(int n1, int n2) {
cout<<"Inside Constructor"<<endl;
num1 = n1;
num2 = n2;
}
void display() {
cout<<"num1 = "<< num1 <<endl;
cout<<"num2 = "<< num2 <<endl;
}
~Demo() {
cout<<"Inside Destructor";
}
};
int main() {
Demo obj1(10, 20);
obj1.display();
return 0;
}
Output
Inside Constructor
num1 = 10
num2 = 20
Inside Destructor
QUESTION:21
Write a Program to demonstrate constructor overloading
#include <iostream>
using namespace std;
class Person {
private:
int age;
public:
// 1. Constructor with no arguments
Person() {
age = 20;
}
// 2. Constructor with an argument
Person(int a) {
age = a;
}
int getAge() {
return age;
}
};
int main() {
Person person1, person2(45);
cout << "Person1 Age = " << person1.getAge() << endl;
cout << "Person2 Age = " << person2.getAge() << endl;
return 0;
}
Output:-
Person1 Age = 20
Person2 Age = 45
Comments
Post a Comment
Please Subscribe and Comments my blog site.