C++ assignment 2 (Deepti)
C++ assignment 2 (Deepti)
#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;
}
#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;
}
#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;
return 0;
}
#include <iostream>
using namespace std;
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;
}
#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;
}
#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;
#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;
}
return 0;
}
#include <iostream>
#include <string>
using namespace std;
return 0;
}
#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;
int main() {
string inputString;
#include <iostream>
using namespace std;
int main() {
int num1, num2;
cout << "Enter first number: ";
cin >> num1;
cout << "Enter second number: ";
cin >> num2;
swapNumbers(&num1, &num2);
cout << "\nAfter swapping:" << endl;
cout << "First number: " << num1 << endl;
cout << "Second number: " << num2 << endl;
return 0;
}
#include <iostream>
using namespace std;
return 0;
}
#include <iostream>
using namespace std;
void mergeArrays(int arr1[], int n1, int arr2[], int n2, int arr3[]) {
int i = 0, j = 0, k = 0;
int main() {
int arr1[] = {1, 3, 5};
int n1 = sizeof(arr1) / sizeof(arr1[0]);
return 0;
}