Example 1: Printing Numbers From 1 to 5
#include <iostream>
using namespace std;
int main() {
for (int i = 1; i <= 5; ++i) {
cout << i << " ";
return 0;
Display a text 5 times
// C++ Program to display a text 5 times
#include <iostream>
using namespace std;
int main() {
for (int i = 1; i <= 5; ++i) {
cout << "Hello World! " << endl;
return 0;
}
Find the sum of first n Natural Numbers
// C++ program to find the sum of first n natural numbers
// positive integers such as 1,2,3,...n are known as natural numbers
#include <iostream>
using namespace std;
int main() {
int num, sum;
sum = 0;
cout << "Enter a positive integer: ";
cin >> num;
for (int i = 1; i <= num; ++i) {
sum += i;
cout << "Sum = " << sum << endl;
return 0;