Hot Posts

3/recent/ticker-posts

Simple Programs solved with Cpp


# Program 1: Hello World

```cpp

#include <iostream>


int main() {

    std::cout << "Hello, World!";

    return 0;

}

```

Explanation: This program prints "Hello, World!" to the console.


# Program 2: Addition of Two Numbers

```cpp

#include <iostream>


int main() {

    int num1 = 5, num2 = 10, sum;

    sum = num1 + num2;

    std::cout << "Sum of " << num1 << " and " << num2 << " is: " << sum;

    return 0;

}

```

Explanation: Calculates and prints the sum of two numbers.


# Program 3: Factorial Calculation

```cpp

#include <iostream>


int main() {

    int n = 5, factorial = 1;

    for (int i = 1; i <= n; ++i) {

        factorial *= i;

    }

    std::cout << "Factorial of " << n << " is: " << factorial;

    return 0;

}

```

Explanation: Computes and displays the factorial of a given number (`n = 5` in this case).


# Program 4: Check Even or Odd

```cpp

#include <iostream>


int main() {

    int num = 6;

    if (num % 2 == 0) {

        std::cout << num << " is even.";

    } else {

        std::cout << num << " is odd.";

    }

    return 0;

}

```

Explanation: Determines if a number is even or odd (`num = 6` in this example).


# Program 5: Simple Interest Calculation

```cpp

#include <iostream>


int main() {

    float principal = 1000, rate = 5, time = 2, simple_interest;

    simple_interest = (principal * rate * time) / 100;

    std::cout << "Simple interest is: " << simple_interest;

    return 0;

}

```

Explanation: Calculates and displays simple interest based on principal amount, rate, and time (`principal = 1000`, `rate = 5`, `time = 2` in this example).


These programs demonstrate basic operations and calculations using C++. You can compile and run these programs using a C++ compiler to see their output.


Post a Comment

0 Comments