0% found this document useful (0 votes)
5 views

Basic CPP Programs

The document contains a series of C++ programs demonstrating basic programming concepts. Each program covers different functionalities such as printing 'Hello World', calculating the sum of two numbers, checking if a number is even or odd, and generating a Fibonacci series. Additional examples include swapping numbers, checking for prime numbers, and creating a simple calculator.

Uploaded by

moktandeepesh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

Basic CPP Programs

The document contains a series of C++ programs demonstrating basic programming concepts. Each program covers different functionalities such as printing 'Hello World', calculating the sum of two numbers, checking if a number is even or odd, and generating a Fibonacci series. Additional examples include swapping numbers, checking for prime numbers, and creating a simple calculator.

Uploaded by

moktandeepesh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

1.

Hello World
#include <iostream>
using namespace std;
int main() {
cout << "Hello, World!";
return 0;
}

2. Sum of Two Numbers


#include <iostream>
using namespace std;
int main() {
int a = 5, b = 3;
int sum = a + b;
cout << "Sum: " << sum;
return 0;
}

3. Even or Odd
#include <iostream>
using namespace std;
int main() {
int num;
cout << "Enter a number: ";
cin >> num;
if(num % 2 == 0)
cout << "Even";
else
cout << "Odd";
return 0;
}

4. Factorial of a Number
#include <iostream>
using namespace std;
int main() {
int n, factorial = 1;
cout << "Enter a number: ";
cin >> n;
for(int i = 1; i <= n; ++i)
factorial *= i;
cout << "Factorial: " << factorial;
return 0;
}

5. Fibonacci Series
#include <iostream>
using namespace std;
int main() {
int n = 10, t1 = 0, t2 = 1, nextTerm;
cout << "Fibonacci Series: ";
for(int i = 1; i <= n; ++i) {
cout << t1 << " ";
nextTerm = t1 + t2;
t1 = t2;
t2 = nextTerm;
}
return 0;
}

6. Swap Two Numbers


#include <iostream>
using namespace std;
int main() {
int a = 5, b = 10, temp;
temp = a;
a = b;
b = temp;
cout << "a: " << a << ", b: " << b;
return 0;
}

7. Check Prime Number


#include <iostream>
using namespace std;
int main() {
int n, flag = 0;
cout << "Enter a number: ";
cin >> n;
for(int i = 2; i <= n/2; ++i) {
if(n % i == 0) {
flag = 1;
break;
}
}
if(n == 1)
cout << "Not prime";
else if(flag == 0)
cout << "Prime";
else
cout << "Not prime";
return 0;
}

8. Find Largest Number


#include <iostream>
using namespace std;
int main() {
int a = 10, b = 20;
if(a > b)
cout << "Largest: " << a;
else
cout << "Largest: " << b;
return 0;
}

9. Simple Calculator
#include <iostream>
using namespace std;
int main() {
char op;
float a, b;
cout << "Enter operator (+, -, *, /): ";
cin >> op;
cout << "Enter two numbers: ";
cin >> a >> b;
switch(op) {
case '+': cout << a + b; break;
case '-': cout << a - b; break;
case '*': cout << a * b; break;
case '/': cout << a / b; break;
default: cout << "Invalid operator";
}
return 0;
}

10. Reverse a Number


#include <iostream>
using namespace std;
int main() {
int n, rev = 0, rem;
cout << "Enter a number: ";
cin >> n;
while(n != 0) {
rem = n % 10;
rev = rev * 10 + rem;
n /= 10;
}
cout << "Reversed Number: " << rev;
return 0;
}

11. Palindrome Number


#include <iostream>
using namespace std;
int main() {
int n, reversed = 0, temp, remainder;
cout << "Enter an integer: ";
cin >> n;
temp = n;
while(temp != 0) {
remainder = temp % 10;
reversed = reversed * 10 + remainder;
temp /= 10;
}
if(n == reversed)
cout << n << " is a palindrome.";
else
cout << n << " is not a palindrome.";
return 0;
}

12. Multiplication Table


#include <iostream>
using namespace std;
int main() {
int n;
cout << "Enter a number: ";
cin >> n;
for(int i = 1; i <= 10; ++i)
cout << n << " * " << i << " = " << n * i << endl;
return 0;
}

You might also like