0% found this document useful (0 votes)
9 views36 pages

I Ai&Ds Computer Programming Lab

The document outlines a series of C and C++ programming exercises, each with a specific aim, algorithm, coding example, and output results. Exercises include calculating sum, average, and standard deviation, generating prime numbers, Fibonacci series, sorting numbers, counting vowels, and using classes for employee details. The document also covers advanced topics such as virtual functions, function overloading, file creation, and merging files.

Uploaded by

priya.eccs
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views36 pages

I Ai&Ds Computer Programming Lab

The document outlines a series of C and C++ programming exercises, each with a specific aim, algorithm, coding example, and output results. Exercises include calculating sum, average, and standard deviation, generating prime numbers, Fibonacci series, sorting numbers, counting vowels, and using classes for employee details. The document also covers advanced topics such as virtual functions, function overloading, file creation, and merging files.

Uploaded by

priya.eccs
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

EX.

NO:01
FIND THE SUM, AVERAGE, AND STANDARD DEVIATION
DATE:

AIM:

To write a C program to find the sum, average, and standard deviation for a
given set of numbers.

ALGORITHM:

STEP 1: Start the program.


STEP 2: Read how many numbers n you want to process.
STEP 3: Read the n numbers from the user.
STEP 4: Compute the sum of the numbers.
STEP 5: Find the average = avg=sum/(float)n.
STEP 6: Find the standard deviation using:
variance=varsum/(float)n
stddev=sqrt(variance)
STEP 7: Display the sum, average, and standard deviation.
STEP 8: Stop the program.
CODING:
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
float x[50];
int i,n;
float sum=0,avg,variance,stddev,d,varsum=0;
printf("\n Enter number of values\n");
scanf("%d",&n);
printf("\n Enter the values\n");
for(i=0;i<n;i++)
{
scanf("%f",&x[i]);
sum=sum+x[i];
}
avg=sum/(float)n;
for(i=0;i<n;i++)
{
d=x[i]-avg;
varsum=varsum+pow(d,2);
}
variance=varsum/(float)n;
stddev=sqrt(variance);
printf("\n Sum =%.2f",sum);
printf("\n Average =%.2f",avg);
printf("\n Standard Deviation =%.2f",stddev);
getch();
}
OUTPUT

RESULT:

Thus the above program has been executed successfully.


AIM:

[Link]
GENERATE N PRIME NUMBERS
DATE:

To write a C program to generate the first n prime numbers.

ALGORITHM:

STEP 1: Start the program.


STEP 2: Read the value of n (how many prime numbers to generate).
STEP 3: Initialize a counter (count = 0) and a number (num = 2).
STEP 4: Repeat until count equals n:
Check if num is prime:
 A number is prime if it is divisible only by 1 and itself.
 If num is prime, display it and increment count.
 Increment num by 1.
STEP 5: Stop the program.
CODING:

#include<stdio.h>
#include<conio.h>
void main()
{
int i, num, n, count;
clrscr();
printf("Enter the range: ");
scanf("%d", &n);
printf("The prime numbers in between the range 1 to %d:",n);
for(num = 1;num<=n;num++)
{
count = 0;
for(i=2;i<=num/2;i++)
{
if(num%i==0)
{
count++;
break;
}
}
if(count==0 && num!= 1)
printf("%d ",num);
}
getch();
}
OUTPUT:
Enter the range:50

The prime numbers in between the range 1 to 50:

2 3 5 7 11 13 17 19 23 29 31 37 41 43 47
RESULT:

Thus the above program has been executed successfully.

[Link]
FIBONACCI SERIES
DATE:

AIM:

To write a C program to generate the Fibonacci series up to n terms.

ALGORITHM:

STEP 1: Start the program.


STEP 2: Read how many terms n you want in the Fibonacci series.
STEP 3: Initialize the first two terms:
 first = 0
 second = 1
STEP 4: Display the first two terms.
STEP 5: Use a loop to generate the remaining terms:
 next = first + second
 Print next
 Update: first = second, second = next.
STEP 6: Stop the program.
CODING:

#include<stdio.h>
#include<conio.h>
void main()
{
int i, n;
int t1 = 0, t2 = 1;
int nextTerm = t1 + t2;
clrscr();
printf("Enter the number of terms: ");
scanf("%d", &n);
printf("Fibonacci Series: ") ;
if(n==1)
printf("%d",t1);
else if(n>1)
printf("%d, %d, ", t1, t2);
else
printf("Enter number greater than 0");
for (i = 3; i <= n; i++)
{
printf("%d, ", nextTerm);
t1 = t2;
t2 = nextTerm;
nextTerm = t1 + t2;
}
getch();
}
OUTPUT:

Enter the number of terms: 6

Fibonacci Series: 0, 1, 1, 2, 3, 5,
RESULT:

Thus the above program has been executed successfully.

[Link]
NUMBERS IN ASCENDING ORDER
DATE:

AIM :

To write a C program to sort a given set of numbers in ascending order.

ALGORITHM :

STEP 1: Start the program.


STEP 2: Read how many numbers n you want to sort.
STEP 3: Read n numbers into an array.

STEP 4: Use a simple sorting method (Bubble Sort):


Repeat for i = 0 to n-2
For j = 0 to n-i-2
If arr[j] > arr[j+1], swap them.

STEP 5: Display the numbers after sorting.

STEP 6: Stop the program.


CODING:

#include<stdio.h>
#include<conio.h>
void main()
{
int a[10],n,i,j,temp;
clrscr();
printf("How many elements? ");
scanf("%d",&n);
printf("Enter the elements\n");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
printf("Sorted elements are\n");
for(i=0;i<n;i++)
for(j=i+1;j<n;j++)
{
if(a[i]>a[j])
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
for(i=0;i<n;i++)
printf("%d\n",a[i]);
getch();
}

Output:
How many elements? 5

Enter the elements

Sorted elements are

5
RESULT:

Thus the above program has been executed successfully.

[Link]

COUNT THE NUMBER OF VOWELS IN A GIVEN SENTENCE


DATE:

AIM:

To write a C program to count the number of vowels in a given sentence.

ALGORITHM:

STEP 1: Start the program.

STEP 2: Read a sentence from the user.


STEP 3: Initialize a counter count = 0.

STEP 4: Traverse each character of the sentence:

o If the character is a vowel (a, e, i, o, u or A, E, I, O, U), increment


count.
STEP 5: Display the total number of vowels.
STEP 6: Stop the program.
CODING:

#include<stdio.h>
#include<conio.h>
void main()
{
char str[100];
int i,count=0;
clrscr();
printf("Enter the sentence to count vowels:");
gets(str);
for(i=0;str[i]!='\0';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')
{
count++;
}

}
printf("\nNumber of vowels in given sentence: %d\n",count);
getch();
}
OUTPUT:

Enter the sentence to count vowels: This is a program to count vowels

Number of vowels in given sentence: 10


RESULT:

Thus the above program has been executed successfully.

[Link]

DATE: STORE AND DISPLAY EMPLOYEE DETAIL

AIM:

To write a C++ program using a class to store and display employee details
like Employee Number, Employee Name, Department, Basic Pay, Salary, and
Grade.

ALGORITHM:

STEP 1: Start the program.


STEP 2: Create a class Employee.
STEP 3: Define data members: Employee Number, Employee Name,
Department, Basic, Salary, and Grade.
STEP 4: Create member functions:
getData() → to input employee details.
displayData() → to display employee details.
STEP 5: In main(), create an object of the class.
STEP 6: Call getData() to read values from the user.
STEP 7: Call displayData() to print the details.
STEP 8: Stop the program.

CODING:
#include<iostream.h>
#include<conio.h>
class Employee
{
int number;
char name[30], department[20], grade[5];
float basic, salary;
public:
void getData()
{
cout << "Enter Employee Number: ";
cin >> number;
cout << "Enter Employee Name: ";
cin >> name;
cout << "Enter Department: ";
cin >> department;
cout << "Enter Basic Pay: ";
cin >> basic;
cout << "Enter Salary: ";
cin >> salary;
cout << "Enter Grade: ";
cin >> grade;
}
void displayData()
{
cout << "\n--- Employee Details ---\n";
cout << "Employee Number : " <<number << endl;
cout << "Employee Name : " << name << endl;
cout << "Department : " << department << endl;
cout << "Basic Pay : " << basic << endl;
cout << "Salary : " << salary << endl;
cout << "Grade : " << grade << endl;
}
};
void main()
{
Employee emp;
[Link]();
[Link]();
getch();
}
OUTPUT:
RESULT:
Thus the above program has been executed successfully.

[Link]

DATE: VIRTUAL FUNCTIONS

AIM:
To write a C++ program to create a class SHAPE that uses virtual functions
to calculate the area of different shapes (e.g., Rectangle and Circle).

ALGORITHM:

STEP 1: Start the program.


STEP 2: Create a base class SHAPE with two virtual functions: getData()
and area().
STEP 3: Create derived classes Rectangle and Circle that override these
virtual functions.
STEP 4: In each derived class:
o getData() → input dimensions of the shape.
o area() → calculate and display the area.
STEP 5: In main(), create objects for Rectangle and Circle.
STEP 6: Use the dot operator (.) to call the functions for each object.
STEP 7: Display the calculated areas.
STEP 8: Stop the program.

CODING:
#include<iostream.h>
#include<conio.h>
class SHAPE
{
public:
virtual void getData()
{
}
virtual void area()
{
}
};
class Rectangle : public SHAPE
{
float length, breadth;
public:
void getData()
{
cout << "Enter length and breadth of Rectangle: ";
cin >> length >> breadth;
}
void area()
{
cout << "Area of Rectangle = " << (length * breadth) << endl;
}
};
class Circle : public SHAPE
{
float radius;
public:
void getData()
{
cout << "Enter radius of Circle: ";
cin >> radius;
}
void area()
{
cout << "Area of Circle = " << (3.1416 * radius * radius) << endl;
}
};
int main()
{
Rectangle r;
Circle c;
cout << "\n--- Rectangle ---\n"; [Link]();
[Link]();
cout << "\n--- Circle ---\n"; [Link]();
[Link]();
return 0;
}
OUTPUT:
RESULT:
Thus the above program has been executed successfully.

[Link]

DATE: FUNCTION OVERLOADING

AIM:

To write a simple C++ program using function overloading to read and


display two values of different data types such as integer and floating-point
numbers.

ALGORITHM:

STEP 1: Start the program.


STEP 2: Create a class display.
STEP 3: Overload the function readdata() in two versions:
 One takes two integers.
 Another takes two floats.
STEP 4: In each version, read and display the values.
STEP 5: In main(), call the overloaded functions using integer and float
values.
STEP 6: Stop the program.

CODING:
#include<iostream.h>
class display
{
public:
void readdata(int a, int b) {
cout << "Integer values: " << a << " , " << b << endl;
}
void readdata(float x, float y)
{
cout << "Float values: " << x << " , " << y << endl;
}
};
int main()
{
display obj;
int a, b;
float x, y;
cout << "Enter two integer values: ";
cin >> a >> b;
[Link](a, b);
cout << "Enter two float values: ";
cin >> x >> y;
[Link](x, y);
return 0;
}

OUTPUT:
RESULT:

Thus the above program has been executed successfully.

[Link]

CREATE A FILE
DATE:

AIM:

To write a C++ program to create a file and display its contents along with
line numbers.

ALGORITHM:

STEP 1: Start the program.

STEP 2: Include the header file <fstream> for file handling.


STEP 3: Declare an ofstream object to create and write to a file.

STEP 4: Ask the user to enter text (multiple lines).

STEP 5: Write the text to the file.

STEP 6: Close the file.

STEP 7: Open the same file using ifstream.


STEP 8: Read each line and display it with its line number.
STEP 9: Close the file and stop the program.
CODING:
#include<iostream.h>
#include<fstream.h>
#include<stdio.h>
#include<string.h>
#include<conio.h>
int main()
{
char fname[30], str[100];
fstream fp;
int i=0;
clrscr();
cout<<"Enter the Name of File: ";
gets(fname);
[Link](fname, fstream::out);
if(!fp)
{
cout<<"\nError Occurred!";
return 0;
}
cout<<"Enter the Data: ";
gets(str);
while(strlen(str)>0)
{
fp<<str;
fp<<'\n';
gets(str);
}
[Link]();
[Link](fname, fstream::in);
if(!fp)
{
cout<<"\nError Occurred!";
return 0;
}
cout<<"\nContent of "<<fname<<":-\n";
[Link](str, 1000);
while(strlen(str)>0)
{
cout<<++i<<".";
cout << str << endl;
[Link](str, 1000);
}
[Link]();
getch();
return 0;
}
OUTPUT:

RESULT:

Thus the above program has been executed successfully.


[Link]

DATE: MERGE TWO FILES

AIM:

To write a C++ program to merge two files into a single file

ALGORITHM:

STEP 1: Start the program.


STEP 2: Include the header <fstream> for file operations.
STEP 3: Open the first file in read mode.
STEP 4: Open the second file in read mode.
STEP 5: Open a third file in write mode.
STEP 6: Copy all the contents of the first file into the third file.
STEP 7: Copy all the contents of the second file into the third file.
STEP 8: Close all the files.
STEP 9: Stop the program.
CODING:
#include<iostream.h>
#include<fstream.h>
#include<stdio.h>
#include<stdlib.h>
int main()
{
ifstream fp1, fp2;
ofstream fp3;
char ch, fname1[100], fname2[100], fname3[100];
cout<<"Enter first file name with extension: ";
cin>>fname1;
cout<<"\nEnter second file name with extension: ";
cin>>fname2;
cout<<"\nEnter target file with extension to copy: ";
cin>>fname3;
[Link](fname1);
[Link](fname2);
[Link](fname3);
if(!fp1 || !fp2 || !fp3)
{
perror("\nError Message in file opening ");
exit(EXIT_FAILURE);
}
while(![Link]())
{
fp1>>ch;
fp3<<ch;
}
while(![Link]())
{
fp2>>ch;
fp3<<ch;
}
cout<<"\nThe two files were merged into "<<fname3<<" file
successfully....!!\n";
[Link]();
[Link]();
[Link]();
return 0;
}
OUTPUT:

File Location in BIN:

Output File:

RESULT:
Thus the above program has been executed successfully.

You might also like