-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.cpp
More file actions
47 lines (38 loc) · 1.08 KB
/
test.cpp
File metadata and controls
47 lines (38 loc) · 1.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#include <iostream>
#include <string>
// Function to greet the user
void greetUser(const std::string& name) {
std::cout << "Hello, " << name << "! Welcome to C++.\n";
}
// Function to compute factorial
int factorial(int n) {
int result = 1;
for (int i = 1; i <= n; ++i)
result *= i;
return result;
}
int main() {
std::string name;
int number;
// Change
int a = 0;
// Ask for user's name
std::cout << "Enter your name: ";
std::getline(std::cin, name);
greetUser(name);
// Ask for a number to compute factorial
std::cout << "Enter a positive integer to compute its factorial: ";
std::cin >> number;
if (number < 0) {
std::cout << "Factorial is not defined for negative numbers.\n";
} else {
std::cout << "Factorial of " << number << " is " << factorial(number) << "\n";
}
// Simple loop demonstration
std::cout << "Counting from 1 to 5:\n";
for (int i = 1; i <= 5; ++i) {
std::cout << i << " ";
}
std::cout << "\n";
return 0;
}