Functions: # Find The Sum of 2 Numbers
Functions: # Find The Sum of 2 Numbers
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);
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
Output
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
}
}
for (i=0;i<r;i++)
{for(j=0;j<c;j++)
{cout<<tran[i][j]<<"\t";
}
cout<<"\n";
}
getch();
}
Output
}
cout<<"Sorted Array:\n";
for (i=0;i<n;i++)
{cout<<arr[i]<<"\n";}
getch();
}
Output
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