Category: C++ Interview Questions

https://cdn3d.iconscout.com/3d/premium/thumb/interview-question-3d-icon-download-in-png-blend-fbx-gltf-file-formats–interviewer-hr-manager-pack-business-icons-9684875.png

  •  Write a Program to Check Palindrome

    C++// C++ program to check if a // number is Palindrome or not #include <iostream> using namespace std; // Function to check Palindrome bool checkPalindrome(int n) { int ans = 0; int temp = n; while (temp != 0) { ans = (ans * 10) + (temp % 10); temp = temp / 10; } return (ans == n); } int main() { int n = 12321; if (checkPalindrome(n) == 1) { cout << "Yes\n"; } else { cout << "No\n"; } return 0; }

    Output

    Yes
  • Write a Program to Calculate the Greatest Common Divisor of Two Numbers

    C++// C++ program to find // GCD of two numbers #include <iostream> using namespace std; // Function to return gcd of a and b int gcd(int a, int b) { int result = min(a, b); while (result > 0) { if (a % result == 0 && b % result == 0) { break; } result--; } return result; } int main() { int a = 54, b = 33; cout << "GCD: " << gcd(a, b); return 0; }

    Output

    GCD: 3
  • Write a Program to Find the Nth Term of the Fibonacci Series

    C++// C++ Program to Find the // Nth Term of the Fibonacci Series #include <iostream> using namespace std; int fib(int n) { int first = 0, second = 1, ans; if (n == 0) return first; for (int i = 2; i <= n; i++) { ans = first + second; first = second; second = ans; } return ans; } int main() { int n = 13; cout << fib(n); return 0; }

    Output

    233
  • Write a Program to Check Whether a Number is an Armstrong Number or Not

    C++// C++ Program to check // if number is Armstrong // or not #include <iostream> using namespace std; int main() { int n = 153; int temp = n; int ans = 0; // function to calculate // the sum of individual digits while (n > 0) { int rem = n % 10; ans = (ans) + (rem * rem * rem); n = n / 10; } // condition to check if (temp == ans) { cout << ("Yes, it is Armstrong Number"); } else { cout << ("No, it is not an Armstrong Number"); } return 0; }

    Output

    Yes, it is Armstrong Number
  •  Write a Program to Check Palindrome

    C++// C++ program to check if a // number is Palindrome or not #include <iostream> using namespace std; // Function to check Palindrome bool checkPalindrome(int n) { int ans = 0; int temp = n; while (temp != 0) { ans = (ans * 10) + (temp % 10); temp = temp / 10; } return (ans == n); } int main() { int n = 12321; if (checkPalindrome(n) == 1) { cout << "Yes\n"; } else { cout << "No\n"; } return 0; }

    Output

    Yes
  • Write a Program to Check the Prime Number

    C++// C++ program to check if a // Number is prime #include <iostream> using namespace std; bool isPrime(int n) { // base condition if (n <= 1) return false; // Check from 2 to n-1 for (int i = 2; i < n; i++) if (n % i == 0) return false; return true; } int main() { isPrime(21) ? cout << " true\n" : cout << " false\n"; isPrime(17) ? cout << " true\n" : cout << " false\n"; return 0; }

    Output

     false
     true
  • Write a Program to Find a Leap Year or Not

    C++// C++ program to check if a given // year is leap year or not #include <iostream> using namespace std; bool checkYear(int year) { // leap year if (year % 400 == 0) return true; // Not leap year if (year % 100 == 0) return false; // leap year if (year % 4 == 0) return true; // Not leap year return false; } int main() { int year = 2000; if (checkYear(year)) cout << "Leap Year"; else cout << "Not a Leap Year"; return 0; }

    Output

    Leap Year
  • Write a Program to Find the Factorial of a Number Using Loops

    C++// C++ program to find factorial using loops #include <bits/stdc++.h> using namespace std; // function to find factorial int factorial(int n) { int fact = 1; while (n > 1) { fact *= n; n--; } return fact; } // driver code int main() { int num = 5; cout << factorial(num); return 0; }

    Output

    120
  • Write a Program to Find the Sum of the First N Natural Numbers

    C++// C++ program to find // Sum of first // n natural numbers. #include <iostream> using namespace std; // Function to find sum int findSum(int n) { int sum = 0; for (int i = 1; i <= n; i++) sum = sum + i; return sum; } int main() { int n = 7; cout << findSum(n); return 0; }

    Output

    28
  • Write a Program to Remove Spaces From a String

    C++// C++ Program to remove spaces from a string #include <iostream> #include <string> using namespace std; string remove_spaces(string str) { string result = ""; for (char c : str) { if (c != ' ') { result += c; } } return result; } int main() { string str = "Gfg to the moon"; cout << "Without spaces: " << remove_spaces(str) << endl; return 0; }

    Output

    Without spaces: Gfgtothemoon