0% found this document useful (0 votes)
5 views

lab manual c++

The document contains a lab manual for the CA-113 Programming in C++ course, detailing various practical exercises. Each practical includes code examples for arithmetic operations, conditional statements, loops, arrays, structures, and unions. The document is compiled by Mr. Vinod S. Mahajan and serves as a guide for students to practice programming concepts.

Uploaded by

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

lab manual c++

The document contains a lab manual for the CA-113 Programming in C++ course, detailing various practical exercises. Each practical includes code examples for arithmetic operations, conditional statements, loops, arrays, structures, and unions. The document is compiled by Mr. Vinod S. Mahajan and serves as a guide for students to practice programming concepts.

Uploaded by

pranavgirase32
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 24

RCPET’s Institute of Management Research and Development Shirpur

Class: FYBCA
Subject: CA-113 Lab on Programming in C++
Lab Manual
//Practical 1 : Program using various arithmetic operators
#include<iostream.h>
#include<conio.h>
void main()
{
int a,b,sum,sub,mul,div,rem;
clrscr();
cout<<"\nEnter any two numbers=";
cin>>a>>b;
sum=a+b;
sub=a-b;
mul=a*b;
div=a/b;
rem=a%b;
cout<<"\nAddition="<<sum;
cout<<"\nSubstraction="<<sub;
cout<<"\nMultiplication="<<mul;
cout<<"\nDivision="<<div;
cout<<"\nReminder="<<rem;
getch();
}

Compiled By: Mr. Vinod S. Mahajan


//Practical 2-a Program to find out maximum between two (if statement)
#include<iostream.h>
#include<conio.h>
void main()
{
int a,b;
clrscr();
cout<<"\nEnter any two numbers=";
cin>>a>>b;
if(a>b)
cout<<"\nMaximum="<<a;
if(b>a)
cout<<"\nMaximum="<<b;
if(a==b)
cout<<"\nBoth are same";
getch();
}

Compiled By: Mr. Vinod S. Mahajan


//Practical 2b- Program for ODD or EVEN (if else)
#include<iostream.h>
#include<conio.h>
void main()
{
int a;
clrscr();
cout<<"\nEnter any number=";
cin>>a;
if(a%2==0)
cout<<"\nNumber is EVEN";
else
cout<<"\nNumber is ODD";
getch();
}

Compiled By: Mr. Vinod S. Mahajan


//Practical 2-c Print grade as per the percentage (if else if)
#include<iostream.h>
#include<conio.h>
void main()
{
double per;
clrscr();
cout<<"\nEnter your percentage=";
cin>>per;
if(per>=80 && per<=100)
cout<<"\nA Grade";
else if(per>=60 && per<80)
cout<<"\nB Grade";
else if(per>=50 && per<60)
cout<<"\nC Grade";
else if(per>=40 && per<50)
cout<<"\nD Grade";
else if(per>=0 && per<40)
cout<<"\n Fail";
else
cout<<"\nInvalid Percentage";
getch();
}

Compiled By: Mr. Vinod S. Mahajan


//Practical 2d-Program for vowels (switch case)
#include<iostream.h>
#include<conio.h>
void main()
{
char ch;
clrscr();
cout<<"\nEnter any character=";
cin>>ch;
switch(ch)
{
case 'a':
case 'A':
case 'i':
case 'I':
case 'e':
case 'E':
case 'o':
case 'O':
case 'u':
case 'U':cout<<"\nCharacter is vowel";break;
default:cout<<"\nCharacter is consonent";
}
getch();
}

Compiled By: Mr. Vinod S. Mahajan


//Practical 3A Program to print "IMRD" no of times (for loop)
#include<iostream.h>
#include<conio.h>
void main()
{
int i,n;
cout<<"\nHow many times=";
cin>>n;
for(i=1;i<=n;i++)
cout<<"\nIMRD";
getch();
}

Compiled By: Mr. Vinod S. Mahajan


//Practical 3b- Program for palindrome no (while loop)
#include<iostream.h>
#include<conio.h>
void main()
{
int n,temp,r,rev=0;
clrscr();
cout<<"\nEnter any number=";
cin>>n;
temp=n;
while(n!=0)
{
r=n%10;
rev=rev*10+r;
n=n/10;
}
if(rev==temp)
cout<<"\nNumber is palindrome";
else
cout<<"\nNumber is not palindrome";
getch();
}

Compiled By: Mr. Vinod S. Mahajan


//Practical 3c- program to perform arithmetic operation on user choice(do-while)
#include<iostream.h>
#include<conio.h>
void main()
{
int a,b,choice,result;
clrscr();
do
{
cout<<"\n\n\tMENU";
cout<<"\n 1 : Addition";
cout<<"\n 2 : Subtraction";
cout<<"\n 3 : Multiplication";
cout<<"\n 4 : Division";
cout<<"\n 5 : Exit";
cout<<"\n\nEnter your choice=";
cin>>choice;
if(choice>=1 && choice<=4)
{
cout<<"\nEnter any two numbers=";
cin>>a>>b;
}
switch(choice)
{
case 1:result=a+b;
cout<<"\nAddition="<<result;break;
case 2:result=a-b;
cout<<"\nSubtraction="<<result;break;
case 3:result=a*b;
cout<<"\nMultiplication="<<result;break;
case 4:result=a/b;
cout<<"\nDivision="<<result;break;
case 5:break;
default:cout<<"\nInvalid choice try again";
}
}while(choice!=5);
getch();
}

Compiled By: Mr. Vinod S. Mahajan


//practical 3d program to print tables between given range (nested loop)
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int start,end;
cout<<"\nEnter start value=";
cin>>start;
cout<<"\nEnter end value";
cin>>end;
if(start<=end)
{
for(int i=1;i<=10;i++)
{
for(int j=start;j<=end;j++)
cout<<i*j<<"\t";
cout<<"\n";
}
}else
cout<<"\n Start value must be smaller than end value:";
getch();
}

Compiled By: Mr. Vinod S. Mahajan


//Practical 4 : Program to check given number is prime or not
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int n,i,count=0;
cout<<"\nEnter any number=";
cin>>n;
for(i=1;i<=n;i++)
{
if(n%i==0)
count++;
}
if(count==2)
cout<<"\nNumber is prime";
else
cout<<"\nNumber is not prime";
getch();
}

Compiled By: Mr. Vinod S. Mahajan


//practical 5 Program for factorial
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int i,n,f=1;
cout<<"\nEnter any number";
cin>>n;
for(i=1;i<=n;i++)
f=f*i;
cout<<"\nFactorial of "<<n<<" = "<<f;
getch();
}

Compiled By: Mr. Vinod S. Mahajan


//practical 6 Program to print Fibonacci series up to given terms
#include<iostream.h>
#include<conio.h>
void main()
{
int a=0,b=1,c,n;
clrscr();
cout<<"\nHow many terms=";
cin>>n;
cout<<"\nFibonacci Series as follows=";
cout<<"\n"<<a<<"\t"<<b;
for(int i=1;i<=n-2;i++)
{
c=a+b;
cout<<"\t"<<c;
a=b;
b=c;
}
getch();
}

Compiled By: Mr. Vinod S. Mahajan


/*Practical 7A Program to read 5 elements in an array and print additon and
average of them.*/
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int a[5],sum=0;
double avg;
cout<<"\nEnter 5 elements in an aray=";
for(int i=0;i<5;i++)
{
cin>>a[i];
sum=sum+a[i];
}
avg=sum/5.0;
cout<<"\nAddition="<<sum;
cout<<"\nAverage="<<avg;
getch();
}

Compiled By: Mr. Vinod S. Mahajan


//Practical 7B Program to print matrix addition
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int a[3][3], b[3][3], add[3][3],r,c;
cout<<"\nEnter 9 numbers for first matrix=";
for(r=0;r<3;r++)
{
for(c=0;c<3;c++)
cin>>a[r][c];
}
cout<<"\nEnter 9 numbers for second matrix=";
for(r=0;r<3;r++)
{
for(c=0;c<3;c++)
cin>>b[r][c];
}
for(r=0;r<3;r++)
{
for(c=0;c<3;c++)
add[r][c]=a[r][c]+b[r][c];
}
cout<<"\nMatrix additoin as follows\n";
for(r=0;r<3;r++)
{
for(c=0;c<3;c++)
cout<<add[r][c]<<"\t";
cout<<"\n";
}
getch();
}

Compiled By: Mr. Vinod S. Mahajan


//Practical 8A - Program for swapping using call by value
#include<conio.h>
void swap(int,int);
void main()
{
clrscr();
int a=10,b=20;
cout<<"\nBefore swapping=";
cout<<"\na="<<a<<"\nb="<<b;
swap(a,b);
cout<<"\nAfter swapping in swap function=";
cout<<"\na="<<a<<"\nb="<<b;
getch();
}
void swap(int x, int y)
{
int t=x;
x=y;
y=t;
cout<<"\nAfter swapping=";
cout<<"\nx="<<x<<"\ny="<<y;
}

Compiled By: Mr. Vinod S. Mahajan


//Practical 8B - Program for swapping using call by reference
#include<iostream.h>
#include<conio.h>
void swap(int&,int&);
void main()
{
clrscr();
int a=10,b=20;
cout<<"\nBefore swapping=";
cout<<"\na="<<a<<"\nb="<<b;
swap(a,b);
cout<<"\nAfter swapping in swap function=";
cout<<"\na="<<a<<"\nb="<<b;
getch();
}
void swap(int &x, int &y)
{
int t=x;
x=y;
y=t;
cout<<"\nAfter swapping=";
cout<<"\nx="<<x<<"\ny="<<y;
}

Compiled By: Mr. Vinod S. Mahajan


//Practical 8C Program for Factorial using recursion
#include<iostream.h>
#include<conio.h>
int fact(int);
void main()
{
clrscr();
int n;
cout<<"\nEnter any number=";
cin>>n;
int f=fact(n);
cout<<"\nFactorial of "<<n<<"="<<f;
getch();
}
int fact(int n)
{
if(n==1)
return(n);
else
return n*fact(n-1);
}

Compiled By: Mr. Vinod S. Mahajan


//Practical 9 - Program for standard string functions.
#include<iostream.h>
#include<conio.h>
#include<string.h>
void main()
{
clrscr();
char str1[]="rcp";
char str2[]="imrd";
char str3[30];
cout<<"\nstr1="<<str1;
cout<<"\nstr2="<<str2;
//strlen
int len=strlen(str2);
cout<<"\nLength of str2="<<len;
//strcpy
strcpy(str3,str2);
cout<<"\nStr 2 copied into str3="<<str3;
//strcmp
int d=strcmp(str1,str2);
if(d==0)
cout<<"\nBoth are same";
else
cout<<"\nBoth are different";
//strcat
strcat(str2,str1);
cout<<"\nJoined string="<<str2;
//strrev
strrev(str2);
cout<<"\nreverse string ="<<str2;
getch();
}

Compiled By: Mr. Vinod S. Mahajan


//Practical 10 A – Use of structure
#include <conio.h>
#include<iostream.h>
struct employee
{
int id;
char name[50];
float salary;
}e1,e2; //declaring e1 and e2 variables for structure
void main( )
{
clrscr();
e1.id=101;
e1.name=”Harshal”;
e1.salary=56000;

e2.id=102;
e2.name=”Namrata”;
e2.salary=126000;

cout<<"employee 1 id : “<<e1.id<<endl;
cout<<"employee 1 name : “<<e1.name<<endl;
cout<<"employee 1 salary : “<<e1.salary<<endl;

cout<<"employee 2 id : “<<e2.id<<endl;
cout<<"employee 2 name : “<<e2.name<<endl;
cout<<"employee 2 salary : “<<e2.salary<<endl; return 0;
}

Compiled By: Mr. Vinod S. Mahajan


//Practical 10 B use of union
#include <iostream.h>
#include<conio.h>
union employee
{
int id;
char name[50];
}e1,e2; //declaring e1 variable for union
void main( )
{
clrscr();

e1.id=101;
cout<<"employee 1 id :"<<e1.id<<endl;
e1.name=”harshal”;
cout<<"employee 1 name :"<<e1.name<<endl;

e2.id=102;
cout<<"employee 2 id :"<<e2.id<<endl;
e2.name=”Namrata”;
cout<<"employee 2 name :"<<e2.name<<endl;
getch();
}

Compiled By: Mr. Vinod S. Mahajan


Logical Experiment

11: The annual examination results of 10 students are tabulated as follows:

Roll no Sub1 Subj2 Subj3


2022001 40 87 74
2022002 50 56 62
2022003 65 97 64
2022004 78 45 75
2022005 98 32 85
2022006 45 65 56
2022007 63 45 77
2022008 25 87 64
2022009 72 23 68
2022010 63 69 78

Write a program to read the data and determine the following.


(a) Total marks obtained by each student.
(b) The highest marks in each subject and the roll no of the student who secured it.
(c) The student who obtained the highest total marks.

#include <iostream.h>
#include <conio.h>
void main()
{
clrscr();
// Define the number of students
const int NUM_STUDENTS = 10;
// Define arrays to hold the marks for 3 subjects and other necessary information
long roll_no[NUM_STUDENTS]={2022001, 2022002, 2022003, 2022004, 2022005,2022006,
2022007, 2022008, 2022009, 2022010};
int marks_sub1[NUM_STUDENTS]={40,50,65,78,98,45,63,25,72,63};
int marks_sub2[NUM_STUDENTS]={87,56,97,45,32,65,45,87,23,69};
int marks_sub3[NUM_STUDENTS]={74,62,64,75,85,56,77,64,68,78};
int total_marks[NUM_STUDENTS];

// calculate the total marks for each student


for (int i=0;i<NUM_STUDENTS;i++)
{
total_marks[i] = marks_sub1[i] + marks_sub2[i] + marks_sub3[i];
}
// Initialize variables to track highest marks and corresponding student roll numbers
int highest_sub1 = marks_sub1[0];
int highest_sub2 = marks_sub2[0];
int highest_sub3 = marks_sub3[0];

long highest_sub1_roll = roll_no[0];


long highest_sub2_roll = roll_no[0];
long highest_sub3_roll = roll_no[0];

int highest_total = total_marks[0];


long highest_total_roll = roll_no[0];

Compiled By: Mr. Vinod S. Mahajan


// Find the highest marks in each subject and the student who obtained them
for(i=1;i<NUM_STUDENTS;i++)
{
if(marks_sub1[i] > highest_sub1)
{
highest_sub1 = marks_sub1[i];
highest_sub1_roll = roll_no[i];
}
if(marks_sub2[i] > highest_sub2)
{
highest_sub2 = marks_sub2[i];
highest_sub2_roll = roll_no[i];
}
if(marks_sub3[i] > highest_sub3)
{
highest_sub3 = marks_sub3[i];
highest_sub3_roll = roll_no[i];
}
if(total_marks[i] > highest_total)
{
highest_total = total_marks[i];
highest_total_roll = roll_no[i];
}
}

// Output the results


cout << "\nTotal marks obtained by each student:" << endl;
for(i=0;i<NUM_STUDENTS;i++)
{
cout << "Roll No: " << roll_no[i] << " - Total Marks: " << total_marks[i] << endl;
}

cout<< "\nHighest marks in each subject:" << endl;


cout<< "Subject 1 - Highest Marks: "<< highest_sub1 << " by Roll No: "<< highest_sub1_roll << endl;
cout<< "Subject 2 - Highest Marks: " << highest_sub2 << " by Roll No: " << highest_sub2_roll << endl;
cout<< "Subject 3 - Highest Marks: " << highest_sub3 << " by Roll No: " << highest_sub3_roll << endl;

cout<< "\nStudent with highest total marks:" << endl;


cout<< "Roll No: " << highest_total_roll << " - Total Marks: " << highest_total << endl;

getch();
}

Compiled By: Mr. Vinod S. Mahajan


/*Practical 12:
Write a function for finding the average age of a class student. Pass an array of
student as a parameter to that function. Assume the default class strength to be 50.
Provide default strength as a default argument to the function. Display average age
of a class student using a program. */

#include<iostream.h>
#include<conio.h>
// Function to calculate average age
double calculateAverageAge(int ages[], int classStrength = 50)
{
int totalAge = 0;
// Calculate the total age by summing up all the ages
for(int i=0;i<classStrength;i++)
{
totalAge += ages[i];
}
// Calculate and return the average age
return (double)totalAge / classStrength;
}
void main()
{
clrscr();
// Sample array of student ages (class strength 50 by default)
int studentAges[50] = {16, 17, 18, 19, 17, 16, 18, 19, 17, 16,
18, 17, 16, 19, 18, 17, 16, 18, 19, 17,
16, 18, 17, 19, 18, 16, 17, 16, 18, 19,
17, 16, 18, 19, 17, 16, 18, 19, 16, 17,
18, 17, 16, 19, 18, 16, 17, 18, 19, 17};
// Call the function to calculate the average age
double averageAge = calculateAverageAge(studentAges);
// Display the average age
cout<< "The average age of the class is: " << averageAge << endl;
getch();
}

Compiled By: Mr. Vinod S. Mahajan


/*Practical 13: Write a program for below given algorithm which accept integer
number N from the user and display following series up to N number.
Test Data:
Input Number is 7 then display
Expected output:
1 4 9 16 25 36 49*/

#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int N;
// Input the number N
cout<<"Enter the number N: ";
cin>>N;
// Display the series of squares up to N
for(int i=1;i<=N;i++)
{
cout<< i * i << " "; // Print the square of the number
}
getch();
}

*****

Compiled By: Mr. Vinod S. Mahajan

You might also like