0% found this document useful (0 votes)
9 views13 pages

My C++

The document contains multiple C++ programs demonstrating various programming concepts. These include swapping variables using call by value and call by reference, calculating the sum of natural numbers, checking for Armstrong numbers, printing prime numbers, generating Fibonacci series, calculating a specific series sum, and computing factorials using both iteration and recursion. Each program includes code snippets, objectives, and sample outputs.

Uploaded by

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

My C++

The document contains multiple C++ programs demonstrating various programming concepts. These include swapping variables using call by value and call by reference, calculating the sum of natural numbers, checking for Armstrong numbers, printing prime numbers, generating Fibonacci series, calculating a specific series sum, and computing factorials using both iteration and recursion. Each program includes code snippets, objectives, and sample outputs.

Uploaded by

Devj Joshi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Objective:

The objective of this C++ program is to swap the values of two variables using two different
methods:

1. Call by Value:

○ A function is called with copies of the original values.


○ The changes inside the function do not affect the original values.
2. Call by Reference:

○ A function is called with references to the original variables.


○ The changes inside the function directly modify the original values.

Q12. Write a C++ program to swap the content of two variable


1. Call by Value
2. Call by Reference
#include
<iostream> using
namespace std;
void swapByValue(int x, int
y) { int temp = x;
x = y;
y = temp;
cout << "Inside swapByValue: a = " << x << ", b = " << y << endl;
}
void swapByReference(int &x, int
&y) { int temp = x;
x = y;
y = temp;
cout << "Inside swapByReference: a = " << x << ", b = " << y << endl;
}
int main()
{ int a,
b;
cout << "Enter two
numbers: "; cin >> a >> b;

cout << "\nBefore swapping: a = " << a << ", b = " << b << endl;

// Swapping using Call by


Value swapByValue(a, b);
cout << "After swapByValue (No effect in main): a = " << a << ", b = " << b << endl;

// Swapping using Call by Reference


swapByReference(a, b);
cout << "After swapByReference (Changes reflect in main): a = " << a << ", b = " <<
b << endl;

return 0;
1
}

Output:
Enter two numbers: 5 10

Before swapping: a = 5, b =
10 Inside swapByValue: a =
10, b = 5
After swapByValue (No effect in main): a = 5, b =
10 Inside swapByReference: a = 10, b = 5
After swapByReference (Changes reflect in main): a = 10, b = 5

2
Name : Nikhil Parmar course : BCA
Roll no. : 36 section : D1

Objective:
The objective of this C++ program is to calculate the sum of the first n
natural numbers using a formula and a loop
Q13. Write a C++ program to calculate sum of n natural numbers.
#include

<iostream> using

namespace std; int

main() {

int n, sum_formula, sum_loop =

0; cout << "Enter a positive

integer (n): "; cin >> n;

if (n < 1) {

cout << "Please enter a positive integer!" <<

endl; return 1;

sum_formula = (n * (n +

1)) / 2; for (int i = 1; i <=

n; i++) {

sum_loop += i;

cout << "Sum using formula: " << sum_formula

<< endl; cout << "Sum using loop: " <<

sum_loop << endl;

return 0;

}
Output:

3
Name : Nikhil Parmar course : BCA
Roll no. : 36 section : D1
Enter a positive integer (n):

10 Sum using formula: 55

Sum using loop: 55

Objective:
The objective of this C++ program is to check whether a given number is an Armstrong
number or not.

Q14. Write a program to check whether a given number


is armstrong or not.
#include

<iostream>

#include <cmath>

using namespace

std;

bool isArmstrong(int num) {

int originalNum = num, sum = 0,

digits = 0; int temp = num;

while (temp > 0)

{ temp /= 10;

digits++;

temp = num;

while (temp >

0) {

int digit = temp %

10; sum +=

pow(digit, digits);

temp /= 10;

4
Name : Nikhil Parmar course : BCA
Roll no. : 36 section : D1
}

return (sum == originalNum);

int main()

{ int

num;

cout << "Enter a

number: "; cin >> num;

if (isArmstrong(num))

cout << num << " is an Armstrong number."

<< endl; else

cout << num << " is NOT an Armstrong number." <<

endl; return 0;

Output 1:
Enter a number: 153

153 is an Armstrong number.

Output 2:
Enter a number: 123

123 is NOT an Armstrong number

5
Name : Nikhil Parmar course : BCA
Roll no. : 36 section : D1

Objective:
The objective of this C++ program is to print the first 50 prime numbers efficiently.

Q15. Write a program to print first 50 prime


numbers.
#include
<iostream>
#include <cmath>
using namespace
std; bool
isPrime(int num) {
if (num < 2) return false;
for (int i = 2; i <=
sqrt(num); i++) { if (num
% i == 0)
return false;
}
return true;
}

int main() {
int count = 0, num = 2;
cout << "First 50 prime
numbers:\n"; while (count <
50) {
if (isPrime(num)) {
cout << num << " ";
count++;
}
num++; }

cout <<
endl; return
0;
}

Output:
First 50 prime numbers:
2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97 101 103 107
109 113 127
131 137 139 149 151 157 163 167 173 179 181 191 193 197 199 211 223 227 229

6
Name : Nikhil Parmar course : BCA
Roll no. : 36 section : D1

Objective:
The objective of this C++ program is to print the Fibonacci
series up to n terms, where n is entered by the user.

Q16. Write a program to print fibonacci series


upto n terms.
#include
<iostream> using
namespace std;
void
printFibonacci(int
n) {
int first = 0, second
= 1, next; cout <<
"Fibonacci Series: ";
for (int i = 0; i < n;
i++) {
cout << first
<< " "; next =
first + second;
first = second;
second =
next;
}

cout << endl;


}

int
main()
{ int
n;
cout << "Enter the number
of terms: "; cin >> n;

if (n <= 0) {
cout << "Please enter a positive number!" << endl;

7
Name : Nikhil Parmar course : BCA
Roll no. : 36 section : D1
} else {
printFibonacci(n);
}

return 0;
}

Output 1:
Enter the number of terms:
10 Fibonacci Series: 0 1 1 2
3 5 8 13 21 34

Output 2:
Enter the number of
terms: -5 Please enter a
positive number!

8
Name : Nikhil Parmar course : BCA
Roll no. : 36 section : D1

Objective:
The objective of this C++ program is to calculate the sum of the series:

S=1+12∗2+13∗2+14∗2+...+1n∗2S = 1 + \frac{1}{2*2} + \frac{1}{3*2} + \

frac{1}{4*2} + ... +
\frac{1}

{n*2}S=1+2∗21+3∗21+4∗21+...

+n∗21 where n is entered by the

user.

Q17. Write a program to calculate sum of


the series 1+½2+⅓*2+¼*2…..1/n*2.
#include
<iostream> using
namespace std; int
main() {
int n;
double sum = 0.0;
cout << "Enter the number of terms
(n): "; cin >> n;

if (n <= 0) {
cout << "Please enter a positive integer!" <<
endl; return 1; // Exit the program
}

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


sum += 1.0 / (i * 2); // Using 1.0 to ensure floating-point division
}

cout << "Sum of the series up to " << n << " terms is: " << sum
<< endl; return 0;
}

Output 1:
Enter the number of terms (n): 5
Sum of the series up to 5 terms is: 0.783333

9
Name : Nikhil Parmar course : BCA
Roll no. : 36 section : D1
Output 2:
Enter the number of terms
(n): -3 Please enter a positive
integer!

10
Name : Nikhil Parmar course : BCA
Roll no. : 36 section : D1

Objective:
The objective of this C++ program is to calculate the factorial of a given number
using both iteration (loop) and recursion

Q18. Write a program to calculate factorial of a given number.


#include

<iostream> using

namespace std;

long long

factorialRecursive(int n) { if

(n == 0 || n == 1)

return 1;

return n * factorialRecursive(n - 1);

long long factorialIterative(int

n) { long long fact = 1;

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

+) { fact *= i;

return fact;

int main()

{ int

num;

cout << "Enter a

number: "; cin >> num;

if (num < 0) {

cout << "Factorial of a negative number is not defined!" << endl;

11
Name : Nikhil Parmar course : BCA
Roll no. : 36 section : D1

} else {

12
Name : Nikhil Parmar course : BCA
Roll no. : 36 section : D1

cout << "Factorial (Using Loop): " <<

factorialIterative(num) << endl; cout << "Factorial (Using

Recursion): " << factorialRecursive(num) << endl;

return 0;

Output 1:
Enter a number: 5

Factorial (Using Loop):

120

Factorial (Using Recursion): 120

Output 2:
Enter a number: -4

Factorial of a negative number is not defined!

13

You might also like