Name |
Code |
Exponentiation using multiplication
|
#include <iostream.h> int exp (int b, int e) { int result; result = 1; while (e != 0) { result = result * b; e = e - 1; } return(result); } int main () { int b, e; cout << "Enter base and exponent: "; cin >> b >> e; cout << b << " to the " << e << " = " << exp(b,e) << endl; return(0); }
|
Factorial
|
#include <iostream> #include <iomanip> using namespace std; #define LENGTH 20 long double iterativeFunction(unsigned int n); // Iterative solution long double recursiveFunction(unsigned int n); // Recursive solution int main() { unsigned int n; cout << fixed << setprecision(0); cout << setw(10) << "n" << setw(30) << "Factorial of n" << " (Iterative solution)\n" << endl; for( n = 0; n <= LENGTH; ++n) cout << setw(10) << n << setw(30) << iterativeFunction(n) << endl; cout << "Go on with "; cin.get(); cout << setw(10) << "n" << setw(30) << "Factorial of n" << " (Recursive solution)\n" << endl; for( n = 0; n <= LENGTH; ++n) cout << setw(10) << n << setw(30) << recursiveFunction(n) << endl; cout << endl; return 0; } long double iterativeFunction(unsigned int n) // Iterative solution. { long double result = 1.0; for( unsigned int i = 2; i <= n; ++i) result *= i; return result; } long double recursiveFunction(unsigned int n) // Recursive solution. { if( n <= 1) return 1.0; else return recursiveFunction(n-1) * n; }
|
Factorial2
|
#include <iostream.h> // sequence is 0, 1, 1, 2, 3, 5, 8, 13, ... int fib (int i) { int pred, result, temp; pred = 1; result = 0; while (i > 0) { temp = pred + result; result = pred; pred = temp; i = i-1; } return(result); } int main () { int n; cout << "Enter a natural number: "; cin >> n; while (n < 0) { cout << "Please re-enter: "; cin >> n; } cout << "fib(" << n << ") = " << fib(n) << endl; return(0); }
|
Fibonacci Series
|
#include<iostream.h> #include<conio.h> main() { const unsigned long limit=4294967295; unsigned long next=0; unsigned long last=1; long sum; clrscr(); cout<<"\n\nThis program will print the Fibonacci series :\n\n "; while(next<limit/2) { cout<<last <<" "; sum=next+last; next=last; last=sum; } getch(); }
|
File Operations
|
#include< iostream.h > #include< conio.h > #include< fstream.h > class student { private: int rno; char name[10]; float fees; public: void getdata() { cout<<"roll number"; cin>>rno; cout<< endl; cout<<"enter name:"; cin >> name; cout<< endl <<"enter fees:"; cin>>fees; } void dispdata() { cout<<"Roll number"<< rno << endl; cout<<"Name"<< name << endl; cout<<"Fees"<< fees; } }; void main() { student s1; clrscr(); ofstream stdfile("c:\\std.txt"); //fstream stdfile; //stdfile.open("c:\\std.txt",ios::out|ios::in); //open file for output char wish; //writing to the file do { s1.getdata(); stdfile.write((char*)& s1,sizeof(student)); cout << "continue ? y/n"; cin >> wish; } while(wish=='y'||wish=='Y'); stdfile.close(); //close the file getch(); }
|
Find out Number is Even or Odd
|
#include <iostream.h> #include <conio.h> void main() { clrscr(); int x; cout << "Enter an integer : "; cin>>x; if(x%2==0) cout << "The number " << x << " is even."; else cout << "The number " << x << " is odd."; getch(); }
|
Floyd warshall algorithm
|
#include<piostream.h> #include<conio.h> #include<stdio.h> #include<stdlib.h> class path { int n; int p[10][10]; int a[10][10]; int c[10][10]; public: void get(); void pm(); void ap(); void disp(); }; void path::get() { int i,j,k; clrscr(); cout<<"Enter the no. of nodes in the graph :"; cin>>n; cout<<"Enter the adjacency matrix :"; for(i=1;i<=n;i++) { for(j=1;j<=n;j++) { cin>>a[i][j]; p[i][j]=0; } } cout<<"Enter The cost matrix is :"; for(i=1;i<=n;i++) { for(j=1;j<=n;j++) { cin>>c[i][j]; } } for(i=1;i<=n;i++) { for(j=1;j<=n;j++) { p[i][j]=a[i][j]; } } } void path::disp() { // cout<<"The output matrix for the given graph is :"; for(int i=1;i<=n;i++) { for(int j=1;j<=n;j++) { cout<<p[i][j]<< " "; } cout<<endl; } } void path::pm() { int i,j,k; for(k=1;k<=n;k++) { for(i=1;i<=n;i++) { for(j=1;j<=n;j++) { p[i][j]=p[i][j] || p[i][k] && p[k][j]; } } } } void path::ap() { int i,j,k; for(i=1;i<=n;i++) { for(j=1;j<=n;j++) { p[i][j]=c[i][j]; } } for(k=1;k<=n;k++) { for(i=1;i<=n;i++) { for(j=1;j<=n;j++) { if(p[i][j]<p[i][k]+p[k][j]) { p[i][j]=p[i][j]; } else { p[i][j]=p[i][k]+p[k][j]; } } } } } void main() { path p; p.get(); p.pm(); cout<<"path matrix is :"; p.disp(); getch(); p.ap(); cout<<"all pair shortest path matrix is :"; p.disp(); getch(); }
|
Function overloading
|
# include<iostream.h> # include<conio.h> int area(int side) { return side*side; } int area(int l , int b) { return l*b; } void main() { clrscr(); int (*p1)(int); int (*p2)(int,int); p1=area; p2=area; cout<<"Address of area(int)="<<(unsigned int)p1< cout<<"Address of area(int,int)="<<(unsigned int)p2< cout<<"Invoking area(int) via p1 "< cout<<"Invoking area(int,int) via p2 "< getch(); }
|
Heap Sort
|
#include < iostream.h > const int MAX = 10 ; class array { private : int arr[MAX] ; int count ; public : array( ) ; void add ( int num ) ; void makeheap(int ) ; void heapsort( ) ; void display( ) ; } ; array :: array( ) { count = 0 ; for ( int i = 0 ; i < MAX ; i++ ) arr[MAX] = 0 ; } void array :: add ( int num ) { if ( count < MAX ) { arr[count] = num ; count++ ; } else cout << "\nArray is full" << endl ; } void array :: makeheap(int c) { for ( int i = 1 ; i < c ; i++ ) { int val = arr[i] ; int s = i ; int f = ( s - 1 ) / 2 ; while ( s > 0 && arr[f] < val ) { arr[s] = arr[f] ; s = f ; f = ( s - 1 ) / 2 ; } arr[s] = val ; } } void array :: heapsort( ) { for ( int i = count - 1 ; i > 0 ; i-- ) { int ivalue = arr[i] ; arr[i] = arr[0] ; arr[0]=ivalue; makeheap(i); } } void array :: display( ) { for ( int i = 0 ; i < count ; i++ ) cout << arr[i] << "\t" ; cout << endl ; } void main( ) { array a ; a.add ( 11 ) ; a.add ( 2 ) ; a.add ( 9 ) ; a.add ( 13 ) ; a.add ( 57 ) ; a.add ( 25 ) ; a.add ( 17 ) ; a.add ( 1 ) ; a.add ( 90 ) ; a.add ( 3 ) ; a.makeheap(10) ; cout << "\nHeap Sort.\n" ; cout << "\nBefore Sorting:\n" ; a.display( ) ; a.heapsort( ) ; cout << "\nAfter Sorting:\n" ; a.display( ) ; }
|
Inline Functions
|
#include <iostream> using namespace std; inline double Sum(const double * Numbers, const int Count) { double s = 0; for(int i = 0; i < Count; i++) s += Numbers[i]; return s; } int main() { double Nbr[] = { 15.66, 18, 25, 128.62, 12.06, 22.18 }; double Total = Sum(Nbr, 6); cout << "Sum = " << Total << endl; return 0; } If you first declare a function that would be defined somewhere else, when implementing the function, you can type or omit the inline keyword: #include <iostream> using namespace std; inline double Sum(const double * Numbers, const int Count); int main() { double Nbr[] = { 15.66, 18, 25, 128.62, 12.06, 22.18 }; double Total = Sum(Nbr, 6); cout << "Sum = " << Total << endl; return 0; } inline double Sum(const double * Numbers, const int Count) { double s = 0; for(int i = 0; i < Count; i++) s += Numbers[i]; return s; }
|