FCP LAB Programs
FCP LAB Programs
Program:
#include <stdio.h>
#include <math.h>
#define MAXSIZE 10
void main()
float x[MAXSIZE];
int i, n;
clrscr();
scanf("%f", &x[i]);
std_deviation = sqrt(variance);
Output:
Enter the value of N
34
88
32
12
10
Program:
#include<stdio.h>
#include<conio.h>
int main()
clrscr();
scanf("%d", &n);
while(count <= n)
{
flag = 0;
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;
scanf("%d", &n);
t2 = nextTerm;
nextTerm = t1 + t2;
return 0;
Output:
Enter the number of terms: 10
Program:
#include <stdio.h>
void main ()
{
int num[20];
int i, j, a, n;
scanf("%d", &n);
scanf("%d", &num[i]);
for (i = 0; i < n; ++i)
a = num[i];
num[i] = num[j];
num[j] = a;
} } }
{
printf("%d", num[i]); } }
Output:
enter number of elements in an array
12
23
89
11
22
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;
scanf("%[^\n]s",&str);
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++;
Output:
Enter the string: I am human
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;
}