Some Basic C++ Programs
Some Basic C++ Programs
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>
int main()
char str[80];
int i,count;
gets(str);
for(i=0;str[i]!='\0';i++)
if(str[i]==' ')
count++;
return 0;
}
ii) Number of characters
#include<iostream>
int main()
char ch;
cout<<"enter character\n";
cin.get(ch);
while(ch!=ent)
if (ch>='A'&&ch<='z')
{ chcount++;
cout.put(ch);
cin.get(ch);
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>
int main()
int sales[5][12];
int i,j;
for(i=0;i<5;i++)
{ total=0;
for(j=0;j<12;j++)
{ cout<<"month"<<j+1<<":";
cin>>sales[i][j];
total+=sales[i][j];
cout<<" ";
return 0;
}
3. Write a program to reverse all the strings(3) stored in an array.
#include<iostream>
#include<stdio.h>
#include<string.h>
int main()
{ int l,i,k=0;
char str[80],word[80];
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>
int main()
cout<<"2. sum the elements above and below the main diagonal of a matrix."<<"\n";
cout<<"choose option"<<"\n";
cin>>choice;
if (choice==1)
for(i=0;i<3;i++)
for(j=0;j<3;j++)
cout<<"a["<<i<<"]["<<j<<"]=";
cin>>a[i][j];
cout<<endl;
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++)
flag==1;
break;
if(flag)
else
else
if (choice==2)
cin>>u;
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];
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:
#include<stdio.h>
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')
exit(0);
}
else
i=temp;
temp=0;
if(temp==0)
return 0;
}
7. Write a program to find the substring of a given string
#include<iostream>
#include<string.h>
int main()
char mainstr[50],substr[50];
int count,pos,i,j,len,num,x1;
cin.getline(mainstr,50);
len= strlen(mainstr);
cin>>pos;
if(pos>len)
cin>>count;
if(pos<=0)
else if(((pos+count)-1)>len)
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>
int main()
int i,n,ch,ar[5],sum=0,pro=1,large=0,small=10000000000;
char ch1;
do
cin>>ch;
cin>>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];
}
break;}
case 2:{
for(i=0;i<n;++i)
if(ar[i]<small)
small=ar[i];
break;}
case 3:{
for(i=0;i<n;i++)
sum+=ar[i];
break;}
case 4:{
for(i=0;i<n;++i)
pro+=ar[i];
break;}
default:
cin>>ch1;
if(ch=='y' ||ch=='Y')
cout<<"\nPlease enter your choice again";
}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>
int main()
{ string str;
cout<<"enter string";
cin>>str;
int ln = str.length();
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>
struct complex
};
int main()
int choice, x, y, z;
struct complex a, b, c;
while(1)
cin>>choice;
{
cout<<"Enter a and b where a + ib is the first complex number.";
cout<<"\na = ";
cin>>a.real;
cout<<"b = ";
cin>>a.img;
cout<<"\nc = ";
cin>>b.real;
cout<<"d = ";
cin>>b.img;
if (choice == 1)
if (c.img >= 0)
else
else if (choice == 2)
if (c.img >= 0)
else
}
else if (choice == 3)
if (c.img >= 0)
else
else if (choice == 4)
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 (y/z >= 0)
else
if (y/z >= 0)
if (y/z >= 0)
else
else
if (y/z >= 0)
else
else
cout<<"Invalid choice.";