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

Important C++ Practical Programs

The document outlines four practical C++ programs for second-year finals, including addition of two numbers, calculation of factorial, generation of Fibonacci series, and prime number checking. Each program includes an objective, algorithm, code, and example output. These programs serve as fundamental exercises in basic arithmetic operations, loops, and conditional statements in C++.

Uploaded by

arena360yt
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Important C++ Practical Programs

The document outlines four practical C++ programs for second-year finals, including addition of two numbers, calculation of factorial, generation of Fibonacci series, and prime number checking. Each program includes an objective, algorithm, code, and example output. These programs serve as fundamental exercises in basic arithmetic operations, loops, and conditional statements in C++.

Uploaded by

arena360yt
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

Important Practical Programs for 2nd Year Finals

1. Addition of Two Numbers

**Objective:** To perform basic arithmetic operations in C++.

**Algorithm:**

1. Start

2. Input two numbers, a and b.

3. Add the two numbers and store the result in sum.

4. Display the value of sum.

5. Stop.

**Code:**

#include <iostream>

using namespace std;

int main() {

int a, b, sum;

cout << "Enter two numbers: ";

cin >> a >> b;

sum = a + b;

cout << "Sum: " << sum << endl;

return 0;

**Output:**

| Input | Output |

|-------|--------|

| a=5, b=10 | Sum=15 |


2. Factorial of a Number

**Objective:** To calculate the factorial of a number using loops in C++.

**Algorithm:**

1. Start

2. Input a number, n.

3. Initialize factorial as 1.

4. Use a loop from 1 to n, multiplying factorial by i in each iteration.

5. Display the factorial value.

6. Stop.

**Code:**

#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 << endl;

return 0;

}
**Output:**

| Input | Output |

|-------|----------------|

| n=5 | Factorial=120 |
3. Fibonacci Series

**Objective:** To display the Fibonacci series up to n terms.

**Algorithm:**

1. Start

2. Input the number of terms, n.

3. Initialize t1=0 and t2=1.

4. Use a loop to calculate the next term as t1+t2 and update t1 and t2.

5. Display the terms.

6. Stop.

**Code:**

#include <iostream>

using namespace std;

int main() {

int n, t1 = 0, t2 = 1, nextTerm;

cout << "Enter the number of terms: ";

cin >> n;

for (int i = 1; i <= n; i++) {

cout << t1 << " ";

nextTerm = t1 + t2;

t1 = t2;

t2 = nextTerm;

return 0;
}

**Output:**

| Input | Output |

|-------|-----------------|

| n=5 | 0 1 1 2 3 |
4. Prime Number Check

**Objective:** To check if a number is prime or not.

**Algorithm:**

1. Start

2. Input a number, n.

3. Check if n <= 1, then it's not prime.

4. Use a loop from 2 to n/2 to check divisibility.

5. If divisible, set isPrime to false.

6. Display if n is prime or not.

7. Stop.

**Code:**

#include <iostream>

using namespace std;

int main() {

int n, i;

bool isPrime = true;

cout << "Enter a positive integer: ";

cin >> n;

if (n <= 1) {

isPrime = false;

} else {

for (i = 2; i <= n / 2; i++) {


if (n % i == 0) {

isPrime = false;

break;

if (isPrime)

cout << n << " is a prime number.";

else

cout << n << " is not a prime number.";

return 0;

**Output:**

| Input | Output |

|-------|-----------------------|

| n=7 | 7 is a prime number. |

| n=10 | 10 is not a prime number. |

You might also like