1.
Program to find factorial of a number
#include <iostream>
using namespace std;
int main() {
int n;
long long factorial = 1;
cout << "Enter a positive number: ";
cin >> n;
for(int i = 1; i <= n; ++i) {
factorial *= i;
}
cout << "Factorial of " << n << " = " << factorial;
return 0;
}
2. Program to check if number is prime
#include <iostream>
using namespace std;
int main() {
int num, i;
bool isPrime = true;
cout << "Enter a positive integer: ";
cin >> num;
if (num <= 1) isPrime = false;
for(i = 2; i <= num / 2; ++i) {
if(num % i == 0) {
isPrime = false;
break;
}
}
if (isPrime)
cout << num << " is a prime number.";
else
cout << num << " is not a prime number.";
return 0;
}
3. Program to print Fibonacci series
#include <iostream>
using namespace std;
int main() {
int n, t1 = 0, t2 = 1, nextTerm;
cout << "Enter the number of terms: ";
cin >> n;
cout << "Fibonacci Series: ";
for (int i = 1; i <= n; ++i) {
cout << t1 << " ";
nextTerm = t1 + t2;
t1 = t2;
t2 = nextTerm;
}
return 0;
}
4. Program to check if number is even or odd
#include <iostream>
using namespace std;
int main() {
int num;
cout << "Enter an integer: ";
cin >> num;
if (num % 2 == 0)
cout << num << " is even.";
else
cout << num << " is odd.";
return 0;
}
5. Program to swap two numbers using third variable
#include <iostream>
using namespace std;
int main() {
int a, b, temp;
cout << "Enter two numbers: ";
cin >> a >> b;
temp = a;
a = b;
b = temp;
cout << "After swapping: a = " << a << ", b = " << b;
return 0;
}
6. Program to swap two numbers without using third variable
#include <iostream>
using namespace std;
int main() {
int a, b;
cout << "Enter two numbers: ";
cin >> a >> b;
// Swapping without third variable
a = a + b;
b = a - b;
a = a - b;
cout << "After swapping: a = " << a << ", b = " << b;
return 0;
}
7. Program to find the largest of three numbers
#include <iostream>
using namespace std;
int main() {
int a, b, c;
cout << "Enter three numbers: ";
cin >> a >> b >> c;
if (a >= b && a >= c)
cout << "Largest number is: " << a;
else if (b >= a && b >= c)
cout << "Largest number is: " << b;
else
cout << "Largest number is: " << c;
return 0;
}
8. Program to check whether a number is positive, negative or zero
#include <iostream>
using namespace std;
int main() {
int num;
cout << "Enter a number: ";
cin >> num;
if (num > 0)
cout << "The number is positive.";
else if (num < 0)
cout << "The number is negative.";
else
cout << "The number is zero.";
return 0;
}