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 thecout
object for outputting data.using namespace std;
: This line brings the standard namespace into scope, avoiding the need to prefix standard elements (likecout
) withstd::
.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 thecout
object. Theendl
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;
andcin >> num2;
: These lines read input from the user and store it in thenum1
andnum2
variables, respectively.sum = num1 + num2;
: This line adds the two numbers and stores the result in thesum
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 anif
statement to compare the two numbers.cout << num1 << " is the largest number." << endl;
: Ifnum1
is greater thannum2
, this line displays the message accordingly.cout << num2 << " is the largest number." << endl;
: Ifnum2
is greater than or equal tonum1
, 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 andfactorial
to store the calculated factorial (initialized to 1).for (int i = 1; i <= num; i++) { ... }
: This loop iterates from 1 tonum
.factorial *= i;
: Inside the loop, this line multiplies the current value offactorial
withi
, 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.