Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
17 views

C++ assignment 2 (Deepti)

Uploaded by

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

C++ assignment 2 (Deepti)

Uploaded by

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

C++ Assignment-2

B.Sc. (H) Computer


Science
Sem-2

Submitted by: DEEPTI


(230421)
Section-A
1. WAP to compute the sum of the first n terms of the following
series S = 1+1/2+1/3+1/4+……

#include <iostream>
using namespace std;
int main() {
int n;
double sum = 0.0;
cout << "Enter the number of terms: ";
cin >> n;
for(int i = 1; i <= n; i++) {
sum += 1.0 / i; // Each term is 1/i
}

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

2. WAP to compute the sum of the first n terms of the following


series S =1-2+3-4+5…………….

#include <iostream>
using namespace std;
int main() {
int n;
int sum = 0;
cout << "Enter the number of terms: ";
cin >> n;
for(int i = 1; i <= n; i++) {
if (i % 2 == 0) {
sum -= i; // Subtract if 'i' is even
} else {
sum += i; // Add if 'i' is odd
}
}
cout << "Sum of the first " << n << " terms is: " << sum << endl;
return 0;
}

3. WAP to compute the sum of the first n terms of the following


series S = 1 + 1/2! + 1/3! + 1/4! +……

#include <iostream>
using namespace std;
int main() {
int n;
double sum = 1.0;
double term = 1.0;
cout << "Enter the number of terms: ";
cin >> n;

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


term /= i;
sum += term;
}
cout << "Sum of the first " << n << " terms is: " << sum << endl;

return 0;
}

4. Write a function to find whether a given no. is prime or not. Use


the same to generate prime numbers less than 100.

#include <iostream>
using namespace std;

// Function to check if a number is prime


bool isPrime(int num) {
if (num <= 1) return false;
for (int i = 2; i * i <= num; i++) {
if (num % i == 0) return false;
}
return true;
}

int main() {
cout << "Prime numbers less than 100:" << endl;
for (int i = 2; i < 100; i++) {
if (isPrime(i)) {
cout << i << " ";
}
}
cout << endl;
return 0;
}

5. WAP to compute the factors of a given number

#include <iostream>
using namespace std;
// Function to print factors of a given number
void printFactors(int num) {
cout << "Factors of " << num << " are: ";
for (int i = 1; i <= num; i++) {
if (num % i == 0) {
cout << i << " ";
}
}
cout << endl;
}

int main() {
int number;
cout << "Enter a number: ";
cin >> number;
printFactors(number);

return 0;
}

6. Calculate the GCD (HCF) and LCM of the two given numbers.

#include <iostream>
using namespace std;
// Function to calculate HCF of two numbers
int gcd(int a, int b) {
while (b != 0) {
int temp = b;
b = a % b;
a = temp;
}

return a;
}
// Function to calculate LCM of two numbers
int lcm(int a, int b) {
return (a * b) / gcd(a, b);
}

int main() {
int num1, num2;
cout << "Enter two numbers: ";
cin >> num1 >> num2;
cout << "GCD (HCF) of " << num1 << " and " << num2 << " is: " <<
gcd(num1,num2) << endl;
cout << "LCM of " << num1 << " and " << num2 << " is: "
<<lcm(num1, num2) <<endl;
return 0;
}

7. Print Fibonacci series

#include <iostream>
using namespace std;

int main() {
int numTerms, first = 0, second = 1, next;
cout << "Enter the number of terms: ";
cin >> numTerms;
cout << "Fibonacci Series up to " << numTerms << " terms:" <<
endl;

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


if (i == 1)
cout << first << " ";
else if (i == 2)
cout << second << " ";
else {
next = first + second;
cout << next << " ";
first = second;
second = next;
}
}
return 0;
}

8. Print the ASCII code of all the numbers in a given range.

#include <iostream>
using namespace std;

int main() {
int startNum, endNum;
cout << "Enter the starting number: ";
cin >> startNum;
cout << "Enter the ending number: ";
cin >> endNum;
cout << "ASCII codes of numbers from " << startNum << " to " <<
endNum << "are:" << endl;
for (int i = startNum; i <= endNum; ++i) {
cout << "ASCII code of " << i << " is " << char(i) << endl;
}
return 0;
}
9. Display the count and sum of digits of a given number.

#include <iostream>
using namespace std;

int main() {
int num, digit, sum = 0, count = 0;
cout << "Enter a number: ";
cin >> num;
int temp = num;
while (temp != 0) {
digit = temp % 10;
sum += digit;
count++;
temp /= 10;
}

cout << "Count of digits: " << count << endl;


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

return 0;
}

10. Print a given number in words.

#include <iostream>
#include <string>
using namespace std;

string digitToWord(int digit) {


switch (digit) {
case 0: return "zero";
case 1: return "one";
case 2: return "two";
case 3: return "three";
case 4: return "four";
case 5: return "five";
case 6: return "six";
case 7: return "seven";
case 8: return "eight";
case 9: return "nine";
default: return "";
}
}
int main() {
int number;

cout << "Enter a number: ";


cin >> number;
string result = "";
while (number > 0) {
int digit = number % 10;
string word = digitToWord(digit);
result = word + " " + result;
number /= 10;
}
cout << "Number in words: " << result << endl;

return 0;
}

11. Convert a given decimal number to its binary equivalent.

#include <iostream>
#include <string>
using namespace std;

string decimalToBinary(int n) {
string binary = "";
while (n > 0) {
int rem = n % 2;
binary = to_string(rem) + binary;
n /= 2;
}
return binary;
}

int main() {
int decimalNumber;
cout << "Enter a decimal number: ";
cin >> decimalNumber;

string binaryNumber = decimalToBinary(decimalNumber);


cout << "Binary equivalent: " << binaryNumber << endl;
return 0;
}

12. Write a function that checks whether a given string is


Palindrome or not. Use this function to find whether the string
entered by user is Palindrome or not.
#include <iostream>
#include <string>
using namespace std;

bool isPalindrome(const string& str) {


int left = 0;
int right = str.length() - 1;

while (left < right) {


if (str[left++] != str[right--]) {
return false;
}
}
return true;
}

int main() {
string inputString;

cout << "Enter a string: ";


getline(cin, inputString);
if (isPalindrome(inputString)) {
cout << "Palindrome" << endl;
} else {
cout << "Not a palindrome" << endl;
}
return 0;
}

13. Write a program that swaps two numbers using pointers.

#include <iostream>
using namespace std;

void swapNumbers(int* ptr1, int* ptr2) {


int temp = *ptr1;
*ptr1 = *ptr2;
*ptr2 = temp;
}

int main() {
int num1, num2;
cout << "Enter first number: ";
cin >> num1;
cout << "Enter second number: ";
cin >> num2;

cout << "Before swapping:" << endl;


cout << "First number: " << num1 << endl;
cout << "Second number: " << num2 << endl;

swapNumbers(&num1, &num2);
cout << "\nAfter swapping:" << endl;
cout << "First number: " << num1 << endl;
cout << "Second number: " << num2 << endl;

return 0;
}

14. WAP to print a triangle of stars as follows (take the number


of lines from the user):
* * * *
*** ** ** *****
***** *** *** *******
******* **** **** *****
********* ***** ***** ***
*
1 ABCDEFGHI
121 ABCD FGHI
12321 ABC GHI
1234321 AB HI
123454321 A I
12345654321

#include <iostream>
using namespace std;

void printPattern1(int numLines) {


for (int i = 1; i <= numLines; i++) {
for (int j = 0; j < 2 * i - 1; j++) {
cout << "*";
}
cout << endl;
}
}

void printPattern2(int numLines) {


for (int i = 1; i <= numLines; i++) {
for (int j = 1; j <= i; j++) {
cout << "*";
}
cout << endl;
}
}

void printPattern3(int numLines) {


for (int i = numLines; i >= 1; i--) {
for (int j = 1; j <= i; j++) {
cout << "*";
}
cout << endl;
}
}
void printPattern4(int numLines) {
for (int i = 1; i <= numLines; i++) {
for (int j = 1; j <= i; j++) {
cout << j;
}
for (int j = i - 1; j >= 1; j--) {
cout << j;
}
cout << endl;
}
}
void printPattern5(int numLines) {
char ch = 'A';
for (int i = 0; i < numLines; i++) {
for (int j = 0; j <= i; j++) {
cout << ch++;
}
cout << endl;
}
}
void printPattern6(int numLines) {
int space = numLines - 1;
for (int i = 0; i < numLines; i++) {
for (int j = 0; j < space; j++) {
cout << " ";
}
space--;
for (int j = 0; j <= i; j++) {
cout << "*";
}
cout << endl;
}
}
int main() {
int numLines;
cout << "Enter the number of lines: ";
cin >> numLines;

cout << "Pattern 1:" << endl;


printPattern1(numLines);

cout << "\nPattern 2:" << endl;


printPattern2(numLines);

cout << "\nPattern 3:" << endl;


printPattern3(numLines);
cout << "\nPattern 4:" << endl;
printPattern4(numLines);

cout << "\nPattern 5:" << endl;


printPattern5(numLines);

cout << "\nPattern 6:" << endl;


printPattern6(numLines);

return 0;
}

15. Given two ordered arrays of integers, write a program to


merge the two arrays to get an ordered array.

#include <iostream>
using namespace std;

void mergeArrays(int arr1[], int n1, int arr2[], int n2, int arr3[]) {
int i = 0, j = 0, k = 0;

while (i < n1 && j < n2) {


if (arr1[i] < arr2[j]) {
arr3[k++] = arr1[i++];
} else {
arr3[k++] = arr2[j++];
}
}

while (i < n1) {


arr3[k++] = arr1[i++];
}

while (j < n2) {


arr3[k++] = arr2[j++];
}
}

int main() {
int arr1[] = {1, 3, 5};
int n1 = sizeof(arr1) / sizeof(arr1[0]);

int arr2[] = {2, 4, 6};


int n2 = sizeof(arr2) / sizeof(arr2[0]);

int arr3[n1 + n2]; // Enough space for merged array

mergeArrays(arr1, n1, arr2, n2, arr3);

cout << "Merged array: ";


for (int i = 0; i < n1 + n2; i++) {
cout << arr3[i] << " ";
}
cout << endl;

return 0;
}

You might also like