C++ Programs@2020
C++ Programs@2020
10/28/2020
Basic C++ Programs...
[OM DWIVEDI]
C++ "Hello, World!" Program
// Your First C++ Program
#include <iostream>
int main() {
std::cout << "Hello World!";
return 0;
}
Output
Hello World!
9956774468om@123 udemy
Example: Print Number Entered by User
#include <iostream>
using namespace std;
int main()
{
int number;
Output
Enter an integer: 23
You entered 23
Example: Program to Add Two Integers
#include <iostream>
using namespace std;
int main()
{
int firstNumber, secondNumber, sumOfTwoNumbers;
// Prints sum
cout << firstNumber << " + " << secondNumber << " = " <<
sumOfTwoNumbers;
return 0;
}
Output
int main()
{
int divisor, dividend, quotient, remainder;
return 0;
}
Output
Enter dividend: 13
Enter divisor: 4
Quotient = 3
Remainder = 1
Example: Find Size of a Variable
#include <iostream>
using namespace std;
int main()
{
cout << "Size of char: " << sizeof(char) << " byte" << endl;
cout << "Size of int: " << sizeof(int) << " bytes" << endl;
cout << "Size of float: " << sizeof(float) << " bytes" << endl;
cout << "Size of double: " << sizeof(double) << " bytes" << endl;
return 0;
}
Output
int main()
{
int a = 5, b = 10, temp;
temp = a;
a = b;
b = temp;
return 0;
}
Output
Before swapping.
a = 5, b = 10
After swapping.
a = 10, b = 5
Example 1: Check Whether Number is Even or Odd using if else
#include <iostream>
using namespace std;
int main()
{
int n;
if ( n % 2 == 0)
cout << n << " is even.";
else
cout << n << " is odd.";
return 0;
}
Output
Enter an integer: 23
23 is odd.
int main()
{
int n;
(n % 2 == 0) ? cout << n << " is even." : cout << n << " is odd.";
return 0;
}
Example: Check Vowel or a Consonant Manually
#include <iostream>
using namespace std;
int main()
{
char c;
int isLowercaseVowel, isUppercaseVowel;
return 0;
}
Output
Enter an alphabet: u
u is a vowel.
int main() {
float n1, n2, n3;
return 0;
}
Output
int main() {
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/(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
This program takes a positive integer from user (suppose user entered n) then, this program
displays the value of 1+2+3+.... +n.
int main()
{
int n, sum = 0;
Output
For example: 2012, 2004, 1968 etc are leap year but, 1971, 2006 etc are not leap year.
Similarly, 1200, 1600, 2000, 2400 are leap years but, 1700, 1800, 1900 etc are not.
int main()
{
int year;
if (year % 4 == 0)
{
if (year % 100 == 0)
{
if (year % 400 == 0)
cout << year << " is a leap year.";
else
cout << year << " is not a leap year.";
}
else
cout << year << " is a leap year.";
}
else
cout << year << " is not a leap year.";
return 0;
}
Output
factorial = 1*2*3...*n
In this program below, user is asked to enter a positive integer. Then the factorial of that
number is computed and displayed in the screen.
int main()
{
unsigned int n;
unsigned long long factorial = 1;
cout << "Factorial of " << n << " = " << factorial;
return 0;
}
Output
int main()
{
int n;
cout << "Enter a positive integer: ";
cin >> n;
return 0;
}
Output
Enter an integer: 5
5 * 1 = 5
5 * 2 = 10
5 * 3 = 15
5 * 4 = 20
5 * 5 = 25
5 * 6 = 30
5 * 7 = 35
5 * 8 = 40
5 * 9 = 45
5 * 10 = 50
int main()
{
int n, range;
return 0;
}
Output
Enter an integer: 8
Enter range: 12
8 * 1 = 8
8 * 2 = 16
8 * 3 = 24
8 * 4 = 32
8 * 5 = 40
8 * 6 = 48
8 * 7 = 56
8 * 8 = 64
8 * 9 = 72
8 * 10 = 80
8 * 11 = 88
8 * 12 = 96
int main()
{
int n, t1 = 0, t2 = 1, nextTerm = 0;
Output
int main()
{
int t1 = 0, t2 = 1, nextTerm = 0, n;
nextTerm = t1 + t2;
while(nextTerm <= n)
{
cout << nextTerm << ", ";
t1 = t2;
t2 = nextTerm;
nextTerm = t1 + t2;
}
return 0;
}
Output
int main()
{
int n1, n2;
while(n1 != n2)
{
if(n1 > n2)
n1 -= n2;
else
n2 -= n1;
}
Output
int main () {
int n1, n2, hcf;
cout << "Enter two numbers: ";
cin >> n1 >> n2;
int main()
{
int n1, n2, max;
do
{
if (max % n1 == 0 && max % n2 == 0)
{
cout << "LCM = " << max;
break;
}
else
++max;
} while (true);
return 0;
}
Output
#include <iostream>
using namespace std;
int main()
{
int n1, n2, hcf, temp, lcm;
hcf = n1;
temp = n2;
while(hcf != temp)
{
if(hcf > temp)
hcf -= temp;
else
temp -= hcf;
}
#include <iostream>
using namespace std;
int main() {
int n, reversedNumber = 0, remainder;
while(n != 0) {
remainder = n%10;
reversedNumber = reversedNumber*10 + remainder;
n /= 10;
}
return 0;
}
Output
int main()
{
int exponent;
float base, result = 1;
cout << base << "^" << exponent << " = ";
while (exponent != 0) {
result *= base;
--exponent;
}
return 0;
}
Output
int main()
{
float base, exponent, result;
cout << "Enter base and exponent respectively: ";
cin >> base >> exponent;
cout << base << "^" << exponent << " = " << result;
return 0;
}
Output
class Check
{
private:
int i;
public:
Check(): i(0) { }
void operator ++()
{ ++i; }
void Display()
{ cout << "i=" << i << endl; }
};
int main()
{
Check obj;
return 0;
}
Output
i=0
i=1
class Check
{
private:
int i;
public:
Check(): i(0) { }
return temp;
}
void Display()
{ cout << "i = " << i << endl; }
};
int main()
{
Check obj, obj1;
obj.Display();
obj1.Display();
obj1 = ++obj;
obj.Display();
obj1.Display();
return 0;
}
Output
i = 0
i = 0
i = 1
i = 1
Example 3: Postfix Increment ++ Operator Overloading
Overloading of increment operator up to this point is only true if it is used in prefix form.
This is the modification of above program to make this work both for prefix form and
postfix form.
#include <iostream>
using namespace std;
class Check
{
private:
int i;
public:
Check(): i(0) { }
Check operator ++ ()
{
Check temp;
temp.i = ++i;
return temp;
}
void Display()
{ cout << "i = "<< i <<endl; }
};
int main()
{
Check obj, obj1;
obj.Display();
obj1.Display();
return 0;
}
Output
i = 0
i = 0
i = 1
i = 1
i = 2
i = 1
#include <iostream>
using namespace std;
class Check
{
private:
int i;
public:
Check(): i(3) { }
Check operator -- ()
{
Check temp;
temp.i = --i;
return temp;
}
void Display()
{ cout << "i = "<< i <<endl; }
};
int main()
{
Check obj, obj1;
obj.Display();
obj1.Display();
return 0;
}
Output
i = 3
i = 3
i = 2
i = 2
i = 1
i = 2
class Complex
{
private:
float real;
float imag;
public:
Complex(): real(0), imag(0){ }
void input()
{
cout << "Enter real and imaginary parts respectively: ";
cin >> real;
cin >> imag;
}
// Operator overloading
Complex operator - (Complex c2)
{
Complex temp;
temp.real = real - c2.real;
temp.imag = imag - c2.imag;
return temp;
}
void output()
{
if(imag < 0)
cout << "Output Complex number: "<< real << imag << "i";
else
cout << "Output Complex number: " << real << "+" << imag
<< "i";
}
};
int main()
{
Complex c1, c2, result;
return 0;
}
int main ()
{
char c;
cout << "Enter a character: ";
cin >> c;
cout << "ASCII Value of " << c << " is " << int(c);
return 0;
}
Output
Enter a character: p
ASCII Value of p is 112
C++Program to Multiply Two Numbers
#include <iostream>
using namespace std;
int main()
{
double firstNumber, secondNumber, productOfTwoNumbers;
cout << "Enter two numbers: ";
return 0;
}
Output
int main()
{
int n, num, digit, rev = 0;
n = num;
do
{
digit = num % 10;
rev = (rev * 10) + digit;
num = num / 10;
} while (num != 0);
cout << " The reverse of the number is: " << rev << endl;
if (n == rev)
cout << " The number is a palindrome.";
else
cout << " The number is not a palindrome.";
return 0;
}
Output
For example: 13 is a prime number because it is only divisible by 1 and 13 but, 15 is not
prime number because it is divisible by 1, 3, 5 and 15.
int main() {
int i, n;
bool isPrime = true;
return 0;
}
Output
int main() {
int low, high, i;
bool isPrime = true;
cout << "\nPrime numbers between " << low << " and " << high << "
are: " << endl;
if (isPrime)
cout << low << " ";
++low;
}
return 0;
}
Output
Example #2: Display Prime Numbers When Larger Number is Entered first
#include <iostream>
using namespace std;
int main()
{
int low, high, temp, i;
bool isPrime;
if (isPrime)
cout << low << " ";
++low;
}
return 0;
}
Output
abcd... = an + bn + cn + dn + ...
In the case of an Armstrong number of 3 digits, the sum of cubes of each digit is equal to
the number itself. For example, 153 is an Armstrong number because
int main() {
int num, originalNum, remainder, result = 0;
cout << "Enter a three-digit integer: ";
cin >> num;
originalNum = num;
while (originalNum != 0) {
// remainder contains the last digit
remainder = originalNum % 10;
if (result == num)
cout << num << " is an Armstrong number.";
else
cout << num << " is not an Armstrong number.";
return 0;
}
Output
int main() {
int num, originalNum, remainder, n = 0, result = 0, power;
cout << "Enter an integer: ";
cin >> num;
originalNum = num;
while (originalNum != 0) {
originalNum /= 10;
++n;
}
originalNum = num;
while (originalNum != 0) {
remainder = originalNum % 10;
Output
If you don't know how to check whether a number is Armstrong or not in programming
then, this program may seem little complex.
int main()
{
int num1, num2, i, num, digit, sum;
cout << "Armstrong numbers between " << num1 << " and " << num2 << "
are: " << endl;
for(i = num1; i <= num2; i++)
{
sum = 0;
num = i;
for(; num > 0; num /= 10)
{
digit = num % 10;
sum = sum + digit * digit * digit;
}
if(sum == i)
{
cout << i << endl;
}
}
return 0;
}
Output
int main()
{
int n, i;
return 0;
}
Output
Source Code
#include <iostream>
using namespace std;
int main()
{
int rows;
Source Code
#include <iostream>
using namespace std;
int main()
{
int rows;
Source Code
#include <iostream>
using namespace std;
int main()
{
char input, alphabet = 'A';
cout << "Enter the uppercase character you want to print in the last
row: ";
cin >> input;
Source Code
#include <iostream>
using namespace std;
int main()
{
int rows;
return 0;
}
Source Code
#include <iostream>
using namespace std;
int main()
{
int rows;
return 0;
}
Programs to display pyramid and inverted pyramid using * and digits
Source Code
#include <iostream>
using namespace std;
int main()
{
int space, rows;
while(k != 2*i-1)
{
cout << "* ";
++k;
}
cout << endl;
}
return 0;
}
Example 7: Program to print pyramid using numbers
1
2 3 2
3 4 5 4 3
4 5 6 7 6 5 4
5 6 7 8 9 8 7 6 5
Source Code
#include <iostream>
using namespace std;
int main()
{
int rows, count = 0, count1 = 0, k = 0;
while(k != 2*i-1)
{
if (count <= rows-1)
{
cout << i+k << " ";
++count;
}
else
{
++count1;
cout << i+k-2*count1 << " ";
}
++k;
}
count1 = count = k = 0;
Source Code
#include <iostream>
using namespace std;
int main()
{
int rows;
return 0;
}
Example 9: Print Pascal's triangle
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
1 5 10 10 5 1
Source Code
#include <iostream>
using namespace std;
int main()
{
int rows, coef = 1;
return 0;
}
Example 10: Print Floyd's Triangle.
1
2 3
4 5 6
7 8 9 10
Source Code
#include <iostream>
using namespace std;
int main()
{
int rows, number = 1;
return 0;
}