0% found this document useful (0 votes)
24 views68 pages

C++ Programming Exercises Collection

The document contains a list of 27 programming problems with their page numbers and a teacher signature section. It includes problems related to grade calculation, mathematical operations, prime number checking, pattern printing, string operations like palindrome checking and reversing, and matrix operations. The problems cover basic to intermediate level concepts in programming.

Uploaded by

g
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)
24 views68 pages

C++ Programming Exercises Collection

The document contains a list of 27 programming problems with their page numbers and a teacher signature section. It includes problems related to grade calculation, mathematical operations, prime number checking, pattern printing, string operations like palindrome checking and reversing, and matrix operations. The problems cover basic to intermediate level concepts in programming.

Uploaded by

g
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

1

Index
[Link] Program Name Page number(s) Teacher sign
1 Grade Program 2-3
2 Square for odd and cube for even 4-5
3 Arthematic operations using switch 6-7
4 Largest of three numbers 8-9
5 Fibonacci Series 10-11
6 Palindrome or not(integer) 12-13
7 Prime number or not 14-15
8 Multiplication Table 16-17
9 Star(*) pattern program 18-19
10 ‘’&’’ pattern program 20-21
11 Palindrome or not (String) 22-23
12 Reversing a sentence word by word 24-25
13 Program to capitalize the first letter of each word 26-27
14 Program to count number of alphabets in string 28-29
15 To count number of lower case characters in a string
16 Comparing two strings 30-31
17 Change case (lower to upper , upper to lower) 32-33
18 Sum of elements in an array 34-35
19 Linear Search 36-37
20 Binary Search 38-39
21 Sum of two matrices 40-43
22 Matrix multiplication 44-47
23 Transpose of a Matrix 48-49
24 Trace of a Matrix 50-51
25 To print the lower half elements in a matrix 52-53
26 the sum of elements in columns and rows in a matrix 54-55
27 To Read five strings and reverse them 56-57
28
29
30
31
32
33
34
35
36
37
38
39
40
2

Output:
3

Program no:1
Aim: to find the grade of a student by entering a mark.
1.//student grade program
#include<iostream>
#include<conio.h>
using namespace std;
int main()
{
int a;
char i;
do{
cout<<endl;
cout<<"enter your marks to evaluate your grade"<<endl;
cin>>a;
if(a>=90)
{
cout<<"your grade is A ";
}
else if(a>=80)
{
cout<<"your grade is B";
}
else if(a>=70)
{
cout<<"your grade is C";
}else if(a>=60)
{cout<<"your grade is D";
}
else
{cout<<"Your grade is E";
}
cout<<endl;
cout<<"do you want to repeat(y/n)";
cin>>i;
}while(i=='y');
cout<<endl;
return 0;
}
4

Output:
5

Program no:2
Aim: To find the square if the number is odd and cube if the number is even.
Source code
2.//square cube
#include<iostream>
using namespace std;
int main()
{

int x;
char q;
do{

cout<<"Enter the number : ";


cin>>x;
if(x%2==0)
{
cout<<"The number "<<x<<" is even and its cube is : "<<x*x<<endl;
}
else
{
cout<<"The number "<<x<<" is odd and its square is : "<<x*x*x<<endl;
}
cout<<"do you want to repeat(y/n)";
cin>>q;
cout<<endl;
}while(q=='y'||q=='Y');
return 0;
}
6
7

Program no:3
Aim: to perform arthematic operations on numbers using switch statement.
Source code
//arthematic operations
#include<iostream>

using namespace std;


int main()
{
int a,b;
char n;
cout<<"enter two numbers"<<endl;
cin>>a>>b;
cout<<"enter the operator to work on those two numbers(+,-,*,/)"<<endl;
cin>>n;
switch(n)
{
case '+': cout<<a+b;
break;
case '-': cout<<a-b;
break;
case '*':cout<<a*b;
break;
case '/':cout<<a/b;break;
default :cout<<"make sure your senses are working properly"<<endl;
}
//getch();
return 0;
}
8
9

Program no:4
Aim: to find the biggest of three
Source code
//biggest of three
#include<iostream>
using namespace std;
int main()
{
int a,b,c;

cout<<"enter any three numbers to check and say which one is big \n";
cin>>a>>b>>c;
if((a>b&&a>c))
{
cout<<"the biggest is "<<a;
}else if((b>c&&b>a))
{ cout<<"the biggest is "<<b;
}else
{
cout<<"the biggest is "<<c;
}

return 0;
}
10
11

Program no: 5
Aim: to print Fibonacci series up to a given number.
Source code
//Fibonacci series
#include<iostream>
#include<iomanip>
using namespace std;
int main()
{
int n2,n1,n,n3;

cout<<"enter n value";
cout<<endl;
cin>>n;
while(n3<n)
{
cout<<n3;
cout<<setw(3);
n3=n1+n2;
n1=n2;
n2=n3;
}

return 0;
}
12
13

Program no: 6
Aim: To find whether a number is palindrome or not
Source code
6. //palindrome or not
#include<iostream>
using namespace std;
int main()
{
int n,r=0,d,x;

cout<<"enter n value";
cout<<endl;
cin>>n;
cout<<endl;
x=n;
while(n>0)
{
d=n%10;
r=r*10+d;
n=n/10;
}

if(x==r)
{
cout<<"it is a palindrome";
}
else
{ cout<<"not a palindrome";
}

return 0;
}
14
15

Program no: 7
Aim: To check whether a number is prime number or not.
Source code
7. //prime number program
#include<iostream>
using namespace std;
int main()
{
int i,n;

char x;
do{
cout<<"enter a number to check whether a number is prime or not"<<endl;
cin>>n;
if(n==1)
cout<<"it is not a prime number"<<endl;
int code=0;
for(i=2;i<=n/2;i++)
{
if(n%i==0)
code=1;
break;
}
if(code==1)
{
cout<<"it is not a prime number"<<endl;
}
else
{
cout<<"it is a prime number"<<endl;
}
cout<<endl;
cout<<"do you want to check again(y/n)"<<endl;
cin>>x;
}while(x=='y'||x=='Y');

return 0;
}
16
17

Program no: 8
Aim: To print multiplication table up to a certain limit given by the user.
Source code
multiplication table
#include<iostream>
using namespace std;
int main()
{
int i,j,n;
cout<<"enter a limit for printing multiplication table";
cout<<endl;
cin>>n;
for(i=1;i<=n;i++)
{
for(j=1;j<=10;j++)
{
cout<<i<<"*"<<j<<"="<<i*j<<endl;
}
cout<<endl;
}

return 0;
}
18
19

Program no: 9
Aim: to print star pattern
Source code
//*pattern program
#include<iostream>
using namespace std;
int main()
{
int i,j,n;

cout<<"enter n value"<<endl;
cin>>n;
for(i=0;i<=n;i++)
{
for(j=0;j<i;j++)
{
cout<<"*";
}
cout<<endl;
}

return 0;
}
20
21

Program no: 10
Aim: To print ‘&’ pattern.
Source code
//& pattern
#include<iostream>
#include<iomanip>
using namespace std;
int main()
{
int i,j,n,k;
for(i=1,j=0,k=10;i<=6;i++,j=j+4,k=k-2)
{
if(i==1)
cout<<setw(k)<<"&"<<endl;
else if((i>=2&&i<6))
cout<<setw(k)<<"&"<<setw(j)<<"&"<<endl;
else if(i==6)
cout<<"& & & & & & & & & & ";
else cout<<" ";
}

return 0;
}
22
23

Program no: 11
Aim: To find whether a given string is palindrome or not.
Source code
//palindrome program using string
#include<iostream>
#include<cstring>
using namespace std;
int main()
{
int palindrome(char a[]);

char str[20];
cout<<"the the string"<<endl;
cin>>str;
int r=palindrome(str);
if(r==1)
cout<<"It is a palindrome"<<endl;
else
cout<<"It is not a palindrome"<<endl;

return 0;
}
int palindrome(char p[])
{
int i,l;
l=strlen(p);
for(i=0;i<l/2;i++)
{
if(p[i]!=p[l-i-1])
return 0;
}
return 1;
}
24
25

Program no: 12
Aim: to reverse a sentence word by word.
Source code
12. //reversing a sentence , word by word
#include<iostream>
#include<cstring>
#include<cctype>
using namespace std;
int main()
{
int i,j,p,a,k;
char str[20],temp[20];
cout<<"enter a string"<<endl;
gets(str);
for(i=0;p=0,str[i]!='\0';i++)
{
if(str[i]==' ')
{
a=0;
for(j=p;j<i;j++)
{

temp[a]=str[j];
a++;

}
temp[a]='\0';

p=i+1;

}
}

if(str[i]=='\0')
{
a=0;

for(k=p;k<i;k++)
{
temp[a]=str[k];
a++;
}
temp[a]='\0';
cout<<strrev(temp);
}

return 0;
}
26
27

Program no: 13
Aim: to capitalize first letter of each word.
Source code
13. //program to capitalize first letter of each string
#include<iostream>
#include<cstring>
#include<cctype>
using namespace std;
int main()
{
int i=0;
char str[20];
puts("enter a string");
gets(str);
str[i]=(char)toupper(str[i]);
for(i=0;str[i]!='\0';i++)
{

if(str[i]==' ')
{

str[i+1]=(char)toupper(str[i+1]);
}
}
puts(str);
return 0;

}
28
29

Program no: 14
Aim: to count the number of alphabets.
Source code
//to count the number of alphabets
#include<iostream>
#include<cstring>
#include<cctype>
using namespace std;
int main ()
{
int i=0,l=0,count=0;
char str[20];
puts("enter a string");
gets(str);
l=strlen(str);
for(i=0;i<l;i++)
{
if (isalpha(str[i]))
count++;
}
puts("the number of alphabets = ");
// puts(count);
cout<<count;
return 0;
}
30
31

Program no: 15
Aim: to count the number of alphabets.
Source code
#include <iostream>
#include <cstring>
#include<cctype>
using namespace std;

int main()
{
int count(char ch[],int l);
int l=0,k;
//char str
char str[80];

puts("Enter The String : ");


cout<<endl;
gets(str);
l=strlen(str);
k=count(str,l);

puts("\nLowercase Letters : ");


cout<<k;
return 0;
}

int count(char ch[],int l)


{
int j=0;
for(int i=0;i<l;i++)
{

if (ch[i] >= 'a' && ch[i] <= 'z')


j++;

return (j);
}
32
33

Program no: 16
Aim: to compare two strings .
Source code
//compare two strings
#include<iostream>
using namespace std;
#include<cstring>

int main()
{

char str1[100], str2[100];


cout<<"Enter first string : ";
gets(str1);
cout<<"Enter second string : ";
gets(str2);
if(strcmp(str1, str2)==0)
{
cout<<"Both the strings are equal";
}
else
{
cout<<"Both the strings are not equal";
}
return 0;

}
34
35

Program no: 17
Aim: to convert lower case to upper case , vice versa.
Source code
//lower case to upper, uppercase to lower
using namespace std;
#include<iostream>
#include<cstring>
#include<cctype>
int main()
{
char str[80];
int i,l;
char c,d;
puts("Enter the String to convert uppercase letters to lowercase");
cout<<endl;
gets(str);
l=strlen(str);
for(i=0;i<l;i++)
{
if((str[i]>='a' && str[i]<='z') )
{
c=toupper(str[i]);
cout<<c;

}
else if((str[i]>='A' && str[i]<='z') )
{
d=tolower(str[i]);
cout<<d;
}
}
return 0;
}
36
37

Program no: 18
Aim: to find the sum of elements in an array.
Source code
//sum of elements in an array
#include<iostream>
#include<cctype>
#include<cstring>
using namespace std;
int main()
{
int a[100];
int k,sum=0
,size,i;
cout<<"enter the size of the array";
cin>>size;
cout<<"enter the elements ";
for(i=0;i<size;i++)
{
cin>>a[i];
}
for(k=0;k<size;k++)
{
sum=sum+a[k];
}
cout<<endl;
cout<<endl;
cout<<"the sum of all the elements that you have entered = "<<sum;
return 0;
}
38
39

Program no: 19
Aim: to find a given element entered by the user by linear search.
Source code
//linear search
#include<iostream>
#include<cctype>
#include<cstring>
using namespace std;
int main()
{
int a[100];
int sum=0,flag=1,size,i,j,n;
cout<<"enter the size of the array";
cin>>size;
for(i=0;i<size;i++)
{
cin>>a[i];
}
cout<<"enter the element that you want to search"<<endl;
cin>>n;
cout<<"using linear search";
cout<<endl;
cout<<endl;
for(i=0;i<size;i++)
{
if(a[i]==n)
{
cout<<"the element is present in postion = "<<i+1;
flag=0;
}else continue;

}
if(flag==1)
cout<<"not found!!!";
return 0;
}
40
41

Program no: 20
Aim: to find a given element entered by the user by binary search.
Source code
//binary search
#include<iostream>
using namespace std;
int main()
{
int a[100];
int sum=0,flag=1,size,i,j,n;
cout<<"enter the size of the array";
cin>>size;
cout<<"enter the elements in ascending order";
for(i=0;i<size;i++)
{ cin>>a[i]; }
cout<<"enter the element that you want to search";
cin>>n;
cout<<endl;
cout<<"using Binary Search"<<endl;
int top=9;
int bottom=0;
int mid;
mid=(top+bottom)/2;

while(bottom<top)
{
mid=(top+bottom)/2;
if(n>a[mid])
{
bottom=mid+1; mid=(top+bottom)/2;
}
if(n==a[mid])
{
cout<<"you have found the element postion = "<<mid+1;
flag=0; break;
}
if(n<a[mid])
{
top=mid-1;
mid=(top+bottom)/2;
}
}
if(flag==1)
cout<<"not found!!!";
int k;
for(k=0;k<size;k++)
{
sum=sum+a[k];
}
return 0;
}
42
43

Program no: 21
Aim: To find sum of two matrices using 2 dimensional array.
Source code
21. //sum of a matrix
#include<iostream>
using namespace std;
int main()
{
int sum(int a[10][10],int b[10][10],int n);
int a[10][10],b[10][10],y[10][10];
int i,j,r1,r2,c1,c2,n;
cout<<"enter the rows and columns(first one)" <<endl;
cin>>r1>>c1;
cout<<" enter the first matrix elements"<<endl;
for(i=0;i<c1;i++)
{
for(j=0;j<c1;j++)
{
cin>>a[i][j];
}
}
cout<<"enter the rows and columns(second one)" <<endl;
cin>>r2>>c2;
cout<<" enter the second matrix elements"<<endl;
for(i=0;i<c2;i++)
{
for(j=0;j<c2;j++)
{
cin>>b[i][j];
}
}
n=c1;
cout<<"the input matrix(first one)"<<endl;
for(i=0;i<r1;i++)
{
for(j=0;j<c1;j++)
{
cout<<a[i][j]<<" ";
}
cout<<endl;
}
cout<<endl;
cout<<endl;
cout<<"the input matrix(second one)"<<endl;
for(i=0;i<r1;i++)
{
for(j=0;j<c1;j++)
{
cout<<b[i][j]<<" ";
} cout<<endl;
}
44
45

cout<<endl;
cout<<endl;
cout<<endl;

if(r1==r2&&c1==c2)
{
cout<<"the added matrix"<<endl;
sum(a,b,c1);
}return 0;
}
void sum(int a[][10],int b[][10],int n)
{
int sum[10][10];
int i,j;
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
sum[i][j]=a[i][j]+b[i][j];

}
} for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
cout<<sum[i][j]<<" ";
}
cout<<endl; }
}
46
47

Program no: 22
Aim: to find product of two matrices using 2 dimensional array.
Source code
//product of matrices
#include<iostream>
#include<cctype>
#include<cstring>
using namespace std;
int main()
{

int product[100][100];
int a[10][10],b[10][10];
int i,j,r1,r2,c1,c2,n;
cout<<"enter the rows and columns" <<endl;
cin>>r1>>c1;
cout<<" enter the first matrix elements"<<endl;
for(i=0;i<r1;i++)
{
for(j=0;j<c1;j++)
{
cin>>a[i][j];
}
}

cout<<"enter the rows and columns" <<endl;


cin>>r2>>c2;
cout<<" enter the second matrix elements"<<endl;
for(i=0;i<r2;i++)
{
for(j=0;j<c2;j++)
{
cin>>b[i][j];
}
}
int k;
int p[10][10];

cout<<"the input matrix(first one)"<<endl;


for(i=0;i<r1;i++)
{
for(j=0;j<c1;j++)
{
cout<<a[i][j]<<" ";
}cout<<endl;
}

cout<<"the input matrix(second one)"<<endl;


48
49

for(i=0;i<r2;i++)
{

for(j=0;j<c2;j++)
{
cout<<b[i][j]<<" ";
}cout<<endl;

}
for(i=0;i<r1;i++)
{
for(j=0;j<c2;j++)
{
for(k=0;k<c1;k++)
{
product[i][j]+=a[i][k]*b[k][j];

}
}

cout<<endl;
cout<<"after multiplication"<<endl;

for(i=0;i<r1;i++)
{
for(j=0;j<c2;j++)
{

cout<<product[i][j]<<" ";

}cout<<endl;

}
cout<<endl;
cout<<endl;

return 0;
}
50
51

Program no: 23
Aim: To find the Transpose of a matrix.
Source code
//transpose of a matrix
#include<iostream>
#include<cctype>
#include<iomanip>
#include<cstring>
using namespace std;
int main()
{
int a[10][10],b[10][10],y[10][10];
int i,j,r1,c1;
cout<<"enter the rows and columns" <<endl;
cin>>r1>>c1;
cout<<" enter the matrix elements"<<endl;
for(i=0;i<r1;i++)
{
for(j=0;j<c1;j++)
{
cin>>a[i][j];
}
}
cout<<"the input matrix"<<endl;
for(i=0;i<r1;i++)
{
for(j=0;j<c1;j++)
{
cout<<a[i][j]<<" ";
}
cout<<endl;
}

for(i=0;i<r1;i++)
{
for(j=0;j<c1;j++)
{b[j][i]=a[i][j]; }
}
cout<<"after transposing the matrix"<<endl;

for(i=0;i<c1;i++)
{
for(j=0;j<r1;j++)
{
cout<<b[i][j]<<" ";
}
cout<<endl;
}
return 0;
}
52
53

Program no: 24
Aim: To find the trace of a matrix.
Source code
//trace of a matrix
#include<iostream>
using namespace std;
int main()
{
int r1,c1,a[10][10];
int i,j;
cout<<"enter the dimensions of your array(first enter rows) to find its trace(sum of the
diagonal elements) "<<endl;
cin>>r1>>c1;
if(r1==c1)
{
cout<<"enter the elements "<<endl;
for(i=0;i<r1;i++)
{
for(j=0;j<c1;j++)
{ cin>>a[i][j];}
}
cout<<"the input matrix"<<endl;
for(i=0;i<r1;i++)
{
for(j=0;j<c1;j++)
{
cout<<a[i][j]<<" ";
}
cout<<endl;
}
}else
cout<<"enter symmetrical array only !!!!!"<<endl;
int sum=0;
cout<<endl;
if(r1==c1)
{
for(i=0;i<r1;i++)
{
for(j=0;j<c1;j++)
{
if(i==j)
{
sum=sum+a[i][j];
}
}
} }
cout<<"the sum of diagonal elements = "<<sum;
cout<<endl;

return 0;
}
54
55

Program no: 25
Aim: To print the lower half elements of a matrix.
Source code
//lower half of matrix
#include<iostream>
#include<cctype>
#include<iomanip>
#include<cstring>
using namespace std;
int main()
{
int a[10][10],b[10][10],y[10][10];
int i,j,r1,c1;
cout<<"enter the rows and columns" <<endl;
cin>>r1>>c1;
cout<<" enter the matrix elements"<<endl;
for(i=0;i<r1;i++)
{
for(j=0;j<c1;j++)
{
cin>>a[i][j];
}
}
cout<<"the input matrix"<<endl;
for(i=0;i<r1;i++)
{
for(j=0;j<c1;j++)
{
cout<<a[i][j]<<" ";
}
cout<<endl;
}

if(r1==c1)
{
cout<<"the lower half elements are :"<<endl;
for(i=0;i<c1;i++)
{
for(j=0;j<=i;j++)
{

cout<<a[i][j]<<" ";
}
}cout<<endl;
}

return 0;
}
56
57

Program no: 26
Aim: to find the sum of elements of rows and the sum of elements of columns of a matrix.
Source code
//sum of row elements and column elements in a matrix
#include<iostream>
#include<cctype>
#include<iomanip>
#include<cstring>
using namespace std;
int main()
{
int r1,c1,a[10][10];
int i,j,sum2,sum;
cout<<"enter the dimensions of your array(first enter rows)"<<endl;
cin>>r1>>c1;
cout<<"enter the elements "<<endl;
for(i=0;i<r1;i++)
{
for(j=0;j<c1;j++)
{
cin>>a[i][j];
}
}
cout<<"the input matrix"<<endl;
for(i=0;i<r1;i++)
{
for(j=0;j<c1;j++)
{
cout<<a[i][j]<<" ";
}
cout<<endl;
}cout<<"sum of the elements in rows:"<<endl;
for(i=0;i<r1;i++)
{
sum=0;
for(j=0;j<c1;j++)
{ sum=sum+a[i][j];
}
cout<<"sum of elements in row :"<<i+1<<" = "<<sum<<endl;
}
for(j=0;j<c1;j++)
{
sum2=0;
for(i=0;i<r1;i++)
{ sum2=sum2+a[i][j];
}
cout<<"sum of elements in column :"<<j+1<<" = "<<sum2<<endl;
}
return 0;
}
58
59

Program no: 27
Aim: to reverse five strings entered by the user.
Source code
//to reverse 5 strings
#include<iostream>
#include<cctype>
#include<iomanip>
#include<cstring>
using namespace std;
int main()
{
int i;
char str[5][25],str2[5][25];
for(i=0;i<5;i++)
{
cout<<"enter the string to reverse them"<<endl;
gets(str[i]);
}

cout<<"the reversed strings are:"<<endl;


for(i=0;i<5;i++)
{
cout<<strrev(str[i])<<endl;

}
cout<<endl;

return 0;
}
60
61

Program no: 28
Aim: To make a structure Program by taking name , address and marks .
Source code
// Structures(first programme)
using namespace std;
#include<iostream>
int main()
{
struct address
{
char streetname[20];
int housenumber;
};
struct students
{
char name[20];
int marks;
char phone[10];
address adr;
} stu1,stu2;
int i=1;
cout<<"enter the details of the student in this sequence "<<endl;
cout<< "(name,marks,address,phone number)";
cout<<"enter the name for the student 1"<<endl;
cin>>[Link];
cout<<"enter marks"<<endl;
cin>>[Link];
cout<<"enter address(Street name, house number) "<<endl;
cin>>[Link];
cin>>[Link];
cout<<"enter phone number"<<endl;
cin>>[Link];
cout<<"enter the name for the student 2"<<endl;
cin>>[Link];
cout<<"enter marks"<<endl;
cin>>[Link];
cout<<"enter address(Street name, house number) "<<endl;
cin>>[Link];
cin>>[Link];
cout<<"enter phone number"<<endl;
cin>>[Link];
cout<<" the entered data"<<endl;
cout<<" name : "<<[Link]<<endl;
cout<<" Address : " <<[Link]<<", "<< [Link]<<endl;
cout<<" Ph number : " <<[Link]<<endl;
cout<<" name : "<<[Link]<<endl;
cout<<" Address : " <<[Link]<<", "<< [Link]<<endl;
cout<<" Ph number : " <<[Link]<<endl;
return 0;
}
62
63

Program no: 29
Aim: To make a structure of array.
Source code
using namespace std;
#include<iostream>
int i;
int main()
{
struct address
{
char streetname[20];
int housenumber;
};

struct students
{
char name[20];
int marks;
float phone;
address adr;
} stu[3];

i=1;
cout<<"enter the details of the student in this sequence "<<endl;
cout<< "(name,marks,address,phone number)"<<endl;

for(i=0;i<3;i++)
{
cout<<"enter the name for the student"<<" "<<i+1<<endl;
cin>>stu[i].name;
cout<<"enter marks"<<endl;
cin>>stu[i].marks;
cout<<"enter address(Street name, house number)"<<endl;
cin>>stu[i].[Link];
cin>>stu[i].[Link];
cout<<"enter phone number"<<endl;
cin>>stu[i].phone;
}
return 0;
}
64
65

Program no: 30
Aim: to calculate average salary .
Source code
//Employee average salary
#include<iostream>
#include<iomanip>
using namespace std;
float avg();
void print(float q);
int i;
struct employee
{ char name[20];
float salary;
float exp;
char desig[20];
} empl,e[3];
void readdata();
int main()
{
int g;
readdata();
g=avg();
print(g);
return 0;}
void readdata()
{
cout<<"enter name and salary"<<endl;
for(i=0;i<3;i++)
{
cout<<"name of the person "<<i+1<<endl;
cin>>e[i].name;
puts("Designation ");
cin>>e[i].desig;
puts("Experience in years (example 1,1.5) ");
cin>>e[i].exp;
puts("Salary ");
cin>>e[i].salary;
}
cout<<endl;
}
float avg()
{int j;
float w=0;
for(j=0;j<3;j++)
{w=e[j].salary+w;}
return (w/3);}
void print(float q)
{cout<<"the entered data:"<<endl;
cout<<endl;
cout<<" "<<"name"<<" "<<"Designation"<<" "<<"Experience"<<" "<<"salary"<<endl;
for(i=0;i<3;i++)
{cout<<" "<<e[i].name<<" "<<e[i].desig<<" "<<e[i].exp<<" "<<e[i].salary<<endl;}
cout<<"-----------------------------------------------------------------"<<endl;
cout<<setw(5)<<"the average "<<setw(15)<<q;
}
66
67

Program no: 31
Aim: to use random (inbuilt function) to generate random numbers/outputs.
Source code
#include<iostream.h>
#include<stdlib.h>
#include<iomanip.h>
#include<conio.h>
using namespace std;
int main()
{
int r,c;
clrscr();
int n=random(3);
int m=random(2);
randomize();
int doc[3][3]={{1,2,3},{2,3,4},{3,4,5}};
for(r=0;r<n;r++)
{
for(c=0;c<m;c++)
{
cout<<r<<setw(5)<<c<<doc[r][c];

} cout<<endl;
}
getch();
return 0;
}
68

You might also like