Computer Science Practical
Computer Science Practical
11
Practical Programs – Hand Book
Instructions:
3. Distribution of Marks
Total 20 Marks
1
INDEX
2 CS2 Percentage 5
3 CS3 Palindrome 7
2
CS1 - GROSS SALARY
Write a C++ program to input basic salary of an employee and calculate its Gross
CS-1
salary according to following:
Coding
#include <iostream>
#include<iomanip>
using namespace std;
int main()
{
float basic, gross, da,hra;
/* Input basic salary of employee */
cout<<"Enter basic salary of an employee: ";
cin>>basic;
/* Calculate D.A and H.R.A according to specified conditions */
if (basic <25000)
{
da = basic *80/100;
hra= basic *20/100;
}
else if (basic >=25000 && basic<40000)
{
da = basic *90/100;
hra= basic *25/100;
}
else if (basic>=40000)
{
3
da = basic *95/100;
hra= basic *30/100;
}
/* Calculate gross salary */
gross= basic +hra+ da;
cout<<setw (25) << "Basic Pay "<<setw (10)<< basic<<endl;
cout<< setw (25) << " Dearness Allowance" << setw (10)<< da <<endl;
cout<< setw (25) "House Rent Allowance "<<setw (10)<<hra<<endl;
cout<< setw (25) " "<<setw (10) <<"------------"<<endl;
cout<< setw (25) "Gross Salary "<<setw (10) << gross <<endl;
cout<< setw (25) " "<<setw (10) <<"------------" <<endl;
return 0;
}
Output
4
CS2 - PERCENTAGE
Write a C++ program to check percentage of a student and display the division
CS-2 (distinction, first, second, third or fail) scored using switch case
Percentage Division
>=80 Distinction
>=60 and <80 First division
>=50 and <60 Second Division
>=40 and <50 Third Division
<40 Fail
Coding
#include <iostream>
using namespace std;
int main()
{
float percent;
int x;
cout<<"Enter your percentage: ";
cin>>percent;
cout<<"You scored "<<percent<<"%"<<endl;
x = percent/10;
switch (x)
{
case 10:
case 9:
case 8:
cout<<"You have passed with Distinction";
break;
case 7:
case 6:
cout<<"You have passed with First division";
5
break;
case 5:
cout<<"You have passed with Second division";
break;
case 4:
cout<<"You have passed with Third division";
break;
default:
cout<<"Sorry: You have failed";
}
return 0;
}
Output 1
6
CS3 - PALINDROME
Write a C++ program to enter any number and check whether the number is palindrome
CS-3 or not using while loop.
Coding
#include <iostream>
using namespace std;
int main()
{
int n,num, digit, rev =0;
cout<<"Enter a positive number: ";
cin>>num;
n =num;
while (num)
{
digit=num%10;
rev=(rev *10)+ digit;
num=num/10;
}
cout<<" The reverse of the number is: "<< rev <<endl;
if (n == rev)
cout<<" The number is a palindrome";
else
cout<<" The number is not a palindrome";
return 0;
}
Output 1
CS-4 Using do while loop create the following menu based C++ program
Coding
#include <iostream>
#include <cmath>
using namespace std;
int main()
{
int dec,d,i,temp,ch;
long int bin;
do
{
dec=bin=d=i=0;
cout<<"\n\n\t\tMENU\n1. Decimal to Binary number\n2.Binary to Decimal number\
n3.Exit\n";
cout <<"Enter your choice(1/2/3)";
cin>>ch;
switch (ch)
{
case 1: cout << "Enter a decimal number: "; cin >> dec;
temp=dec;
while (dec!=0)
{
d = dec%2;
bin += d * pow(10,i);
8
dec /= 2;
i++;
}
cout << temp << " in decimal = " << bin << " in binary" << endl ;break;
case 2: cout << "Enter a binary number: "; cin >> bin;
temp=bin;
while (bin!=0)
{
d = bin%10;
dec += d*pow(2,i);
bin /= 10;
i++;
}
cout << temp << " in binary = " <<dec << " in decimal";
break;
case 3: break;
default : cout<<"Invalid choice";
}
} while (ch!=3);
return 0;
}
Output 1
MENU
1.Decimal to Binary number
2.Binary to Decimal number
3.Exit
Enter your choice(1/2/3)1
Enter a decimal number: 23
23 in decimal = 10111 in binary
MENU
1.Decimal to Binary number
9
2.Binary to Decimal number
3.Exit
Enter your choice(1/2/3)2
Enter a binary number: 11001
11001 in binary = 25 in decimal
MENU
1.Decimal to Binary number
2.Binary to Decimal number
3.Exit
Enter your choice(1/2/3)3
Output 2
MENU
1.Decimal to Binary number
2.Binary to Decimal number
3.Exit
Enter your choice(1/2/3)4
Invalid choice
MENU
1.Decimal to Binary number
2.Binary to Decimal number
3.Exit
Enter your choice(1/2/3)3
10
CS5 - FIBONACCI PRIME SERIES
Write a C++ program using a user defined function to generate the Fibonacci series till
CS-5 n terms and print if each term is prime or Composite.
Coding
#include <iostream>
#include <stdlib.h>
using namespace std;
void Primechk (int a )
{ int j;
if ( a == 0 || a == 1 )
{ cout<< " NEITHER PRIME NOR COMPOSITE ";}
else
{
for (j = 2 ; j<a; j++)
{ if (a%j==0)
{ cout<< "\tCOMPOSITE" ;
break ;
}
}
if ( a==j )
cout<< "\tPRIME" ;
}
}
void fibo ( int n )
{ int a = -1 , b = 1 ,c=0 ;
for ( int i = 1 ; i <= n ; i++)
{
cout<<endl;
c=a+b;
11
cout<<c;
Primechk(c);
a = b;
b=c;
}
}
int main ()
{
int n ;
cout << " ENTER THE NUMBER OF REQUIRED FIBO TERMS " ;
cin >> n ;
cout<< "\n\tFIBONACCI SERIES\n " ;
fibo (n) ;
return 0;
}
Output
12
CS6 - INSERT / DELETE ELEMENTS IN AN ARRAY
Write a menu driven C++ program to Insert and Delete elements in a single dimension
CS-6 array of integers and print the array after insertion or deletion
Coding
#include<iostream>
using namespace std;
int a[20],b[20],c[40];
int m,n,p,val,i,j,key,pos,temp;
/*Function Prototype*/
void display();
void insert();
void del();
int main()
{
int choice;
cout<<"\nEnter the size of the array elements:\t";
cin>>n;
cout<<"\nEnter the elements for the array:\n";
for (i=0;i<n;i++)
{
cin>>a[i];
}
do {
cout<<"\n\n--------Menu-----------\n";
cout<<"1.Insert\n";
cout<<"2.Delete\n";
cout<<"3.Exit\n";
cout<<"-----------------------";
cout<<"\nEnter your choice:\t";
cin>>choice;
13
switch (choice)
{
case 1: insert();
break;
case 2: del();
break;
case 3:break;
default :cout<<"\nInvalid choice:\n";
}
} while (choice!=3);
return 0;
}
void display()//displaying an array elements
{
int i;
cout<<"\nThe array elements are:\n";
for(i=0;i<n;i++)
{
cout<<a[i]<<" ";
}
}//end of display()
void insert()//inserting an element in to an array
{
cout<<"\nEnter the position for the new element:\t";
cin>>pos;
cout<<"\nEnter the element to be inserted :\t";
cin>>val;
for (i=n; i>=pos-1; i--)
{
a[i+1]=a[i];
}
14
a[pos-1]=val;
n=n+1;
display();
}//end of insert()
void del()//deleting an array element
{
cout<<"\n Enter the position of the element to be deleted:\t";
cin>> pos;
val= a [pos];
for (i= pos;i<n-1;i++)
{
a[i]=a[i+1];
}
n=n-1;
cout<<"\nThe deleted element is = "<<val;
display();
}//end of delete()
Output
16
CS 7 - Boundary Element of a Matrix
Coding
#include <iostream>
using namespace std;
void printBoundary (int a[][10], int m, int n)
{
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++)
{
if (i==0|| j==0||i==m-1||j==n-1)
cout<<a[i][j]<<" ";
else
cout<<" ";
}
cout <<endl ;
}
}
// Driver code
int main()
{
int a[10][10] ,i,j,m,n;
cout<<"Enter more than 3 number of rows and columns"<<endl;
cin>>m>>n;
for (i=0;i<m;i++)
{
for (j=0;j<n;j++)
{
cout<<"enter the value for array["<<i+1<<"]"<<"["<<j+1<<"] :";
17
cin>>a[i][j];
}
}
system("cls");
cout<<"\n\nOriginal Array\n";
for (i=0;i<m;i++)
{
for (j=0;j<n;j++)
{
cout<<a[i][j]<<" ";
}
cout<<endl;
}
cout<<"\n\n The Boundry element\n";
printBoundary(a, m, n);
return 0;
}
Output
18
enter the value for array[3][4] :2
enter the value for array[4][1] :3
enter the value for array[4][2] :4
enter the value for array[4][3] :5
enter the value for array[4][4] :6
Original Array
1234
5678
9012
3456
The Boundary element
1 2 3 4
5 8
9 2
3 4 5 6
19
CS8 - ABC PUBLISHERS
CS-8 Define a class named Publisher in C++ with the following descriptions
private members
Bookno integer
Title 20 characters
Author 10 characters
price float
Totamt float
Define a member function called calculate() to calculate the number of copies and the price
and return the total amount.
Public members
• A default constructor function to initialize all data members.The book number must be
automatically generated staring from 1001
• Readdata() function to accept values for Title,Author and price.Get the number of copies
from the user and invoke calculate().
• Display data () function display all the data members in the following format
ABC PUBLISHERS
~~~~~~~~~~~~~~~~~
INVOICE
~~~~~~~~~
==================================
Book Number :
Title :
Author Name :
Price Per Book :
Total Amount :
==================================
Coding
#include<iostream>
#include<stdlib.h>
using namespace std;
int id=1001;
20
class Publisher
{
int Bookno;
char Title[20];
char Author [10];
float Price;
float Totamt;
float calculate (int);
public:
Publisher()
{Bookno=id;
Title[0]='\0';
Author[0]='\0';
Price=0;
Totamt=0;
id++;
}
void Readdata();
void Displaydata();
};
void Publisher::Readdata()
{
int nocopies;
cout<<"Enter the Title name ";cin>>Title;
cout<<"Enter the Author name ";cin>>Author;
cout<<"Enter the Price ";cin>>Price;
cout<<"Enter the Number of copies ";cin>>nocopies;
Totamt=calculate(nocopies);
}
float Publisher::calculate(int x)
{
21
return x*Price;
}
void Publisher::Displaydata()
{
cout<<"\n\t\tABC PUBLISHERS\n";
cout<<"\t\t~~~~~~~~~~~~~~\n";
cout<<"\t\t INVOICE\n";
cout<<"\t\t ~~~~~~~\n";
cout<<"\n==================================\n";
cout<<" Book Number : "<<Bookno<<endl;
cout<<"Title : "<<Title<<endl;
cout<<"Author Name : "<<Author<<endl;
cout<<"Price Per Book : "<<Price<<endl;
cout<<"Total Amount : "<<Totamt<<endl;
cout<<"\n==================================\n";
}
int main()
{
int n,i;
Publisher p[10];
cout<<"Enter the number of object to be created";cin>>n;
for (i=0;i<n;i++)
p[i].Readdata();
for (i=0;i<n;i++)
p[i].Displaydata();
return 0;
}
Output
22
Enter the Price 500
Enter the Number of copies 3
Enter the Title name CoreJava
Enter the Author name Xavier
Enter the Price 250
Enter the Number of copies 5
ABC PUBLISHERS
~~~~~~~~~~~~~~
INVOICE
~~~~~~~
==================================
Book Number : 1001
Title : C++Programming
Author Name : Balaguru
Price Per Book : 500
Total Amount : 1500
=================================
ABC PUBLISHERS
~~~~~~~~~~~~~~
INVOICE
~~~~~~~
==================================
Book Number : 1002
Title : CoreJava
Author Name : Xavier
Price Per Book : 250
Total Amount : 1250
==================================
23
CS9 - EMPLOYEE DETAILS USING CLASS
Create a C++ program to create a class employee containg the following members
CS-9
in public.
Public members
eno integer
name 20 characters
des 20 characters
member Function
void get() to accept values for all data members
Declare the derived class called Salary which contain the following details.
Public members
bp, hra, da, pf, np float
member Function
void get1() to accept values for bp,hra,da and pf and invoke calculate()
calculate() calculate the np by adding bp,hra,da subtracting pf
display() Display all the details
Create the derived class object and read the number of employees.Call the function
get(),get1() for each employee and display the details
Coding
#include<iostream>
using namespace std;
class emp{
public:
int eno;
char name[20], des[20];
void get(){
cout<<"Enter the employee number:";
cin>>eno;
cout<<"Enter the employee name:";
24
cin>>name;
cout<<"Enter the designation:";
cin>>des;
}
};
class salary :public emp
{
float bp,hra, da,pf,np;
public:
void get1()
{
cout<<"Enter the basic pay:";
cin>>bp;
cout<<"Enter the HouseRent Allowance:";
cin>>hra;
cout<<"Enter the Dearness Allowance :";
cin>>da;
cout<<"Enter the Provident Fund:";
cin>>pf;
}
void calculate()
{
np=bp+hra+ da -pf;
}
void display()
{
cout<<eno<<"\t"<<name<<"\t"<<des<<"\t"<<bp<<"\t"<<hra<<"\t"<<da<<"\t"<<pf<<"\
t"<<np<<"\n";
}
};
int main(){
int i, n;
25
char ch;
salary s[10];
cout<<"Enter the number of employee:";
cin>>n;
for (i =0; i < n; i++){
s[i].get();
s[i].get1();
s[i].calculate();
}
cout<<"\n\t\t\tEmployee Details\n";
cout<<"\ne_no \t e_name\t des \t bp \t hra \t da \t pf \t np \n";
for (i =0; i < n; i++){
s[i].display();
}
return 0;
}
Output
26
CS10 -STUDENT DETAILS
CS-10 Write a C++ program to create a class called Student with the following details
Protected member
Rno integer
Public members
void Readno(int); to accept roll number and assign to Rno
void Writeno(); To display Rno.
The class Test is derived Publically from the Student class contains the following details
Protected member
Mark1 float
Mark2 float
Public members
void Readmark(float,float); To accept mark1 and mark2
void Writemark(); To display the marks
Create a class called Sports with the following detail
Protected members
score integer
Public members
void Readscore(int); To accept the score
void Writescore(); To display the score
The class Result is derived Publically from Test and Sports class contains the following
details
Private member
Total float
Public member
void display() assign the sum of mark1 ,mark2,score in total.
invokeWriteno(),Writemark() and Writescore() .Display the total also.
27
Coding
#include<iostream>
using namespace std;
class Student
{
protected:
int Rno;
public:
void Readno(int r)
{
Rno=r;
}
void Writeno()
{
cout<<"\nRoll no : "<<Rno;
}
};
class Test :public Student
{
protected:
float Mark1,Mark2;
public:
void Readmark (float m1,float m2)
{
Mark1=m1;
Mark2=m2;
}
void Writemark()
{
cout<<"\n\n\tMarks Obtained\n ";
cout<<"\n Mark1 : "<<Mark1;
28
cout<<"\n Mark2 : "<<Mark2;
}
};
class Sports
{
protected:
int score;// score = Sports mark
public:
void Readscore (int s)
{
score=s;
}
void Writescore()
{
cout<<"\n Sports Score : "<<score;
}
};
class Result :public Test,public Sports
{
int Total;
public:
void display()
{
Total = Mark1 + Mark2 + score;
Writeno();
Writemark();
Writescore();
cout<<"\n\n Total Marks Obtained : "<< Total<<endl;
}
};
int main()
29
{
Result stud1;
stud1.Readno(1201);
stud1.Readmark(93.5,95);
stud1.Readscore(80);
cout<<"\n\t\t\t HYBRID INHERITANCE PROGRAM\n";
stud1.display();
return 0;
}
Output
30
Internal Choice For The Programs
Insert / Delete
CS1 Gross Salary CS6 CS1 or CS7
Elements In An Array
Boundary Element Of
CS2 Percentage CS7 CS2 or CS9
A Matrix
Employee Details
CS4 Number Conversion CS9 CS4 or CS6
Using Class
31