CRT Tae 63
CRT Tae 63
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>
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;
}