Sure! Here are some simple C++ tasks for you to practice your skills:
Task 1: Hello World
Write a C++ program that prints "Hello, World!" to the console.
Task 2: Sum of Two Numbers
Create a program that prompts the user to enter two integers and then calculates and displays their sum.
Task 3: Odd or Even
Write a program that asks the user for an integer and determines whether it is odd or even.
Task 4: Factorial Calculation
Write a program that calculates the factorial of a number provided by the user using a loop.
Task 5: Reverse a String
Create a program that prompts the user to enter a string and then displays the string in reverse order.
User
Sgow me the answers, please
Assistant
User
Can you create another simple tasks
Assistant
Certainly! Here are some more simple C++ tasks for you to practice:
Task 11: Count Vowels and Consonants
Write a program that prompts the user to enter a string, then counts and displays the number of vowels and consonants in that string.
Task 12: Find the Maximum Element in an Array
Create a program that takes 5 integers as input from the user and finds the maximum element among them.
Task 13: Simple Interest Calculator
Write a program that calculates the simple interest based on principal, rate of interest, and time. Prompt the user for these values and display the result.
Task 6: Fibonacci Sequence
Write a program that prints the first 10 numbers in the Fibonacci sequence.
Task 7: Array Average
Create a program that prompts the user to enter 5 numbers into an array and then calculates and prints the average of those numbers.
Task 8: Prime Number Check
Write a program that checks if a number entered by the user is a prime number.
Task 9: Simple Calculator
Create a simple calculator program that can perform addition, subtraction, multiplication, and division based on user input.
Task 10: Temperature Conversion
Write a program that converts Celsius to Fahrenheit. The program should prompt the user to enter a temperature in Celsius and then display the converted temperature in Fahrenheit.
Bonus Task: Simple Class
Define a simple class called Rectangle that has two attributes: width and height. Include methods to calculate the area and perimeter of the rectangle, and display them.
Feel free to ask if you need any guidance or solutions for these tasks! Happy coding!
Sure! Below are the solutions for each of the tasks.
Task 1: Hello World
cpp
#include <iostream>
using namespace std;
int main() {
cout << "Hello, World!" << endl;
return 0;
}
Task 2: Sum of Two Numbers
cpp
#include <iostream>
using namespace std;
int main() {
int num1, num2;
cout << "Enter two integers: ";
cin >> num1 >> num2;
cout << "Sum: " << num1 + num2 << endl;
return 0;
}
Task 3: Odd or Even
cpp
#include <iostream>
using namespace std;
int main() {
int num;
cout << "Enter an integer: ";
cin >> num;
if (num % 2 == 0) {
cout << num << " is even." << endl;
} else {
cout << num << " is odd." << endl;
}
return 0;
}
Task 4: Factorial Calculation
cpp
#include <iostream>
using namespace std;
int main() {
int num;
long long factorial = 1;
cout << "Enter a positive integer: ";
cin >> num;
for (int i = 1; i <= num; ++i) {
factorial *= i;
}
cout << "Factorial of " << num << " is " << factorial << endl;
return 0;
}
#include <iostream>
using namespace std;
int main() {
int n = 10; // Number of Fibonacci numbers to print
int a = 0, b = 1;
cout << "Fibonacci sequence: ";
for (int i = 0; i < n; ++i) {
cout << a << " ";
int next = a + b;
a = b;
b = next;
}
cout << endl;
return 0;
}
Task 7: Array Average
cpp
#include <iostream>
using namespace std;
int main() {
const int size = 5;
int numbers[size];
int sum = 0;
cout << "Enter " << size << " numbers: ";
for (int i = 0; i < size; ++i) {
cin >> numbers[i];
sum += numbers[i];
}
double average = static_cast<double>(sum) / size;
cout << "Average: " << average << endl;
return 0;
}
Task 8: Prime Number Check
cpp
#include <iostream>
using namespace std;
int main() {
int num;
bool isPrime = true;
cout << "Enter a positive integer: ";
cin >> num;
if (num <= 1) {
isPrime = false;
} else {
for (int i = 2; i <= num / 2; ++i) {
if (num % i == 0) {
isPrime = false;
break;
}
}
}
if (isPrime)
cout << num << " is a prime number." << endl;
else
cout << num << " is not a prime number." << endl;
return 0;
}
Feel free to run these programs in your C++ environment and modify them as needed to enhance your learning! If you have any questions or need further assistance, let me know!
Task 14: Palindrome Check
Create a program that checks if a given string is a palindrome (reads the same forwards and backwards).
Task 15: Generate Multiplication Table
Write a program that takes a number from the user and prints the multiplication table for that number up to 10.
Task 16: Inserting Elements in an Array
Write a program that initializes an array of size 10 with integers. Allow the user to insert a new element at a specific position in the array. Display the updated array.
Task 17: Count Total Digits, Letters, and Special Characters
Write a program that reads a line of text and counts the number of digits, letters, and special characters present in that line.
Task 18: Create a Simple Stopwatch
Write a program that measures the time it takes for the user to enter a specific command (like a single letter). Use the <chrono> library for timing.
Task 19: Character Frequency
Create a program that counts how many times a particular character appears in a user-inputted string.
Task 20: Basic Quiz Program
Write a program that poses three simple math questions to the user (e.g., 5 + 3, 7 * 6, etc.). Check their answers and provide feedback.
Feel free to ask for help or solutions for any of these tasks! Enjoy coding!