C++ More Programming Solutions
C++ More Programming Solutions
#include <iostream>
using namespace std;
int main() {
int num;
cout << "Enter a number: ";
cin >> num;
if (num % 2 == 0)
cout << num << " is even.";
else
cout << num << " is odd.";
return 0;
}
#include <iostream>
using namespace std;
int main() {
int a, b, c;
cout << "Enter three numbers: ";
cin >> a >> b >> c;
if (a >= b && a >= c)
cout << "Largest number is " << a;
else if (b >= a && b >= c)
cout << "Largest number is " << b;
else
cout << "Largest number is " << c;
return 0;
}
#include <iostream>
#include <cmath>
using namespace std;
int main() {
double base, exponent, result;
cout << "Enter base and exponent: ";
cin >> base >> exponent;
result = pow(base, exponent);
cout << base << "^" << exponent << " = " << result;
return 0;
}
#include <iostream>
using namespace std;
int main() {
int num;
cout << "Enter a number: ";
cin >> num;
for (int i = 1; i <= 10; i++) {
cout << num << " x " << i << " = " << num * i <<
endl;
}
return 0;
}
Q16: Find GCD of Two Numbers
#include <iostream>
using namespace std;
int main() {
int num1, num2;
cout << "Enter two numbers: ";
cin >> num1 >> num2;
cout << "GCD: " << gcd(num1, num2);
return 0;
}
#include <iostream>
using namespace std;
int main() {
int num1, num2;
cout << "Enter two numbers: ";
cin >> num1 >> num2;
cout << "LCM: " << lcm(num1, num2);
return 0;
}
#include <iostream>
#include <algorithm>
using namespace std;
int main() {
string str;
cout << "Enter a string: ";
cin >> str;
reverse(str.begin(), str.end());
cout << "Reversed string: " << str;
return 0;
}
#include <iostream>
using namespace std;
int main() {
string str;
int vowels = 0, consonants = 0;
cout << "Enter a string: ";
cin >> str;
for (char ch : str) {
ch = tolower(ch);
if (ch == 'a' || ch == 'e' || ch == 'i' || ch ==
'o' || ch == 'u')
vowels++;
else if (isalpha(ch))
consonants++;
}
cout << "Vowels: " << vowels << ", Consonants: " <<
consonants;
return 0;
}
#include <iostream>
using namespace std;
int main() {
int year;
cout << "Enter a year: ";
cin >> year;
if ((year % 4 == 0 && year % 100 != 0) || (year % 400
== 0))
cout << year << " is a leap year.";
else
cout << year << " is not a leap year.";
return 0;
}