diff --git a/Basic C++ Programs/Array.txt b/Basic C++ Programs/Array.txt new file mode 100644 index 0000000..fb8f6e6 --- /dev/null +++ b/Basic C++ Programs/Array.txt @@ -0,0 +1,18 @@ +#include +using namespace std; + +int main() +{ + int numbers[5], sum = 0; + cout << "Enter 5 numbers!"<> numbers[i]; + sum += numbers[i]; + } + + cout << "Sum = " << sum << endl; + + return 0; +} \ No newline at end of file diff --git a/Basic C++ Programs/Calculator.txt b/Basic C++ Programs/Calculator.txt new file mode 100644 index 0000000..c4c8743 --- /dev/null +++ b/Basic C++ Programs/Calculator.txt @@ -0,0 +1,36 @@ +#include +using namespace std; + +int main() +{ + char o; + float num1, num2; + + cout << "Enter an operator (+, -, *, /): "; + cin >> o; + + cout << "Enter two operands: "; + cin >> num1 >> num2; + + switch (o) + { + case '+': + cout << num1 << " + " << num2 << " = " << num1+num2; + break; + case '-': + cout << num1 << " - " << num2 << " = " << num1-num2; + break; + case '*': + cout << num1 << " * " << num2 << " = " << num1*num2; + break; + case '/': + cout << num1 << " / " << num2 << " = " << num1/num2; + break; + default: + // if operator doesn't match any case constant (+, -, *, /) + cout << "Error! operator is not correct"; + break; + } + + return 0; +} \ No newline at end of file diff --git a/Basic C++ Programs/If-else.txt b/Basic C++ Programs/If-else.txt new file mode 100644 index 0000000..627d59a --- /dev/null +++ b/Basic C++ Programs/If-else.txt @@ -0,0 +1,26 @@ +#include +using namespace std; + +int main() +{ + int number; + cout << "Enter an integer: "; + cin >> number; + + // checks if the number is positive + if ( number > 0) + { + cout << "You entered a positive integer: " << number << endl; + } + else if(number==0) + { + cout<<"You entered 0!"< +using namespace std; + +int main() +{ + int i, n; + float arr[100]; + cout << "Enter total number of elements(1 to 100): "; + cin >> n; + cout << endl; + for(i = 0; i < n; ++i) + { + cout << "Enter Number " << i + 1 << " : "; + cin >> arr[i]; + } + for(i = 1;i < n; ++i) + { + + if(arr[0] < arr[i]) + arr[0] = arr[i]; + } + cout << "Largest element = " << arr[0]; + return 0; +} \ No newline at end of file diff --git a/Basic C++ Programs/Prime or Not.txt b/Basic C++ Programs/Prime or Not.txt new file mode 100644 index 0000000..02503c8 --- /dev/null +++ b/Basic C++ Programs/Prime or Not.txt @@ -0,0 +1,26 @@ +#include +using namespace std; + +int main() +{ + int n, i; + bool isPrime = true; + + cout << "Enter a positive integer: "; + cin >> n; + + for(i = 2; i <= n / 2; ++i) + { + if(n % i == 0) + { + isPrime = false; + break; + } + } + if (isPrime) + cout << "This is a prime number"; + else + cout << "This is not a prime number"; + + return 0; +} \ No newline at end of file