LTImindtree Code
LTImindtree Code
#include<iostream>
using namespace std;
int main(){
int num =5;
cout<< "factorial of "<<num<<" is "<<factorial(num)<<endl;
return 0;
}
bool isPrimeBruteForce(int n) {
if (n <= 1) return false;
for (int i = 2; i < n; i++) {
if (n % i == 0) return false;
}
return true;
}
int main() {
int n;
cout << "Enter a number: ";
cin >> n;
if (isPrimeBruteForce(n))
cout << n << " is a prime number.\n";
else
cout << n << " is not a prime number.\n";
return 0;
}
Optimized – O(root n)
#include <iostream>
#include <cmath>
using namespace std;
bool isPrimeOptimized(int n) {
if (n <= 1) return false;
if (n == 2 || n == 3) return true;
if (n % 2 == 0 || n % 3 == 0) return false;
return secondLargest;
}
int main() {
int n;
cout << "Enter the number of elements in the array: ";
cin >> n;
int arr[n];
cout << "Enter the elements of the array: ";
for (int i = 0; i < n; i++) {
cin >> arr[i];
}
if (secondLargest != -1)
cout << "The second largest element is: " << secondLargest << endl;
return 0;
}
Fibonacci Series
#include <iostream>
using namespace std;
void printFibonacci(int n) {
int t1 = 0, t2 = 1, nextTerm;
int main() {
int n;
cout << "Enter the number of terms: ";
cin >> n;
return 0;
}
Amstrong number –
#include <iostream>
using namespace std;
bool isArmstrong(int n) {
int originalNum = n;
int sum = 0;
int numDigits = 0;
// Calculate the number of digits
while (n != 0) {
n /= 10;
++numDigits;
}
// Calculate the sum of the digits each raised to the power of the number of digits
while (n != 0) {
int digit = n % 10;
sum += power;
n /= 10;
}
int main() {
int n;
cout << "Enter a number: ";
cin >> n;
if (isArmstrong(n))
cout << n << " is an Armstrong number.\n";
else
cout << n << " is not an Armstrong number.\n";
return 0;
}
Reverse string –
#include <iostream>
using namespace std;
int main() {
string str;
cout << "Enter a string: ";
getline(cin, str);
// Reverse the string
reverseString(str);
cout << "Reversed string: " << str << endl;
return 0;
}
Leap year –
#include <iostream>
using namespace std;
int main() {
int year;
cout << "Enter a year: ";
cin >> year;
if (isLeapYear(year)) {
cout << year << " is a leap year." << endl;
} else {
cout << year << " is not a leap year." << endl;
}
return 0;
}
Member of febonacci -
#include <iostream>
using namespace std;
bool isFibonacci(int n) {
if (n < 0) {
return false; // Negative numbers are not in the Fibonacci sequence
}
int main() {
int n;
cout << "Enter a number: ";
cin >> n;
if (isFibonacci(n)) {
cout << "true" << endl;
} else {
cout << "false" << endl;
}
return 0;
}
Palindrome –
#include <iostream>
#include <string>
using namespace std;
int original = n;
int reversed = 0;
while (n > 0) {
int digit = n % 10;
reversed = reversed * 10 + digit;
n /= 10;
}
return original == reversed;
}
int main() {
string str;
int num;
return count;
}
int main() {
int size, element;
Anagrams -
#include <iostream>
#include <algorithm> // For sort() function
#include <cctype> // For tolower() function
using namespace std;
if (areAnagrams(str1, str2)) {
cout << "\"" << str1 << "\" and \"" << str2 << "\" are anagrams." << endl;
} else {
cout << "\"" << str1 << "\" and \"" << str2 << "\" are not anagrams." << endl;
}
return 0;
}
int main()
{
//Initializing variables.
char str[100];
int i;
int freq[256] = {0};
//Accepting inputs.
cout<<"Enter the string: ";
gets(str);
int main() {
int n;
cin >> n;
int arr[n];
for (int i = 0; i < n; ++i) cin >> arr[i];
bubbleSort(arr, n);
for (int i = 0; i < n; ++i) cout << arr[i] << " ";
cout << endl;
return 0;
}
Merge sort –
#include <iostream>
using namespace std;
int main() {
int n;
cin >> n; // Input the number of elements
int arr[n];
return 0;
}
Sum of natural numbers -
#include<bits/stdc++.h>
using namespace std;
int getSum(int n)
{
if(n==0)
return n;
return n + getSum(n-1);
}
int main()
{
int n;
cout << "Enter a number : ";
cin >> n;
return 0;
}
Binary to decimal -
#include <iostream>
#include <string>
using namespace std;
// Start from the last digit and move to the first digit
for (int i = binary.length() - 1; i >= 0; --i) {
if (binary[i] == '1') {
decimal += base;
}
base *= 2; // Move to the next power of 2
}
return decimal;
}
int main() {
string binary;
cout << "Enter a binary number: ";
cin >> binary;
return 0;
}
Vowel –
//C++ Program to check whether alphabet is vowel or consonant
#include <iostream>
using namespace std;
//main function
int main()
{
char c;
cout<<"Enter an alphabet: ";
cin>>c;
//checking for vowels
if(c=='a'||c=='e'||c=='i'||c=='o'||c=='u'||c=='A'||c=='E'||c=='I'||c=='O'||c=='U')
{
cout<<c<<" is a vowel"; //condition true input is vowel
}
else
{
cout<<c<<" is a consonant"; //condition false input is consonant
}
return 0;
}
Ascii –
#include<iostream>
using namespace std;
//main program
int main()
{
char val;
cout<<"Enter a character: ";
cin>>val;
#include <iostream>
using namespace std;
int main()
{
//Initializing variable.
char str[100];
int i, j;
//Accepting input.
cout<<"Enter a string : ";
gets(str);
//C++ Program
//Sum of digits in a number
#include
using namespace std;
int main ()
{
int num, sum = 0;
//output
cout <<"\nSum of digits : " << sum;
return 0;
}
Power of number -
int main()
{
double base = 1.5;
double expo1 = 2.5;
double expo2 = -2.5;
double res1, res2;
cout << base << " ^ " << expo1 << " = " << res1 << endl;
cout << base << " ^ " << expo2 << " = " << res2 << endl;
return 0;
}