Skip to content

Commit 0cf8e08

Browse files
Add files via upload
1 parent b1e5ec6 commit 0cf8e08

15 files changed

+475
-0
lines changed

10whileloop.cpp

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#include <iostream>
2+
using namespace std;
3+
4+
int main()
5+
{
6+
int i = 0;
7+
while (i < 5)
8+
{
9+
cout << i << "\n";
10+
i++;
11+
}
12+
13+
return 0;
14+
}
15+
16+
//The while loop loops through a block of code as long as a specified condition is true:

11dowhileloop.cpp

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#include <iostream>
2+
using namespace std;
3+
4+
int main()
5+
{
6+
int i = 0;
7+
do
8+
{
9+
cout << i << "\n";
10+
i++;
11+
} while (i < 5);
12+
13+
return 0;
14+
}
15+
16+
//The do/while loop is a variant of the while loop. This loop will execute the code block once, before checking if the condition is true, then it will repeat the loop as long as the condition is true.

12forloop.cpp

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#include <iostream>
2+
using namespace std;
3+
4+
int main()
5+
{
6+
for (int i = 0; i < 5; i++)
7+
{
8+
cout << i << "\n";
9+
}
10+
11+
cout<<"\n";
12+
13+
for (int i = 0; i <= 10; i = i + 2)
14+
{
15+
cout << i << "\n";
16+
}
17+
18+
return 0;
19+
}
20+
21+
//When you know exactly how many times you want to loop through a block of code, use the for loop instead of a while loop
22+
// Syntax: for (statement 1; statement 2; statement 3) {
23+
// code block to be executed
24+
// }
25+
//Statement 1 is executed (one time) before the execution of the code block.
26+
//Statement 2 defines the condition for executing the code block.
27+
//Statement 3 is executed (every time) after the code block has been executed.

13breakandcontinue.cpp

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
#include <iostream>
2+
using namespace std;
3+
4+
int main()
5+
{
6+
for (int i = 0; i < 10; i++)
7+
{
8+
if (i == 4)
9+
{
10+
break;
11+
}
12+
cout << i << "\n";
13+
}
14+
15+
cout << "\n";
16+
17+
for (int i = 0; i < 10; i++)
18+
{
19+
if (i == 4)
20+
{
21+
continue;
22+
}
23+
cout << i << "\n";
24+
}
25+
26+
return 0;
27+
}
28+
29+
// You have already seen the break statement used in an earlier program of 9switchstatement.
30+
// It was used to "jump out" of a switch statement.
31+
// The break statement can also be used to jump out of a loop.

14math.cpp

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
// C++ has many functions that allows you to perform mathematical tasks on numbers.
2+
// For that we have to include cmath libary (header file)
3+
4+
#include<iostream>
5+
#include<cmath> //in other old compilers it is #include<math.h>
6+
using namespace std;
7+
8+
int main()
9+
{
10+
cout << max(5, 10);
11+
cout << min(5, 10);
12+
cout << sqrt(64);
13+
cout << round(2.6);
14+
cout << log(2);
15+
16+
return 0;
17+
}
18+
19+
/*
20+
21+
A list of other popular Math functions (from the <cmath> library) can be found in the table below:
22+
23+
Function Description
24+
abs(x) Returns the absolute value of x
25+
acos(x) Returns the arccosine of x
26+
asin(x) Returns the arcsine of x
27+
atan(x) Returns the arctangent of x
28+
cbrt(x) Returns the cube root of x
29+
ceil(x) Returns the value of x rounded up to its nearest integer
30+
cos(x) Returns the cosine of x
31+
cosh(x) Returns the hyperbolic cosine of x
32+
exp(x) Returns the value of Ex
33+
expm1(x) Returns ex -1
34+
fabs(x) Returns the absolute value of a floating x
35+
fdim(x, y) Returns the positive difference between x and y
36+
floor(x) Returns the value of x rounded down to its nearest integer
37+
hypot(x, y) Returns sqrt(x2 +y2) without intermediate overflow or underflow
38+
fma(x, y, z) Returns x*y+z without losing precision
39+
fmax(x, y) Returns the highest value of a floating x and y
40+
fmin(x, y) Returns the lowest value of a floating x and y
41+
fmod(x, y) Returns the floating point remainder of x/y
42+
pow(x, y) Returns the value of x to the power of y
43+
sin(x) Returns the sine of x (x is in radians)
44+
sinh(x) Returns the hyperbolic sine of a double value
45+
tan(x) Returns the tangent of an angle
46+
tanh(x) Returns the hyperbolic tangent of a double value
47+
48+
/*

15calculator.cpp

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
//It is a program calculates sum, sub, mul, div, and modulus of 2 numbers
2+
#include <iostream>
3+
using namespace std;
4+
5+
int main()
6+
{
7+
8+
int x, y;
9+
int sum;
10+
int sub;
11+
int mul;
12+
int div;
13+
int mod;
14+
15+
cout << "Type a number:\n";
16+
cin >> x;
17+
cout << "Type another number:\n";
18+
cin >> y;
19+
sum = x + y;
20+
sub = x - y;
21+
mul = x * y;
22+
div = x / y;
23+
mod = x % y;
24+
25+
cout << "Addition is: " << sum << "\n";
26+
cout << "Subtraction is: " << sub << "\n";
27+
cout << "Multiplication is: " << mul << "\n";
28+
cout << "Division is: " << div << "\n";
29+
cout << "Modulus is: " << mod;
30+
31+
return 0;
32+
}

1printhelloworld.cpp

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
//Program to print Hello World
2+
#include <iostream>
3+
using namespace std;
4+
int sum(int,int);
5+
int main() {
6+
cout <<sum(10,20);
7+
}
8+
int sum(int a, int b)
9+
{
10+
return a+b;
11+
}
12+
/* Example Explained below:
13+
Line 1: #include <iostream> is a header file library that lets us work with input and output objects, such as cout (used in line 5). Header files add functionality to C++ programs.
14+
15+
Line 2: using namespace std means that we can use names for objects and variables from the standard library.
16+
17+
Don't worry if you don't understand how #include <iostream> and using namespace std works. Just think of it as something that (almost) always appears in your program.
18+
19+
Line 3: A blank line. C++ ignores white space.
20+
21+
Line 4: Another thing that always appear in a C++ program, is int main(). This is called a function. Any code inside its curly brackets {} will be executed.
22+
23+
Line 5: cout (pronounced "see-out") is an object used together with the insertion operator (<<) to output/print text. In our example it will output "Hello World".
24+
*/
25+
26+
27+
//In turbo C++ and other old compliers it is like this :
28+
//#include<iostream.h> //For input output operations
29+
//#include<conio.h> For //using getch() or clrscr()
30+
//void main() //For main function we use void to avoid writing return 0 in end of program
31+
//{
32+
// cout<<"Hello World";
33+
// getch();
34+
//}

2comment.cpp

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/*
2+
3+
Comments can be used to explain C++ code, and to make it more readable. It can also be used to prevent execution when testing alternative code.
4+
Comments can be singled-lined or multi-lined.
5+
6+
Single-line comments start with two forward slashes (//).
7+
8+
Any text between // and the end of the line is ignored by the compiler
9+
10+
Example Below
11+
*/
12+
13+
#include<iostream>
14+
using namespace std;
15+
int main()
16+
{
17+
// This is a comment
18+
cout<< "Hello World!";
19+
20+
/* The code below will print the word Bye!
21+
to the screen, and it is amazing */
22+
cout << " Bye!";
23+
24+
return 0;
25+
}

3variables.cpp

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
#include <iostream>
2+
using namespace std;
3+
4+
int main() {
5+
int myNum = 15; //Integer variable
6+
cout << myNum<<"\n"; // \n is used to go to next line
7+
8+
int myNumber; //Integer variable
9+
myNumber = 15; //Variable Initialization : Giving value to a variable
10+
cout << myNumber<<"\n"; //For output of given variable
11+
12+
int num = 5; // Integer (whole number without decimals)
13+
double myFloatNum = 5.99; // Floating point number (with decimals)
14+
char myLetter = 'D'; // Character
15+
string myText = "Hello"; // String (text)
16+
bool myBoolean = true; // Boolean (true(1) or false(0))
17+
18+
cout<<"Integer : "<<num<<"\n";
19+
cout<<"Float : "<<myFloatNum<<"\n";
20+
cout<<"Character : "<<myLetter<<"\n";
21+
cout<<"String : "<<myText<<"\n";
22+
cout<<"Boolean : "<<myBoolean<<"\n";
23+
24+
25+
return 0;
26+
}
27+
28+
29+
// Variables are containers for storing data values.
30+
31+
// In C++, there are different types of variables (defined with different keywords), for example:
32+
33+
// int - stores integers (whole numbers), without decimals, such as 123 or -123
34+
// double - stores floating point numbers, with decimals, such as 19.99 or -19.99
35+
// char - stores single characters, such as 'a' or 'B'. Char values are surrounded by single quotes
36+
// string - stores text, such as "Hello World". String values are surrounded by double quotes
37+
// bool - stores values with two states: true or false
38+
// Syntax: type variable = value;

4takeuserinput.cpp

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
//ou have already learned that cout is used to output (print) values. Now we will use cin to get user input.
2+
//cin is a predefined variable that reads data from the keyboard with the extraction operator (>>).
3+
4+
#include <iostream>
5+
using namespace std;
6+
7+
int main() {
8+
int x;
9+
cout << "Type a number: "; // Type a number and press enter
10+
cin >> x; // Get user input from the keyboard
11+
cout << "Your number is: " << x; // Display the input value
12+
13+
return 0;
14+
}

5datatypes.cpp

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// As explained in the 3variables program , a variable in C++ must be a specified data type:
2+
// Can see output of below using cut<<variablename;
3+
4+
#include <iostream>
5+
using namespace std;
6+
7+
int main()
8+
{
9+
int myNum = 5; // Integer (whole number)
10+
float myFloatNum = 5.99; // Floating point number
11+
double myDoubleNum = 9.98; // Floating point number
12+
char myLetter = 'D'; // Character
13+
bool myBoolean = true; // Boolean
14+
string myText = "Hello"; // String
15+
16+
return 0;
17+
}
18+
19+
/*
20+
21+
22+
Data Type Size Description
23+
int 4 bytes Stores whole numbers, without decimals
24+
float 4 bytes Stores fractional numbers, containing one or more decimals. Sufficient for storing 7 decimal digits
25+
double 8 bytes Stores fractional numbers, containing one or more decimals. Sufficient for storing 15 decimal digits
26+
boolean 1 byte Stores true or false values
27+
char 1 byte Stores a single character/letter/number, or ASCII values
28+
29+
*/

0 commit comments

Comments
 (0)