C++ Code Examples: Essential Algorithms & Programs

Classified in Computers

Written at on English with a size of 5.54 KB.

C++ Code Examples: Essential Algorithms & Programs

Here are some fundamental C++ code examples covering various algorithms and programming concepts:

Factorial Calculation

#include <iostream>
using namespace std;

long factorial(int x) {
 int i, f = 1;
 for (i = 1; i <= x; i++) {
 f = f * i;
 }
 return f;
}

int main() {
 int n;
 cout << "Enter the number: ";
 cin >> n;
 if (n < 0) {
 cout << "Factorial is not defined for negative numbers";
 } else {
 cout << "Factorial = " << factorial(n);
 }
 return 0;
}

String Length Finder

#include <iostream>
using namespace std;

int main() {
 int length = 0, i;
 char s[20];
 cout << "Enter the string: ";
 cin.getline(s, 20);
 for (i = 0; s[i] != '\0'; i++) {
 length = length + 1;
 }
 cout << "The length of string is: " << length;
 return 0;
}

Quadratic Equation Solver

#include <iostream>
#include <cmath>
using namespace std;

int main() {
 float a, b, c, r1, r2, d;
 cout << "Enter the 3 coefficients: ";
 cin >> a >> b >> c;
 d = b * b - 4 * a * c;
 if (d > 0) {
 r1 = (-b + sqrt(d)) / (2 * a);
 r2 = (-b - sqrt(d)) / (2 * a);
 cout << "Root 1 = " << r1 << '\t' << "Root 2 = " << r2;
 } else if (d == 0) {
 r1 = r2 = -b / (2 * a);
 cout << "The roots are equal.\n";
 cout << "Root 1 = " << r1 << '\t' << "Root 2 = " << r2;
 } else {
 cout << "The roots are imaginary";
 }
 return 0;
}

Sum of Digits

#include <iostream>
using namespace std;

int main() {
 int num, rem, sum = 0;
 cout << "Enter a number: ";
 cin >> num;
 while (num > 0) {
 rem = num % 10;
 sum = sum + rem;
 num = num / 10;
 }
 cout << "Sum of digits: " << sum;
 return 0;
}

Sum of Squares (Incorrect Code)

Note: The original code appears to be for finding the length of a string, not the sum of squares.

#include <iostream>
using namespace std;

int main() {
 int length = 0, i;
 char s[20];
 cout << "Enter the string: ";
 cin.getline(s, 20);
 for (i = 0; s[i] != '\0'; i++) {
 length = length + 1;
 }
 cout << "The length of string is: " << length;
 return 0;
}

Fibonacci Series

#include <iostream>
using namespace std;

int main() {
 int first, second, current, i, limit;
 cout << "Enter the limit: ";
 cin >> limit;
 first = 0;
 second = 1;
 current = first;
 for (i = 0; i < limit; i++) {
 cout << '\t' << current;
 first = second;
 second = current;
 current = first + second;
 }
 return 0;
}

Prime Numbers Below 100

#include <iostream>
using namespace std;

int main() {
 int i, flag, j;
 for (i = 2; i <= 100; i++) {
 flag = 0;
 for (j = 2; j <= i / 2; j++) {
 if (i % j == 0) {
 flag = 1;
 break;
 }
 }
 if (flag == 0) {
 cout << '\t' << i;
 }
 }
 return 0;
}

Employee Salary Calculation

#include <iostream>
using namespace std;

struct employee {
 int emp_code;
 char emp_name[20];
 int basic_pay, da, hra, pf;
};

int main() {
 employee emp;
 cout << "Enter employee code: ";
 cin >> emp.emp_code;
 cout << "\nEnter employee name: ";
 cin >> emp.emp_name;
 cout << "\nEnter basic pay: ";
 cin >> emp.basic_pay;
 cout << "\nEnter DA: ";
 cin >> emp.da;
 cout << "\nEnter HRA: ";
 cin >> emp.hra;
 cout << "\nEnter PF: ";
 cin >> emp.pf;
 int total = emp.basic_pay + emp.da + emp.hra - emp.pf;
 cout << "\nNet salary: " << total;
 return 0;
}

Decimal to Binary Conversion

#include <iostream>
using namespace std;

void binary(int x) {
 if (x > 1) {
 binary(x / 2);
 }
 cout << x % 2;
}

int main() {
 int n;
 cout << "Enter the decimal number: ";
 cin >> n;
 cout << "Binary equivalent is: ";
 binary(n);
 return 0;
}

Palindrome Check

#include <iostream>
using namespace std;

int main() {
 int n, rev = 0, rem, copy;
 cout << "Enter the number: ";
 cin >> n;
 copy = n;
 while (n > 0) {
 rem = n % 10;
 rev = rev * 10 + rem;
 n = n / 10;
 }
 if (copy == rev) {
 cout << "The number is a palindrome";
 } else {
 cout << "The number is not a palindrome";
 }
 return 0;
}

Entradas relacionadas: