C++ Program to Find Factorial
Iterative factorial.
#include <iostream>
int main(){
int n=5; long long f=1;
for(int i=2;i<=n;i++) f*=i;
std::cout << "Factorial = " << f << std::endl;
return 0;
}Factorial = 120
Key takeaway: Practice C++ with 63+ runnable code examples — I/O, loops, math, strings, arrays, pointers, classes, and operator overloading — all with expected output to verify your understanding.
C++ is a general-purpose, object-oriented programming language used for game engines, system software, embedded devices, and high-performance applications. These examples are for CS students, interview candidates, and developers building C++ skills.
Last updated: October 2026 · 63 examples · All platforms
Who is this for: CS students, game dev learners, competitive programmers, and job seekers preparing for C++ interviews. Use this when: you need verified C++ reference code or practice problem solutions.
Related:Online C++ Compiler · C++ Tutorial · C Examples
Iterative factorial.
#include <iostream>
int main(){
int n=5; long long f=1;
for(int i=2;i<=n;i++) f*=i;
std::cout << "Factorial = " << f << std::endl;
return 0;
}Factorial = 120
Sum 1..n.
#include <iostream>
int main(){
int n=100; long long s=(long long)n*(n+1)/2;
std::cout << "Sum = " << s << std::endl;
return 0;
}Sum = 5050
Table of a number.
#include <iostream>
int main(){
int n=5;
for(int i=1;i<=10;i++){
std::cout << n << " x " << i << " = " << (n*i) << std::endl;
}
return 0;
}5 x 1 = 5 5 x 2 = 10 5 x 3 = 15 5 x 4 = 20 5 x 5 = 25 5 x 6 = 30 5 x 7 = 35 5 x 8 = 40 5 x 9 = 45 5 x 10 = 50
Generates first N numbers.
#include <iostream>
int main(){
int n=10; long long a=0,b=1;
for(int i=1;i<=n;i++){
std::cout << a;
if(i<n) std::cout << ' ';
long long c=a+b; a=b; b=c;
}
std::cout << std::endl;
return 0;
}0 1 1 2 3 5 8 13 21 34
Euclidean algorithm.
#include <iostream>
int gcd(int a,int b){ while(b){ int t=a%b; a=b; b=t; } return a; }
int main(){
std::cout << "GCD = " << gcd(48,18) << std::endl;
return 0;
}GCD = 6
LCM via GCD.
#include <iostream>
int gcd(int a,int b){ while(b){ int t=a%b; a=b; b=t; } return a; }
int main(){
int a=12,b=18;
int l = (a/gcd(a,b))*b;
std::cout << "LCM = " << l << std::endl;
return 0;
}LCM = 36
Reverses digits.
#include <iostream>
int main(){
int n=1234,r=0;
while(n){ r = r*10 + n%10; n/=10; }
std::cout << "Reverse = " << r << std::endl;
return 0;
}Reverse = 4321
Computes base^exp.
#include <iostream>
#include <cmath>
int main(){
double b=2; int e=10;
std::cout << "Power = " << std::pow(b,e) << std::endl;
return 0;
}Power = 1024
Checks if reversed equals original.
#include <iostream>
int main(){
int n=121, t=n, r=0;
while(t){ r=r*10 + t%10; t/=10; }
std::cout << n << " is " << ((r==n)?"palindrome":"not palindrome") << std::endl;
return 0;
}121 is palindrome
Trial division up to sqrt.
#include <iostream>
#include <cmath>
int main(){
int n=29; bool p = n>1;
for(int i=2;i<=std::sqrt(n)&&p;i++) if(n%i==0) p=false;
std::cout << n << " is " << (p?"prime":"not prime") << std::endl;
return 0;
}29 is prime
Lists primes in a range.
#include <iostream>
bool isPrime(int n){ if(n<2) return false; for(int i=2;i*i<=n;i++) if(n%i==0) return false; return true; }
int main(){
int l=10,h=50; bool first=true;
for(int n=l;n<=h;n++){ if(isPrime(n)){ if(!first) std::cout << ' '; std::cout << n; first=false; } }
std::cout << std::endl;
return 0;
}11 13 17 19 23 29 31 37 41 43 47
Sum of powered digits equals number.
#include <iostream>
#include <cmath>
int main(){
int n=153,t=n, sum=0, p=0;
for(int x=t;x;x/=10) p++;
t=n; while(t){ sum += (int)std::pow(t%10,p); t/=10; }
std::cout << n << " is " << (sum==n?"Armstrong":"not Armstrong") << std::endl;
return 0;
}153 is Armstrong
Lists Armstrong numbers.
#include <iostream>
#include <cmath>
bool isA(int n){ int t=n,p=0,sum=0; for(int x=t;x;x/=10) p++; t=n; while(t){ sum += (int)std::pow(t%10,p); t/=10; } return sum==n; }
int main(){
int l=100,h=999; bool first=true;
for(int n=l;n<=h;n++){ if(isA(n)){ if(!first) std::cout << ' '; std::cout << n; first=false; } }
std::cout << std::endl;
return 0;
}153 370 371 407
Lists divisors.
#include <iostream>
int main(){
int n=28; bool first=true;
for(int i=1;i<=n;i++){ if(n%i==0){ if(!first) std::cout << ' '; std::cout << i; first=false; } }
std::cout << std::endl;
return 0;
}1 2 4 7 14 28
Uses helper function.
#include <iostream>
bool isPrime(int n){ if(n<2) return false; for(int i=2;i*i<=n;i++) if(n%i==0) return false; return true; }
int main(){
int l=10,h=30; bool first=true;
for(int n=l;n<=h;n++){ if(isPrime(n)){ if(!first) std::cout << ' '; std::cout << n; first=false; } }
std::cout << std::endl;
return 0;
}11 13 17 19 23 29
Checks prime using a function.
#include <iostream>
bool isPrime(int n){ if(n<2) return false; for(int i=2;i*i<=n;i++) if(n%i==0) return false; return true; }
int main(){
int n=31; std::cout << n << " is " << (isPrime(n)?"prime":"not prime") << std::endl;
return 0;
}31 is prime
Finds prime pair for N.
#include <iostream>
bool isPrime(int n){ if(n<2) return false; for(int i=2;i*i<=n;i++) if(n%i==0) return false; return true; }
int main(){
int n=34; bool found=false;
for(int i=2;i<=n/2;i++){ if(isPrime(i) && isPrime(n-i)){ std::cout << n << " = " << i << " + " << (n-i) << std::endl; found=true; break; } }
if(!found) std::cout << "No representation" << std::endl;
return 0;
}34 = 3 + 31
Recursive sum.
#include <iostream>
int sumN(int n){ return n<=1? n: n+sumN(n-1); }
int main(){
int n=10; std::cout << "Sum = " << sumN(n) << std::endl;
return 0;
}Sum = 55
Recursive factorial.
#include <iostream>
long long fact(int n){ return n<=1?1: n*fact(n-1); }
int main(){
int n=6; std::cout << "Factorial = " << fact(n) << std::endl;
return 0;
}Factorial = 720
Recursive gcd.
#include <iostream>
int gcd(int a,int b){ return b==0? a: gcd(b, a%b); }
int main(){
std::cout << "GCD = " << gcd(48,18) << std::endl;
return 0;
}GCD = 6
Recursively prints reversed string.
#include <iostream>
void rev(const char* s){ if(*s){ rev(s+1); std::cout << *s; } }
int main(){
const char* s="hello world"; rev(s); std::cout << std::endl;
return 0;
}dlrow olleh
Recursive exponent.
#include <iostream>
long long powi(long long b,int e){ return e==0?1: b*powi(b,e-1); }
int main(){
std::cout << "Power = " << powi(2,10) << std::endl;
return 0;
}Power = 1024
g++ program.cpp -std=c++17 -O2 -o program && ./program. Or use our free online C++ compiler. -std=c++17 flag when compiling. Who is this for: Multiple audiences across industries and experience levels:
Use this when: you need verified reference code, want to complete an assignment, prepare for interviews, or warm up before a systems/game project. Best for beginners through intermediate-level programmers.
The TIOBE Index ranked C++ as #2 in 2024. It powers:
C++ roles pay $120K–$200K+ in these industries.
| Feature | C++ | C |
|---|---|---|
| OOP | Classes, inheritance, polymorphism | No |
| STL | vector, map, set, algorithms | Manual arrays |
| Memory | new/delete + smart pointers | malloc/free |
| Strings | std::string | char arrays |
| Errors | try/catch exceptions | Error codes |
Related:C++ Tutorial · Online C++ Compiler · C Examples
Direct answer: C++ developers work in high-performance computing, game development, finance, embedded systems, and infrastructure. Key roles include:
Absolutely yes. C++ is one of the highest-paying programming languages because the jobs that require it are technically demanding and mission-critical. While there are fewer C++ jobs than JavaScript or Python jobs in raw count, C++ positions pay 20–40% more on average because the supply of skilled C++ developers is much smaller than demand. According to the StackOverflow 2024 Survey, C++ developers rank among the top 5 highest-paid globally.
| Experience | India (LPA) | USA (USD/year) | Typical Role |
|---|---|---|---|
| Fresher (0–1 yr) | ₹5–10 LPA | $70,000–$100,000 | Junior Developer |
| Mid (3–5 yrs) | ₹15–30 LPA | $120,000–$160,000 | Software Engineer |
| Senior (5–10 yrs) | ₹30–60 LPA | $160,000–$250,000 | Senior/Staff Engineer |
| Quant/HFT | ₹50–1.5 Cr | $200,000–$500,000+ | Quantitative Developer |
Yes, but you'll need complementary skills. C++ alone gets you interviews, but companies also expect: data structures & algorithms (essential for all C++ roles), one domain specialization (game dev, embedded, finance), familiarity with build tools (CMake, Make), and version control (Git). For embedded roles, add hardware knowledge. For game dev, add linear algebra and a game engine. For finance, add math/statistics.
Top employers by category:
Timeline:
Total: 9–12 months of consistent daily practice (2–3 hours/day) to be job-ready for entry-level C++ positions.
Yes, significantly. C++ has manual memory management, pointers, templates, multiple inheritance, undefined behavior, and a complex compilation model. Python abstracts all of this away. However, learning C++ first makes you a stronger programmer overall because you understand what's happening "under the hood." Developers who know C++ find Python trivially easy to learn later — the reverse is much harder.
Core interview topics:
No. Entry-level positions expect you to know: basic syntax, OOP, pointers, STL basics, and solve medium-level DSA problems in C++. You don't need to understand template metaprogramming, SFINAE, or write lock-free code for your first job. That comes with experience. Focus on: clean code, correct memory management, and problem-solving ability.
| Language | Avg Salary (US) | Avg Salary (India) | Job Availability |
|---|---|---|---|
| C++ | $135,000 | ₹18 LPA | Moderate (niche) |
| Python | $120,000 | ₹12 LPA | Very High |
| Java | $115,000 | ₹14 LPA | High |
| Rust | $140,000 | ₹20 LPA | Low (growing) |
| Go | $130,000 | ₹16 LPA | Moderate |
C++ pays more per role because the jobs are harder and candidates are fewer. HFT (high-frequency trading) C++ roles are the highest-paid programming jobs in the world.
Top 10 skills from job postings (2025–2026):
Yes, but it's harder. C++ is not the friendliest first language — Python or JavaScript are easier starting points. However, many CS programs teach C++ as the first language (IITs, NITs use C/C++ in first year). If you're disciplined and willing to struggle with pointers and memory for the first 2 months, you can absolutely learn C++ from scratch. Use the examples on this page to practice — they start from Hello World and build up gradually.
Growing in specialized domains. While web development moved to JavaScript/Python, C++ demand is increasing in: electric vehicles (autonomous driving), AI inference (TensorRT, ONNX Runtime are C++), gaming (Unreal Engine 5), HFT (latency-critical trading), and edge computing. The TIOBE Index shows C++ consistently in top 3. Companies are hiring more C++ developers for performance-critical workloads as software becomes more latency-sensitive.
| Industry | Use Case | Example Companies |
|---|---|---|
| Gaming | Game engines, physics, rendering | Epic, EA, Ubisoft |
| Finance/HFT | Low-latency trading, risk engines | Citadel, Tower, DE Shaw |
| Automotive | Self-driving, ADAS, ECU firmware | Tesla, Waymo, Bosch |
| Big Tech | Browsers, databases, OS | Google, Microsoft, Meta |
| Aerospace | Flight systems, simulation | Boeing, Lockheed, ISRO |
| AI/ML Infrastructure | Inference engines, GPU kernels | NVIDIA, OpenAI, Google |
Honest assessment: C++ is one of the hardest mainstream languages to learn. The difficulty comes from: manual memory management (no garbage collector), undefined behavior (code that compiles but crashes), template syntax (notoriously ugly), and the language having 40+ years of features stacked on top of each other. However, you don't need to learn ALL of C++ — modern C++ (C++17/20) is cleaner, and you can be productive knowing ~30% of the language features. Focus on STL, smart pointers, RAII, and basic templates.
new but forgetting delete — use smart pointers insteadstd::vector is faster and safer-Wall -Werror)malloc/printf instead of new/cout, raw arrays instead of vectorYes — increasingly common post-2020. Game development, finance (quant roles), and Big Tech infrastructure teams offer remote C++ positions. Embedded roles are harder to do remotely (need hardware access), though some companies ship dev kits to remote workers. Remote C++ salaries are often 80–100% of on-site pay at major companies. Platforms like LinkedIn, Indeed, and Turing.com list remote C++ positions globally.
Sources: StackOverflow Developer Survey 2024, TIOBE Index, LinkedIn Job Postings 2024–2025, Glassdoor salary data. Practice C++ with the examples above — each runs directly in your browser.