0% found this document useful (0 votes)
3 views

C++ More Programming Solutions

The document contains a series of C++ programming questions and solutions, including checking if a number is even or odd, finding the greatest of three numbers, calculating the power of a number, generating a multiplication table, and finding the GCD and LCM of two numbers. It also includes string manipulation tasks such as reversing a string and counting vowels and consonants, as well as determining if a year is a leap year. Each solution is presented with sample code for implementation.

Uploaded by

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

C++ More Programming Solutions

The document contains a series of C++ programming questions and solutions, including checking if a number is even or odd, finding the greatest of three numbers, calculating the power of a number, generating a multiplication table, and finding the GCD and LCM of two numbers. It also includes string manipulation tasks such as reversing a string and counting vowels and consonants, as well as determining if a year is a leap year. Each solution is presented with sample code for implementation.

Uploaded by

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

C++ Programming Questions & Solutions (Continued)

Q12: Check Whether a Number is Even or Odd

#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;
}

Q13: Find the Greatest of Three Numbers

#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;
}

Q14: Calculate the Power of a Number

#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;
}

Q15: Generate a Multiplication Table

#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 gcd(int a, int b) {


while (b != 0) {
int temp = b;
b = a % b;
a = temp;
}
return a;
}

int main() {
int num1, num2;
cout << "Enter two numbers: ";
cin >> num1 >> num2;
cout << "GCD: " << gcd(num1, num2);
return 0;
}

Q17: Find LCM of Two Numbers

#include <iostream>
using namespace std;

int gcd(int a, int b) {


while (b != 0) {
int temp = b;
b = a % b;
a = temp;
}
return a;
}

int lcm(int a, int b) {


return (a * b) / gcd(a, b);
}

int main() {
int num1, num2;
cout << "Enter two numbers: ";
cin >> num1 >> num2;
cout << "LCM: " << lcm(num1, num2);
return 0;
}

Q18: Reverse a String

#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;
}

Q19: Count Vowels and Consonants in a String

#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;
}

Q20: Check Leap Year

#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;
}

You might also like