Category: C++ Interview Questions
-
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; }…
-
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; }…
-
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…
-
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…
-
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; }…
-
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; }…
-
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 ==…
-
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; }…
-
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;…
-
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…