Example in C ++
Example in C ++
int main()
{
int a=10;
int &b = a;
return 0;
}
Value of a: 10
Value of b: 10
Output
Output
Output
Before swapping.
a = 5, b = 10
After swapping.
a = 10, b = 5
Output
Enter an alphabet: u
u is a vowel.
Output
8.3
-4.2
Output
Enter three numbers: 2.3
8.3
-4.2
Output
8.3
-4.2
if (discriminant > 0) {
x1 = (-b + sqrt(discriminant)) / (2*a);
x2 = (-b - sqrt(discriminant)) / (2*a);
cout << "Roots are real and different." << endl;
cout << "x1 = " << x1 << endl;
cout << "x2 = " << x2 << endl;
}
else if (discriminant == 0) {
cout << "Roots are real and same." << endl;
x1 = (-b + sqrt(discriminant)) / (2*a);
cout << "x1 = x2 =" << x1 << endl;
}
else {
realPart = -b/(2*a);
imaginaryPart =sqrt(-discriminant)/(2*a);
cout << "Roots are complex and different." << endl;
cout << "x1 = " << realPart << "+" << imaginaryPart << "i" << endl;
cout << "x2 = " << realPart << "-" << imaginaryPart << "i" << endl;
}
return 0;
}
Output
x1 = -0.25
x2 = -1
Output
Factorial of 12 = 479001600
int main()
{
Test o1, o2;
float secondDataOfObject2;
o1.insertIntegerData(12);
secondDataOfObject2 = o2.insertFloatData();
cout << "You entered " << secondDataOfObject2;
return 0;
}
Output
Number: 12
C++ program to store and calculate the sum of 5 numbers entered by the user using
arrays.
#include <iostream>
using namespace std;
int main()
{
int numbers[5], sum = 0;
cout << "Enter 5 numbers: ";
// Storing 5 number entered by user in an array
// Finding the sum of numbers entered
for (int i = 0; i < 5; ++i)
{
cin >> numbers[i];
sum += numbers[i];
}
cout << "Sum = " << sum << endl;
return 0;
}
Output
Enter 5 numbers: 3
Sum = 18
#include <iostream>
using namespace std;
int main()
{
int test[3][2] =
{
{2, -5},
{4, 0},
{9, 1}
};
// Accessing two dimensional array using
// nested for loops
for(int i = 0; i < 3; ++i)
{
for(int j = 0; j < 2; ++j)
{
cou t<< "test[" << i << "][" << j << "] = " << test[i][j] << endl;
}
}
return 0;
}
Output
test[0][0] = 2
test[0][1] = -5
test[1][0] = 4
test[1][1] = 0
test[2][0] = 9
test[2][1] = 1
#include <iostream>
using namespace std;
const int CITY = 2;
const int WEEK = 7;
int main()
{
int temperature[CITY][WEEK];
cout << "Enter all temperature for a week of first city and then second
city. \n";
// Inserting the values into the temperature array
for (int i = 0; i < CITY; ++i)
{
for(int j = 0; j < WEEK; ++j)
{
cout << "City " << i + 1 << ", Day " << j + 1 << " : ";
cin >> temperature[i][j];
}
}
cout << "\n\nDisplaying Values:\n";
// Accessing the values from the temperature array
for (int i = 0; i < CITY; ++i)
{
for(int j = 0; j < WEEK; ++j)
{
cout << "City " << i + 1 << ", Day " << j + 1 << " = " <<
temperature[i][j] << endl;
}
}
return 0;
}
Output
Enter all temperature for a week of first city and then second city.
City 1, Day 1 : 32
City 1, Day 2 : 33
City 1, Day 3 : 32
City 1, Day 4 : 34
City 1, Day 5 : 35
City 1, Day 6 : 36
City 1, Day 7 : 38
City 2, Day 1 : 23
City 2, Day 2 : 24
City 2, Day 3 : 26
City 2, Day 4 : 22
City 2, Day 5 : 29
City 2, Day 6 : 27
City 2, Day 7 : 23
Displaying Values:
City 1, Day 1 = 32
City 1, Day 2 = 33
City 1, Day 3 = 32
City 1, Day 4 = 34
City 1, Day 5 = 35
City 1, Day 6 = 36
City 1, Day 7 = 38
City 2, Day 1 = 23
City 2, Day 2 = 24
City 2, Day 3 = 26
City 2, Day 4 = 22
City 2, Day 5 = 29
City 2, Day 6 = 27
City 2, Day 7 = 23
Example 3: Three Dimensional Array
C++ Program to Store value entered by user in three dimensional array and display it.
#include <iostream>
using namespace std;
int main()
{
// This array can store upto 12 elements (2x3x2)
int test[2][3][2];
cout << "Enter 12 values: \n";
// Inserting the values into the test array
// using 3 nested for loops.
for(int i = 0; i < 2; ++i)
{
for (int j = 0; j < 3; ++j)
{
for(int k = 0; k < 2; ++k )
{
cin >> test[i][j][k];
}
}
}
cout<<"\nDisplaying Value stored:"<<endl;
// Displaying the values with proper index.
for(int i = 0; i < 2; ++i)
{
for (int j = 0; j < 3; ++j)
{
for(int k = 0; k < 2; ++k)
{
cout << "test[" << i << "][" << j << "][" << k << "] = " <<
test[i][j][k] << endl;
}
}
}
return 0;
}
Output
Enter 12 values:
10
11
12
test[0][0][0] = 1
test[0][0][1] = 2
test[0][1][0] = 3
test[0][1][1] = 4
test[0][2][0] = 5
test[0][2][1] = 6
test[1][0][0] = 7
test[1][0][1] = 8
test[1][1][0] = 9
test[1][1][1] = 10
test[1][2][0] = 11
test[1][2][1] = 12