0% found this document useful (0 votes)
16 views17 pages

Document (8)

Uploaded by

dipeshkaushik720
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views17 pages

Document (8)

Uploaded by

dipeshkaushik720
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 17

1.

Program to Perform All Arithmetic


Operations
#include <iostream>
Using namespace std;

Int main() {
Float num1, num2;

Cout << “Enter two numbers: “;


Cin >> num1 >> num2;

Cout << “Addition: “ << num1 + num2 << endl;


Cout << “Subtraction: “ << num1 – num2 << endl;
Cout << “Multiplication: “ << num1 * num2 << endl;
Cout << “Division: “;
If (num2 != 0)
Cout << num1 / num2 << endl;
Else
Cout << “Undefined (division by zero)” << endl;
Return 0;
}
2. Program to Compare Two Numbers
Using if-else Statement

#include <iostream>
Using namespace std;

Int main() {
Int num1, num2;

Cout << “Enter two numbers: “;


Cin >> num1 >> num2;

If (num1 > num2) {


Cout << num1 << “ is greater than “ << num2 <<
endl;
} else if (num1 < num2) {
Cout << num2 << “ is greater than “ << num1 <<
endl;
} else {
Cout << “Both numbers are equal.” << endl;
}

Return 0;
}
3. Program to Implement Call by
Value
#include <iostream>
Using namespace std;

// Function to swap two numbers using call by value


Void swapNumbers(int a, int b) {
Int temp = a;
A = b;
B = temp;
Cout << “Inside swapNumbers function: a = “ << a << “, b =
“ << b << endl;
}

Int main() {
Int num1, num2;

Cout << “Enter two numbers: “;


Cin >> num1 >> num2;

Cout << “Before swapping in main: num1 = “ << num1 << “,


num2 = “ << num2 << endl;

swapNumbers(num1, num2);
cout << “After swapping in main: num1 = “ << num1 << “,
num2 = “ << num2 << endl;

return 0;
}
}
4. Program to Print a Matrix

#include <iostream>
Using namespace std;

Int main() {
Int rows, cols;

Cout << “Enter the number of rows and columns of the matrix:
“;
Cin >> rows >> cols;

Int matrix[rows][cols];

Cout << “Enter elements of the matrix:\n”;


For (int I = 0; I < rows; i++) {
For (int j = 0; j < cols; j++) {
Cout << “Enter element at position [“ << I + 1 << “][“
<< j + 1 << “]: “;
Cin >> matrix[i][j];
}
}

Cout << “\nThe matrix is:\n”;


For (int I = 0; I < rows; i++) {
For (int j = 0; j < cols; j++) {
Cout << matrix[i][j] << “ “;
}
Cout << endl;

Return 0;

}
5. Program to Implement Call by
Reference
#include <iostream>
Using namespace std;

// Function to swap two numbers using call by reference


Void swapNumbers(int &a, int &b) {
Int temp = a;
A = b;
B = temp;
Cout << “Inside swapNumbers function: a = “ << a << “,
b = “ << b << endl;
}

Int main() {
Int num1, num2;

Cout << “Enter two numbers: “;


Cin >> num1 >> num2;

Cout << “Before swapping in main: num1 = “ << num1


<< “, num2 = “ << num2 << endl;

swapNumbers(num1, num2);

cout << “After swapping in main: num1 = “ << num1 <<


“, num2 = “ << num2 << endl;

return 0;
}
6. Program to Print the First 10
Multiples of a Given Number Using a
while Loop
#include <iostream>
Using namespace std;

Int main() {
Int num, I = 1;

Cout << “Enter a number: “;


Cin >> num;

Cout << “The first 10 multiples of “ << num << “ are:” << endl;

While (I <= 10) {


Cout << num * I << “ “;
I++;
}

Cout << endl;


Return 0;
}
}
7. A program on String

#include <iostream>
#include <string>
Using namespace std;

Int main() {
String str;

Cout << “Enter a string: “;


Getline(cin, str); // Reads a string including spaces

Cout << “\nString entered: “ << str << endl;

// Length of the string


Cout << “Length of the string: “ << str.length() << endl;

// Reverse the string


String reversedStr = string(str.rbegin(), str.rend());
Cout << “Reversed string: “ << reversedStr << endl;

// Convert to uppercase
For (char &c : str) {
C = toupper©;
}
Cout << “String in uppercase: “ << str << endl;

Return 0;
}
}

8. Program to Insert an Element in


an Array
#include <iostream>
Using namespace std;

Int main() {
Int n, pos, element;

Cout << “Enter the size of the array: “;


Cin >> n;

Int arr[n + 1]; // Array size increased by 1 to accommodate


the new element

Cout << “Enter “ << n << “ elements of the array:\n”;


For (int I = 0; I < n; i++) {
Cin >> arr[i];
}

Cout << “Enter the position to insert the new element (1 to “


<< n + 1 << “): “;
Cin >> pos;

If (pos < 1 || pos > n + 1) {


Cout << “Invalid position!” << endl;
Return 0;
}
Cout << “Enter the element to insert: “;
Cin >> element;

// Shift elements to the right from the position


For (int I = n; I >= pos; i--) {
Arr[i] = arr[I – 1];
}
Arr[pos – 1] = element;

Cout << “Array after insertion:\n”;


For (int I = 0; I <= n; i++) {
Cout << arr[i] << “ “;
}
Cout << endl;

Return 0;
}

9.Program to Implement Default


Constructors

#include <iostream>
Using namespace std;

// Class with a default constructor


Class Example {
Int number;

Public:
// Default constructor
Example() {
Number = 0;
Cout << “Default constructor called. Number initialized to
0.” << endl;
}

Void display() {
Cout << “Number: “ << number << endl;
}
};

Int main() {
// Creating an object of the class
Example obj;

// Display the value of the number


Obj.display();

Return 0;
}
10.Program Using a Class to Find the
Largest Number from Given Three
Numbers
#include <iostream>
Using namespace std;
Class LargestNumber {
Int num1, num2, num3;

Public:
// Function to accept three numbers
Void inputNumbers() {
Cout << “Enter three numbers: “;
Cin >> num1 >> num2 >> num3;
}

// Function to find and display the largest number


Void findLargest() {
Int largest;
If (num1 > num2 && num1 > num3) {
Largest = num1;
} else if (num2 > num1 && num2 > num3) {
Largest = num2;
} else {
Largest = num3;
}
Cout << “The largest number is: “ << largest << endl;
}
};

Int main() {
LargestNumber obj;

Obj.inputNumbers(); // Accept the numbers


Obj.findLargest(); // Find and display the largest number

Return 0;
}

You might also like