1.
FIND SUM, AVERAGE, STANDARD DEVIATION
Program:
#include <stdio.h>
#include <math.h>
#define MAXSIZE 10
void main()
float x[MAXSIZE];
int i, n;
float average, variance, std_deviation, sum = 0, sum1 = 0;
clrscr();
printf("Enter the value of N \n");
scanf("%d", &n);
printf("Enter %d real numbers \n", n);
for (i = 0; i < n; i++)
scanf("%f", &x[i]);
for (i = 0; i < n; i++)
{
sum = sum + x[i];
average = sum / (float)n;
for (i = 0; i < n; i++)
sum1 = sum1 + pow((x[i] - average), 2);
}
variance = sum1 / (float)n;
std_deviation = sqrt(variance);
printf("Average of all elements = %.2f\n", average);
printf("variance of all elements = %.2f\n", variance);
printf("Standard deviation = %.2f\n", std_deviation);
Output:
Enter the value of N
Enter 5 real numbers
34
88
32
12
10
Average of all elements = 35.20
variance of all elements = 794.56
Standard deviation = 28.19
2. N PRIME NUMBERS
Program:
#include<stdio.h>
#include<conio.h>
int main()
int n, count=1, flag, i=2, j;
clrscr();
printf("Enter how many prime numbers? \n");
scanf("%d", &n);
while(count <= n)
{
flag = 0;
for(j=2; j <= i/2; j++)
if(i%j==0)
flag=1;
break;
if(flag==0)
printf("%d\t",i);
count++;
}
i++;
getch();
return(0);
}
Output:
Enter how many prime numbers?
10
2 3 5 7 11 13 17 19 23 29
3. FIBONACCI SERIES
Program
#include <stdio.h>
int main() {
int i, n;
int t1 = 0, t2 = 1;
int nextTerm = t1 + t2;
printf("Enter the number of terms: ");
scanf("%d", &n);
printf("Fibonacci Series: %d, %d, ", t1, t2);
for (i = 3; i <= n; ++i) {
printf("%d, ", nextTerm);
t1 = t2;
t2 = nextTerm;
nextTerm = t1 + t2;
return 0;
Output:
Enter the number of terms: 10
Fibonacci Series: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34
4. SORT THE GIVEN SET OF NUMBERS IN ASCENDING ORDER
Program:
#include <stdio.h>
void main ()
{
int num[20];
int i, j, a, n;
printf("enter number of elements in an array");
scanf("%d", &n);
printf("Enter the elements");
for (i = 0; i < n; ++i)
scanf("%d", &num[i]);
for (i = 0; i < n; ++i)
for (j = i + 1; j < n; ++j)
if (num[i] > num[j])
a = num[i];
num[i] = num[j];
num[j] = a;
} } }
printf("The numbers in ascending order is:");
for (i = 0; i < n; ++i)
{
printf("%d", num[i]); } }
Output:
enter number of elements in an array
Enter the elements
12
23
89
11
22
The numbers in ascending order is:
11
12
22
23
89
5. VOWELS IN THE GIVEN SENTENCE
Program:
#include <stdio.h>
#include <ctype.h>
int main()
{
char str[100];
int i, vowels = 0;
printf("Enter the string: ");
scanf("%[^\n]s",&str);
for(i = 0; str[i]; i++)
if(str[i]=='a'|| str[i]=='e'||str[i]=='i'||
str[i]=='o'|| str[i]=='u'||str[i]=='A'||
str[i]=='E'||str[i]=='I'||str[i]=='O' ||str[i]=='U')
vowels++;
printf("Total number of vowels: = %d\n",vowels);
return 0;
Output:
Enter the string: I am human
Total number of vowels: = 4
6. EMPLOYEE DETAILS
Program:
#include <iostream>
#include <string>
using namespace std;
class Employee {
private:
int empNumber;
string empName;
string department;
double basicSalary;
double totalSalary;
char grade;
public:
void getDetails() {
cout << "Enter Employee Number: ";
cin >> empNumber;
cin.ignore(); // To ignore the newline character left by cin
cout << "Enter Employee Name: ";
getline(cin, empName);
cout << "Enter Department: ";
getline(cin, department);
cout << "Enter Basic Salary: ";
cin >> basicSalary;
totalSalary = basicSalary + 0.20 * basicSalary; // Example calculation
if (totalSalary >= 50000) {
grade = 'A';
} else if (totalSalary >= 30000) {
grade = 'B';
} else {
grade = 'C';
}
}
void displayDetails() const {
cout << "\nEmployee Details:\n";
cout << "Employee Number: " << empNumber << endl;
cout << "Employee Name: " << empName << endl;
cout << "Department: " << department << endl;
cout << "Basic Salary: $" << basicSalary << endl;
cout << "Total Salary: $" << totalSalary << endl;
cout << "Grade: " << grade << endl;
}
};
int main() {
Employee emp;
emp.getDetails();
emp.displayDetails();
return 0;
}
Output:
Enter Employee Number: 1001
Enter Employee Name: Abi
Enter Department: Computer Science
Enter Basic Salary: 50000
Employee Details:
Employee Number: 1001
Employee Name: Abi
Department: Computer Science
Basic Salary: $50000
Total Salary: $60000
Grade: A
7. VIRTUAL FUNCTIONS
Program:
#include <iostream>
using namespace std;
class Shape {
public:
virtual double area() const {
return 0.0;
}
virtual double perimeter() const {
return 0.0;
}
virtual ~Shape() {}
};
class Rectangle : public Shape {
private:
double width, height;
public:
Rectangle(double w, double h) : width(w), height(h) {}
double area() const override {
return width * height;
}
double perimeter() const override {
return 2 * (width + height);
}
};
int main() {
Shape* shape = new Rectangle(4.0, 6.0); // Rectangle with width 4.0 and height 6.0
cout << "Area: " << shape->area() << endl;
cout << "Perimeter: " << shape->perimeter() << endl;
delete shape;
return 0;
}
Output:
Area: 24
Perimeter: 20
8. FUNCTION OVERLOADING
Program:
#include <iostream>
#include <vector>
using namespace std;
void readMatrix(vector<vector<int>>& matrix, int rows, int cols) {
cout << "Enter the elements of the integer matrix:" << endl;
for (int i = 0; i < rows; ++i) {
for (int j = 0; j < cols; ++j) {
cout << "Element (" << i << ", " << j << "): ";
cin >> matrix[i][j];
} } }
void readMatrix(vector<vector<float>>& matrix, int rows, int cols) {
cout << "Enter the elements of the floating-point matrix:" << endl;
for (int i = 0; i < rows; ++i) {
for (int j = 0; j < cols; ++j) {
cout << "Element (" << i << ", " << j << "): ";
cin >> matrix[i][j];
} } }
template <typename T>
void displayMatrix(const vector<vector<T>>& matrix) {
cout << "Matrix contents:" << endl;
for (const auto& row : matrix) {
for (const auto& elem : row) {
cout << elem << " "; }
cout << endl; } }
int main() {
int rows, cols;
cout << "Enter the number of rows and columns for the integer matrix: ";
cin >> rows >> cols;
vector<vector<int>> intMatrix(rows, vector<int>(cols));
readMatrix(intMatrix, rows, cols);
displayMatrix(intMatrix);
cout << "Enter the number of rows and columns for the floating-point matrix: ";
cin >> rows >> cols;
vector<vector<float>> floatMatrix(rows, vector<float>(cols));
readMatrix(floatMatrix, rows, cols);
displayMatrix(floatMatrix);
return 0;
}
Output:
Enter the number of rows and columns for the integer matrix: 2
2
Enter the elements of the integer matrix:
Element (0, 0): 4
Element (0, 1): 2
Element (1, 0): 5
Element (1, 1): 8
Matrix contents:
42
58
9. FILE CREATION
Program:
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
void createFile(const string& filename) {
ofstream outfile(filename); // Open file for writing
if (!outfile) {
cerr << "Error creating file." << endl;
return;
}
outfile << "This is the first line." << endl;
outfile << "This is the second line." << endl;
outfile << "This is the third line." << endl;
outfile.close(); // Close the file after writing
}
void displayFileWithLineNumbers(const string& filename) {
ifstream infile(filename); // Open file for reading
if (!infile) {
cerr << "Error opening file." << endl;
return;
}
string line;
int lineNumber = 1;
while (getline(infile, line)) {
cout << lineNumber++ << ": " << line << endl;
}
infile.close(); // Close the file after reading
}
int main() {
string filename = "example.txt";
createFile(filename);
cout << "Contents of the file with line numbers:" << endl;
displayFileWithLineNumbers(filename);
return 0;
}
10. MERGE TWO FILES
Program:
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
void mergeFiles(const string& file1, const string& file2, const string& outputFile) {
ifstream infile1(file1); // Open first file for reading
ifstream infile2(file2); // Open second file for reading
ofstream outfile(outputFile); // Open output file for writing
if (!infile1) {
cerr << "Error opening file: " << file1 << endl;
return;
}
if (!infile2) {
cerr << "Error opening file: " << file2 << endl;
return;
}
if (!outfile) {
cerr << "Error creating file: " << outputFile << endl;
return;
}
string line;
while (getline(infile1, line)) {
outfile << line << endl;
}
while (getline(infile2, line)) {
outfile << line << endl;
}
infile1.close();
infile2.close();
outfile.close();
cout << "Files merged successfully into " << outputFile << endl;
}
int main() {
string file1, file2, outputFile;
cout << "Enter the name of the first file: ";
cin >> file1;
cout << "Enter the name of the second file: ";
cin >> file2;
cout << "Enter the name of the output file: ";
cin >> outputFile;
mergeFiles(file1, file2, outputFile);
return 0;
}