You are on page 1of 23

FUNCTIONS

# Find the sum of 2 numbers.


/*Program with functions with no return value and no
arguments*/
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
void calc();
calc();
getch();
}
void calc()
{int num1,num2,sum;
cout<<"Enter 2 no.s";
cin>>num1>>num2;
sum=num1+num2;
cout<<"\nSum="<<sum;
}

2.
//Program with no return value,with arguments.
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int num1,num2;
void calc(int a,int b);
cout<<"\nEnter number 1:";
cin>>num1;
cout<<"\nEnter number 2:";
cin>>num2;
calc(num1,num2);
getch(); }
void calc(int a, int b)
{ int sum;
sum=a+b;
cout<<"\nsum="<<sum; }

3.
//Program with return value and no arguments.
#include<iostream.h>
#include<conio.h>
void main()
{ clrscr();
int num1,num2;
int calc();
int sum=calc();
cout<<"\n sum="<<sum;
getch(); }
int calc()
{ int num1,num2;
cout<<"Enter number 1:";
cin>>num1;
cout<<"Enter number 2:";
cin>>num2;
int sum;
sum=num1+num2;
return sum;
}

4.
//Program with return value and an argument.
#include<iostream.h>
#include<conio.h>
void main()
{ clrscr();
int num1,num2;
int calc(int a,int b);
cout<<"\nEnter number 1:";
cin>>num1;
cout<<"\nEnter number 2:";
cin>>num2;
int s=calc(num1,num2);
cout<<"sum="<<s;
getch(); }
int calc(int a, int b)
{ int sum1;
sum1=a+b;
return sum1; }

STRUCTURES
1.
//Program to enter details about the book and print the info.
#include <iostream.h>
#include <conio.h>
#include <string.h>
#include<stdio.h>
struct pub
{
char pname[55];
char com[55];
char add[55];
int no;
};
struct book
{
int bno;
char bname[100];
char sub[50];
int price;
int edit;
pub pub1;
}
books;
void main ()
{
clrscr();
cout<<"\nTokyo Library\n";
cout<<"\nEnter book no.:";

cin>>books.bno;
cout<<"\nEnter name of the book:";
gets(books.bname);
cout<<"\nEnter subject it is based on:";
cin>>(books.sub);
cout<<"\nEnter the price:";
cin>>(books.price);
cout<<"\nEnter the edition:";
cin>>(books.edit);
cout<<"\nEnter the proprietor's name:";
gets(books.pub1.pname);
cout<<"\nEnter the company's name:";
gets(books.pub1.com);
cout<<"\nEnter the address:";
cin>>(books.pub1.add);
cout<<"\nEnter the phone number:";
cin>>(books.pub1.no);
clrscr();
cout<<"\nTokyo Library\n";
cout<<"\nBook no.:";
cout<<(books.bno);
cout<<"\nName of the book:";
cout<<(books.bname);
cout<<"\nSubject it is based on:";
cout<<(books.sub);
cout<<"\nThe price:";
cout<<(books.price);
cout<<"\nThe edition:";
cout<<(books.edit);

cout<<"\nThe proprietor's name:";


cout<<(books.pub1.pname);
cout<<"\nThe company's name:";
cout<<(books.pub1.com);
cout<<"\nThe address:";
cout<<(books.pub1.add);
cout<<"\nThe phone number:";
cout<<(books.pub1.no);
getch();
}

2.
//Program to accept the marks and print them.
#include <iostream.h>
#include <conio.h>
#include <string.h>
#include<stdio.h>
struct grade{int rollno;
char sname[20];
float marks[3];
};
void main()
{
clrscr();
int n;
cout<<"\nEnter the no of students whose data are to be
inputed:";
cin>>n;
grade grade1[10];
for (int i=0;i<n;i++)
{cout<<"Enter roll no:";
cin>>grade1[i].rollno;
cout<<"\nEnter name:";
gets(grade1[i].sname);
for (int j=0;j<3;j++)
{ cout<<"Enter marks in the subject:";
cin>>grade1[i].marks[j];
}
cout<<"\n\nDATA ARE:";

cout<<"\nRoll no:"<<grade1[i].rollno;
cout<<"\nName:"<<grade1[i].sname;
for (j=0;j<3;j++)
{
cout<<"\nmarks"<<j+1<<":"<<grade1[i].mar
ks[j];
}
}
getch();
}

Output

10) Program to concatenate two strings.


#include <iostream.h>
#include <conio.h>
#include <string.h>
void main ()
{
clrscr();
char str1[25],str2[25],str3[25];
cout<<"Enter the first string:";
cin>>str1;
cout<<"Enter the second string:";
cin>>str2;
for (int i=0;str1[i]!='\0';i++)
{str3[i]=str1[i];}
for (int j=0;str2[j]!='\0';j++)
{str3[i+j]=str2[j];}
str3[i+j]='\0';
cout<<str3;
getch();}

9) Program to check whether a string is palindrome or not.


#include<iostream.h>
#include<conio.h>
#include<stdio.h>
#include<string.h>
void main()
{
clrscr();
char str[100],temp,str1[100];
int length;
cout<<"Enter your string:";
gets(str);
for(int j=0;str[j]!='\0';j++)
{str1[j]=str[j];}
length=strlen(str);
for(int i=0;i<length/2;i++)
{ temp=str[i];
str[i]=str[length-(1+i)];
str[length-(1+i)]=temp;
}
cout<<"\nThe reverse string is:"<<str;
cout<<"\nThe original string is:"<<str1;
if(strcmp(str,str1)==0)
{cout<<"\n\n=>PALINDROME STRING";
}
else
{cout<<"\n\n=>NOT PALINDROME STRING";}
getch();
}

Output

8) To find the multiplication of elements of two matrices.


#include <iostream.h>
#include <conio.h>
#include <string.h>
void main ()
{clrscr();
int mat[10][10],r,c,r1,c1,mat1[10][10],mat2[10][10],i,j;
cout<<"MATRIX 1:\n";
cout<<"Enter the no of rows:";
cin>>r;
cout<<"\nEnter the no of columns:";
cin>>c;
cout<<"\nMATRIX 2:\n";
cout<<"Enter the no of rows:";
cin>>r1;
cout<<"\nEnter the no of columns:";
cin>>c1;
if (c==r1)
{cout<<"Enter "<<r*c<<" elements for matrix 1:\n";
for (i=0;i<r;i++)
{for (j=0;j<c;j++)
{cin>>mat[i][j];}}
cout<<"Enter "<<r1*c1<<" elements for matrix 2:\n";
for (i=0;i<r1;i++)
{for (j=0;j<c1;j++)
{ cin>>mat1[i][j];}}
clrscr();
cout<<"MATRIX 1:\n";
for (i=0;i<r;i++)
{for (j=0;j<c;j++)
{ cout<<mat[i][j]<<"\t";}
cout<<"\n";}
cout<<"\n";
cout<<"MATRIX 2:\n";
for (i=0;i<r1;i++)
{for (j=0;j<c1;j++)
{ cout<<mat1[i][j]<<"\t";}

cout<<"\n";}
cout<<"\n";
for (i=0;i<r;i++)
{for (j=0;j<r1;j++)
{mat2[i][j]=0;
for (int k=0;k<c;k++)
{mat2[i][j]+=mat[i][k]*mat1[k][j];}}}
cout<<"RESULT MATRIX:\n";
for (i=0;i<r;i++)
{for (j=0;j<c1;j++)
{cout<<mat2[i][j]<<"\t";}
cout<<"\n";}}
else
cout<<"Not a matrix that can be multiplied !!!";
getch();}

Output

7) Program to find transpose of a matrix.


#include <iostream.h>
#include <conio.h>
#include <string.h>
void main ()
{
clrscr();
int mat[10][10],tran[10][10],i,j,r,c;
cout<<"Enter the no of rows:";
cin>>r;
cout<<"Enter the no of columns:";
cin>>c;
cout<<"Enter "<<r*c<<" elements:";
for ( i=0;i<r;i++)
{for (j=0;j<c;j++)
{cin>>mat[i][j];
}
}
for (i=0;i<r;i++)
{for(j=0;j<c;j++)
{cout<<mat[i][j]<<"\t";
}
cout<<"\n";
}
cout<<"\n\n";
for (i=0;i<c;i++)
{for (j=0;j<r;j++)
{tran[i][j]=mat[j][i];

}
}
for (i=0;i<r;i++)
{for(j=0;j<c;j++)
{cout<<tran[i][j]<<"\t";
}
cout<<"\n";
}
getch();
}
Output

4) Write a program to sort the array in ascending order using


selection sort.
#include <iostream.h>
#include <conio.h>
#include <string.h>
void main ()
{
clrscr();
int n,arr[50];
cout<<"Enter the number of elements:";
cin>>n;
cout<<"Enter the elements:\n";
for (int i=0;i<n;i++)
{cin>>arr[i];}
cout<<"Inputed Array:\n";
for (i=0;i<n;i++)
{cout<<arr[i]<<"\n";}
int temp=0;
for (i=0;i<n;i++)
{ for (int j=i+1;j<n;j++)
{ if (arr[i]>arr[j])
{temp=arr[i];
arr[i]=arr[j];
arr[j]=temp;
}
else
{}
}

}
cout<<"Sorted Array:\n";
for (i=0;i<n;i++)
{cout<<arr[i]<<"\n";}
getch();
}
Output

5) Program to sort the array using bubble sort.


#include <iostream.h>
#include <conio.h>
#include <string.h>
void main ()
{
clrscr();
int arr[10],temp=0,c=0;
cout<<"Enter the number of elements:";
int n;
cin>>n;
cout<<"Enter the elements:";
for(int i=0;i<n;i++)
{cin>>arr[i];
}
cout<<"Array is:\n";
for (i=0;i<n;i++)
{cout<<arr[i]<<"\t";
}
for (i=0;i<n;i++)
{for (int j=i+1;j<n;j++)
{if (arr[i]>arr[j])
{temp=arr[i];
arr[i]=arr[j];
arr[j]=temp;
}
}
cout<<"\nArray after iteration of "<<++c<<":\n";

for(int k=0;k<n;k++)
{cout<<arr[k]<<" ";
}
cout<<"\n";
}
getch();
}

Output

6) MERGE SORT
#include <iostream.h>
#include <conio.h>
#include <string.h>
void main ()
{clrscr();
int a[50],b[50],c[50],n1,n2,n3,k,temp=0;
cout<<"First Array:\n";
cout<<"Enter the number of the elements:\n";
cin>>n1;
cout<<"Enter the elements:\n";
for (int i=0;i<n1;i++)
{cin>>a[i];}
cout<<"Second Array:\n";
cout<<"Enter the number of the elements:\n";
cin>>n2;
cout<<"Enter the elements:\n";
for (i=0;i<n2;i++)
{cin>>b[i];}
cout<<"First Array:\n";
for (i=0;i<n1;i++)
{cout<<a[i]<<" ";}
cout<<"\nSecond Array:\n";
for (i=0;i<n2;i++)
{cout<<b[i]<<" ";}
n3=n1+n2;
for (i=0;i<=n1;i++)

{c[i]=a[i];}
for (i=0,k=n2;i<=n3;i++,k++)
{c[k]=b[i];}
cout<<"\nMerged array:";
for (i=0;i<n3;i++)
{cout<<c[i]<<" ";}
for(i=0;i<n3;i++)
{for (int j=i+1;j<n3;j++)
{if (c[i]>c[j])
{temp=c[i];
c[i]=c[j];
c[j]=temp; }}}
cout<<"\nSorted Merged Array:";
for(i=0;i<n3;i++)
{cout<<c[i]<<" ";}
getch();}
Output