Sum of Naturalnumbers
#include <iostream>
using namespace std;
int main() {
int n, sum = 0, i = 1;
cout << "Enter a number: ";
cin >> n;
while (i <= n) {
sum += i; // Add i to sum
i++; // Increment i
}
cout << "Sum of first " << n << " numbers is: " << sum << endl;
return 0;
}
3.
Print Even numbers
#include<iostream>
using namespace std;
int main() {
int n, i;
cout << "Enter a number: ";
cin >> n;
while (i < n) {
i+=2; // Increment i
cout<< i << endl;
}
return 0;
}
4.
Do-While Loop
The do...whileloop is a variant of the while loop with one important difference: the
body of do...while loop is executed once before the condition is checked.
Syntax:
do {
// body of loop;
}
while (condition);
5.
Display Numbers from1 to 5
#include <iostream>
using namespace std;
int main() {
int i = 1;
// do...while loop from 1 to 5
do {
cout << i << " ";
++i;
}
while (i <= 5);
return 0;
}
6.
#include <iostream>
using namespacestd;
int main() {
int number = 0;
int sum = 0;
do {
sum += number;
// take input from the user
cout << "Enter a number: ";
cin >> number;
}
while (number >= 0);
// display the sum
cout << "n The sum is " << sum << endl;
return 0;
}
Program to find the sum of positive numbers If the user enters a negative number, the
loop ends the negative number entered is not added to the sum.
7.
Conditional Statements
You alreadyknow that C++ supports the usual logical conditions from mathematics:
• Less than: a < b
• Less than or equal to: a <= b
• Greater than: a > b
• Greater than or equal to: a >= b
• Equal to a == b
• Not Equal to: a != b
• You can use these conditions to perform different actions for different decisions.
• C++ has the following conditional statements:
• Use if to specify a block of code to be executed, if a specified condition is true
• Use else to specify a block of code to be executed, if the same condition is false
• Use switch to specify many alternative blocks of code to be executed
8.
IF Statement
Use theif statement to specify a block of C++ code to be executed if a condition is true.
Syntax:
if (condition) {
// block of code to be executed if the condition is true
}
Program to printpositive number entered by the user, If the user
enters a negative number, it is skipped.
#include <iostream>
using namespace std;
int main() {
int number;
cout << "Enter an integer: ";
cin >> number;
// checks if the number is positive
if (number > 0) {
cout << "You entered a positive integer: " << number << endl;
}
cout << "This statement is always executed.";
return 0;
}
12.
IF-Else Statement
Use theelse statement to specify a block of code to be executed if the condition is false.
The if statement can have an optional else clause.
Syntax:
if (condition) {
// block of code if condition is true
}
else {
// block of code if condition is false
}
13.
Compare time
• #include<iostream>
• using namespace std;
• int main() {
• int time = 20;
• if (time < 18) {
• cout << "Good day.";
• } else {
• cout << "Good evening.";
• }
• return 0;
• }
14.
Program to checkwhether an integer is positive or negative This
program considers 0 as a positive number
#include <iostream>
using namespace std;
int main() {
int number;
cout << "Enter an integer: ";
cin >> number;
if (number >= 0) {
cout << "You entered a positive integer: " << number << endl;
}
else {
cout << "You entered a negative integer: " << number << endl;
}
cout << "This line is always printed.";
return 0;
}
15.
Switch Statement
Use theswitch statement to select one of many code blocks to be executed.
Syntax:
switch(expression) {
case x:
// code block
break;
case y:
// code block
break;
default:
// code block
}
16.
Weekday number tocalculate the weekday name:
#include <iostream>
using namespace std;
int main() {
int day = 4;
switch (day) {
case 1:
cout << "Monday";
break;
case 2:
cout << "Tuesday";
break;
case 3:
cout << "Wednesday";
break;
case 4:
cout << "Thursday";
break;
case 5:
cout << "Friday";
break;
case 6:
cout << "Saturday";
break;
case 7:
cout << "Sunday";
break;}
return 0;}
17.
Program to builda simple calculator using switch Statement
#include <iostream>
using namespace std;
int main() {
char oper;
float num1, num2;
cout << "Enter an operator (+, -, *, /): ";
cin >> oper;
cout << "Enter two numbers: " << endl;
cin >> num1 >> num2;
switch (oper) {
case '+':
cout << num1 << " + " << num2 << " = " << num1 + num2;
break;
case '-':
cout << num1 << " - " << num2 << " = " << num1 - num2;
break;
case '*':
cout << num1 << " * " << num2 << " = " << num1 * num2;
break;
case '/':
cout << num1 << " / " << num2 << " = " << num1 / num2;
break;
default:
// operator is doesn't match any case constant (+, -, *, /)
cout << "Error! The operator is not correct";
break;
}
return 0;}