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

Some Basic C++ Programs

The document contains 10 C++ programs with their summaries: 1. A program to count the number of words and characters in a line of text. 2. A program to accept sales of 5 salesmen over 12 months and print the total sales of each. 3. A program to reverse all the strings stored in an array.

Uploaded by

aF
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)
87 views

Some Basic C++ Programs

The document contains 10 C++ programs with their summaries: 1. A program to count the number of words and characters in a line of text. 2. A program to accept sales of 5 salesmen over 12 months and print the total sales of each. 3. A program to reverse all the strings stored in an array.

Uploaded by

aF
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
You are on page 1/ 32

C++ PRACTICAL FILE

Afif Deshmukh

11-C
ROLL NO 10
Index
1. Program to read a line of text from the keyboard and display the
following information on the screen : i) Number of words ii) Number of
characters
2. Program to accept sales of 5 salesmen in 12 months and print the
total sales made by each salesman.
3. Program to reverse all the strings(3) stored in an array.
4. Write a menu driven program to i) read and check the equality of
two matrices and ii) to sum the elements above and below the main
diagonal of a matrix.
5. Write a menu driven program to i) add two matrices, ii) subtract two
matrices and iii) multiply two matrices.
6. Program to find the position of a pattern string in the main string.
7. Program to find the substring of a given string.
8. Program to print the largest, smallest, sum and product of n
elements of an array.
9. Program to convert lowercase letters in a given string to
corresponding upper case letters and vice versa.
10. program to operate on complex numbers
1. Write a program to read a line of text from the keyboard and display the
following information on the screen : i) Number of words
#include<iostream>

#include<stdio.h>

using namespace std;

int main()

char str[80];

int i,count;

cout<<"enter any string";

gets(str);

for(i=0;str[i]!='\0';i++)

if(str[i]==' ')

count++;

cout<<" number of words in given string are"<<count;

return 0;

}
ii) Number of characters
#include<iostream>

using namespace std;

int main()

int chcount =0;

const char ent='\n';

char ch;

cout<<"enter character\n";

cin.get(ch);

while(ch!=ent)

if (ch>='A'&&ch<='z')

{ chcount++;

cout.put(ch);

cin.get(ch);

cout<<" the number of characters ="<<chcount<<" ";

return 0;

}
2. Write a program to accept sales of 5 salesmen in 12 months and print the total
sales made by each salesman
#include<iostream>

using namespace std;

int main()

int sales[5][12];

int i,j;

unsigned long total;

for(i=0;i<5;i++)

{ total=0;

cout<<" enter sales for salesman"<<i+1<<":"<<"\n";

for(j=0;j<12;j++)

{ cout<<"month"<<j+1<<":";

cin>>sales[i][j];

total+=sales[i][j];

cout<<" ";

cout<<"enter sales of sales man"<<i+1<<"=rs"<<total;

return 0;

}
3. Write a program to reverse all the strings(3) stored in an array.
#include<iostream>

#include<stdio.h>

#include<string.h>

using namespace std;

int main()

{ int l,i,k=0;

char str[80],word[80];

cout<<"enter any string"<<endl;

gets(str);

strcat(str," ");

for(i=0;str[i]!='\0';i++)

if(str[i]!="")

{ word[k]=str[i];

k=k+1;

else

while(k>0)

{ cout<<word[--k];

cout<<str[i];

return 0;

}
4. Write a menu driven program to i) read and check the equality of two matrices
and ii) to sum the elements above and below the main diagonal of a matrix .
#include<iostream>

#include<stdio.h>

#include<string.h>

using namespace std;

int main()

{ int flag=0,a[3][3],b[3][3],i,j,choice, arr[5][5],c=0,d=0,p,q,u;

cout<<"1. read and check the equality of two matrices"<<"\n";

cout<<"2. sum the elements above and below the main diagonal of a matrix."<<"\n";

cout<<"choose option"<<"\n";

cin>>choice;

if (choice==1)

{ cout<<"ENTER FIRST MATRIX ROW WISE:\n";

for(i=0;i<3;i++)

for(j=0;j<3;j++)

cout<<"a["<<i<<"]["<<j<<"]=";

cin>>a[i][j];

cout<<endl;

cout<<"ENTER SECOND MATRIX ROW WISE:\n";

for(i=0;i<3;i++)

for(j=0;j<3;j++)

{
cout<<"b["<<i<<"]["<<j<<"]=";

cin>>b[i][j];

cout<<endl;

for(i=0;i<3;i++)

for(j=0;j<3;j++)

if(a[i][j]!= b[i][j]) //checking for equality

flag==1;

break;

if(flag)

cout<<"MATRICES ARE UNEQUAL";

else

cout<<"MATRICES ARE EQUAL";

else

if (choice==2)

{ cout<<"Enter size of matrix(max 5):";

cin>>u;

cout<<"Enter the matrix:\n";


for(p=0;p<u;++p)

for(q=0;q<u;++q)

cin>>arr[p][q];

for(p=0;p<u;++p)

for(q=0;q<u;++q)

if(q>p)

c+=arr[i][j];

else

if(i>j)

d+=arr[i][j];

cout<<"\nSum of elements above the diagonal:"<<c;

cout<<"\nSum of elements below the diagonal:"<<d;

return 0;}
}

Output 2
5. Write a menu driven program to i) add two matrices, ii) subtract two matrices
and iii) multiply two matrices.
#include<iostream>
using namespace std;
int main()
{ int row, col, m1[3][3], m2[3][3], m3[3][3],choice,i,j,k,sum;
cout<<"1. add matrices"<<"\n";
cout<<"2. subtract matrices"<<"\n";
cout<<"3. multiply matrices"<<"\n";
cout<<"choose preffered operation"<<"\n";
cin>>choice;
switch(choice)
{case 1: cout<<"Enter the number of rows(should be >0 and <4): ";
cin>>row;
cout<<"Enter the number of column(should be >0 and <4): ";
cin>>col;
cout << "Enter the elements of first 1st matrix: ";
for (int i = 0;i<row;i++ ) {
for (int j = 0;j < col;j++ ) {
cin>>m1[i][j];
}
}
cout << "Enter the elements of first 2nd matrix: ";
for (int i = 0;i<row;i++ ) {
for (int j = 0;j<col;j++ ) {
cin>>m2[i][j];
}
}

cout<<"Output: ";
for (int i = 0;i<row;i++ ) {
for (int j = 0;j<col;j++ ) {
m3[i][j]=m1[i][j]+m2[i][j];
cout<<m3[i][j]<<" ";
}
}
break;
case 2:

cout<<"\n Enter First Matrix Elements : \n";


for(i=0; i<1; i++)
{
for(j=0; j<3; j++)
{
cout<<" ";
cin>>m1[i][j];
}
}
cout<<"\n Enter Second Matrix Elements : \n";
for(i=0; i<1; i++)
{
for(j=0; j<3; j++)
{
cout<<" ";
cin>>m2[i][j];
}
}
cout<<"\n Difference of Two Matrices : \n\n";
for(i=0; i<1; i++)
{
for(j=0; j<3; j++)
{
m3[i][j]=m1[i][j]-m2[i][j];
}
}
for(i=0; i<1; i++)
{
cout<<" ";
for(j=0; j<3; j++)
{
cout<<m3[i][j]<<"\t";
}
cout<<"\n";
break;
case 3:
cout<<"Enter first matrix element : ";
for(i=0; i<3; i++)
{
for(j=0; j<3; j++)
{
cin>>m1[i][j];
}
}
cout<<"Enter second matrix element : ";
for(i=0; i<3; i++)
{
for(j=0; j<3; j++)
{
cin>>m2[i][j];
}
}
cout<<"Multiplying two matrices...\n";
for(i=0; i<3; i++)
{
for(j=0; j<3; j++)
{
sum=0;
for(k=0; k<3; k++)
{
sum = sum + m1[i][k] * m2[k][j];
}
m3[i][j] = sum;
}
}
cout<<"\nMultiplication of two Matrices : \n";
for(i=0; i<3; i++)
{
for(j=0; j<3; j++)
{
cout<<m3[i][j]<<" ";
}
cout<<"\n";
}
break;
default :
cout<<"wrong choice";
} return 0; }}
6. Write a program to find the position of a pattern string in the main string
#include<iostream>

#include<stdio.h>

using namespace std;

int main()

int i,j,temp;

char str[100];

char substr[20];

cout<<"enter string";

gets(str);

cout<<"enter pattern";

gets(substr);

for(i=0;str[i]!='\0';i++)

j=0;

if(str[i]==substr[j])

temp=i+1;

while(str[i]==substr[j])

i++;

j++;

if(substr[j]=='\0')

cout<<"The substring is present in given string at position \n"<<temp;

exit(0);

}
else

i=temp;

temp=0;

if(temp==0)

cout<<"The substring is not present in given string\n";

return 0;

}
7. Write a program to find the substring of a given string
#include<iostream>

#include<string.h>

using namespace std;

int main()

char mainstr[50],substr[50];

int count,pos,i,j,len,num,x1;

cout<<"\n enter the main string\n";

cin.getline(mainstr,50);

len= strlen(mainstr);

cout<<"\n enter starting position of substring\n";

cin>>pos;

if(pos>len)

{ cout<<"\n starting position exceeds the total"

<<"lenght of the string!!!";

cout<<"\n the number of characters in substring\n";

cin>>count;

if(pos<=0)

{ cout<<"\nextracted string is empty\n";

else if(((pos+count)-1)>len)

{ cout<<"\n\nstring to be extracted exceeds limit\n";

num=(len-pos);

else

{ num=count;

cout<<"\nNUM"<<num;

}
j=0;

for(i=--pos;i<=(pos+num);++i)

{ substr[j]=mainstr[i];

j++;

cout<<"\nsubstring is\n";

cout.write(substr,j);

return 0;

}
8. Write a program to print the largest, smallest, sum and product of n elements
of an array
#include <iostream>

using namespace std;

int main()

int i,n,ch,ar[5],sum=0,pro=1,large=0,small=10000000000;

char ch1;

cout<<"\nPlease choice from the list below";

cout<<"\n1. Largest of n numbers";

cout<<"\n2. Smallest of n numbers";

cout<<"\n3. Sum of n numbers";

cout<<"\n4. Product of n numbers\n";

do

cin>>ch;

if(ch==1 || ch==2 || ch==3 || ch==4)

cout<<"\nHow many numbers?\n";

cin>>n;

cout<<"\nEnter the numbers\n";

for(i=0;i<n;++i)

cin>>ar[i];

switch(ch)

case 1:{

for(i=0;i<n;++i)

if(ar[i]>large)

large=ar[i];
}

cout<<"\nThe largest number is: "<<large;

break;}

case 2:{

for(i=0;i<n;++i)

if(ar[i]<small)

small=ar[i];

cout<<"\nThe smallest number is: "<<small;

break;}

case 3:{

for(i=0;i<n;i++)

sum+=ar[i];

cout<<"\nThe sum of numbers is: "<<sum;

break;}

case 4:{

for(i=0;i<n;++i)

pro+=ar[i];

cout<<"\nThe product of numbers is: ";

break;}

default:

cout<<"\nPLEASE ENTER A VALID CHOICE";

cout<<"\nDo you want to continue\n";

cin>>ch1;

if(ch=='y' ||ch=='Y')
cout<<"\nPlease enter your choice again";

cout<<"\n1. Largest of n numbers";

cout<<"\n2. Smallest of n numbers";

cout<<"\n3. Sum of n numbers";

cout<<"\n4. Product of n numbers\n";

}while(ch1=='y'||ch1=='Y');

return 0;

}
9,Write a program to convert lowercase letters in a given string to corresponding
upper case letters and vice versa.
#include<iostream>

using namespace std;

int main()

{ string str;

cout<<"enter string";

cin>>str;

int ln = str.length();

for (int i=0; i<ln; i++)

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

str[i] = str[i] - 32;

else if(str[i]>='A' && str[i]<='Z')

str[i] = str[i] + 32;

cout<<str;

return 0;

}
10.Create a structure called complex number with real part and imaginary part.
Define functions add(), sub(), multi() & div() with suitable arguments and return
values. Also define a function display() to display a complex number. Write menu
based program to illustrate the use of all these functions.
#include<iostream>

#include <stdio.h>

#include <stdlib.h>

using namespace std;

struct complex

int real, img;

};

int main()

int choice, x, y, z;

struct complex a, b, c;

while(1)

cout<<"Press 1 to add two complex numbers.\n";

cout<<"Press 2 to subtract two complex numbers.\n";

cout<<"Press 3 to multiply two complex numbers.\n";

cout<<"Press 4 to divide two complex numbers.\n";

cout<<"Enter your choice\n";

cin>>choice;

if (choice >= 1 && choice <= 4)

{
cout<<"Enter a and b where a + ib is the first complex number.";

cout<<"\na = ";

cin>>a.real;

cout<<"b = ";

cin>>a.img;

cout<<"Enter c and d where c + id is the second complex number.";

cout<<"\nc = ";

cin>>b.real;

cout<<"d = ";

cin>>b.img;

if (choice == 1)

c.real = a.real + b.real;

c.img = a.img + b.img;

if (c.img >= 0)

cout<<"Sum of the complex numbers = "<<c.real<<"+"<<c.img<<"i";

else

cout<<"Sum of the complex numbers = "<<c.real<<"+"<<c.img<<"i";

else if (choice == 2)

c.real = a.real - b.real;

c.img = a.img - b.img;

if (c.img >= 0)

cout<<"Difference of the complex numbers = "<<c.real<<"+"<<c.img<<"i";

else

cout<<"Difference of the complex numbers = "<<c.real<<"i"<<c.img<<"i";

}
else if (choice == 3)

c.real = a.real*b.real - a.img*b.img;

c.img = a.img*b.real + a.real*b.img;

if (c.img >= 0)

cout<<"Multiplication of the complex numbers = "<<c.real<<"+"<<c.img<<"i";

else

cout<<"Multiplication of the complex numbers = "<<c.real<<"+"<<c.img<<"i";

else if (choice == 4)

if (b.real == 0 && b.img == 0)

cout<<"Division by 0 + 0i isn't allowed.";

else

x = a.real*b.real + a.img*b.img;

y = a.img*b.real - a.real*b.img;

z = b.real*b.real + b.img*b.img;

if (x%z == 0 && y%z == 0)

if (y/z >= 0)

cout<<"Division of the complex numbers = "<<x/z<<y/z;

else

cout<<"Division of the complex numbers = "<<x/z<<y/z;

else if (x%z == 0 && y%z != 0)

if (y/z >= 0)

cout<<"Division of two complex numbers = "<<x/z<<y<<z;


else

cout<<"Division of two complex numbers = "<<x/z<<y<<z;

else if (x%z != 0 && y%z == 0)

if (y/z >= 0)

cout<<"Division of two complex numbers = "<<x<<z<<y/z;

else

cout<<"Division of two complex numbers = "<<x<<z<<y/z;

else

if (y/z >= 0)

cout<<"Division of two complex numbers = "<<x<<z<<y<<z;

else

cout<<"Division of two complex numbers = "<<x<<z<<y<<z;

else

cout<<"Invalid choice.";

cout<<"\nPress any key to enter choice again...\n";

You might also like