0% found this document useful (0 votes)
13 views9 pages

CRT Tae 63

The document contains several C++ code snippets that demonstrate basic programming tasks, including calculating the sum of the sum-series of natural numbers, finding the greatest of three numbers, summing odd and even numbers in an array, checking for palindromes, calculating the mean of array elements, determining leap years, calculating factorials, and checking for prime numbers. Each code snippet includes a function and a main method to execute the task. The document serves as a practical guide for implementing fundamental algorithms in C++.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views9 pages

CRT Tae 63

The document contains several C++ code snippets that demonstrate basic programming tasks, including calculating the sum of the sum-series of natural numbers, finding the greatest of three numbers, summing odd and even numbers in an array, checking for palindromes, calculating the mean of array elements, determining leap years, calculating factorials, and checking for prime numbers. Each code snippet includes a function and a main method to execute the task. The document serves as a practical guide for implementing fundamental algorithms in C++.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

Name: Shruthi Cherkuthotawar

TAE-III,IV,V
Roll.no: 63 (B-AI)

1. Given a natural number, find the sum of the sum-series of the first N
natural number.
Code:
#include <iostream>
using namespace std;
int sumSeries(int N) {
return N * (N + 1) * (N + 2) / 6;
}
int main() {
int N;
cout << "Enter a natural number: ";
cin >> N;
cout << "Sum of the sum-series: " << sumSeries(N) << endl;
return 0;
}
2. find greatest of three numbers.
Code:
#include <iostream>

using namespace std;


int greatestOfThree(int a, int b, int c) {
if (a >= b && a >= c)
return a;
else if (b >= a && b >= c)
return b;
else
return c;
}
int main() {
int a, b, c;
cout << "Enter three numbers: ";
cin >> a >> b >> c;
cout << "You entered: " << a << ", " << b << ", " << c << endl;
cout << "Greatest number: " << greatestOfThree(a, b, c) << endl;
return 0;
}
3. given an array find the sum of odd numbers and even numbers separately
Code: #include <iostream>

using namespace std;


void sumOddEven(int arr[], int size, int& sumOdd, int& sumEven) {
sumOdd = sumEven = 0;
for (int i = 0; i < size; i++) {
if (arr[i] % 2 == 0)
sumEven += arr[i];
else
sumOdd += arr[i];
}
}
int main() {
int n;
cout << "Enter the number of elements: ";
cin >> n;
int arr[n];
cout << "Enter the elements: ";
for (int i = 0; i < n; i++)
cin >> arr[i];
int sumOdd, sumEven;
sumOddEven(arr, n, sumOdd, sumEven);
cout << "Sum of odd numbers: " << sumOdd << endl;
cout << "Sum of even numbers: " << sumEven << endl;
return 0;
}

4. check if the string is palindrome or not.


Code:
#include <iostream>
using namespace std;
bool isPalindrome(string str) {
int l = 0, r = str.length() - 1;
while (l < r) {
if (str[l++] != str[r--])
return false;
}
return true;
}
int main() {
string str;
cout << "Enter a string: ";
cin >> str
if (isPalindrome(str))
cout << "The string is a palindrome." << endl;
else
cout << "The string is not a palindrome." << endl;
return 0;
}
5. find the mean of elements in the array
Code:
#include <iostream>
using namespace std;
double findMean(int arr[], int size) {
int sum = 0;
for (int i = 0; i < size; i++)
sum += arr[i];
return (double)sum / size;
}
int main() {
int n;
cout << "Enter the number of elements: ";
cin >> n;
int arr[n];
cout << "Enter the elements: ";
for (int i = 0; i < n; i++)
cin >> arr[i];
cout << "Mean of the elements: " << findMean(arr, n) << endl;
return 0;
}
6. check if given year is leap year or not
Code: #include <iostream>
using namespace std;
bool isLeapYear(int year) {
return (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0);
}
int main() {
int year;
cout << "Enter a year: ";
cin >> year;

if (isLeapYear(year))
cout << year << " is a leap year." << endl;
else
cout << year << " is not a leap year." << endl;
return 0;
}
7. find factorial of a number.
Code:
#include <iostream>
using namespace std;
long long factorial(int n) {
long long fact = 1;
for (int i = 2; i <= n; i++)
fact *= i;
return fact;
}
int main() {
int n;
cout << "Enter a number: ";
cin >> n;
cout << "Factorial of " << n << " is: " << factorial(n) << endl;
return 0;
}
8. check if given number is prime or not
Code:
#include <iostream>
using namespace std;
bool isPrime(int num) {
if (num < 2)
return false;
for (int i = 2; i * i <= num; i++) {
if (num % i == 0)
return false;
}
return true;
}
int main() {
int num;
cout << "Enter a number: ";
cin >> num;
if (isPrime(num))
cout << num << " is a prime number." << endl;
else
cout << num << " is not a prime number." << endl;
return 0;
}

You might also like