1. I PUC Computer Science Lab Manual
Prof. K. Adisesha 1
CONTENTS
Pgm
No
Program Name
Page
No
1 Write a program to interchange the values of two variables
a. Using a third variable. b. Without using a third variable.
2
2 Write a program to find the area and circumference of a circle. 3
3 Write a program to find the area of a triangle given three sides. 3
4 Write a program to convert days into years, months and days. (all months have 30 days) 4
5 Write a program to find the largest, smallest and second largest of three numbers using
simple if statement.
4
6 Write a program to input the total amount in a bill, if the amount is greater than 1000 the
discount of 8% is given otherwise no discount is given, output the total amount, the
discount amount and the final amount, use simple if statement.
5
7 Write a program to check whether a given year is a leap year or not using if- else
statement.
6
8 Write a program to input a character and find out whether it is a lower case or upper case
character using if-else statement.
6
9 Write a program to input the number of units of electricity consumed in a house and
calculate the final amount using nested-if statement. Use the following data for
calculation
7
10 Write a program to input the marks of four subjects, calculate the total percentage and
output the result as either “First class”, or “Second class”, or “Pass class” or “Fails”
using switch statement.
8
11 Write a program to find the sum of all the digits of a number using while statement 9
12 Write a program to input principal amount, rate of interest and time period and calculate
compound interest using while statement. (Hint: CI = P * (1 + R / 100) T).
9
13 Write a program to check whether a given number is a power of 2. 10
14 Write a program to find the factorial of a number using for statement. 10
15 Write a program to generate the Fibonacci sequence up to a limit using for statement. 11
16 Write a program to find the sum and average of “N” numbers. 11
17 Write a program to find the second largest of “N” numbers. 12
18 Write a program to arrange a list of numbers in ascending order. 13
19 Write a program to check whether a given string is a palindrome or not. 13
20 Write a program to find the position of a given number in an array. 14
21 Write a program to sum of all the rows and the sum of all the columns of a matrix
separately
14
22 Write a program to find the sum of two compatible matrices. 15
23 Write a program to count the number of vowels and consonants in a string. 16
24 Write a program to find the GCD and LCM of two numbers using functions. 16
25 Write a program to input the register number, name and class of all the students in a
class into a structure and output the data in a tabular manner with proper heading
17
2. I PUC Computer Science Lab Manual
Prof. K. Adisesha 2
Section A
C++ Programs
PROGRAM 1(a):
Write a C++ program to interchange the values of two variables using a
third variable.
#include<iostream.h>
#include<conio.h>
void main( )
{
int a, b, temp;
clrscr( );
cout<<"Enter the two numbers";
cin>>a>>b;
cout<<"Before Interchanging : a = " <<a<<" and b = "<<b<<endl;
temp = a;
a = b;
b = temp;
cout<<"After Interchanging : a = " <<a<<" and b = "<<b<<endl;
getch();
}
OUTPUT:
PROGRAM 1(b):
Write a C++ program to interchange the values of two variables without
using third variable.
#include<iostream.h>
#include<conio.h>
void main( )
{
int a, b;
clrscr( );
cout<<"Enter the two numbers:"<<endl;
cin>>a>>b;
cout<<"Before Interchanging : a = " <<a<<" and b = "<<b<<endl;
a = a + b;
b = a - b;
a = a - b;
cout<<"After Interchanging : a = " <<a<<" and b = "<<b<<endl;
getch();
}
OUTPUT:
3. I PUC Computer Science Lab Manual
Prof. K. Adisesha 3
PROGRAM 2:
Write a C++ program to find the area and circumference of a circle.
#include<iostream.h>
#include<conio.h>
void main( )
{
float rad, area, circum;
clrscr( );
cout<<"Enter the radius:";
cin>>rad;
area = 3.142 * rad * rad;
circum = 2 * 3.142 * rad;
cout<<"Area of circle = "<<area<<endl;
cout<<"Circumference of circle = "<<circum<<endl;
getch();
}
OUTPUT :
PROGRAM 3:
Write a C++ program to find the area of triangle given three sides.
#include<iostream.h>
#include<conio.h>
#include<math.h>
void main( )
{
float s1, s2, s3, s, area;
clrscr( );
cout<<"Enter the length of three sides:";
cin>>s1>>s2>>s3;
s = (s1 + s2+ s3)/2;
area = sqrt( s* (s-s1) * (s-s2) * (s-s3));
cout<<"Area of triangle = "<<area<<endl;
getch();
}
OUTPUT :
4. I PUC Computer Science Lab Manual
Prof. K. Adisesha 4
PROGRAM 4:
Write a C++ program to convert days into years, months and days.
(Hint: Assume all months have 30 days)
#include<iostream.h>
#include<conio.h>
void main( )
{
int totaldays, days, year, month;
clrscr( );
cout<<"Enter the total days:";
cin>>totaldays;
year = totaldays/365;
totaldays = totaldays % 365;
month = totaldays/30;
days = totaldays % 30;
cout<<"Years = "<< years<<endl;
cout<<"Months = "<<month<<endl;
cout<<"Days = "<<days<<endl;
getch();
}
OUTPUT :
PROGRAM 5:
Write a C++ program to find the largest, smallest and second largest of
three numbers using simple if statement.
#include<iostream.h>
#include<conio.h>
void main( )
{
int a, b, c;
int largest, smallest, seclargest;
clrscr( );
cout<<"Enter the three numbers"<<endl;
cin>>a>>b>>c;
largest = a; //Assume first number as largest
if(b>largest)
largest = b;
if(c>largest)
largest = c;
smallest = a; //Assume first number as smallest
if(b<smallest)
smallest = b;
if(c<smallest)
smallest = c;
seclargest = (a + b + c) - (largest + smallest);
5. I PUC Computer Science Lab Manual
Prof. K. Adisesha 5
cout<<"Smallest Number is = "<<smallest<<endl;
cout<<"Second Largest Number is = "<<seclargest<<endl;
cout<<"Largest Number is = "<< largest<<endl;
getch( );
}
OUTPUT:
PROGRAM 6:
Write a C++ program to input the total amount in a bill, if the amount is
greater than 1000, a discount of 8% is given. Otherwise, no discount is given.
Output the total amount, discount and the final amount. Use simple if statement.
#include<iostream.h>
#include<conio.h>
void main( )
{
float TAmount, discount, FAmount ;
clrscr( );
cout<<"Enter the Total Amount"<<endl;
cin>>TAmount;
discount = 0; //Calculate Discount
if(TAmount>1000)
discount = (8/100.0) * TAmount;
FAmount = TAmount - discount; //Calculate Final Amount
cout<<"Total Amount = "<<TAmount<<endl;
cout<<"Discount = "<<discount<<endl;
cout<<"Final Amount = "<< FAmount<<endl;
getch( );
}
OUTPUT :
6. I PUC Computer Science Lab Manual
Prof. K. Adisesha 6
PROGRAM 7:
Write a C++ program to check whether a given year is a leap year not,
Using if - else statement.
#include<iostream.h>
#include<conio.h>
void main( )
{
int year ;
clrscr( );
cout<<"Enter the Year in the form YYYY"<<endl;
cin>>year;
if(year%4 ==0 && year%100!=0 || year%400 ==0)
cout<<year<<" is a leap year"<<endl;
else
cout<<year<<" is not leap year"<<endl;
getch( );
}
OUTPUT :
PROGRAM 8:
Write a C++ program to accept a character. Determine whether the
character is a lower-case or upper-case letter.
#include<iostream.h>
#include<conio.h>
void main( )
{
char ch ;
clrscr( );
cout<<"Enter the Character"<<endl;
cin>>ch;
if(ch>= 'A' && ch <='Z')
cout<<ch<<" is an Upper-Case Character"<<endl;
else
if(ch>= 'a' && ch <='z')
cout<<ch<<" is an Lower-Case Character"<<endl;
else
cout<<ch<<" is not an alphabet"<<endl;
getch( );
}
OUTPUT:
7. I PUC Computer Science Lab Manual
Prof. K. Adisesha 7
PROGRAM 9:
Write a C++ program to input the number of units of electricity consumed in a
house and calculate the final amount using nested-if statement.
Use the following data for calculation.
#include<iostream.h>
#include<conio.h>
void main( )
{
int units ;
float Billamount;
clrscr( );
cout<<"Enter the number of units consumed:"<<endl;
cin>>units;
if(units < 30)
Billamount = units * 3.50 ;
else
if(units < 50)
Billamount = 29 * 3.50 + (units - 29) * 4.25 ;
else
if(units < 100)
Billamount = 29 * 3.50 + 19 * 4.25 + (units - 49) * 5.25 ;
else
Billamount = 29 * 3.50 + 19 * 4.25 + 49 * 5.25 + (units - 99) * 5.85 ;
cout<<"Total Units Consumed ="<<units<<endl;
cout<<"Total Amount = "<<Billamount<<endl;
getch( );
}
OUTPUT:
8. I PUC Computer Science Lab Manual
Prof. K. Adisesha 8
PROGRAM 10:
Write a C++ program to input the marks of four subjects. Calculate the total
percentage and output the result as either “First Class” or “Second Class” or
“Pass Class” or “Fail” using switch statement.
Class Range (%)
First Class Between 60% to 100%
Second Class Between 50% to 59%
Pass Class Between 40% to 49%
Fail Less than 40%
#include<iostream.h>
#include<conio.h>
void main( )
{
int m1, m2, m3, m4, total, choice;
float per;
clrscr( );
cout<<"Enter the First subject marks:";
cin>>m1;
cout<<"Enter the Second subject marks:";
cin>>m2;
cout<<"Enter the Third subject marks:";
cin>>m3;
cout<<"Enter the Fourth subject marks:";
cin>>m4;
total = m1 + m2 + m3 + m4;
per = total / 4.0;
cout<<"Total Marks = "<<total<<endl;
cout<<"Percentage = "<<per<<endl;
choice = (int) per/10;
cout<<"The result of the student is: "<<endl;
switch(choice)
{
case 10:
case 9:
case 8:
case 7:
case 6: cout<<"First Class "<<endl;
break;
case 5: cout<<"Second Class"<<endl;
break;
case 4: cout<<"Pass Class"<<endl;
break;
default: cout<<"Fail"<<endl;
}
getch( );
}
OUTPUT 1:
9. I PUC Computer Science Lab Manual
Prof. K. Adisesha 9
PROGRAM 11:
Write a C++ program to find sum of all the digits of a number using while
statement.
#include<iostream.h>
#include<conio.h>
void main( )
{
int num, sum, rem;
clrscr( );
cout<<"Enter the Number"<<endl;
cin>>num;
sum = 0;
while(num!=0)
{
rem = num % 10;
sum = sum + rem;
num = num/10;
}
cout<<"Sum of the digits is "<<sum<<endl;
getch( );
}
PROGRAM 12:
Write a C++ program to input principal amount, rate of interest and time
period. Calculate compound interest using while statement.
(Hint: Amount = P * (1 + R/100)T, Compound Interest = Amount – P)
#include<iostream.h>
#include<conio.h>
void main( )
{
float pri, amt, priamt, rate, ci;
int time, year;
clrscr( );
cout<<"Enter the Principal amount, rate of interest and time:"<<endl;
cin>>pri>>rate>>time;
year = 1;
priamt = pri;
while(year <= time)
{
amt = pri * (1 + rate/100);
pri=amt;
year ++;
}
ci = amt - priamt;
cout<<"Compound Interest = "<<ci<<endl;
cout<<"Net Amount = "<<amt<<endl;
getch( );
}
10. I PUC Computer Science Lab Manual
Prof. K. Adisesha 10
PROGRAM 13:
Write a C++ program to check whether the given number is power of 2.
#include<iostream.h>
#include<conio.h>
void main( )
{ int num, m, flag;
clrscr( );
cout<<"Enter the Number"<<endl;
cin>>num;
m = num;
flag = 1;
while(num>2)
if(num % 2 ==1)
{
flag = 0;
break;
}
else
num = num/2;
if(flag)
cout<<m<<" is power of 2 "<<endl;
else
cout<<m<<" is not power of 2 "<<endl;
getch( );
}
PROGRAM 14:
Write a C++ program to find the factorial of a number using for statement.
(Hint: 5! = 5 * 4 * 3 * 2 * 1 = 120)
#include<iostream.h>
#include<conio.h>
void main( )
{
int num, fact, i;
clrscr( );
cout<<"Enter the number"<<endl;
cin>>num;
fact = 1;
for( i = 1; i<= num; i++)
fact = fact * i;
cout<<" The factorial of a "<<num<<"! is: "<<fact<<endl;
getch( );
}
OUTPUT :
11. I PUC Computer Science Lab Manual
Prof. K. Adisesha 11
PROGRAM 15:
Write a C++ program to generate the Fibonacci sequence up to a limit using for
statement. (Hint: 5 = 0 1 1 2 3)
#include<iostream.h>
#include<conio.h>
void main( )
{
int num, first, second, count, third;
clrscr( );
cout<<"Enter the number limit for Fibonacci Series"<<endl;
cin>>num;
first = 0;
second = 1;
cout<<first<<"t"<<second;
third = first + second;
for( count = 2; third<=num; count++)
{
cout<<"t"<<third
first = second;
second = third;
third = first + second;
}
cout<<"n Total terms = "<<count<<endl;
getch( );
}
OUTPUT :
PROGRAM 16:
Write a C++ program to find the sum and average of n number of the array.
#include<iostream.h>
#include<conio.h>
#include<iomanip.h>
void main( )
{
int a[50], i, n, sum;
float avg;
clrscr();
cout<<"How many elements?"<<endl;
cin>>n;
cout<<"Enter the elements:"<<endl;
for(i=0; i<n; i++)
cin>>a[i];
sum = 0;
for(i=0; i<n; i++)
sum = sum + a[i];
avg=(float) sum / n;
cout<<"Sum = "<<sum<<endl;
12. I PUC Computer Science Lab Manual
Prof. K. Adisesha 12
cout<<"Average = "<<avg<<endl;
getch( );
}
OUTPUT:
PROGRAM 17:
Write a C++ program to find the second largest of n number in the array.
#include<iostream.h>
#include<conio.h>
#include<iomanip.h>
void main( )
{
int a[50], i, n, largest, secondlar;
clrscr( );
cout<<"How many elements?"<<endl;
cin>>n;
cout<<"Enter the elements:"<<endl;
for(i=0; i<n; i++)
cin>>a[i];
if(a[0] > a[1])
{
largest = a[0];
secondlar = a[1];
}
else
{
largest=a[1];
secondlar=a[0];
}
for(i=2; i<n; i++)
if(a[i] > largest)
{
secondlar = largest;
largest = a[i];
}
else
if(a[i] > secondlar)
secondlar = a[i];
cout<<"Largest = "<<largest<<endl;
cout<<"Second Largest = "<<secondlar<<endl;
getch( );
}
OUTPUT:
13. I PUC Computer Science Lab Manual
Prof. K. Adisesha 13
PROGRAM 18:
Write a C++ program to arrange a list of numbers in ascending order.
Sorting: The process of arrangement of data items in ascending or descending order is called
sorting.
#include<iostream.h>
#include<conio.h>
#include<iomanip.h>
void main( )
{
int a[50], i, temp, j, n;
clrscr( );
cout<<"Enter the number of elements:"<<endl;
cin>>n;
cout<<"Enter the elements:"<<endl;
for(i=0; i<n; i++)
cin>>a[i];
for(i=1; i<n; i++)
{
for(j=0; j<n-i; j++)
if(a[j] > a[j+1])
{
temp = a[j];
a[j] = a[j+1];
a[j+1] = temp;
}
}
cout<<"The sorted elements are:"<<endl;
for(i=0; i<n; i++)
cout<<a[i]<<"t";
getch( );
}
PROGRAM 19:
Write a C++ program to determine whether the string is a palindrome.
#include<iostream.h>
#include<conio.h>
#include<string.h>
void main( )
{
char s[100], r[100]; //s is the string and r is the reserved string
clrscr( );
cout<<"Enter the String:";
cin.getline(s, 100);
strcpy (r, s); // Copy the characters of the string s to r
strrev (r); // Reverse the characters of the string r
if(strcmpi(s, r) == 0)
cout<<"It is palindrome"<<endl;
else
cout<<"It is not palindrome"<<endl;
getch( );
}
14. I PUC Computer Science Lab Manual
Prof. K. Adisesha 14
PROGRAM 20:
Write a C++ program to find position of a given number in the array.
Searching: The process of finding the position of a data item in the given collection of data items is
called searching.
#include<iostream.h>
#include<conio.h>
#include<iomanip.h>
void main( )
{
int a[50], i, pos, ele, n;
clrscr( );
cout<<"Enter the number of elements:"<<endl;
cin>>n;
cout<<"Enter the elements:"<<endl;
for(i=0; i<n; i++)
cin>>a[i];
cout<<"Enter the search element:"<<endl;
cin>>ele;
pos=-1;
for(i=0; i<n; i++)
if(ele == a[i])
{
pos = i;
break;
}
if(pos >= 0)
cout<<ele<<" is present at position "<<pos<<endl;
else
cout<<ele<<" is not present"<<endl;
getch( );
}
PROGRAM 21:
Write a C++ program to sum of all the row and sum of all the column in
matrices separately.
#include<iostream.h>
#include<conio.h>
void main( )
{
int a[5][5], r, c, i, j, rsum, csum;
clrscr( );
cout << "Enter the order of matrix: ";
cin >> r>>c;
// Storing elements of matrix entered by user.
cout << "Enter elements of the matrix: " << endl;
for (i = 0; i < r; i++)
for (j = 0; j < c; j++)
cin >> a[i][j];
for(i = 0; i < r; i++)
{
15. I PUC Computer Science Lab Manual
Prof. K. Adisesha 15
rsum = 0;
for(j = 0; j < c; j++)
rsum = rsum + a[i][j];
cout<<"Sum of row no "<<i+1<<" = "<<rsum<<endl;
}
for(i = 0; i < c; i++)
{
csum = 0;
for(j = 0; j < r; j++)
csum = csum + a[j][i];
cout<<"Sum of column no "<<i+1<<" = "<<csum<<endl;
}
getch( );
}
PROGRAM 22:
Write a C++ program to find the sum of two compatible matrices:
#include<iostream.h>
#include<conio.h>
void main( )
{
int a[5][5], b[5][5], sum[5][5], r1, c1, r2, c2, i, j;
clrscr( );
cout << "Enter the order of first matrix: ";
cin >> r1>>c1;
cout << "Enter the order of second matrix: ";
cin >> r2>>c2;
cout << "Enter elements of 1st matrix: " << endl;
for (i = 0; i < r1; i++)
for (j = 0; j < c1; j++)
cin >> a[i][j];
cout << "Enter elements of 2nd matrix: " << endl;
for(i = 0; i < r2; i++)
for(j = 0; j < c2; j++)
cin >> b[i][j];
if ( (r1 == r2) && (c1==c2))
{
for(i = 0; i < r1; i++)
for(j = 0; j < c1; j++)
sum[i][j] = a[i][j] + b[i][j]; // Adding Two matrices
cout << "Sum of two matrix is: " << endl;
for(i = 0; i < r1; i++)
{
for(j = 0; j < c1; j++)
cout << sum[i][j] << "t"; // Displaying the resultant sum matrix.
cout << endl;
} }
else
cout<<"Matrices are not compatible..."<<endl;
getch( );
}
16. I PUC Computer Science Lab Manual
Prof. K. Adisesha 16
PROGRAM 23:
Write a C++ program to count the number of vowels and consonants in a
string.
#include<iostream.h>
#include<conio.h>
#include<string.h>
#include<ctype.h>
void main( )
{
char s[100];
int len, i, cons=0, vow=0;
clrscr( );
cout<<"Enter the string:";
cin.getline(s,100);
len=strlen(s);
for(i=0;i<len;i++)
if( isalpha (s[i]) )
switch (toupper (s[i]) )
{
case'A':
case'E':
case'I':
case'O':
case'U': vow++;
break;
default: cons++;
}
cout<<"Number of Vowles:"<<vow<<endl;
cout<<"Number of Consonants:"<<cons<<endl;
getch();
}
PROGRAM 24:
Write a C++ program to find the GCD and LCM of two numbers using
functions.
#include<iostream.h>
#include<conio.h>
void main( )
{
int gcd(int, int); //Function Prototype
int a, b, g, lcm;
clrscr( );
cout<<"Enter two number:"<<endl;
cin>>a>>b;
g = gcd(a,b);
lcm = (a * b)/g;
cout<<"GCD of "<<a<<" and "<<b<<" is "<<g<<endl;
cout<<"LCM of "<<a<<" and "<<b<<" is "<<lcm<<endl;
getch( );
}
17. I PUC Computer Science Lab Manual
Prof. K. Adisesha 17
int gcd( int x, int y)
{
int rem;
while(y != 0)
{
rem = x % y;
x = y;
y = rem;
}
return(x);
}
PROGRAM 25:
Write a C++ program to input the register number, name and class of all the
students in a class into a structure and output the data in a tabular manner with
proper heading.
#include<iostream.h>
#include<conio.h>
struct student
{
int regno;
char name[15];
char section[4];
};
void main( )
{
student s[50];
int i, j, n;
clrscr( );
cout<<"How many students?"<<endl;
cin>>n;
for(i=0; i<n; i++)
{
cout<<"Enter the Reg No of the Student "<<i+1<<":";
cin>>s[i].regno;
cout<<"Enter the Name of the Student "<<i+1<<":";
cin>>s[i].name;
cout<<"Enter the Class of the Student "<<i+1<<":";
cin>>s[i].section;
}
cout<<"REG_NO t NAME t CLASS t"<<endl;
for(i=0; i<n; i++)
cout<<s[i].regno<<"t"<<s[i].name<<"t"<< s[i].section<<endl;
getch( );
}
OUTPUT:
18. I PUC Computer Science Lab Manual
Prof. K. Adisesha 18
Section B
(SPREADSHEET & HTML)