C++ Basic Programs Javatpoint

6 min read Jul 01, 2024
C++ Basic Programs Javatpoint

C++ Basic Programs - Javatpoint

C++ is a powerful, general-purpose programming language that is widely used in various applications, including system programming, game development, and data science. This article will explore some basic C++ programs, providing a foundation for understanding the language's syntax and concepts.

1. "Hello, World!" Program

This program is the traditional starting point for learning any programming language. It simply prints the message "Hello, World!" to the console.

#include 

using namespace std;

int main() {
    cout << "Hello, World!" << endl;
    return 0;
}

Explanation:

  • #include <iostream>: This line includes the input/output stream library, which provides the cout object for outputting data.
  • using namespace std;: This line brings the standard namespace into scope, avoiding the need to prefix standard elements (like cout) with std::.
  • int main() { ... }: This defines the main function, the entry point of every C++ program.
  • cout << "Hello, World!" << endl;: This line outputs the string "Hello, World!" to the console using the cout object. The endl manipulator inserts a newline character, moving the cursor to the next line.
  • return 0;: This statement indicates that the program executed successfully.

2. Adding Two Numbers

This program demonstrates how to take input from the user, perform a simple calculation, and display the result.

#include 

using namespace std;

int main() {
    int num1, num2, sum;

    cout << "Enter first number: ";
    cin >> num1;

    cout << "Enter second number: ";
    cin >> num2;

    sum = num1 + num2;

    cout << "Sum of two numbers: " << sum << endl;

    return 0;
}

Explanation:

  • int num1, num2, sum;: This line declares three integer variables to store the input numbers and the calculated sum.
  • cin >> num1; and cin >> num2;: These lines read input from the user and store it in the num1 and num2 variables, respectively.
  • sum = num1 + num2;: This line adds the two numbers and stores the result in the sum variable.
  • cout << "Sum of two numbers: " << sum << endl;: This line displays the result to the user.

3. Finding the Largest Number

This program compares two numbers and determines which one is larger.

#include 

using namespace std;

int main() {
    int num1, num2;

    cout << "Enter first number: ";
    cin >> num1;

    cout << "Enter second number: ";
    cin >> num2;

    if (num1 > num2) {
        cout << num1 << " is the largest number." << endl;
    } else {
        cout << num2 << " is the largest number." << endl;
    }

    return 0;
}

Explanation:

  • if (num1 > num2) { ... } else { ... }: This block of code uses an if statement to compare the two numbers.
  • cout << num1 << " is the largest number." << endl;: If num1 is greater than num2, this line displays the message accordingly.
  • cout << num2 << " is the largest number." << endl;: If num2 is greater than or equal to num1, this line displays the message accordingly.

4. Calculating the Factorial

This program calculates the factorial of a number entered by the user.

#include 

using namespace std;

int main() {
    int num, factorial = 1;

    cout << "Enter a number: ";
    cin >> num;

    for (int i = 1; i <= num; i++) {
        factorial *= i;
    }

    cout << "Factorial of " << num << " is " << factorial << endl;

    return 0;
}

Explanation:

  • int num, factorial = 1;: This line declares two integer variables: num to store the input number and factorial to store the calculated factorial (initialized to 1).
  • for (int i = 1; i <= num; i++) { ... }: This loop iterates from 1 to num.
  • factorial *= i;: Inside the loop, this line multiplies the current value of factorial with i, updating the factorial value.
  • cout << "Factorial of " << num << " is " << factorial << endl;: This line displays the calculated factorial to the user.

These basic programs demonstrate fundamental C++ concepts, including input/output operations, data types, arithmetic operators, conditional statements, and loops. They serve as a foundation for building more complex programs and exploring advanced C++ features.

Latest Posts