Sample Programs : Day 1
Find addition of two numbers
#include <iostream>
using namespace std;
int main()
{
int num1, num2;
cout << num1 << endl;
cout << num2 << endl;
cout << num1 + num2;
}
C++ Program to find the Curved Surface Area of a cylinder (CSA) (CSA = 2 pi r * h)
#include <iostream>
using namespace std;
int main()
{
float pi = 3.14, radius, height, CSA;
cout << "\n Curved Surface Area of a cylinder";
cout << "\n Enter Radius (in cm): ";
cin >> radius;
cout << "\n Enter Height (in cm): ";
cin >> height;
CSA = (2*pi*radius)*height;
cout << "\n Radius: " << radius <<"cm";
cout << "\n Height: " << height << "cm";
cout << "\n Curved Surface Area of a Cylinder is " << CSA <<" sq. cm.";
}
Write a C++ Program to add two numbers and save it in sum variable.
#include <iostream>
using namespace std;
int main()
{
int num1, num2;
cout << "\n Enter number 1: ";
cin >> num1;
cout << "\n Enter number 2: ";
cin >> num2;
int sum = num1 + num2; // Dynamic initialization
cout << "\n Average: " << sum /2;
}
C++ program to find the perimeter and area of a semi circle
#include <iostream>
using namespace std;
int main()
{
int radius;
float pi = 3.14;
cout << "\n Enter Radius (in cm): ";
cin >> radius;
float perimeter = (pi+2)*radius; // dynamic initialization
float area = (pi*radius*radius)/2; // dynamic initialization
cout << "\n Perimeter of the semicircle is " << perimeter << " cm";
cout << "\n Area of the semicircle is " << area << " [Link]";
}
Write a c++ program to demonstrate const keyword.
#include <iostream>
using namespace std;
int main()
{
const int num=100;
cout << "\n Value of num is = " << num;
num = num + 1; // Trying to increment the constant
cout << "\n Value of num after increment " << num;
}
Byte size and range in C++:
#include <iostream>
#include <climits> // Integer limits
#include <cfloat> // Floating-point limits
using namespace std;
int main() {
// char
cout << "Size of char: " << sizeof(char) << " byte" << endl;
cout << "Signed char range: " << SCHAR_MIN << " to " << SCHAR_MAX << endl;
cout << "Unsigned char range: 0 to " << UCHAR_MAX << endl << endl;
// int
cout << "Size of int: " << sizeof(int) << " bytes" << endl;
cout << "int range: " << INT_MIN << " to " << INT_MAX << endl << endl;
// unsigned int
cout << "Size of unsigned int: " << sizeof(unsigned int) << " bytes" << endl;
cout << "unsigned int range: 0 to " << UINT_MAX << endl << endl;
// long
cout << "Size of long: " << sizeof(long) << " bytes" << endl;
cout << "long range: " << LONG_MIN << " to " << LONG_MAX << endl << endl;
// unsigned long
cout << "Size of unsigned long: " << sizeof(unsigned long) << " bytes" << endl;
cout << "unsigned long range: 0 to " << ULONG_MAX << endl << endl;
// long long
cout << "Size of long long: " << sizeof(long long) << " bytes" << endl;
cout << "long long range: " << LLONG_MIN << " to " << LLONG_MAX << endl << endl;
// unsigned long long
cout << "Size of unsigned long long: " << sizeof(unsigned long long) << " bytes" << endl;
cout << "unsigned long long range: 0 to " << ULLONG_MAX << endl << endl;
// float
cout << "Size of float: " << sizeof(float) << " bytes" << endl;
cout << "float range: " << -FLT_MAX << " to " << FLT_MAX << endl << endl;
// double
cout << "Size of double: " << sizeof(double) << " bytes" << endl;
cout << "double range: " << -DBL_MAX << " to " << DBL_MAX << endl;
return 0;
}