Detailed_C++_Practical_Programs
Detailed_C++_Practical_Programs
Answer:
This program demonstrates basic input and output operations in C++. Two numbers are taken as
Code:
#include <iostream>
int main() {
int a, b, sum;
sum = a + b;
return 0;
Sample Output:
Input:
Output:
Sum: 15
Question: Write a C++ program to calculate the factorial of a number.
Answer:
This program calculates the factorial of a number using a for loop. Factorial is calculated as the
Code:
#include <iostream>
int main() {
int n, factorial = 1;
cin >> n;
factorial *= i;
return 0;
Sample Output:
Input:
Enter a number: 5
Output:
Factorial: 120
Question: Write a C++ program to display the Fibonacci series up to n terms.
Answer:
This program generates the Fibonacci series, where each term is the sum of the two preceding
Code:
#include <iostream>
int main() {
int n, t1 = 0, t2 = 1, nextTerm;
cin >> n;
nextTerm = t1 + t2;
t1 = t2;
t2 = nextTerm;
return 0;
Sample Output:
Input:
Output:
01123
Question: Write a C++ program to check if a number is prime.
Answer:
This program checks if a number is prime by verifying whether it has any divisors other than 1 and
Code:
#include <iostream>
int main() {
int n, i;
cin >> n;
if (n <= 1) {
isPrime = false;
} else {
if (n % i == 0) {
isPrime = false;
break;
}
if (isPrime)
else
return 0;
Sample Output:
Input:
Output:
7 is a prime number.