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

Function-With No Parameters No Return

The document contains code snippets demonstrating different functions in C++ including functions with and without parameters, returning values, default arguments, function overloading, and working with matrices including addition, multiplication, finding the inverse and smallest element. It also includes examples of working with strings such as finding the length, adding strings, reversing strings, and checking for palindromes. Structures and arrays of structures are demonstrated including a structure within a structure and an array of student structures.

Uploaded by

Vibha Sethi
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
74 views

Function-With No Parameters No Return

The document contains code snippets demonstrating different functions in C++ including functions with and without parameters, returning values, default arguments, function overloading, and working with matrices including addition, multiplication, finding the inverse and smallest element. It also includes examples of working with strings such as finding the length, adding strings, reversing strings, and checking for palindromes. Structures and arrays of structures are demonstrated including a structure within a structure and an array of student structures.

Uploaded by

Vibha Sethi
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 10

FUNCTION-WITH NO PARAMETERS NO RETURN

#include<iostream.h> #include<conio.h> fact() {int n,f=1; cout<<"Enter a number"; cin>>n; for(int i=n;i>1;i--) f=f*i; cout<<"The factorial is"<<f; } void main() { clrscr(); fact(); getch() }

FUNCTION-WITH ONLY PARAMETERS


#include<iostream.h> #include<conio.h> fact(int n) {int f=1; for(int i=n;i>1;i--) f=f*i; cout<<"The factorial is"<<f; } void main() {clrscr(); int n; cout<<"Enter a number"; cin>>n; fact(n); getch(); }

FUNCTION-WITH PARAMETERS AND RETURN


#include<iostream.h> #include<conio.h> int fact(int n) {int f=1; for(int i=n;i>1;i--) f=f*i; return f; } void main()

{clrscr(); int n,y; cout<<"Enter a number"; cin>>n; y=fact(n); cout<<"The factorial is"<<y; getch(); }

FUNCTION WITH DEFAULT ARGUEMENTS


#include<iostream.h> #include<conio.h> float interest(int time=10,int pr=1000,float rate=5) { return(pr*rate*time/100); } void main() { textcolor(BLACK); textbackground(WHITE); clrscr(); int p,t; float r; cout<<"Enter the time of deposit"; cin>>t; cout<<"The interest on the amount will be"<<interest(t); getch(); }

FUNCTION OVERLOADING
#include<iostream.h> #include<conio.h> #include<dos.h> void area(int r) { cout<<"The area is"<<3.14*r; } void area(int l,int b) { cout<<"The area is"<<l*b; } void main() { textcolor(BLACK); textbackground(WHITE); clrscr(); int c,r,l,b; do {

clrscr(); cout<<"..Area Calculator.."; cout<<"\nEnter"; cout<<"\n1 for area of a circle"; cout<<"\n2 for area of a rectangle"; cout<<"\n0 for exit"; cin>>c; switch(c) { case 1:cout<<"Enter the radius"; cin>>r; area(r); break; case 2:cout<<"Enter the dimensions"; cin>>l>>b; area(l,b); break; } delay(1000); }while(c!=0); getch(); }

MATRIX BASICS
#include<iostream.h> #include<conio.h> void main() { textcolor(BLACK); textbackground(WHITE); clrscr(); int mat[15][15],r,c; cout<<"Enter the order of the matrix(r,c)"; cin>>r>>c; cout<<"Enter the matrix"; for(int i=0;i<r;i++) { for(int j=0;j<c;j++) { cin>>mat[i][j];} } cout<<"The array is:"<<"\n"; for(i=0;i<r;i++) { for(int j=0;j<c;j++) {cout<<mat[i][j]<<" ";} cout<<'\n'; } getch(); }

MATRIX ADDITION
#include<iostream.h> #include<conio.h> void main() { textcolor(BLACK); textbackground(WHITE); clrscr(); int r1,r2,c1,c2; int mat1[15][15],mat2[15][15],mat[15][15]; cout<<"Enter the order of both the matrices(r,c)"; cin>>r1>>c1>>r2>>c2; cout<<"Enter the first matrix"; for(int i=0;i<r1;i++) { for(int j=0;j<c1;j++) { cin>>mat1[i][j];} } cout<<"Enter the second matrix"; for( i=0;i<r2;i++) { for(int j=0;j<c2;j++) { cin>>mat2[i][j];} } if((r1==r2)&&(c1==c2)) { for(i=0;i<r1;i++) { for(int j=0;j<c1;j++) { mat[i][j]=mat1[i][j]+mat2[i][j]; } } cout<<"The resultant matrix is:\n"; for(int i=0;i<r1;i++) { for(int j=0;j<c1;j++) { cout<<mat[i][j]<<" "; } cout<<'\n'; } } else { cout<<"Matrices cannot be added"; } getch(); }

MATRIX MULTIPLICATION
#include<iostream.h> #include<conio.h> void main() { textcolor(BLACK); textbackground(WHITE); clrscr(); double r1,r2,c1,c2; double mat1[15][15],mat2[15][15],mat[15][15]; cout<<"Enter the order of both the matrices(r,c)"; cin>>r1>>c1>>r2>>c2; cout<<"Enter the first matrix"; for(int i=0;i<r1;i++) { for(int j=0;j<c1;j++) { cin>>mat1[i][j];} } cout<<"Enter the second matrix"; for( i=0;i<r2;i++) { for(int j=0;j<c2;j++) { cin>>mat2[i][j];} } for( i=0;i<r1;i++) { for(int j=0;j<c1;j++) { mat[i][j]=0;} } if(c1==r2) { for(i=0;i<r1;i++) { for(int j=0;j<c2;j++) { for(int k=0;k<c1;k++) { mat[i][j]+=mat1[i][k]*mat2[k][j]; }}} cout<<"The multiplied matrix is:\n"; for(i=0;i<r1;i++) { for(int j=0;j<c1;j++) { cout<<mat[i][i]<<" "; } cout<<"\n"; }

} else { cout<<"Matrices cannot be multiplied"; }

MATRIX: INVERSE
#include<iostream.h> #include<conio.h> void main() { textcolor(BLACK); textbackground(WHITE); clrscr(); int mat[15][15],r,c; cout<<"Enter the order of the matrix(r,c)"; cin>>r>>c; cout<<"Enter the matrix"; for(int i=0;i<r;i++) { for(int j=0;j<c;j++) { cin>>mat[i][j];} } cout<<"The inverted matrix is:"<<"\n"; for(i=0;i<c;i++) { for(int j=0;j<r;j++) {cout<<mat[j][i]<<" ";} cout<<'\n'; } getch(); }

MATRIX:: FINDING-SMALLEST ELEMENT


#include<iostream.h> #include<conio.h> void main() { textcolor(BLACK); textbackground(WHITE); clrscr(); int mat[15][15],r,c,small=0; cout<<"Enter the order of the matrix(r,c)"; cin>>r>>c; cout<<"Enter the matrix"; for(int i=0;i<r;i++) { for(int j=0;j<c;j++) { cin>>mat[i][j];} } small=mat[0][0]; for(i=0;i<r;i++)

{ for(int j=0;j<c;j++) {if(mat[i][j]<small) {small=mat[i][j];} } } cout<<"The smallest element is "<<small; getch(); }

STRING LENGTH
#include<iostream.h> #include<conio.h> #include<stdio.h> void main() { textcolor(BLACK); textbackground(WHITE); clrscr(); char name[15]; int i=0,len=0; cout<<"Enter a name\t"; gets(name); while(name[len]!='\0') { len++; } cout<<"The no. of characters in "; puts(name); cout<<"is "<<len; getch(); }

STRING ADDITION
#include<iostream.h> #include<conio.h> #include<stdio.h> void main() { textcolor(BLACK); textbackground(WHITE); clrscr(); char name1[15],name2[15],name[30]; int i=0,j=0; cout<<"Enter both strings\t"; gets(name1); gets(name2); while(name1[i]!='\0') { name[j++]=name1[i++]; }

i=0; j++; while(name2[i]!='\0') { name[j++]=name2[i++]; } cout<<"Added word\n"; puts(name); getch(); }

STRING INVERSE
#include<iostream.h> #include<conio.h> #include<stdio.h> void main() { textcolor(BLACK); textbackground(WHITE); clrscr(); char name[15]; int i=0,len=0; cout<<"Enter a name\t"; gets(name); while(name[len]!='\0') { len++; } len--; cout<<"The inverse of the string is:"; i=len; while(i>=0) { cout<<name[i]; i--; } getch(); }

STRING PALLINDROME
#include<iostream.h> #include<conio.h> #include<stdio.h>

void main() { textcolor(BLACK); textbackground(WHITE); clrscr(); char name[15],rev[15]; int i=0,len=0; cout<<"Enter a name\t"; gets(name); while(name[len]!='\0') { len++; } len--; i=len; int j=0; while(i>=0) { rev[j]=name[i]; i--; j++; } i=j=0; int flag=1; while(i<len) { if(name[i++]!=rev[j++]) { flag=0; break; } } if(flag==1) cout<<"The entered string is a pallindrome"; else cout<<"The entered string is not a pallindrome"; getch(); }

STRUCTURE WITHIN STRUCTURE


#include<iostream.h> #include<conio.h> struct address{ int hno; int pin; char state[40]; }; struct student{ char name[14];

int roll; address a; }s; void main() { textcolor(BLACK); textbackground(WHITE); clrscr(); cout<<"Enter the details of a student"; cout<<"\nname::"; cin>>s.name; cout<<"\nroll number::" cin>>s.roll; cout<<"\naddress:"; cout<<"house number::"; cin>>s.a.hno; cout<<"\npin code::"; cin>>s.a.pin; cout<<"\nstate::" cin>>s.a.state; getch();

ARRAY OF STRUCTURE
#include<iostream.h> #include<conio.h> struct student{ char name[14]; int roll; }s[150]; void main() { textcolor(BLACK); textbackground(WHITE); clrscr(); int n; cout<<"Enter the number of records you want to enter"; cin>>n; cout<<"Enter the details of the student"; for(int i=1;i<=n;i++) { cout<<"\nname::"<<i<<"::"; cin>>s[i].name; cout<<"\nroll number::"<<i<<"::"; cin>>s[i].roll; } cout<<"data feeding successful..."; getch(); }

You might also like