[Link] a Program to check whether number is prime or not.
#include <iostream>
using namespace std;
int main()
int n, i, m=0, flag=0;
cout << "Enter the Number to check Prime: ";
cin >> n;
m=n/2;
for(i = 2; i <= m; i++)
if(n % i == 0)
cout<<"Number is not Prime."<<endl;
flag=1;
break;
if (flag==0)
cout << "Number is Prime."<<endl;
return 0;
output :
Enter the Number to check Prime: 17
Number is Prime.
Enter the Number to check Prime: 57
Number is not Prime.
2. Write a Program to read number and to display the largest value
between: (a) Two number, (b)Three Numbers, (c) Four numbers by
using switch-case statements.
#include <iostream>
using namespace std;
int main() {
int choice;
cout << "Choose an option to find the largest number:" << endl;
cout << "1. Two Numbers" << endl;
cout << "2. Three Numbers" << endl;
cout << "3. Four Numbers" << endl;
cout << "Enter your choice (1-3): ";
cin >> choice;
switch (choice) {
case 1: {
int a, b;
cout << "Enter two numbers: ";
cin >> a >> b;
int largest = (a > b) ? a : b;
cout << "Largest number is: " << largest << endl;
break;
case 2: {
int a, b, c;
cout << "Enter three numbers: ";
cin >> a >> b >> c;
int largest = (a > b) ? ((a > c) ? a : c) : ((b > c) ? b : c);
cout << "Largest number is: " << largest << endl;
break;
case 3: {
int a, b, c, d;
cout << "Enter four numbers: ";
cin >> a >> b >> c >> d;
int largest = a;
if (b > largest) largest = b;
if (c > largest) largest = c;
if (d > largest) largest = d;
cout << "Largest number is: " << largest << endl;
break;
default:
cout << "Invalid choice. Please select between 1 to 3." << endl;
return 0;
OUTPUT:
Choose an option to find the largest number:
1. Two Numbers
2. Three Numbers
3. Four Numbers
Enter your choice (1-3):
[Link] a Program to find sum of first natural numbers: sum=
1+2+3+4+……. 100 by using(a) for loop, (b)while loop, (c) dowhile loop
#include <iostream>
using namespace std;
int main() {
int sum = 0;
// (a) Using for loop
sum = 0;
for (int i = 1; i <= 100; i++) {
sum += i;
cout << "Sum using for loop: " << sum << endl;
// (b) Using while loop
sum = 0;
int i = 1;
while (i <= 100) {
sum += i;
i++;
cout << "Sum using while loop: " << sum << endl;
// (c) Using do...while loop
sum = 0;
i = 1;
do {
sum += i;
i++;
} while (i <= 100);
cout << "Sum using do...while loop: " << sum << endl;
return 0;
Output:
Sum using for loop: 5050
Sum using while loop: 5050
Sum using do...while loop: 5050
4. Write a Program to find sum of the following series using function
declaration: Sum= x- (x)3/3!+(x)5/5! ............... (x)n/n!
#include <iostream>
#include <cmath>
using namespace std;
// Function to calculate factorial
long long factorial(int n) {
long long fact = 1;
for (int i = 2; i <= n; ++i)
fact *= i;
return fact;
// Function to calculate the series sum
double calculateSeries(double x, int n) {
double sum = 0.0;
int sign = 1;
for (int i = 1; i <= n; i += 2) {
sum += sign * (pow(x, i) / factorial(i));
sign *= -1; // Alternate the sign
return sum;
int main() {
double x;
int n;
cout << "Enter the value of x: ";
cin >> x;
cout << "Enter the highest power (n): ";
cin >> n;
if (n % 2 == 0) {
n--; // Ensure n is odd, since only odd terms are in the series
double result = calculateSeries(x, n);
cout << "The sum of the series is: " << result << endl;
return 0;
Output:
Enter the value of x: 4
Enter the highest power (n): 55
The sum of the series is: -1.77558e+14