This program is printing "Hello Programming School". It introduces the basic structure of a C++ Program. It include the '#include <iostream>' line, which is a preprocessor directive to include the standard input/output library. The 'main' function is the entry point of the program, and 'cout' is used to print the "Hello Programming School!" message to the console.
Variables and Input:
#include <iostream>
using namespace std;
int main() {
int color;
cout<<"Enter your favorite color: ";
cin>> color;
cout<<"You entered: " <<color <<endl;
return 0;
}
• Declares an integer variable ('color').
• Takes user input for the variable using 'cin'.
• Prints the entered value using 'cout'.
Arithmetic Operations:
#include <iostream>
using namespace std;
int main() {
int a = 2, b = 4;
cout<<"Sum: " << a + b <<endl;
cout<<"Difference: " << a - b << endl;
cout<<"Product: " << a * b <<endl;
cout<<"Quotient: " << a / b <<endl;
return 0;
}
• ' int a = 2 , b = 4; ' : Declares and initializes two integer variables, ' a ' and ' b ' .
• Various arithmetic operations are performed (' + , - , * , / ') and the results are printed using ' cout ' .
Conditional Statements:
#include <iostream>
using namespace std;
int main() {
int number;
cout<<"Enter a number: ";
cin>>number;
if (number > 0) {
cout<<" The number is positive." <<endl;
} else if (number < 0) {
cout<<"The number is negative." <<endl;
} else {
cout<< "The number is zero." <<endl;
}
return 0;
}
• ' int number; ' : Declares an integer variable named ' number ' .
• ' cin>> number; ' : Reads an integer input from the user.
• Conditional statements (' if ' , ' else if ' , ' else ') check whether the number is positive , negative , or zero.
• Results are printed accordingly using ' cout '.
For Loop:
#include <iostream>
using namespace std;
int main() {
for (int i = 1; i <= 5; i++) {
cout<< i << " ";
}
cout<<endl;
return 0;
}
• ' for (int i = 1; i <= 5; i++) ' : Initializes a loop variable ' i ' to 1; continues as long as ' i ' is less than or equal to 5; increments ' i ' in each iteration.
• ' cout << i << " "; ' : Prints the value of ' i ' and a space in each iteration.
• ' cout << endl; ' " Prints a newline after the loop.
While Loop:
#include <iostream>
using namespace std;
int main() {
int i = 1;
while (i <= 5) {
cout<< i <<" ";
i++;
}
cout<< endl;
return 0;
}
• ' int i = 1; ' : Initializes a loop variable ' i ' to 1.
• ' while (i <= 5) ' : Executes the loop while ' i ' is less than or equal t 5.
• ' cout << i << " "; ' : Prints the value of ' i ' and a space in each iteration.
• ' i++; ' : Increments ' i ' in each iteration.
• ' cout << endl; ' : Prints a newline after the loop.
Function:
#include <iostream>
using namespace std;
int add(int a, int b) {
return a + b;
}
int main() {
int result = add(3, 4);
cout<< "Sum: " << result <<endl;
return 0;
}
• ' int add(int a, int b) { return a + b; } ' : Declares a function named ' add ' that takes two integer parameters and returns their sum.
• ' int result = add(3, 4); ' : Calls the ' add ' function with arguments 3 and 4 and stores the result in the variable ' result '.
• cout << "Sum: " << result <<endl; ' : Prints the sum.