Uploaded by harsha naidu

oops c++ code

advertisement
1. #include <iostream>
using namespace std;
int main() {
int choice, num1, num2, result;
cout << "MENU\n1. Add\n2. Subtract\n3. Multiply\n4. Divide\n5. Modulus\nEnter your choice: ";
cin >> choice;
cout << "Enter two integers: ";
cin >> num1 >> num2;
switch (choice) {
case 1: result = num1 + num2; break;
case 2: result = num1 - num2; break;
case 3: result = num1 * num2; break;
case 4:
if (num2 != 0) result = num1 / num2;
else { cout << "Error: Division by zero is not allowed.\n"; return 1; }
break;
case 5:
if (num2 != 0) result = num1 % num2;
else { cout << "Error: Modulus division by zero is not allowed.\n"; return 1; }
break;
default: cout << "Error: Invalid choice.\n"; return 1;
}
cout << "Result: " << result << endl;
return 0;
}
2. #include <iostream>
using namespace std;
int main() {
for (int i = 1000; i <= 2000; i++)
if (!(i % 8) && !(i % 5)) cout << i << " ";
cout << endl;
}
3. #include <iostream>
#include <string>
using namespace std;
string reverseString(const string& str) {
string reversed(str.rbegin(), str.rend());
return reversed;
}
int main() {
string input = "1234abcd";
string reversed = reverseString(input);
cout << "Original: " << input << endl;
cout << "Reversed: " << reversed << endl;
return 0;
}
4. #include <iostream>
#include <string>
using namespace std;
string reverseString(const string& str) {
string reversed(str.rbegin(), str.rend());
return reversed;
}
int main() {
string input = "1234abcd";
string reversed = reverseString(input);
cout << "Original: " << input << endl;
cout << "Reversed: " << reversed << endl;
return 0;
}
5. #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";
}
for(int i = rows-1; i >= 1; --i) {
for(int j = 1; j <= i; ++j) {
cout << "* ";
}
cout << "\n";
}
return 0;
}
6. #include <iostream>
int main() {
char word[100];
std::cout << "Enter a word: ";
std::cin.getline(word, 100);
int length = 0;
while (word[length] != '\0') {
length++;
}
for (int i = 0; i < length / 2; i++) {
char temp = word[i];
word[i] = word[length - i - 1];
word[length - i - 1] = temp;
}
std::cout << "Reversed word: " << word << std::endl;
return 0;
}
7. #include <iostream>
int main() {
int m, n;
std::cout << "Enter number of rows: ";
std::cin >> m;
std::cout << "Enter number of columns: ";
std::cin >> n;
int** array = new int* [m];
for (int i = 0; i < m; i++) {
array[i] = new int[n];
for (int j = 0; j < n; j++) {
array[i][j] = i * j;
}
}
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
std::cout << array[i][j] << " ";
}
std::cout << std::endl;
}
for (int i = 0; i < m; i++) {
delete[] array[i];
}
delete[] array;
return 0;
}
8. #include <iostream>
#include <string>
#include <cctype>
int main() {
std::string input;
std::cout << "Enter a string: ";
std::getline(std::cin, input);
int letters = 0;
int digits = 0;
for (char c : input) {
if (std::isalpha(c)) {
letters++;
} else if (std::isdigit(c)) {
digits++;
}
}
Std::cout << "Letters: " << letters << std::endl;
std::cout << "Digits: " << digits << std::endl;
return 0;
9. #include <iostream>
#include <string>
using namespace std;
bool isValidPassword(string password) {
if (password.length() < 6 || password.length() > 16) {
return false;
}
bool hasLower = false, hasUpper = false, hasDigit = false, hasSpecial = false;
for (char c : password) {
if (c >= 'a' && c <= 'z') {
hasLower = true;
} else if (c >= 'A' && c <= 'Z') {
hasUpper = true;
} else if (c >= '0' && c <= '9') {
hasDigit = true;
} else if (c == '$' || c == '#' || c == '@') {
hasSpecial = true;
}
if (hasLower && hasUpper && hasDigit && hasSpecial) {
return true;
}
}
return false;
}
int main() {
string password;
cout << "Enter your password: ";
cin >> password;
if (isValidPassword(password)) {
cout << "Valid Password" << endl;
} else {
cout << "Not a Valid Password" << endl;
}
return 0;
}
10. #include <iostream>
#include <sstream>
using namespace std;
int main() {
string numbers;
for (int i = 100; i <= 400; i++) {
int digit1 = i / 100;
int digit2 = (i / 10) % 10;
int digit3 = i % 10;
if (digit1 % 2 == 0 && digit2 % 2 == 0 && digit3 % 2 == 0) {
if (!numbers.empty()) {
numbers += ",";
}
stringstream ss;
ss << i;
numbers += ss.str();
}
}
cout << numbers << endl;
return 0;
}
11. ________
12. #include <iostream>
using namespace std;
int sum(int a, int b) {
int sum = a + b;
if (sum >= 105 && sum <= 200) {
return 200;
} else {
return sum;
}
}
int main() {
int a, b;
cout << "Enter two integers: ";
cin >> a >> b;
cout << "The sum of " << a << " and " << b << " is " << sum(a, b) << "." << endl;
return 0;
}
13.(err)
#include <iostream>
using namespace std;
int main() {
for (int i = 9; i >= 1; i--) {
for (int j = 1; j <= 9; j++) {
cout << i;
}
cout << endl;
}
return 0;
}
Download