C++ Programs
Explore the list of my C++ programs below:
Conditional Statement
-
Basic Arithamatic Operators
#include <iostream> using namespace std; int main() { int a, b; cout << "enter any number="; cin >> a; cout << "enter any number="; cin >> b; cout << "\nsum of two number=" << a + b << endl; cout << "diff of two number=" << a - b << endl; cout << "division of two number="<< a / b << endl; cout << "product of two number=" << a * b << endl; cout << "modulo of two number=" << a % b << endl; } -
Sum of Even/Odd
#include <iostream> using namespace std; int main() { int n, c; cout << "Choose\n1.for Even\n2.for Odd\n Enter(1/2): "; cin >> c; cout << "Sum upto ? terms: "; cin >> n; cout << "Sum is = " << (c==1 ? n*(n+1) : n*n) << endl; return 0; }
Loops
-
A-Z Character Pattern Print
#include <iostream> using namespace std; int main() { int n; char ch = 'A'; cout << "enter the no. of rows :"; cin >> n; for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { cout << ch << " "; ch++; } cout << endl; } cout << "end value of ch is =" << ch; return 0; } -
Star Matrix
#include <iostream> using namespace std; int main() { int n; cout << "enter n= "; cin >> n; for (int i = 0; i < n; i++) { for (int k = 0; k < n - i - 1; k++) cout << " "; for (int j = 0; j < i + 1; j++) cout << "*"; for (int j = 1; j <= i; j++) cout << "*"; cout << endl; } for (int i = 0; i < n; i++) { for (int k = 0; k < i + 1; k++) cout << " "; for (int j = 1; j <= n - i - 1; j++) cout << "*"; for (int j = 1; j < n - i - 1; j++) cout << "*"; cout << endl; } return 0; } -
Reverse Triangle
#include <iostream> using namespace std; int main() { int n; cout << "enter the no. of rows :"; cin >> n; char ch = 'A'; for (int i = 0; i < n; i++) { for (int j = 1; j < i + 1; j++) cout << " "; for (int j = 0; j < n - i; j++) cout << ch; cout << endl; ch++; } return 0; } -
Pyramid Pattern
#include <iostream> using namespace std; int main(){ int n=4; for(int i=0;i<n;i++){ // spaces for(int k=0;k<n-i-1;k++){ cout<<" "; } // 1st triangle for(int j=1;j<i+1;j++){ cout<<j; } // 2nd triangle for(int j=i+1;j>0;j--){ cout<<j; } cout<<endl; } return 0; } -
Diamond Pattern
#include <iostream> using namespace std; int main() { int n = 9; // top part for (int i = 0; i < n; i++) { // left side spaces for (int k = 0; k < n - i - 1; k++) cout << " "; cout << "*"; if (i != 0) // spaces in middle for (int j = 0; j < 2 * i - 1; j++) cout << " "; cout << "*"; cout << endl; } // bottom part for (int i = 0; i < n - 1; i++) { // left side spaces for (int k = 0; k < i + 1; k++) { cout << " "; } cout << "*"; if (i != n - 2) // spaces in middle for (int j = 0; j < 2 * (n - i - 2) - 1; j++) cout << " "; cout << "*"; cout << endl; } return 0; }
Function
-
Factorial
#include <iostream> using namespace std; int fact(int n) { if (n == 0 or n == 1) return 1; else return n * fact(n - 1); } int main() { int n; cout << "Enter a number: "; cin >> n; cout << "Factorial of " << n << " is " << fact(n) << endl; return 0; } -
Sum Of Digits
#include <iostream> using namespace std; int sumOfDigit(int n) { int sum_of_digit = 0; while (n > 0) { int last_digit = n % 10; n /= 10; sum_of_digit += last_digit; } return sum_of_digit; } int main() { int n; cout << "Enter a number: "; cin >> n; cout << "Sum of digits is: " << sumOfDigit(n) << endl; return 0; } -
nCr (Binomial Coefficient)
#include <iostream> using namespace std; int fact(int n) { if (n == 0 or n == 1) return 1; else return n * fact(n - 1); } int nCr(int n, int r) { int nmr = n - r; return fact(n) / (fact(r) * fact(nmr)); } int main() { int n, r; cout << "Enter n: "; cin >> n; cout << "Enter r: "; cin >> r; cout << "---Factorial of--- " << nCr(n, r) << endl; return 0; } -
Prime Check
#include <iostream> using namespace std; bool isPrime(int n) { for (int i = 2; i * i <= n; i++) { if (n % i == 0) return false; } return n > 1; } int main() { int n; cout << "Enter a number: "; cin >> n; for (int i = 2; i <= n; i++) { if (isPrime(i)) cout << i << " "; } cout << endl; return 0; } -
Fibonacci
#include <iostream> using namespace std; int fib(int n) { if (n == 0) return 0; if (n == 1) return 1; return fib(n - 1) + fib(n - 2); } int main() { int n; cout << "Enter a number: "; cin >> n; cout << "---Fibonacci Series---" << endl; for (int i = 0; i < n; i++) { cout << fib(i) << " "; } cout << endl; return 0; } -
Reverse of Number
#include <iostream> using namespace std; int reverse(int n) { int rev = 0; while (n > 0) { int lastDigit = n % 10; rev = rev * 10 + lastDigit; n /= 10; } return rev; } int main() { int n; cout << "Enter a number: "; cin >> n; cout << "\nReverse of " << n << " is " << reverse(n) << "\n\n"; return 0; }
OOPS
-
Prime Numbers Using Class Inheritance
#include <iostream> using namespace std; class A { public: bool isPrime(int n) { for (int i = 2; i * i <= n; i++) { if (n % i == 0) return false; } return n > 1; } }; class B : public A { protected: // Changed to protected so child class can access it int num; public: void getData() { cout << "Enter a number: "; cin >> num; } void PrintData() { for (int i = 2; i <= num; i++) // Used the 'num' variable from the parent class { if (isPrime(i)) // Check if the number is prime { cout << i << " "; } } } }; int main() { B p; p.getData(); p.PrintData(); return 0; }