0% found this document useful (0 votes)
8 views3 pages

C++ Programming Challenges Solutions

This document contains a series of C++ programming problems and their solutions, including calculating the sum of digits, checking for palindromes, counting digits, finding factorials, determining if a number is prime, printing Fibonacci series, and checking if a number is even or odd. Each problem is accompanied by a code snippet demonstrating the solution. The solutions are structured in a clear and concise manner, making them easy to understand.

Uploaded by

haramyawr3434
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)
8 views3 pages

C++ Programming Challenges Solutions

This document contains a series of C++ programming problems and their solutions, including calculating the sum of digits, checking for palindromes, counting digits, finding factorials, determining if a number is prime, printing Fibonacci series, and checking if a number is even or odd. Each problem is accompanied by a code snippet demonstrating the solution. The solutions are structured in a clear and concise manner, making them easy to understand.

Uploaded by

haramyawr3434
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

C++ Programming Problems and Solutions

1. Sum of Digits of a Number


----------------------------
#include<iostream>
using namespace std;
int main() {
int number, sum = 0;
cout << "Enter a number: ";
cin >> number;
while (number != 0) {
sum += number % 10;
number = number / 10;
}
cout << "Sum of digits: " << sum << endl;
return 0;
}

2. Check if a Number is Palindrome


-----------------------------------
#include<iostream>
using namespace std;
int main() {
int number, originalNumber, reversed = 0, digit;
cout << "Enter a number: ";
cin >> number;
originalNumber = number;
while (number != 0) {
digit = number % 10;
reversed = reversed * 10 + digit;
number = number / 10;
}
if (originalNumber == reversed)
cout << "The number is a palindrome!" << endl;
else
cout << "The number is not a palindrome!" << endl;
return 0;
}

3. Count the Number of Digits in a Number


-----------------------------------------
#include<iostream>
using namespace std;
int main() {
int number, count = 0;
cout << "Enter a number: ";
cin >> number;
while (number != 0) {
number = number / 10;
count++;
}
cout << "The number of digits is: " << count << endl;
return 0;
}

4. Find the Factorial of a Number


---------------------------------
#include<iostream>
using namespace std;
int main() {
int number;
long long factorial = 1;
cout << "Enter a number: ";
cin >> number;
for (int i = 1; i <= number; i++) {
factorial *= i;
}
cout << "Factorial of " << number << " is: " << factorial << endl;
return 0;
}

5. Check if a Number is Prime


-----------------------------
#include<iostream>
using namespace std;
int main() {
int number, i;
bool isPrime = true;
cout << "Enter a number: ";
cin >> number;
for (i = 2; i <= number / 2; i++) {
if (number % i == 0) {
isPrime = false;
break;
}
}
if (isPrime && number > 1)
cout << "The number is prime!" << endl;
else
cout << "The number is not prime!" << endl;
return 0;
}

6. Print Fibonacci Series up to n terms


---------------------------------------
#include<iostream>
using namespace std;
int main() {
int n, t1 = 0, t2 = 1, nextTerm;
cout << "Enter the number of terms: ";
cin >> n;
cout << "Fibonacci Series: ";
for (int i = 1; i <= n; ++i) {
cout << t1 << " ";
nextTerm = t1 + t2;
t1 = t2;
t2 = nextTerm;
}
cout << endl;
return 0;
}

7. Check Whether a Number is Even or Odd


----------------------------------------
#include<iostream>
using namespace std;
int main() {
int number;
cout << "Enter a number: ";
cin >> number;
if (number % 2 == 0) {
cout << "The number is even!" << endl;
} else {
cout << "The number is odd!" << endl;
}
return 0;
}

You might also like