C++ Lab Programs
C++ Lab Programs
14
Program To Generate Histogram
Test case:
Aim:
To find and write a program to generate histogram.
Procedure:
(The algorithm)
(The program)
#include <iostream>
using namespace std;
class histogram {
int a[15],n,*count,max;
public:
void init();
void calculate();
void display();
};
void histogram :: init(){
cin>>n;
for(int i=0;i<n;i++)
cin>>a[i];
}
void histogram :: calculate(){
max = a[0];
for(int i=1;i<n;i++)
if(max<a[i])
max=a[i];
count=new int[max+1];
for(int i=0;i<=max;i++)
count[i]=0;
for(int i=0;i<n;i++)
count[a[i]]++;
}
void histogram :: display(){
for(int i=0;i<=max;i++)
if(count[i]!=0)
cout<<i<<" "<<count[i]<<endl;
}
int main()
{
histogram s;
s.init();
s.calculate();
s.display();
return 1;
}
Result:
The program to generate histogram was successfully executed.
Output:
No.16
(a)
Program To Convert Decimal Number To Binary Number
Test case:
Aim:
To find and write a program for converting decimal number to binary number.
Procedure:
(The algorithm)
Step 1: Divide the number by 2 through % (modulus operator) and store the remainder in array.
Step 2: Divide the number by 2 through / (division operator).
(The program)
#include <iostream>
using namespace std;
int main()
{
int a[10], n, i;
cin>>n;
for(i=0; n>0; i++)
{
a[i]=n%2;
n= n/2;
}
cout<<"Binary of the given number= ";
for(i=i-1 ;i>=0 ;i--)
{
cout<<a[i];
}
}
Result:
The program to convert decimal number to binary number was successfully executed.
Output:
No.16
(b)
Program To Convert Binary Number To Decimal Number
Test case:
Aim:
To find and write a program for converting binary number to decimal number.
Procedure:
(The algorithm)
1. Take a binary number as the input.
2. Divide the number by 10 and store the remainder into variable rem.
3. decimal_num=decimal_num+rem*base;
Initially, the decimal_num is 0, and the base is 1, where the rem variable stores the
remainder of the number.
4. Divide the quotient of the original number by 10.
5. Multiply the base by 2.
6. Print the decimal of the binary number.
(The program)
#include<iostream>
using namespace std;
int main()
{
int binnum, decnum=0, i=1, rem;
cin>>binnum;
while(binnum!=0)
{
rem = binnum%10;
decnum = decnum + (rem*i);
i = i*2;
binnum = binnum/10;
}
cout<<"Equivalent Decimal Value = "<<decnum;
cout<<endl;
return 0;
}
Result:
The program to convert binary number to decimal number was successfully executed.
Output:
No.17
Program To Delete N Characters In A String
Test case:
Aim:
To find and write a program for deleting n characters in a string.
Procedure:
(The algorithm)
Step 1 − Start
Step 2 − Read string at runtime
Step 3 − Read position from where we need to delete the characters
Step 4 − Read n, number of characters to delete from that position
Step 5 − Call the function deletestr(str,p,n) jump to step 7
Step 6 − Stop
Step 7 − Called function deletestr(str,p,n)
(The program)
using namespace std;
#include<iostream>
class string1{
char a[50];
int n,i;
public:
void init();
void delet();
void display();
};
void string1::init(){
cin>>a;
cin>>n>>i;
}
void string1::delet(){
int l=0,j,o;
while(a[l]!='\0')
{
l++;
}
l--;
j=i;
o=i+n;
while(o<=l){
a[j]=a[o];
j++;
o++;
}
a[j]='\0';
}
void string1::display(){
cout<<a;
int main(){
string1 s;
s.init();
s.delet();
s.display();
return 0;
}
Result:
The program to delete n characters in a string was successfully executed.
Output:
No.18
Program To Search Elements Through Linear Search Technique
Test case:
Aim:
To find and write a program for searching elements through linear techniques.
Procedure:
(The algorithm)
Step 1: Set i to 1
Step 2: if i > n then go to step 7
Step 3: if A[i] = x then go to step 6
Step 4: Set i to i + 1
Step 5: Go to Step 2
Step 6: Print Element x Found at index i and go to step 8
Step 7: Print element not found
Step 8: Exit
(The program)
#include<iostream>
using namespace std;
int main()
{
int arr[10], i, num, index;
for(i=0; i<10; i++)
cin>>arr[i];
cin>>num;
for(i=0; i<10; i++)
{
if(arr[i]==num)
{
index = i;
break;
}
}
cout<<"Found at Index No."<<index;
cout<<endl;
return 0;
}
Result:
The program to search elements through linear search technique was successfully executed.
Output:
No.19
(a)
Program To Sort The Elements In Ascending Order
Test case:
Aim:
To find and write a program to sort the elements in ascending order.
Procedure:
(The algorithm)
STEP 1: START
STEP 2: INITIALIZE arr[] ={5, 2, 8, 7, 1 }..
STEP 3: SET temp =0
STEP 4: length= size of(arr)/size of(arr[0])
STEP 5: PRINT "Elements of Original Array"
STEP 6: SET i=0. REPEAT STEP 7 and STEP 8 UNTIL i<length
STEP 7: PRINT arr[i]
STEP 8: i=i+1.
STEP 9: SET i=0. REPEAT STEP 10 to STEP UNTIL i<n
STEP 10: SET j=i+1. REPEAT STEP 11 UNTIL j<length
STEP11: if(arr[i]>arr[j])then
temp=arr[i]
arr[i]=arr[j]
arr[j]=temp
STEP 12: j=j+1.
STEP 13: i=i+1.
STEP 14: PRINT new line
STEP 15: PRINT "Elements of array sorted in ascending order"
STEP 16: SET i=0. REPEAT STEP 17 and STEP 18 UNTIL i<length
STEP 17: PRINT arr[i]
STEP 18: i=i+1.
STEP 19: RETURN 0.
STEP 20: END.
(The program)
#include <iostream>
using namespace std;
int main()
{
//array declaration
int arr[MAX];
int n,i,j;
int temp;
cin>>n;
//check bound
if(n<0 || n>MAX)
{
cout<<"Input valid range!!!"<<endl;
return -1;
}
//read n elements
for(i=0;i<n;i++)
{
cin>>arr[i];
}
return 0;
}
Result:
The program to sort elements in ascending order was successfully executed.
Output:
(b)
Program To Sort The Elements In Descending Order
Test case:
Aim:
To find and write a program for sorting elements in descending order.
Procedure:
(The algorithm)
START
Step 1 : If it is the first element, it is already sorted. return 1;
Step 2 : Pick next element
Step 3 : Compare with all elements in the sorted sub-list
Step 4 : Shift all the elements in the sorted sub-list that is less than the
value to be sorted
Step 5 : Insert the value
Step 6 : Repeat until list is sorted
STOP
(The program)
#include <iostream>
using namespace std;
int main()
{
//array declaration
int arr[MAX];
int n,i,j;
int temp;
cin>>n;
//check bound
if(n<0 || n>MAX)
{
cout<<"Input valid range!!!"<<endl;
return -1;
}
//read n elements
for(i=0;i<n;i++)
{
cin>>arr[i];
}
return 0;
}
Result:
The program to sort elements in descending order was successfully executed.
Output:
No.20
(a)
Program To Find Factorial Of A Given Number Using Recursion
Test case:
Aim:
To find and write a program for finding the factorial of a given number using recursion.
Procedure:
(The algorithm)
Step 1: Start
Step 2: Read number n
Step 3: Call factorial(n)
Step 4: Print factorial f
Step 5: Stop
factorial(n)
Step 1: If n==1 then return 1
Step 2: Else
f=n*factorial(n-1)
Step 3: Re
(The program)
#include<iostream>
using namespace std;
int main()
{
int n;
cin >> n;
cout << "Factorial of " << n << " = " << factorial(n);
return 0;
}
int factorial(int n)
{
if(n > 1)
return n * factorial(n - 1);
else
return 1;
}
Result:
The program to find factorial of a number using recursion was successfully executed.
Output:
(b)
Program to find factorial of a number without using recursion
Test case:
Aim:
To find and write a program for finding the factorial of a given number without using recursion.
Procedure:
(The algorithm)
Step 1: Start
Step 2: Declare Variable n, fact, i
Step 3: Read number from User
Step 4: Initialize Variable fact=1 and i=1
Step 5: Repeat Until i<=number
5.1 fact=fact*i
5.2 i=i+1
Step 6: Print fact
Step 7: Stop
(The program)
#include <iostream>
using namespace std;
int main()
{
int i,fact=1,number;
cin>>number;
for(i=1;i<=number;i++){
fact=fact*i;
}
cout<<"Factorial of " <<number<<" is: "<<fact<<endl;
return 0;
}
Result:
The program to find factorial of a number without using recursion was successfully executed.
Output: