Make some simple tasks for C++, please. Without using of inputs
- User
Make some simple tasks for C++, please. Without using of inputs
- Assistant
Make some simple tasks for C++, please. Without using of inputs
Sure! Here are some simple C++ tasks that don't require user inputs. Each task includes a brief description, and you can implement them for practice.
Write a program that prints the first 10 numbers of the Fibonacci series.
#include <iostream>
using namespace std;
int main() {
int n = 10;
int a = 0, b = 1, c;
cout << "Fibonacci series: ";
for(int i = 0; i < n; ++i) {
cout << a << " ";
c = a + b;
a = b;
b = c;
}
return 0;
}
Create a function that calculates the factorial of a predefined number (e.g., 5) and prints the result.
#include <iostream>
using namespace std;
int factorial(int n) {
return (n == 0 || n == 1) ? 1 : n * factorial(n - 1);
}
int main() {
int num = 5;
cout << "Factorial of " << num << " is " << factorial(num) << endl;
return 0;
}
Define an array of integers and calculate the sum and average of its elements.
#include <iostream>
using namespace std;
int main() {
int arr[] = {10, 20, 30, 40, 50};
int sum = 0;
int n = sizeof(arr) / sizeof(arr[0]);
for(int i = 0; i < n; ++i) {
sum += arr[i];
}
double average = static_cast<double>(sum) / n;
cout << "Sum: " << sum << ", Average: " << average << endl;
return 0;
}
Write a function that checks if a predefined number (e.g., 17) is a prime number and prints the result.
#include <iostream>
using namespace std;
bool isPrime(int n) {
if (n <= 1) return false;
for (int i = 2; i * i <= n; ++i) {
if (n % i == 0) return false;
}
return true;
}
int main() {
int num = 17;
if (isPrime(num))
cout << num << " is a prime number." << endl;
else
cout << num << " is not a prime number." << endl;
return 0;
}
Create a function that reverses a predefined string and prints the reversed result.
#include <iostream>
#include <string>
using namespace std;
string reverseString(const string &str) {
string reversed = "";
for (int i = str.length() - 1; i >= 0; --i) {
reversed += str[i];
}
return reversed;
}
int main() {
string str = "Hello, World!";
cout << "Original string: " << str << endl;
cout << "Reversed string: " << reverseString(str) << endl;
return 0;
}
Feel free to modify the predefined values or enhance each task to add complexity as you practice!
Not yet included in a published release.